GApplication.cpp 3.4 KB

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