GDragOperation.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include <LibDraw/GraphicsBitmap.h>
  2. #include <LibGUI/GDragOperation.h>
  3. #include <LibGUI/GWindowServerConnection.h>
  4. static GDragOperation* s_current_drag_operation;
  5. GDragOperation::GDragOperation(CObject* parent)
  6. : CObject(parent)
  7. {
  8. }
  9. GDragOperation::~GDragOperation()
  10. {
  11. }
  12. GDragOperation::Outcome GDragOperation::exec()
  13. {
  14. ASSERT(!s_current_drag_operation);
  15. ASSERT(!m_event_loop);
  16. int bitmap_id = -1;
  17. Size bitmap_size;
  18. RefPtr<GraphicsBitmap> shared_bitmap;
  19. if (m_bitmap) {
  20. shared_bitmap = m_bitmap->to_shareable_bitmap();
  21. shared_bitmap->shared_buffer()->share_with(GWindowServerConnection::the().server_pid());
  22. bitmap_id = shared_bitmap->shared_buffer_id();
  23. bitmap_size = shared_bitmap->size();
  24. }
  25. auto response = GWindowServerConnection::the().send_sync<WindowServer::StartDrag>(m_text, bitmap_id, bitmap_size);
  26. if (!response->started()) {
  27. m_outcome = Outcome::Cancelled;
  28. return m_outcome;
  29. }
  30. s_current_drag_operation = this;
  31. m_event_loop = make<CEventLoop>();
  32. auto result = m_event_loop->exec();
  33. m_event_loop = nullptr;
  34. dbgprintf("%s: event loop returned with result %d\n", class_name(), result);
  35. remove_from_parent();
  36. s_current_drag_operation = nullptr;
  37. return m_outcome;
  38. }
  39. void GDragOperation::done(Outcome outcome)
  40. {
  41. ASSERT(m_outcome == Outcome::None);
  42. m_outcome = outcome;
  43. m_event_loop->quit(0);
  44. }
  45. void GDragOperation::notify_accepted(Badge<GWindowServerConnection>)
  46. {
  47. ASSERT(s_current_drag_operation);
  48. s_current_drag_operation->done(Outcome::Accepted);
  49. }
  50. void GDragOperation::notify_cancelled(Badge<GWindowServerConnection>)
  51. {
  52. ASSERT(s_current_drag_operation);
  53. s_current_drag_operation->done(Outcome::Cancelled);
  54. }