GDialog.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <LibGUI/GDesktop.h>
  2. #include <LibGUI/GDialog.h>
  3. #include <LibGUI/GEventLoop.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<GEventLoop>();
  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. return result;
  29. }
  30. void GDialog::done(int result)
  31. {
  32. if (!m_event_loop)
  33. return;
  34. m_result = result;
  35. dbgprintf("%s: quit event loop with result %d\n", class_name(), result);
  36. m_event_loop->quit(result);
  37. }
  38. void GDialog::close()
  39. {
  40. GWindow::close();
  41. m_event_loop->quit(ExecCancel);
  42. }