DragOperation.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. int bitmap_id = -1;
  48. Gfx::IntSize bitmap_size;
  49. RefPtr<Gfx::Bitmap> shared_bitmap;
  50. if (m_mime_data->has_format("image/x-raw-bitmap")) {
  51. auto data = m_mime_data->data("image/x-raw-bitmap");
  52. auto bitmap = Gfx::Bitmap::create_from_serialized_byte_buffer(move(data));
  53. shared_bitmap = bitmap->to_bitmap_backed_by_shared_buffer();
  54. shared_bitmap->shared_buffer()->share_with(WindowServerConnection::the().server_pid());
  55. bitmap_id = shared_bitmap->shbuf_id();
  56. bitmap_size = shared_bitmap->size();
  57. }
  58. auto response = WindowServerConnection::the().send_sync<Messages::WindowServer::StartDrag>(
  59. m_mime_data->text(),
  60. m_mime_data->all_data(),
  61. bitmap_id, bitmap_size);
  62. if (!response->started()) {
  63. m_outcome = Outcome::Cancelled;
  64. return m_outcome;
  65. }
  66. s_current_drag_operation = this;
  67. m_event_loop = make<Core::EventLoop>();
  68. auto result = m_event_loop->exec();
  69. m_event_loop = nullptr;
  70. dbgln("{}: event loop returned with result {}", class_name(), result);
  71. remove_from_parent();
  72. s_current_drag_operation = nullptr;
  73. return m_outcome;
  74. }
  75. void DragOperation::done(Outcome outcome)
  76. {
  77. ASSERT(m_outcome == Outcome::None);
  78. m_outcome = outcome;
  79. m_event_loop->quit(0);
  80. }
  81. void DragOperation::notify_accepted(Badge<WindowServerConnection>)
  82. {
  83. ASSERT(s_current_drag_operation);
  84. s_current_drag_operation->done(Outcome::Accepted);
  85. }
  86. void DragOperation::notify_cancelled(Badge<WindowServerConnection>)
  87. {
  88. if (s_current_drag_operation)
  89. s_current_drag_operation->done(Outcome::Cancelled);
  90. }
  91. void DragOperation::set_text(const String& text)
  92. {
  93. if (!m_mime_data)
  94. m_mime_data = Core::MimeData::construct();
  95. m_mime_data->set_text(text);
  96. }
  97. void DragOperation::set_bitmap(const Gfx::Bitmap* bitmap)
  98. {
  99. if (!m_mime_data)
  100. m_mime_data = Core::MimeData::construct();
  101. if (bitmap)
  102. m_mime_data->set_data("image/x-raw-bitmap", bitmap->serialize_to_byte_buffer());
  103. }
  104. void DragOperation::set_data(const String& data_type, const String& data)
  105. {
  106. if (!m_mime_data)
  107. m_mime_data = Core::MimeData::construct();
  108. m_mime_data->set_data(data_type, data.to_byte_buffer());
  109. }
  110. }