| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-16 09:17:49 +01:00
										 |  |  | #include <LibCore/EventLoop.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 20:33:02 +01:00
										 |  |  | #include <LibGUI/Action.h>
 | 
					
						
							|  |  |  | #include <LibGUI/Application.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-14 22:51:15 +02:00
										 |  |  | #include <LibGUI/Clipboard.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 20:33:02 +01:00
										 |  |  | #include <LibGUI/Desktop.h>
 | 
					
						
							|  |  |  | #include <LibGUI/Label.h>
 | 
					
						
							|  |  |  | #include <LibGUI/MenuBar.h>
 | 
					
						
							|  |  |  | #include <LibGUI/Painter.h>
 | 
					
						
							|  |  |  | #include <LibGUI/Window.h>
 | 
					
						
							|  |  |  | #include <LibGUI/WindowServerConnection.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-14 23:53:11 +01:00
										 |  |  | #include <LibGfx/Font.h>
 | 
					
						
							|  |  |  | #include <LibGfx/Palette.h>
 | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | namespace GUI { | 
					
						
							| 
									
										
										
										
											2019-02-11 14:56:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | static Application* s_the; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Application& Application::the() | 
					
						
							| 
									
										
										
										
											2019-02-11 14:56:23 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     ASSERT(s_the); | 
					
						
							|  |  |  |     return *s_the; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | Application::Application(int argc, char** argv) | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-02-11 14:56:23 +01:00
										 |  |  |     ASSERT(!s_the); | 
					
						
							|  |  |  |     s_the = this; | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     m_event_loop = make<Core::EventLoop>(); | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     WindowServerConnection::the(); | 
					
						
							| 
									
										
										
										
											2020-05-14 22:51:15 +02:00
										 |  |  |     Clipboard::initialize({}); | 
					
						
							| 
									
										
										
										
											2019-10-06 22:38:19 +11:00
										 |  |  |     if (argc > 0) | 
					
						
							|  |  |  |         m_invoked_as = argv[0]; | 
					
						
							| 
									
										
										
										
											2020-05-12 15:47:13 +02:00
										 |  |  |     for (int i = 1; i < argc; i++) { | 
					
						
							|  |  |  |         String arg(argv[i]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (arg == "--gui-focus-debug") | 
					
						
							|  |  |  |             m_focus_debugging_enabled = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         m_args.append(move(arg)); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | Application::~Application() | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-03-05 12:48:59 +01:00
										 |  |  |     s_the = nullptr; | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | int Application::exec() | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-01 20:49:00 +02:00
										 |  |  |     return m_event_loop->exec(); | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Application::quit(int exit_code) | 
					
						
							| 
									
										
										
										
											2019-02-11 14:56:23 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-02-17 09:58:35 +01:00
										 |  |  |     m_event_loop->quit(exit_code); | 
					
						
							| 
									
										
										
										
											2019-02-11 14:56:23 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-21 16:01:00 +02:00
										 |  |  | void Application::set_menubar(RefPtr<MenuBar> menubar) | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-02-11 15:37:12 +01:00
										 |  |  |     if (m_menubar) | 
					
						
							| 
									
										
										
										
											2019-05-31 15:44:04 +02:00
										 |  |  |         m_menubar->notify_removed_from_application({}); | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  |     m_menubar = move(menubar); | 
					
						
							| 
									
										
										
										
											2019-02-11 15:37:12 +01:00
										 |  |  |     if (m_menubar) | 
					
						
							| 
									
										
										
										
											2019-05-31 15:44:04 +02:00
										 |  |  |         m_menubar->notify_added_to_application({}); | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Application::register_global_shortcut_action(Badge<Action>, Action& action) | 
					
						
							| 
									
										
										
										
											2019-03-03 12:32:15 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-20 21:56:56 +02:00
										 |  |  |     m_global_shortcut_actions.set(action.shortcut(), &action); | 
					
						
							| 
									
										
										
										
											2019-03-03 12:32:15 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Application::unregister_global_shortcut_action(Badge<Action>, Action& action) | 
					
						
							| 
									
										
										
										
											2019-03-03 12:32:15 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-20 21:56:56 +02:00
										 |  |  |     m_global_shortcut_actions.remove(action.shortcut()); | 
					
						
							| 
									
										
										
										
											2019-03-03 12:32:15 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | Action* Application::action_for_key_event(const KeyEvent& event) | 
					
						
							| 
									
										
										
										
											2019-03-03 12:32:15 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     auto it = m_global_shortcut_actions.find(Shortcut(event.modifiers(), (KeyCode)event.key())); | 
					
						
							| 
									
										
										
										
											2019-04-20 21:56:56 +02:00
										 |  |  |     if (it == m_global_shortcut_actions.end()) | 
					
						
							| 
									
										
										
										
											2019-03-03 12:32:15 +01:00
										 |  |  |         return nullptr; | 
					
						
							|  |  |  |     return (*it).value; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | class Application::TooltipWindow final : public Window { | 
					
						
							| 
									
										
										
										
											2020-02-15 01:27:37 +01:00
										 |  |  |     C_OBJECT(TooltipWindow); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-02-15 01:27:37 +01:00
										 |  |  |     void set_tooltip(const StringView& tooltip) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-03-19 22:29:01 +01:00
										 |  |  |         // FIXME: Add some kind of GUI::Label auto-sizing feature.
 | 
					
						
							| 
									
										
										
										
											2020-02-15 01:27:37 +01:00
										 |  |  |         int text_width = m_label->font().width(tooltip); | 
					
						
							|  |  |  |         set_rect(100, 100, text_width + 10, m_label->font().glyph_height() + 8); | 
					
						
							|  |  |  |         m_label->set_text(tooltip); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  |     TooltipWindow() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |         set_window_type(WindowType::Tooltip); | 
					
						
							| 
									
										
										
										
											2020-03-04 09:46:23 +01:00
										 |  |  |         m_label = set_main_widget<Label>(); | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  |         m_label->set_background_color(Color::from_rgb(0xdac7b5)); | 
					
						
							|  |  |  |         m_label->set_fill_with_background_color(true); | 
					
						
							|  |  |  |         m_label->set_frame_thickness(1); | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |         m_label->set_frame_shape(Gfx::FrameShape::Container); | 
					
						
							|  |  |  |         m_label->set_frame_shadow(Gfx::FrameShadow::Plain); | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     RefPtr<Label> m_label; | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 10:57:59 +02:00
										 |  |  | void Application::show_tooltip(const StringView& tooltip, const Gfx::IntPoint& screen_location) | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!m_tooltip_window) { | 
					
						
							| 
									
										
										
										
											2020-02-15 01:27:37 +01:00
										 |  |  |         m_tooltip_window = TooltipWindow::construct(); | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  |         m_tooltip_window->set_double_buffering_enabled(false); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     m_tooltip_window->set_tooltip(tooltip); | 
					
						
							| 
									
										
										
										
											2019-12-05 17:59:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 10:57:59 +02:00
										 |  |  |     Gfx::IntRect desktop_rect = Desktop::the().rect(); | 
					
						
							| 
									
										
										
										
											2019-12-05 17:59:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const int margin = 30; | 
					
						
							| 
									
										
										
										
											2020-06-10 10:57:59 +02:00
										 |  |  |     Gfx::IntPoint adjusted_pos = screen_location; | 
					
						
							| 
									
										
										
										
											2019-12-05 17:59:06 +01:00
										 |  |  |     if (adjusted_pos.x() + m_tooltip_window->width() >= desktop_rect.width() - margin) { | 
					
						
							|  |  |  |         adjusted_pos = adjusted_pos.translated(-m_tooltip_window->width(), 0); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (adjusted_pos.y() + m_tooltip_window->height() >= desktop_rect.height() - margin) { | 
					
						
							|  |  |  |         adjusted_pos = adjusted_pos.translated(0, -(m_tooltip_window->height() * 2)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     m_tooltip_window->move_to(adjusted_pos); | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  |     m_tooltip_window->show(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Application::hide_tooltip() | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-07-23 20:51:08 +02:00
										 |  |  |     if (m_tooltip_window) { | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  |         m_tooltip_window->hide(); | 
					
						
							| 
									
										
										
										
											2019-07-23 20:51:08 +02:00
										 |  |  |         m_tooltip_window = nullptr; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2019-07-23 18:16:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Application::did_create_window(Badge<Window>) | 
					
						
							| 
									
										
										
										
											2019-10-25 00:23:35 -05:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_event_loop->was_exit_requested()) | 
					
						
							|  |  |  |         m_event_loop->unquit(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Application::did_delete_last_window(Badge<Window>) | 
					
						
							| 
									
										
										
										
											2019-07-23 18:16:25 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_quit_when_last_window_deleted) | 
					
						
							|  |  |  |         m_event_loop->quit(0); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-12-24 20:57:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Application::set_system_palette(SharedBuffer& buffer) | 
					
						
							| 
									
										
										
										
											2019-12-24 20:57:54 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!m_system_palette) | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |         m_system_palette = Gfx::PaletteImpl::create_with_shared_buffer(buffer); | 
					
						
							| 
									
										
										
										
											2019-12-24 20:57:54 +01:00
										 |  |  |     else | 
					
						
							|  |  |  |         m_system_palette->replace_internal_buffer({}, buffer); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!m_palette) | 
					
						
							|  |  |  |         m_palette = m_system_palette; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void Application::set_palette(const Palette& palette) | 
					
						
							| 
									
										
										
										
											2019-12-24 20:57:54 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-12-29 00:47:49 +01:00
										 |  |  |     m_palette = palette.impl(); | 
					
						
							| 
									
										
										
										
											2019-12-24 20:57:54 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-15 01:18:12 +01:00
										 |  |  | Gfx::Palette Application::palette() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return Palette(*m_palette); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | } |