DragOperation.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 <AK/SharedBuffer.h>
  28. #include <LibCore/EventLoop.h>
  29. #include <LibCore/MimeData.h>
  30. #include <LibGUI/DragOperation.h>
  31. #include <LibGUI/WindowServerConnection.h>
  32. #include <LibGfx/Bitmap.h>
  33. namespace GUI {
  34. static DragOperation* s_current_drag_operation;
  35. DragOperation::DragOperation(Core::Object* parent)
  36. : Core::Object(parent)
  37. {
  38. }
  39. DragOperation::~DragOperation()
  40. {
  41. }
  42. DragOperation::Outcome DragOperation::exec()
  43. {
  44. ASSERT(!s_current_drag_operation);
  45. ASSERT(!m_event_loop);
  46. ASSERT(m_mime_data);
  47. Gfx::ShareableBitmap drag_bitmap;
  48. if (m_mime_data->has_format("image/x-raw-bitmap")) {
  49. auto data = m_mime_data->data("image/x-raw-bitmap");
  50. auto bitmap = Gfx::Bitmap::create_from_serialized_byte_buffer(move(data));
  51. drag_bitmap = bitmap->to_shareable_bitmap();
  52. }
  53. auto response = WindowServerConnection::the().send_sync<Messages::WindowServer::StartDrag>(
  54. m_mime_data->text(),
  55. m_mime_data->all_data(),
  56. drag_bitmap);
  57. if (!response->started()) {
  58. m_outcome = Outcome::Cancelled;
  59. return m_outcome;
  60. }
  61. s_current_drag_operation = this;
  62. m_event_loop = make<Core::EventLoop>();
  63. auto result = m_event_loop->exec();
  64. m_event_loop = nullptr;
  65. dbgln("{}: event loop returned with result {}", class_name(), result);
  66. remove_from_parent();
  67. s_current_drag_operation = nullptr;
  68. return m_outcome;
  69. }
  70. void DragOperation::done(Outcome outcome)
  71. {
  72. ASSERT(m_outcome == Outcome::None);
  73. m_outcome = outcome;
  74. m_event_loop->quit(0);
  75. }
  76. void DragOperation::notify_accepted(Badge<WindowServerConnection>)
  77. {
  78. ASSERT(s_current_drag_operation);
  79. s_current_drag_operation->done(Outcome::Accepted);
  80. }
  81. void DragOperation::notify_cancelled(Badge<WindowServerConnection>)
  82. {
  83. if (s_current_drag_operation)
  84. s_current_drag_operation->done(Outcome::Cancelled);
  85. }
  86. void DragOperation::set_text(const String& text)
  87. {
  88. if (!m_mime_data)
  89. m_mime_data = Core::MimeData::construct();
  90. m_mime_data->set_text(text);
  91. }
  92. void DragOperation::set_bitmap(const Gfx::Bitmap* bitmap)
  93. {
  94. if (!m_mime_data)
  95. m_mime_data = Core::MimeData::construct();
  96. if (bitmap)
  97. m_mime_data->set_data("image/x-raw-bitmap", bitmap->serialize_to_byte_buffer());
  98. }
  99. void DragOperation::set_data(const String& data_type, const String& data)
  100. {
  101. if (!m_mime_data)
  102. m_mime_data = Core::MimeData::construct();
  103. m_mime_data->set_data(data_type, data.to_byte_buffer());
  104. }
  105. }