GDialog.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. 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::close()
  40. {
  41. GWindow::close();
  42. m_event_loop->quit(ExecCancel);
  43. }