GDialog.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <LibGUI/GDesktop.h>
  2. #include <LibGUI/GDialog.h>
  3. #include <LibGUI/GEvent.h>
  4. GDialog::GDialog(CObject* parent)
  5. : GWindow(parent)
  6. {
  7. set_modal(true);
  8. }
  9. GDialog::~GDialog()
  10. {
  11. }
  12. int GDialog::exec()
  13. {
  14. ASSERT(!m_event_loop);
  15. m_event_loop = make<CEventLoop>();
  16. auto new_rect = rect();
  17. if (parent() && parent()->is_window()) {
  18. auto& parent_window = *static_cast<GWindow*>(parent());
  19. new_rect.center_within(parent_window.rect());
  20. } else {
  21. new_rect.center_within(GDesktop::the().rect());
  22. }
  23. set_rect(new_rect);
  24. show();
  25. auto result = m_event_loop->exec();
  26. m_event_loop = nullptr;
  27. dbgprintf("%s: event loop returned with result %d\n", class_name(), result);
  28. remove_from_parent();
  29. return result;
  30. }
  31. void GDialog::done(int result)
  32. {
  33. if (!m_event_loop)
  34. return;
  35. m_result = result;
  36. dbgprintf("%s: quit event loop with result %d\n", class_name(), result);
  37. m_event_loop->quit(result);
  38. }
  39. void GDialog::event(CEvent& event)
  40. {
  41. if (event.type() == GEvent::KeyUp) {
  42. auto& key_event = static_cast<GKeyEvent&>(event);
  43. if (key_event.key() == KeyCode::Key_Escape) {
  44. done(ExecCancel);
  45. return;
  46. }
  47. }
  48. GWindow::event(event);
  49. }
  50. void GDialog::close()
  51. {
  52. GWindow::close();
  53. m_event_loop->quit(ExecCancel);
  54. }