GApplication.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <LibGUI/GAction.h>
  2. #include <LibGUI/GApplication.h>
  3. #include <LibGUI/GEventLoop.h>
  4. #include <LibGUI/GLabel.h>
  5. #include <LibGUI/GMenuBar.h>
  6. #include <LibGUI/GPainter.h>
  7. #include <LibGUI/GWindow.h>
  8. #include <WindowServer/WSAPITypes.h>
  9. static GApplication* s_the;
  10. GApplication& GApplication::the()
  11. {
  12. ASSERT(s_the);
  13. return *s_the;
  14. }
  15. GApplication::GApplication(int argc, char** argv)
  16. {
  17. (void)argc;
  18. (void)argv;
  19. ASSERT(!s_the);
  20. s_the = this;
  21. m_event_loop = make<GEventLoop>();
  22. }
  23. GApplication::~GApplication()
  24. {
  25. s_the = nullptr;
  26. }
  27. int GApplication::exec()
  28. {
  29. int exit_code = m_event_loop->exec();
  30. // NOTE: Maybe it would be cool to return instead of exit()?
  31. // This would require cleaning up all the CObjects on the heap.
  32. exit(exit_code);
  33. return exit_code;
  34. }
  35. void GApplication::quit(int exit_code)
  36. {
  37. m_event_loop->quit(exit_code);
  38. }
  39. void GApplication::set_menubar(OwnPtr<GMenuBar>&& menubar)
  40. {
  41. if (m_menubar)
  42. m_menubar->notify_removed_from_application({});
  43. m_menubar = move(menubar);
  44. if (m_menubar)
  45. m_menubar->notify_added_to_application({});
  46. }
  47. void GApplication::register_global_shortcut_action(Badge<GAction>, GAction& action)
  48. {
  49. m_global_shortcut_actions.set(action.shortcut(), &action);
  50. }
  51. void GApplication::unregister_global_shortcut_action(Badge<GAction>, GAction& action)
  52. {
  53. m_global_shortcut_actions.remove(action.shortcut());
  54. }
  55. GAction* GApplication::action_for_key_event(const GKeyEvent& event)
  56. {
  57. auto it = m_global_shortcut_actions.find(GShortcut(event.modifiers(), (KeyCode)event.key()));
  58. if (it == m_global_shortcut_actions.end())
  59. return nullptr;
  60. return (*it).value;
  61. }
  62. class GApplication::TooltipWindow final : public GWindow {
  63. public:
  64. TooltipWindow()
  65. {
  66. set_window_type(GWindowType::Tooltip);
  67. m_label = GLabel::construct();
  68. m_label->set_background_color(Color::from_rgb(0xdac7b5));
  69. m_label->set_fill_with_background_color(true);
  70. m_label->set_frame_thickness(1);
  71. m_label->set_frame_shape(FrameShape::Container);
  72. m_label->set_frame_shadow(FrameShadow::Plain);
  73. set_main_widget(m_label);
  74. }
  75. void set_tooltip(const StringView& tooltip)
  76. {
  77. // FIXME: Add some kind of GLabel auto-sizing feature.
  78. int text_width = m_label->font().width(tooltip);
  79. set_rect(100, 100, text_width + 10, m_label->font().glyph_height() + 8);
  80. m_label->set_text(tooltip);
  81. }
  82. ObjectPtr<GLabel> m_label;
  83. };
  84. void GApplication::show_tooltip(const StringView& tooltip, const Point& screen_location)
  85. {
  86. if (!m_tooltip_window) {
  87. m_tooltip_window = new TooltipWindow;
  88. m_tooltip_window->set_double_buffering_enabled(false);
  89. }
  90. m_tooltip_window->set_tooltip(tooltip);
  91. m_tooltip_window->move_to(screen_location);
  92. m_tooltip_window->show();
  93. }
  94. void GApplication::hide_tooltip()
  95. {
  96. if (m_tooltip_window) {
  97. m_tooltip_window->hide();
  98. delete m_tooltip_window;
  99. m_tooltip_window = nullptr;
  100. }
  101. }
  102. void GApplication::did_delete_last_window(Badge<GWindow>)
  103. {
  104. if (m_quit_when_last_window_deleted)
  105. m_event_loop->quit(0);
  106. }