GApplication.cpp 3.4 KB

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