DragOperation.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <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. ASSERT(!s_current_drag_operation);
  44. ASSERT(!m_event_loop);
  45. int bitmap_id = -1;
  46. Gfx::Size bitmap_size;
  47. RefPtr<Gfx::Bitmap> shared_bitmap;
  48. if (m_bitmap) {
  49. shared_bitmap = m_bitmap->to_bitmap_backed_by_shared_buffer();
  50. shared_bitmap->shared_buffer()->share_with(WindowServerConnection::the().server_pid());
  51. bitmap_id = shared_bitmap->shbuf_id();
  52. bitmap_size = shared_bitmap->size();
  53. }
  54. auto response = WindowServerConnection::the().send_sync<Messages::WindowServer::StartDrag>(m_text, m_data_type, m_data, bitmap_id, bitmap_size);
  55. if (!response->started()) {
  56. m_outcome = Outcome::Cancelled;
  57. return m_outcome;
  58. }
  59. s_current_drag_operation = this;
  60. m_event_loop = make<Core::EventLoop>();
  61. auto result = m_event_loop->exec();
  62. m_event_loop = nullptr;
  63. dbgprintf("%s: event loop returned with result %d\n", class_name(), result);
  64. remove_from_parent();
  65. s_current_drag_operation = nullptr;
  66. return m_outcome;
  67. }
  68. void DragOperation::done(Outcome outcome)
  69. {
  70. ASSERT(m_outcome == Outcome::None);
  71. m_outcome = outcome;
  72. m_event_loop->quit(0);
  73. }
  74. void DragOperation::notify_accepted(Badge<WindowServerConnection>)
  75. {
  76. ASSERT(s_current_drag_operation);
  77. s_current_drag_operation->done(Outcome::Accepted);
  78. }
  79. void DragOperation::notify_cancelled(Badge<WindowServerConnection>)
  80. {
  81. ASSERT(s_current_drag_operation);
  82. s_current_drag_operation->done(Outcome::Cancelled);
  83. }
  84. }