DragOperation.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Badge.h>
  7. #include <LibCore/EventLoop.h>
  8. #include <LibCore/MimeData.h>
  9. #include <LibGUI/ConnectionToWindowServer.h>
  10. #include <LibGUI/DragOperation.h>
  11. #include <LibGfx/Bitmap.h>
  12. namespace GUI {
  13. static DragOperation* s_current_drag_operation;
  14. DragOperation::DragOperation(Core::Object* parent)
  15. : Core::Object(parent)
  16. {
  17. }
  18. DragOperation::~DragOperation()
  19. {
  20. }
  21. DragOperation::Outcome DragOperation::exec()
  22. {
  23. VERIFY(!s_current_drag_operation);
  24. VERIFY(!m_event_loop);
  25. VERIFY(m_mime_data);
  26. Gfx::ShareableBitmap drag_bitmap;
  27. if (m_mime_data->has_format("image/x-raw-bitmap")) {
  28. auto data = m_mime_data->data("image/x-raw-bitmap");
  29. auto bitmap = Gfx::Bitmap::try_create_from_serialized_byte_buffer(move(data)).release_value_but_fixme_should_propagate_errors();
  30. drag_bitmap = bitmap->to_shareable_bitmap();
  31. }
  32. auto started = ConnectionToWindowServer::the().start_drag(
  33. m_mime_data->text(),
  34. m_mime_data->all_data(),
  35. drag_bitmap);
  36. if (!started) {
  37. m_outcome = Outcome::Cancelled;
  38. return m_outcome;
  39. }
  40. s_current_drag_operation = this;
  41. m_event_loop = make<Core::EventLoop>();
  42. auto result = m_event_loop->exec();
  43. m_event_loop = nullptr;
  44. dbgln("{}: event loop returned with result {}", class_name(), result);
  45. remove_from_parent();
  46. s_current_drag_operation = nullptr;
  47. return m_outcome;
  48. }
  49. void DragOperation::done(Outcome outcome)
  50. {
  51. VERIFY(m_outcome == Outcome::None);
  52. m_outcome = outcome;
  53. m_event_loop->quit(0);
  54. }
  55. void DragOperation::notify_accepted(Badge<ConnectionToWindowServer>)
  56. {
  57. VERIFY(s_current_drag_operation);
  58. s_current_drag_operation->done(Outcome::Accepted);
  59. }
  60. void DragOperation::notify_cancelled(Badge<ConnectionToWindowServer>)
  61. {
  62. if (s_current_drag_operation)
  63. s_current_drag_operation->done(Outcome::Cancelled);
  64. }
  65. void DragOperation::set_text(const String& text)
  66. {
  67. if (!m_mime_data)
  68. m_mime_data = Core::MimeData::construct();
  69. m_mime_data->set_text(text);
  70. }
  71. void DragOperation::set_bitmap(const Gfx::Bitmap* bitmap)
  72. {
  73. if (!m_mime_data)
  74. m_mime_data = Core::MimeData::construct();
  75. if (bitmap)
  76. m_mime_data->set_data("image/x-raw-bitmap", bitmap->serialize_to_byte_buffer());
  77. }
  78. void DragOperation::set_data(const String& data_type, const String& data)
  79. {
  80. if (!m_mime_data)
  81. m_mime_data = Core::MimeData::construct();
  82. m_mime_data->set_data(data_type, data.to_byte_buffer());
  83. }
  84. }