Dialog.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <LibCore/EventLoop.h>
  27. #include <LibGUI/Desktop.h>
  28. #include <LibGUI/Dialog.h>
  29. #include <LibGUI/Event.h>
  30. namespace GUI {
  31. Dialog::Dialog(Window* parent_window)
  32. : Window(parent_window)
  33. {
  34. set_modal(true);
  35. }
  36. Dialog::~Dialog()
  37. {
  38. }
  39. int Dialog::exec()
  40. {
  41. ASSERT(!m_event_loop);
  42. m_event_loop = make<Core::EventLoop>();
  43. auto new_rect = rect();
  44. if (parent() && parent()->is_window()) {
  45. auto& parent_window = *static_cast<Window*>(parent());
  46. new_rect.center_within(parent_window.rect());
  47. } else {
  48. new_rect.center_within(Desktop::the().rect());
  49. }
  50. set_rect(new_rect);
  51. show();
  52. auto result = m_event_loop->exec();
  53. m_event_loop = nullptr;
  54. dbgprintf("%s: event loop returned with result %d\n", class_name(), result);
  55. remove_from_parent();
  56. return result;
  57. }
  58. void Dialog::done(int result)
  59. {
  60. if (!m_event_loop)
  61. return;
  62. m_result = result;
  63. dbgprintf("%s: quit event loop with result %d\n", class_name(), result);
  64. m_event_loop->quit(result);
  65. }
  66. void Dialog::event(Core::Event& event)
  67. {
  68. if (event.type() == Event::KeyUp) {
  69. auto& key_event = static_cast<KeyEvent&>(event);
  70. if (key_event.key() == KeyCode::Key_Escape) {
  71. done(ExecCancel);
  72. event.accept();
  73. return;
  74. }
  75. }
  76. Window::event(event);
  77. }
  78. void Dialog::close()
  79. {
  80. Window::close();
  81. m_event_loop->quit(ExecCancel);
  82. }
  83. }