GDragOperation.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 <LibGfx/Bitmap.h>
  27. #include <LibGUI/GDragOperation.h>
  28. #include <LibGUI/GWindowServerConnection.h>
  29. namespace GUI {
  30. static DragOperation* s_current_drag_operation;
  31. DragOperation::DragOperation(Core::Object* parent)
  32. : Core::Object(parent)
  33. {
  34. }
  35. DragOperation::~DragOperation()
  36. {
  37. }
  38. DragOperation::Outcome DragOperation::exec()
  39. {
  40. ASSERT(!s_current_drag_operation);
  41. ASSERT(!m_event_loop);
  42. int bitmap_id = -1;
  43. Size bitmap_size;
  44. RefPtr<Gfx::Bitmap> shared_bitmap;
  45. if (m_bitmap) {
  46. shared_bitmap = m_bitmap->to_shareable_bitmap();
  47. shared_bitmap->shared_buffer()->share_with(WindowServerConnection::the().server_pid());
  48. bitmap_id = shared_bitmap->shared_buffer_id();
  49. bitmap_size = shared_bitmap->size();
  50. }
  51. auto response = WindowServerConnection::the().send_sync<WindowServer::StartDrag>(m_text, m_data_type, m_data, bitmap_id, bitmap_size);
  52. if (!response->started()) {
  53. m_outcome = Outcome::Cancelled;
  54. return m_outcome;
  55. }
  56. s_current_drag_operation = this;
  57. m_event_loop = make<Core::EventLoop>();
  58. auto result = m_event_loop->exec();
  59. m_event_loop = nullptr;
  60. dbgprintf("%s: event loop returned with result %d\n", class_name(), result);
  61. remove_from_parent();
  62. s_current_drag_operation = nullptr;
  63. return m_outcome;
  64. }
  65. void DragOperation::done(Outcome outcome)
  66. {
  67. ASSERT(m_outcome == Outcome::None);
  68. m_outcome = outcome;
  69. m_event_loop->quit(0);
  70. }
  71. void DragOperation::notify_accepted(Badge<WindowServerConnection>)
  72. {
  73. ASSERT(s_current_drag_operation);
  74. s_current_drag_operation->done(Outcome::Accepted);
  75. }
  76. void DragOperation::notify_cancelled(Badge<WindowServerConnection>)
  77. {
  78. ASSERT(s_current_drag_operation);
  79. s_current_drag_operation->done(Outcome::Cancelled);
  80. }
  81. }