Application.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 <LibCore/EventLoop.h>
  27. #include <LibGUI/Action.h>
  28. #include <LibGUI/Application.h>
  29. #include <LibGUI/Desktop.h>
  30. #include <LibGUI/Label.h>
  31. #include <LibGUI/MenuBar.h>
  32. #include <LibGUI/Painter.h>
  33. #include <LibGUI/Window.h>
  34. #include <LibGUI/WindowServerConnection.h>
  35. #include <LibGfx/Font.h>
  36. #include <LibGfx/Palette.h>
  37. namespace GUI {
  38. static Application* s_the;
  39. Application& Application::the()
  40. {
  41. ASSERT(s_the);
  42. return *s_the;
  43. }
  44. Application::Application(int argc, char** argv)
  45. {
  46. (void)argc;
  47. (void)argv;
  48. ASSERT(!s_the);
  49. s_the = this;
  50. m_event_loop = make<Core::EventLoop>();
  51. WindowServerConnection::the();
  52. if (argc > 0)
  53. m_invoked_as = argv[0];
  54. for (int i = 1; i < argc; i++)
  55. m_args.append(argv[i]);
  56. }
  57. Application::~Application()
  58. {
  59. s_the = nullptr;
  60. }
  61. int Application::exec()
  62. {
  63. int exit_code = m_event_loop->exec();
  64. // NOTE: Maybe it would be cool to return instead of exit()?
  65. // This would require cleaning up all the CObjects on the heap.
  66. exit(exit_code);
  67. return exit_code;
  68. }
  69. void Application::quit(int exit_code)
  70. {
  71. m_event_loop->quit(exit_code);
  72. }
  73. void Application::set_menubar(OwnPtr<MenuBar>&& menubar)
  74. {
  75. if (m_menubar)
  76. m_menubar->notify_removed_from_application({});
  77. m_menubar = move(menubar);
  78. if (m_menubar)
  79. m_menubar->notify_added_to_application({});
  80. }
  81. void Application::register_global_shortcut_action(Badge<Action>, Action& action)
  82. {
  83. m_global_shortcut_actions.set(action.shortcut(), &action);
  84. }
  85. void Application::unregister_global_shortcut_action(Badge<Action>, Action& action)
  86. {
  87. m_global_shortcut_actions.remove(action.shortcut());
  88. }
  89. Action* Application::action_for_key_event(const KeyEvent& event)
  90. {
  91. auto it = m_global_shortcut_actions.find(Shortcut(event.modifiers(), (KeyCode)event.key()));
  92. if (it == m_global_shortcut_actions.end())
  93. return nullptr;
  94. return (*it).value;
  95. }
  96. class Application::TooltipWindow final : public Window {
  97. C_OBJECT(TooltipWindow);
  98. public:
  99. void set_tooltip(const StringView& tooltip)
  100. {
  101. // FIXME: Add some kind of GLabel auto-sizing feature.
  102. int text_width = m_label->font().width(tooltip);
  103. set_rect(100, 100, text_width + 10, m_label->font().glyph_height() + 8);
  104. m_label->set_text(tooltip);
  105. }
  106. private:
  107. TooltipWindow()
  108. {
  109. set_window_type(WindowType::Tooltip);
  110. m_label = set_main_widget<Label>();
  111. m_label->set_background_color(Color::from_rgb(0xdac7b5));
  112. m_label->set_fill_with_background_color(true);
  113. m_label->set_frame_thickness(1);
  114. m_label->set_frame_shape(Gfx::FrameShape::Container);
  115. m_label->set_frame_shadow(Gfx::FrameShadow::Plain);
  116. }
  117. RefPtr<Label> m_label;
  118. };
  119. void Application::show_tooltip(const StringView& tooltip, const Gfx::Point& screen_location)
  120. {
  121. if (!m_tooltip_window) {
  122. m_tooltip_window = TooltipWindow::construct();
  123. m_tooltip_window->set_double_buffering_enabled(false);
  124. }
  125. m_tooltip_window->set_tooltip(tooltip);
  126. Gfx::Rect desktop_rect = Desktop::the().rect();
  127. const int margin = 30;
  128. Gfx::Point adjusted_pos = screen_location;
  129. if (adjusted_pos.x() + m_tooltip_window->width() >= desktop_rect.width() - margin) {
  130. adjusted_pos = adjusted_pos.translated(-m_tooltip_window->width(), 0);
  131. }
  132. if (adjusted_pos.y() + m_tooltip_window->height() >= desktop_rect.height() - margin) {
  133. adjusted_pos = adjusted_pos.translated(0, -(m_tooltip_window->height() * 2));
  134. }
  135. m_tooltip_window->move_to(adjusted_pos);
  136. m_tooltip_window->show();
  137. }
  138. void Application::hide_tooltip()
  139. {
  140. if (m_tooltip_window) {
  141. m_tooltip_window->hide();
  142. m_tooltip_window = nullptr;
  143. }
  144. }
  145. void Application::did_create_window(Badge<Window>)
  146. {
  147. if (m_event_loop->was_exit_requested())
  148. m_event_loop->unquit();
  149. }
  150. void Application::did_delete_last_window(Badge<Window>)
  151. {
  152. if (m_quit_when_last_window_deleted)
  153. m_event_loop->quit(0);
  154. }
  155. void Application::set_system_palette(SharedBuffer& buffer)
  156. {
  157. if (!m_system_palette)
  158. m_system_palette = Gfx::PaletteImpl::create_with_shared_buffer(buffer);
  159. else
  160. m_system_palette->replace_internal_buffer({}, buffer);
  161. if (!m_palette)
  162. m_palette = m_system_palette;
  163. }
  164. void Application::set_palette(const Palette& palette)
  165. {
  166. m_palette = palette.impl();
  167. }
  168. Gfx::Palette Application::palette() const
  169. {
  170. return Palette(*m_palette);
  171. }
  172. }