GApplication.cpp 4.2 KB

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