| 
									
										
										
										
											2019-02-12 14:09:48 +01:00
										 |  |  | #include <LibGUI/GAction.h>
 | 
					
						
							| 
									
										
										
										
											2019-07-09 22:10:03 +02:00
										 |  |  | #include <LibGUI/GActionGroup.h>
 | 
					
						
							| 
									
										
										
										
											2019-03-03 12:32:15 +01:00
										 |  |  | #include <LibGUI/GApplication.h>
 | 
					
						
							| 
									
										
										
										
											2019-04-12 02:53:27 +02:00
										 |  |  | #include <LibGUI/GButton.h>
 | 
					
						
							|  |  |  | #include <LibGUI/GMenuItem.h>
 | 
					
						
							| 
									
										
										
										
											2019-02-12 14:09:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-14 22:10:44 +02:00
										 |  |  | namespace GCommonActions { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-14 22:23:49 +02:00
										 |  |  | NonnullRefPtr<GAction> make_undo_action(Function<void(GAction&)> callback, GWidget* widget) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return GAction::create("Undo", { Mod_Ctrl, Key_Z }, GraphicsBitmap::load_from_file("/res/icons/16x16/undo.png"), move(callback), widget); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | NonnullRefPtr<GAction> make_redo_action(Function<void(GAction&)> callback, GWidget* widget) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return GAction::create("Redo", { Mod_Ctrl, Key_Y }, GraphicsBitmap::load_from_file("/res/icons/16x16/redo.png"), move(callback), widget); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-14 22:10:44 +02:00
										 |  |  | NonnullRefPtr<GAction> make_delete_action(Function<void(GAction&)> callback, GWidget* widget) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return GAction::create("Delete", { Mod_None, Key_Delete }, GraphicsBitmap::load_from_file("/res/icons/16x16/delete.png"), move(callback), widget); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | NonnullRefPtr<GAction> make_cut_action(Function<void(GAction&)> callback, GWidget* widget) | 
					
						
							| 
									
										
										
										
											2019-09-01 15:28:07 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-09-14 22:10:44 +02:00
										 |  |  |     return GAction::create("Cut", { Mod_Ctrl, Key_X }, GraphicsBitmap::load_from_file("/res/icons/cut16.png"), move(callback), widget); | 
					
						
							| 
									
										
										
										
											2019-09-01 15:28:07 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-14 22:10:44 +02:00
										 |  |  | NonnullRefPtr<GAction> make_copy_action(Function<void(GAction&)> callback, GWidget* widget) | 
					
						
							| 
									
										
										
										
											2019-09-01 15:28:07 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-09-14 22:10:44 +02:00
										 |  |  |     return GAction::create("Copy", { Mod_Ctrl, Key_C }, GraphicsBitmap::load_from_file("/res/icons/16x16/edit-copy.png"), move(callback), widget); | 
					
						
							| 
									
										
										
										
											2019-09-01 15:28:07 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-14 22:10:44 +02:00
										 |  |  | NonnullRefPtr<GAction> make_paste_action(Function<void(GAction&)> callback, GWidget* widget) | 
					
						
							| 
									
										
										
										
											2019-09-01 15:28:07 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-09-14 22:10:44 +02:00
										 |  |  |     return GAction::create("Paste", { Mod_Ctrl, Key_V }, GraphicsBitmap::load_from_file("/res/icons/paste16.png"), move(callback), widget); | 
					
						
							| 
									
										
										
										
											2019-09-01 15:28:07 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-14 22:10:44 +02:00
										 |  |  | NonnullRefPtr<GAction> make_quit_action(Function<void(GAction&)> callback) | 
					
						
							| 
									
										
										
										
											2019-09-01 15:28:07 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-09-14 22:10:44 +02:00
										 |  |  |     return GAction::create("Quit", { Mod_Alt, Key_F4 }, move(callback)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-01 15:28:07 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 21:28:52 +02:00
										 |  |  | GAction::GAction(const StringView& text, Function<void(GAction&)> on_activation_callback, GWidget* widget) | 
					
						
							| 
									
										
										
										
											2019-02-20 02:39:46 +01:00
										 |  |  |     : on_activation(move(on_activation_callback)) | 
					
						
							|  |  |  |     , m_text(text) | 
					
						
							| 
									
										
										
										
											2019-04-20 21:56:56 +02:00
										 |  |  |     , m_widget(widget ? widget->make_weak_ptr() : nullptr) | 
					
						
							| 
									
										
										
										
											2019-02-12 14:09:48 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-21 18:37:47 +02:00
										 |  |  | GAction::GAction(const StringView& text, RefPtr<GraphicsBitmap>&& icon, Function<void(GAction&)> on_activation_callback, GWidget* widget) | 
					
						
							| 
									
										
										
										
											2019-02-20 02:39:46 +01:00
										 |  |  |     : on_activation(move(on_activation_callback)) | 
					
						
							|  |  |  |     , m_text(text) | 
					
						
							|  |  |  |     , m_icon(move(icon)) | 
					
						
							| 
									
										
										
										
											2019-04-20 21:56:56 +02:00
										 |  |  |     , m_widget(widget ? widget->make_weak_ptr() : nullptr) | 
					
						
							| 
									
										
										
										
											2019-02-20 02:39:46 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-02 14:58:02 +02:00
										 |  |  | GAction::GAction(const StringView& text, const GShortcut& shortcut, Function<void(GAction&)> on_activation_callback, GWidget* widget) | 
					
						
							| 
									
										
										
										
											2019-04-20 21:56:56 +02:00
										 |  |  |     : GAction(text, shortcut, nullptr, move(on_activation_callback), widget) | 
					
						
							| 
									
										
										
										
											2019-03-03 02:52:22 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-21 18:37:47 +02:00
										 |  |  | GAction::GAction(const StringView& text, const GShortcut& shortcut, RefPtr<GraphicsBitmap>&& icon, Function<void(GAction&)> on_activation_callback, GWidget* widget) | 
					
						
							| 
									
										
										
										
											2019-03-02 10:04:49 +01:00
										 |  |  |     : on_activation(move(on_activation_callback)) | 
					
						
							|  |  |  |     , m_text(text) | 
					
						
							|  |  |  |     , m_icon(move(icon)) | 
					
						
							|  |  |  |     , m_shortcut(shortcut) | 
					
						
							| 
									
										
										
										
											2019-04-20 21:56:56 +02:00
										 |  |  |     , m_widget(widget ? widget->make_weak_ptr() : nullptr) | 
					
						
							| 
									
										
										
										
											2019-03-02 10:04:49 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-20 21:56:56 +02:00
										 |  |  |     if (m_widget) { | 
					
						
							|  |  |  |         m_scope = ShortcutScope::WidgetLocal; | 
					
						
							| 
									
										
										
										
											2019-05-31 15:44:04 +02:00
										 |  |  |         m_widget->register_local_shortcut_action({}, *this); | 
					
						
							| 
									
										
										
										
											2019-04-20 21:56:56 +02:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         m_scope = ShortcutScope::ApplicationGlobal; | 
					
						
							| 
									
										
										
										
											2019-05-31 15:44:04 +02:00
										 |  |  |         GApplication::the().register_global_shortcut_action({}, *this); | 
					
						
							| 
									
										
										
										
											2019-04-20 21:56:56 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-03-02 10:04:49 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-12 14:09:48 +01:00
										 |  |  | GAction::~GAction() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-20 21:56:56 +02:00
										 |  |  |     if (m_shortcut.is_valid() && m_scope == ShortcutScope::ApplicationGlobal) | 
					
						
							| 
									
										
										
										
											2019-05-31 15:44:04 +02:00
										 |  |  |         GApplication::the().unregister_global_shortcut_action({}, *this); | 
					
						
							| 
									
										
										
										
											2019-04-20 21:56:56 +02:00
										 |  |  |     if (m_widget && m_scope == ShortcutScope::WidgetLocal) | 
					
						
							| 
									
										
										
										
											2019-05-31 15:44:04 +02:00
										 |  |  |         m_widget->unregister_local_shortcut_action({}, *this); | 
					
						
							| 
									
										
										
										
											2019-02-12 14:09:48 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GAction::activate() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (on_activation) | 
					
						
							|  |  |  |         on_activation(*this); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-04-12 02:53:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | void GAction::register_button(Badge<GButton>, GButton& button) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_buttons.set(&button); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GAction::unregister_button(Badge<GButton>, GButton& button) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_buttons.remove(&button); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GAction::register_menu_item(Badge<GMenuItem>, GMenuItem& menu_item) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_menu_items.set(&menu_item); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GAction::unregister_menu_item(Badge<GMenuItem>, GMenuItem& menu_item) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_menu_items.remove(&menu_item); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<typename Callback> | 
					
						
							|  |  |  | void GAction::for_each_toolbar_button(Callback callback) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     for (auto& it : m_buttons) | 
					
						
							|  |  |  |         callback(*it); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<typename Callback> | 
					
						
							|  |  |  | void GAction::for_each_menu_item(Callback callback) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     for (auto& it : m_menu_items) | 
					
						
							|  |  |  |         callback(*it); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GAction::set_enabled(bool enabled) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_enabled == enabled) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     m_enabled = enabled; | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |     for_each_toolbar_button([enabled](GButton& button) { | 
					
						
							| 
									
										
										
										
											2019-04-12 02:53:27 +02:00
										 |  |  |         button.set_enabled(enabled); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |     for_each_menu_item([enabled](GMenuItem& item) { | 
					
						
							| 
									
										
										
										
											2019-04-12 02:53:27 +02:00
										 |  |  |         item.set_enabled(enabled); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-04-26 21:09:56 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | void GAction::set_checked(bool checked) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_checked == checked) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     m_checked = checked; | 
					
						
							| 
									
										
										
										
											2019-07-09 22:10:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (m_checked && m_action_group) { | 
					
						
							|  |  |  |         m_action_group->for_each_action([this](auto& other_action) { | 
					
						
							|  |  |  |             if (this == &other_action) | 
					
						
							|  |  |  |                 return IterationDecision::Continue; | 
					
						
							|  |  |  |             if (other_action.is_checkable()) | 
					
						
							|  |  |  |                 other_action.set_checked(false); | 
					
						
							|  |  |  |             return IterationDecision::Continue; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |     for_each_toolbar_button([checked](GButton& button) { | 
					
						
							| 
									
										
										
										
											2019-04-26 21:09:56 +02:00
										 |  |  |         button.set_checked(checked); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |     for_each_menu_item([checked](GMenuItem& item) { | 
					
						
							| 
									
										
										
										
											2019-04-26 21:09:56 +02:00
										 |  |  |         item.set_checked(checked); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-07-09 22:10:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | void GAction::set_group(Badge<GActionGroup>, GActionGroup* group) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_action_group = group ? group->make_weak_ptr() : nullptr; | 
					
						
							|  |  |  | } |