| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  | #include <LibGUI/GAction.h>
 | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  | #include <LibGUI/GApplication.h>
 | 
					
						
							|  |  |  | #include <LibGUI/GEventLoop.h>
 | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  | #include <LibGUI/GLabel.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  | #include <LibGUI/GMenuBar.h>
 | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  | #include <LibGUI/GPainter.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  | #include <LibGUI/GWindow.h>
 | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  | #include <WindowServer/WSAPITypes.h>
 | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 14:56:23 +01:00
										 |  |  | static GApplication* s_the; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GApplication& GApplication::the() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     ASSERT(s_the); | 
					
						
							|  |  |  |     return *s_the; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  | GApplication::GApplication(int argc, char** argv) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-02-25 19:05:51 +01:00
										 |  |  |     (void)argc; | 
					
						
							|  |  |  |     (void)argv; | 
					
						
							| 
									
										
										
										
											2019-02-11 14:56:23 +01:00
										 |  |  |     ASSERT(!s_the); | 
					
						
							|  |  |  |     s_the = this; | 
					
						
							| 
									
										
										
										
											2019-09-22 20:50:39 +02:00
										 |  |  |     m_event_loop = make<CEventLoop>(); | 
					
						
							|  |  |  |     GWindowServerConnection::the(); | 
					
						
							| 
									
										
										
										
											2019-10-06 22:38:19 +11:00
										 |  |  |     if (argc > 0) | 
					
						
							|  |  |  |         m_invoked_as = argv[0]; | 
					
						
							|  |  |  |     for (int i = 1; i < argc; i++) | 
					
						
							|  |  |  |         m_args.append(argv[i]); | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GApplication::~GApplication() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-03-05 12:48:59 +01:00
										 |  |  |     s_the = nullptr; | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int GApplication::exec() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-03-05 12:48:59 +01:00
										 |  |  |     int exit_code = m_event_loop->exec(); | 
					
						
							|  |  |  |     // NOTE: Maybe it would be cool to return instead of exit()?
 | 
					
						
							| 
									
										
										
										
											2019-04-10 17:01:54 +02:00
										 |  |  |     //       This would require cleaning up all the CObjects on the heap.
 | 
					
						
							| 
									
										
										
										
											2019-03-05 12:48:59 +01:00
										 |  |  |     exit(exit_code); | 
					
						
							|  |  |  |     return exit_code; | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-17 09:58:35 +01:00
										 |  |  | void GApplication::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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  | void GApplication::set_menubar(OwnPtr<GMenuBar>&& menubar) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-20 21:56:56 +02:00
										 |  |  | void GApplication::register_global_shortcut_action(Badge<GAction>, GAction& 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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-20 21:56:56 +02:00
										 |  |  | void GApplication::unregister_global_shortcut_action(Badge<GAction>, GAction& 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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GAction* GApplication::action_for_key_event(const GKeyEvent& event) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-20 21:56:56 +02:00
										 |  |  |     auto it = m_global_shortcut_actions.find(GShortcut(event.modifiers(), (KeyCode)event.key())); | 
					
						
							|  |  |  |     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
										 |  |  | 
 | 
					
						
							|  |  |  | class GApplication::TooltipWindow final : public GWindow { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     TooltipWindow() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         set_window_type(GWindowType::Tooltip); | 
					
						
							| 
									
										
										
										
											2019-09-21 14:19:05 +02:00
										 |  |  |         m_label = GLabel::construct(); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											2019-04-10 03:43:46 +02:00
										 |  |  |         m_label->set_frame_shape(FrameShape::Container); | 
					
						
							|  |  |  |         m_label->set_frame_shadow(FrameShadow::Plain); | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  |         set_main_widget(m_label); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-02 14:58:02 +02:00
										 |  |  |     void set_tooltip(const StringView& tooltip) | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         // FIXME: Add some kind of GLabel auto-sizing feature.
 | 
					
						
							|  |  |  |         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); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-22 00:31:54 +02:00
										 |  |  |     RefPtr<GLabel> m_label; | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-02 14:58:02 +02:00
										 |  |  | void GApplication::show_tooltip(const StringView& tooltip, const Point& screen_location) | 
					
						
							| 
									
										
										
										
											2019-04-08 18:58:44 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!m_tooltip_window) { | 
					
						
							|  |  |  |         m_tooltip_window = new TooltipWindow; | 
					
						
							|  |  |  |         m_tooltip_window->set_double_buffering_enabled(false); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     m_tooltip_window->set_tooltip(tooltip); | 
					
						
							|  |  |  |     m_tooltip_window->move_to(screen_location); | 
					
						
							|  |  |  |     m_tooltip_window->show(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GApplication::hide_tooltip() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | void GApplication::did_delete_last_window(Badge<GWindow>) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_quit_when_last_window_deleted) | 
					
						
							|  |  |  |         m_event_loop->quit(0); | 
					
						
							|  |  |  | } |