| 
									
										
										
										
											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. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-12 14:09:48 +01:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 11:53:16 +02:00
										 |  |  | #include <AK/Badge.h>
 | 
					
						
							| 
									
										
										
										
											2019-02-12 14:09:48 +01:00
										 |  |  | #include <AK/Function.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-28 11:53:16 +02:00
										 |  |  | #include <AK/HashTable.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-21 18:45:35 +02:00
										 |  |  | #include <AK/NonnullRefPtr.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-01 15:28:07 -05:00
										 |  |  | #include <AK/RefCounted.h>
 | 
					
						
							| 
									
										
										
										
											2020-01-10 19:06:00 +03:00
										 |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2019-04-20 21:56:56 +02:00
										 |  |  | #include <AK/WeakPtr.h>
 | 
					
						
							| 
									
										
										
										
											2019-05-28 11:53:16 +02:00
										 |  |  | #include <AK/Weakable.h>
 | 
					
						
							| 
									
										
										
										
											2019-07-18 10:15:00 +02:00
										 |  |  | #include <LibDraw/GraphicsBitmap.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-01 15:28:07 -05:00
										 |  |  | #include <LibGUI/GShortcut.h>
 | 
					
						
							|  |  |  | #include <LibGUI/GWindow.h>
 | 
					
						
							| 
									
										
										
										
											2019-02-12 14:09:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-01 15:28:07 -05:00
										 |  |  | class GAction; | 
					
						
							| 
									
										
										
										
											2019-07-09 22:10:03 +02:00
										 |  |  | class GActionGroup; | 
					
						
							| 
									
										
										
										
											2019-04-12 02:53:27 +02:00
										 |  |  | class GButton; | 
					
						
							|  |  |  | class GMenuItem; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-01 15:28:07 -05:00
										 |  |  | namespace GCommonActions { | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | NonnullRefPtr<GAction> make_open_action(Function<void(GAction&)>, Core::Object* parent = nullptr); | 
					
						
							|  |  |  | NonnullRefPtr<GAction> make_undo_action(Function<void(GAction&)>, Core::Object* parent = nullptr); | 
					
						
							|  |  |  | NonnullRefPtr<GAction> make_redo_action(Function<void(GAction&)>, Core::Object* parent = nullptr); | 
					
						
							|  |  |  | NonnullRefPtr<GAction> make_cut_action(Function<void(GAction&)>, Core::Object* parent = nullptr); | 
					
						
							|  |  |  | NonnullRefPtr<GAction> make_copy_action(Function<void(GAction&)>, Core::Object* parent = nullptr); | 
					
						
							|  |  |  | NonnullRefPtr<GAction> make_paste_action(Function<void(GAction&)>, Core::Object* parent = nullptr); | 
					
						
							|  |  |  | NonnullRefPtr<GAction> make_delete_action(Function<void(GAction&)>, Core::Object* parent = nullptr); | 
					
						
							|  |  |  | NonnullRefPtr<GAction> make_move_to_front_action(Function<void(GAction&)>, Core::Object* parent = nullptr); | 
					
						
							|  |  |  | NonnullRefPtr<GAction> make_move_to_back_action(Function<void(GAction&)>, Core::Object* parent = nullptr); | 
					
						
							|  |  |  | NonnullRefPtr<GAction> make_fullscreen_action(Function<void(GAction&)>, Core::Object* parent = nullptr); | 
					
						
							| 
									
										
										
										
											2019-09-14 22:10:44 +02:00
										 |  |  | NonnullRefPtr<GAction> make_quit_action(Function<void(GAction&)>); | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | NonnullRefPtr<GAction> make_go_back_action(Function<void(GAction&)>, Core::Object* parent = nullptr); | 
					
						
							|  |  |  | NonnullRefPtr<GAction> make_go_forward_action(Function<void(GAction&)>, Core::Object* parent = nullptr); | 
					
						
							|  |  |  | NonnullRefPtr<GAction> make_go_home_action(Function<void(GAction&)> callback, Core::Object* parent = nullptr); | 
					
						
							|  |  |  | NonnullRefPtr<GAction> make_reload_action(Function<void(GAction&)>, Core::Object* parent = nullptr); | 
					
						
							| 
									
										
										
										
											2019-09-01 15:28:07 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | class GAction final : public Core::Object { | 
					
						
							| 
									
										
										
										
											2020-02-02 01:57:57 +01:00
										 |  |  |     C_OBJECT(GAction) | 
					
						
							| 
									
										
										
										
											2019-02-12 14:09:48 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2019-06-07 17:13:23 +02:00
										 |  |  |     enum class ShortcutScope { | 
					
						
							| 
									
										
										
										
											2019-04-20 21:56:56 +02:00
										 |  |  |         None, | 
					
						
							|  |  |  |         WidgetLocal, | 
					
						
							| 
									
										
										
										
											2020-02-02 01:57:57 +01:00
										 |  |  |         WindowLocal, | 
					
						
							|  |  |  |         ApplicationGlobal, | 
					
						
							| 
									
										
										
										
											2019-04-20 21:56:56 +02:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     static NonnullRefPtr<GAction> create(const StringView& text, Function<void(GAction&)> callback, Core::Object* parent = nullptr) | 
					
						
							| 
									
										
										
										
											2019-02-20 02:39:46 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-02 01:57:57 +01:00
										 |  |  |         return adopt(*new GAction(text, move(callback), parent)); | 
					
						
							| 
									
										
										
										
											2019-02-20 02:39:46 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     static NonnullRefPtr<GAction> create(const StringView& text, RefPtr<GraphicsBitmap>&& icon, Function<void(GAction&)> callback, Core::Object* parent = nullptr) | 
					
						
							| 
									
										
										
										
											2019-02-20 02:39:46 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-02 01:57:57 +01:00
										 |  |  |         return adopt(*new GAction(text, move(icon), move(callback), parent)); | 
					
						
							| 
									
										
										
										
											2019-02-20 02:39:46 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     static NonnullRefPtr<GAction> create(const StringView& text, const GShortcut& shortcut, Function<void(GAction&)> callback, Core::Object* parent = nullptr) | 
					
						
							| 
									
										
										
										
											2019-03-03 02:52:22 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-02 01:57:57 +01:00
										 |  |  |         return adopt(*new GAction(text, shortcut, move(callback), parent)); | 
					
						
							| 
									
										
										
										
											2019-03-03 02:52:22 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     static NonnullRefPtr<GAction> create(const StringView& text, const GShortcut& shortcut, RefPtr<GraphicsBitmap>&& icon, Function<void(GAction&)> callback, Core::Object* parent = nullptr) | 
					
						
							| 
									
										
										
										
											2019-03-02 10:04:49 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-02-02 01:57:57 +01:00
										 |  |  |         return adopt(*new GAction(text, shortcut, move(icon), move(callback), parent)); | 
					
						
							| 
									
										
										
										
											2019-03-02 10:04:49 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-02 01:57:57 +01:00
										 |  |  |     virtual ~GAction() override; | 
					
						
							| 
									
										
										
										
											2019-04-20 21:56:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-12 14:09:48 +01:00
										 |  |  |     String text() const { return m_text; } | 
					
						
							| 
									
										
										
										
											2019-03-02 10:04:49 +01:00
										 |  |  |     GShortcut shortcut() const { return m_shortcut; } | 
					
						
							| 
									
										
										
										
											2019-02-20 02:39:46 +01:00
										 |  |  |     const GraphicsBitmap* icon() const { return m_icon.ptr(); } | 
					
						
							| 
									
										
										
										
											2019-08-26 18:54:44 +02:00
										 |  |  |     void set_icon(const GraphicsBitmap* icon) { m_icon = icon; } | 
					
						
							| 
									
										
										
										
											2019-02-12 14:09:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     const Core::Object* activator() const { return m_activator.ptr(); } | 
					
						
							|  |  |  |     Core::Object* activator() { return m_activator.ptr(); } | 
					
						
							| 
									
										
										
										
											2019-12-09 21:25:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-12 14:09:48 +01:00
										 |  |  |     Function<void(GAction&)> on_activation; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     void activate(Core::Object* activator = nullptr); | 
					
						
							| 
									
										
										
										
											2019-02-12 14:09:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-12 02:53:27 +02:00
										 |  |  |     bool is_enabled() const { return m_enabled; } | 
					
						
							|  |  |  |     void set_enabled(bool); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-26 21:09:56 +02:00
										 |  |  |     bool is_checkable() const { return m_checkable; } | 
					
						
							|  |  |  |     void set_checkable(bool checkable) { m_checkable = checkable; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 11:53:16 +02:00
										 |  |  |     bool is_checked() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         ASSERT(is_checkable()); | 
					
						
							|  |  |  |         return m_checked; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-04-26 21:09:56 +02:00
										 |  |  |     void set_checked(bool); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-12 02:53:27 +02:00
										 |  |  |     void register_button(Badge<GButton>, GButton&); | 
					
						
							|  |  |  |     void unregister_button(Badge<GButton>, GButton&); | 
					
						
							|  |  |  |     void register_menu_item(Badge<GMenuItem>, GMenuItem&); | 
					
						
							|  |  |  |     void unregister_menu_item(Badge<GMenuItem>, GMenuItem&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-09 22:10:03 +02:00
										 |  |  |     const GActionGroup* group() const { return m_action_group.ptr(); } | 
					
						
							|  |  |  |     void set_group(Badge<GActionGroup>, GActionGroup*); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-12 14:09:48 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-02-02 01:57:57 +01:00
										 |  |  |     virtual bool is_action() const override { return true; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     GAction(const StringView& text, Function<void(GAction&)> = nullptr, Core::Object* = nullptr); | 
					
						
							|  |  |  |     GAction(const StringView& text, const GShortcut&, Function<void(GAction&)> = nullptr, Core::Object* = nullptr); | 
					
						
							|  |  |  |     GAction(const StringView& text, const GShortcut&, RefPtr<GraphicsBitmap>&& icon, Function<void(GAction&)> = nullptr, Core::Object* = nullptr); | 
					
						
							|  |  |  |     GAction(const StringView& text, RefPtr<GraphicsBitmap>&& icon, Function<void(GAction&)> = nullptr, Core::Object* = nullptr); | 
					
						
							| 
									
										
										
										
											2019-02-20 02:39:46 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-28 11:53:16 +02:00
										 |  |  |     template<typename Callback> | 
					
						
							|  |  |  |     void for_each_toolbar_button(Callback); | 
					
						
							|  |  |  |     template<typename Callback> | 
					
						
							|  |  |  |     void for_each_menu_item(Callback); | 
					
						
							| 
									
										
										
										
											2019-04-12 02:53:27 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-12 14:09:48 +01:00
										 |  |  |     String m_text; | 
					
						
							| 
									
										
										
										
											2019-06-21 18:37:47 +02:00
										 |  |  |     RefPtr<GraphicsBitmap> m_icon; | 
					
						
							| 
									
										
										
										
											2019-03-02 10:04:49 +01:00
										 |  |  |     GShortcut m_shortcut; | 
					
						
							| 
									
										
										
										
											2019-04-12 02:53:27 +02:00
										 |  |  |     bool m_enabled { true }; | 
					
						
							| 
									
										
										
										
											2019-04-26 21:09:56 +02:00
										 |  |  |     bool m_checkable { false }; | 
					
						
							|  |  |  |     bool m_checked { false }; | 
					
						
							| 
									
										
										
										
											2019-04-20 21:56:56 +02:00
										 |  |  |     ShortcutScope m_scope { ShortcutScope::None }; | 
					
						
							| 
									
										
										
										
											2019-04-12 02:53:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     HashTable<GButton*> m_buttons; | 
					
						
							|  |  |  |     HashTable<GMenuItem*> m_menu_items; | 
					
						
							| 
									
										
										
										
											2019-07-09 22:10:03 +02:00
										 |  |  |     WeakPtr<GActionGroup> m_action_group; | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     WeakPtr<Core::Object> m_activator; | 
					
						
							| 
									
										
										
										
											2019-02-12 14:09:48 +01:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-02-02 01:57:57 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | template<> | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | inline bool Core::is<GAction>(const Core::Object& object) | 
					
						
							| 
									
										
										
										
											2020-02-02 01:57:57 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     return object.is_action(); | 
					
						
							|  |  |  | } |