ladybird/Libraries/LibGUI/GDragOperation.cpp
Andreas Kling a7f414bba7 LibGUI+WindowServer: Start fleshing out drag&drop functionality
This patch enables basic drag&drop between applications.
You initiate a drag by creating a GDragOperation object and calling
exec() on it. This creates a nested event loop in the calling program
that only returns once the drag operation has ended.

On the receiving side, you get a call to GWidget::drop_event() with
a GDropEvent containing information about the dropped data.

The only data passed right now is a piece of text that's also used
to visually indicate that a drag is happening (by showing the text in
a little box that follows the mouse cursor around.)

There are things to fix here, but we're off to a nice start. :^)
2019-12-08 16:50:23 +01:00

53 lines
1.3 KiB
C++

#include <LibGUI/GDragOperation.h>
#include <LibGUI/GWindowServerConnection.h>
static GDragOperation* s_current_drag_operation;
GDragOperation::GDragOperation(CObject* parent)
: CObject(parent)
{
}
GDragOperation::~GDragOperation()
{
}
GDragOperation::Outcome GDragOperation::exec()
{
ASSERT(!s_current_drag_operation);
ASSERT(!m_event_loop);
auto response = GWindowServerConnection::the().send_sync<WindowServer::StartDrag>(m_text, -1, Size());
if (!response->started()) {
m_outcome = Outcome::Cancelled;
return m_outcome;
}
s_current_drag_operation = this;
m_event_loop = make<CEventLoop>();
auto result = m_event_loop->exec();
m_event_loop = nullptr;
dbgprintf("%s: event loop returned with result %d\n", class_name(), result);
remove_from_parent();
s_current_drag_operation = nullptr;
return m_outcome;
}
void GDragOperation::done(Outcome outcome)
{
ASSERT(m_outcome == Outcome::None);
m_outcome = outcome;
m_event_loop->quit(0);
}
void GDragOperation::notify_accepted(Badge<GWindowServerConnection>)
{
ASSERT(s_current_drag_operation);
s_current_drag_operation->done(Outcome::Accepted);
}
void GDragOperation::notify_cancelled(Badge<GWindowServerConnection>)
{
ASSERT(s_current_drag_operation);
s_current_drag_operation->done(Outcome::Cancelled);
}