GApplication.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibDraw/Palette.h>
  27. #include <LibGUI/GAction.h>
  28. #include <LibGUI/GApplication.h>
  29. #include <LibGUI/GDesktop.h>
  30. #include <LibGUI/GLabel.h>
  31. #include <LibGUI/GMenuBar.h>
  32. #include <LibGUI/GPainter.h>
  33. #include <LibGUI/GWindow.h>
  34. #include <LibGUI/GWindowServerConnection.h>
  35. static GApplication* s_the;
  36. GApplication& GApplication::the()
  37. {
  38. ASSERT(s_the);
  39. return *s_the;
  40. }
  41. GApplication::GApplication(int argc, char** argv)
  42. {
  43. (void)argc;
  44. (void)argv;
  45. ASSERT(!s_the);
  46. s_the = this;
  47. m_event_loop = make<CEventLoop>();
  48. GWindowServerConnection::the();
  49. if (argc > 0)
  50. m_invoked_as = argv[0];
  51. for (int i = 1; i < argc; i++)
  52. m_args.append(argv[i]);
  53. }
  54. GApplication::~GApplication()
  55. {
  56. s_the = nullptr;
  57. }
  58. int GApplication::exec()
  59. {
  60. int exit_code = m_event_loop->exec();
  61. // NOTE: Maybe it would be cool to return instead of exit()?
  62. // This would require cleaning up all the CObjects on the heap.
  63. exit(exit_code);
  64. return exit_code;
  65. }
  66. void GApplication::quit(int exit_code)
  67. {
  68. m_event_loop->quit(exit_code);
  69. }
  70. void GApplication::set_menubar(OwnPtr<GMenuBar>&& menubar)
  71. {
  72. if (m_menubar)
  73. m_menubar->notify_removed_from_application({});
  74. m_menubar = move(menubar);
  75. if (m_menubar)
  76. m_menubar->notify_added_to_application({});
  77. }
  78. void GApplication::register_global_shortcut_action(Badge<GAction>, GAction& action)
  79. {
  80. m_global_shortcut_actions.set(action.shortcut(), &action);
  81. }
  82. void GApplication::unregister_global_shortcut_action(Badge<GAction>, GAction& action)
  83. {
  84. m_global_shortcut_actions.remove(action.shortcut());
  85. }
  86. GAction* GApplication::action_for_key_event(const GKeyEvent& event)
  87. {
  88. auto it = m_global_shortcut_actions.find(GShortcut(event.modifiers(), (KeyCode)event.key()));
  89. if (it == m_global_shortcut_actions.end())
  90. return nullptr;
  91. return (*it).value;
  92. }
  93. class GApplication::TooltipWindow final : public GWindow {
  94. public:
  95. TooltipWindow()
  96. {
  97. set_window_type(GWindowType::Tooltip);
  98. m_label = GLabel::construct();
  99. m_label->set_background_color(Color::from_rgb(0xdac7b5));
  100. m_label->set_fill_with_background_color(true);
  101. m_label->set_frame_thickness(1);
  102. m_label->set_frame_shape(FrameShape::Container);
  103. m_label->set_frame_shadow(FrameShadow::Plain);
  104. set_main_widget(m_label);
  105. }
  106. void set_tooltip(const StringView& tooltip)
  107. {
  108. // FIXME: Add some kind of GLabel auto-sizing feature.
  109. int text_width = m_label->font().width(tooltip);
  110. set_rect(100, 100, text_width + 10, m_label->font().glyph_height() + 8);
  111. m_label->set_text(tooltip);
  112. }
  113. RefPtr<GLabel> m_label;
  114. };
  115. void GApplication::show_tooltip(const StringView& tooltip, const Point& screen_location)
  116. {
  117. if (!m_tooltip_window) {
  118. m_tooltip_window = new TooltipWindow;
  119. m_tooltip_window->set_double_buffering_enabled(false);
  120. }
  121. m_tooltip_window->set_tooltip(tooltip);
  122. Rect desktop_rect = GDesktop::the().rect();
  123. const int margin = 30;
  124. Point adjusted_pos = screen_location;
  125. if (adjusted_pos.x() + m_tooltip_window->width() >= desktop_rect.width() - margin) {
  126. adjusted_pos = adjusted_pos.translated(-m_tooltip_window->width(), 0);
  127. }
  128. if (adjusted_pos.y() + m_tooltip_window->height() >= desktop_rect.height() - margin) {
  129. adjusted_pos = adjusted_pos.translated(0, -(m_tooltip_window->height() * 2));
  130. }
  131. m_tooltip_window->move_to(adjusted_pos);
  132. m_tooltip_window->show();
  133. }
  134. void GApplication::hide_tooltip()
  135. {
  136. if (m_tooltip_window) {
  137. m_tooltip_window->hide();
  138. m_tooltip_window = nullptr;
  139. }
  140. }
  141. void GApplication::did_create_window(Badge<GWindow>)
  142. {
  143. if (m_event_loop->was_exit_requested())
  144. m_event_loop->unquit();
  145. }
  146. void GApplication::did_delete_last_window(Badge<GWindow>)
  147. {
  148. if (m_quit_when_last_window_deleted)
  149. m_event_loop->quit(0);
  150. }
  151. void GApplication::set_system_palette(SharedBuffer& buffer)
  152. {
  153. if (!m_system_palette)
  154. m_system_palette = PaletteImpl::create_with_shared_buffer(buffer);
  155. else
  156. m_system_palette->replace_internal_buffer({}, buffer);
  157. if (!m_palette)
  158. m_palette = m_system_palette;
  159. }
  160. void GApplication::set_palette(const Palette& palette)
  161. {
  162. m_palette = palette.impl();
  163. }