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