DragOperation.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Badge.h>
  27. #include <LibCore/EventLoop.h>
  28. #include <LibCore/MimeData.h>
  29. #include <LibGUI/DragOperation.h>
  30. #include <LibGUI/WindowServerConnection.h>
  31. #include <LibGfx/Bitmap.h>
  32. namespace GUI {
  33. static DragOperation* s_current_drag_operation;
  34. DragOperation::DragOperation(Core::Object* parent)
  35. : Core::Object(parent)
  36. {
  37. }
  38. DragOperation::~DragOperation()
  39. {
  40. }
  41. DragOperation::Outcome DragOperation::exec()
  42. {
  43. VERIFY(!s_current_drag_operation);
  44. VERIFY(!m_event_loop);
  45. VERIFY(m_mime_data);
  46. Gfx::ShareableBitmap drag_bitmap;
  47. if (m_mime_data->has_format("image/x-raw-bitmap")) {
  48. auto data = m_mime_data->data("image/x-raw-bitmap");
  49. auto bitmap = Gfx::Bitmap::create_from_serialized_byte_buffer(move(data));
  50. drag_bitmap = bitmap->to_shareable_bitmap();
  51. }
  52. auto response = WindowServerConnection::the().send_sync<Messages::WindowServer::StartDrag>(
  53. m_mime_data->text(),
  54. m_mime_data->all_data(),
  55. drag_bitmap);
  56. if (!response->started()) {
  57. m_outcome = Outcome::Cancelled;
  58. return m_outcome;
  59. }
  60. s_current_drag_operation = this;
  61. m_event_loop = make<Core::EventLoop>();
  62. auto result = m_event_loop->exec();
  63. m_event_loop = nullptr;
  64. dbgln("{}: event loop returned with result {}", class_name(), result);
  65. remove_from_parent();
  66. s_current_drag_operation = nullptr;
  67. return m_outcome;
  68. }
  69. void DragOperation::done(Outcome outcome)
  70. {
  71. VERIFY(m_outcome == Outcome::None);
  72. m_outcome = outcome;
  73. m_event_loop->quit(0);
  74. }
  75. void DragOperation::notify_accepted(Badge<WindowServerConnection>)
  76. {
  77. VERIFY(s_current_drag_operation);
  78. s_current_drag_operation->done(Outcome::Accepted);
  79. }
  80. void DragOperation::notify_cancelled(Badge<WindowServerConnection>)
  81. {
  82. if (s_current_drag_operation)
  83. s_current_drag_operation->done(Outcome::Cancelled);
  84. }
  85. void DragOperation::set_text(const String& text)
  86. {
  87. if (!m_mime_data)
  88. m_mime_data = Core::MimeData::construct();
  89. m_mime_data->set_text(text);
  90. }
  91. void DragOperation::set_bitmap(const Gfx::Bitmap* bitmap)
  92. {
  93. if (!m_mime_data)
  94. m_mime_data = Core::MimeData::construct();
  95. if (bitmap)
  96. m_mime_data->set_data("image/x-raw-bitmap", bitmap->serialize_to_byte_buffer());
  97. }
  98. void DragOperation::set_data(const String& data_type, const String& data)
  99. {
  100. if (!m_mime_data)
  101. m_mime_data = Core::MimeData::construct();
  102. m_mime_data->set_data(data_type, data.to_byte_buffer());
  103. }
  104. }