Action.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #pragma once
  27. #include <AK/Badge.h>
  28. #include <AK/Function.h>
  29. #include <AK/HashTable.h>
  30. #include <AK/NonnullRefPtr.h>
  31. #include <AK/RefCounted.h>
  32. #include <AK/String.h>
  33. #include <AK/WeakPtr.h>
  34. #include <AK/Weakable.h>
  35. #include <LibCore/Object.h>
  36. #include <LibGUI/Forward.h>
  37. #include <LibGUI/Shortcut.h>
  38. #include <LibGfx/Forward.h>
  39. namespace GUI {
  40. namespace CommonActions {
  41. NonnullRefPtr<Action> make_open_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  42. NonnullRefPtr<Action> make_undo_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  43. NonnullRefPtr<Action> make_redo_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  44. NonnullRefPtr<Action> make_cut_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  45. NonnullRefPtr<Action> make_copy_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  46. NonnullRefPtr<Action> make_paste_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  47. NonnullRefPtr<Action> make_delete_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  48. NonnullRefPtr<Action> make_move_to_front_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  49. NonnullRefPtr<Action> make_move_to_back_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  50. NonnullRefPtr<Action> make_fullscreen_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  51. NonnullRefPtr<Action> make_quit_action(Function<void(Action&)>);
  52. NonnullRefPtr<Action> make_go_back_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  53. NonnullRefPtr<Action> make_go_forward_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  54. NonnullRefPtr<Action> make_go_home_action(Function<void(Action&)> callback, Core::Object* parent = nullptr);
  55. NonnullRefPtr<Action> make_reload_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  56. };
  57. class Action final : public Core::Object {
  58. C_OBJECT(Action)
  59. public:
  60. enum class ShortcutScope {
  61. None,
  62. WidgetLocal,
  63. WindowLocal,
  64. ApplicationGlobal,
  65. };
  66. static NonnullRefPtr<Action> create(const StringView& text, Function<void(Action&)> callback, Core::Object* parent = nullptr)
  67. {
  68. return adopt(*new Action(text, move(callback), parent));
  69. }
  70. static NonnullRefPtr<Action> create(const StringView& text, RefPtr<Gfx::Bitmap>&& icon, Function<void(Action&)> callback, Core::Object* parent = nullptr)
  71. {
  72. return adopt(*new Action(text, move(icon), move(callback), parent));
  73. }
  74. static NonnullRefPtr<Action> create(const StringView& text, const Shortcut& shortcut, Function<void(Action&)> callback, Core::Object* parent = nullptr)
  75. {
  76. return adopt(*new Action(text, shortcut, move(callback), parent));
  77. }
  78. static NonnullRefPtr<Action> create(const StringView& text, const Shortcut& shortcut, RefPtr<Gfx::Bitmap>&& icon, Function<void(Action&)> callback, Core::Object* parent = nullptr)
  79. {
  80. return adopt(*new Action(text, shortcut, move(icon), move(callback), parent));
  81. }
  82. static NonnullRefPtr<Action> create_checkable(const StringView& text, Function<void(Action&)> callback, Core::Object* parent = nullptr)
  83. {
  84. return adopt(*new Action(text, move(callback), parent, true));
  85. }
  86. static NonnullRefPtr<Action> create_checkable(const StringView& text, RefPtr<Gfx::Bitmap>&& icon, Function<void(Action&)> callback, Core::Object* parent = nullptr)
  87. {
  88. return adopt(*new Action(text, move(icon), move(callback), parent, true));
  89. }
  90. static NonnullRefPtr<Action> create_checkable(const StringView& text, const Shortcut& shortcut, Function<void(Action&)> callback, Core::Object* parent = nullptr)
  91. {
  92. return adopt(*new Action(text, shortcut, move(callback), parent, true));
  93. }
  94. static NonnullRefPtr<Action> create_checkable(const StringView& text, const Shortcut& shortcut, RefPtr<Gfx::Bitmap>&& icon, Function<void(Action&)> callback, Core::Object* parent = nullptr)
  95. {
  96. return adopt(*new Action(text, shortcut, move(icon), move(callback), parent, true));
  97. }
  98. virtual ~Action() override;
  99. String text() const { return m_text; }
  100. Shortcut shortcut() const { return m_shortcut; }
  101. const Gfx::Bitmap* icon() const { return m_icon.ptr(); }
  102. void set_icon(const Gfx::Bitmap*);
  103. const Core::Object* activator() const { return m_activator.ptr(); }
  104. Core::Object* activator() { return m_activator.ptr(); }
  105. Function<void(Action&)> on_activation;
  106. void activate(Core::Object* activator = nullptr);
  107. bool is_enabled() const { return m_enabled; }
  108. void set_enabled(bool);
  109. bool is_checkable() const { return m_checkable; }
  110. void set_checkable(bool checkable) { m_checkable = checkable; }
  111. bool is_checked() const
  112. {
  113. ASSERT(is_checkable());
  114. return m_checked;
  115. }
  116. void set_checked(bool);
  117. void register_button(Badge<Button>, Button&);
  118. void unregister_button(Badge<Button>, Button&);
  119. void register_menu_item(Badge<MenuItem>, MenuItem&);
  120. void unregister_menu_item(Badge<MenuItem>, MenuItem&);
  121. const ActionGroup* group() const { return m_action_group.ptr(); }
  122. void set_group(Badge<ActionGroup>, ActionGroup*);
  123. private:
  124. virtual bool is_action() const override { return true; }
  125. Action(const StringView& text, Function<void(Action&)> = nullptr, Core::Object* = nullptr, bool checkable = false);
  126. Action(const StringView& text, const Shortcut&, Function<void(Action&)> = nullptr, Core::Object* = nullptr, bool checkable = false);
  127. Action(const StringView& text, const Shortcut&, RefPtr<Gfx::Bitmap>&& icon, Function<void(Action&)> = nullptr, Core::Object* = nullptr, bool checkable = false);
  128. Action(const StringView& text, RefPtr<Gfx::Bitmap>&& icon, Function<void(Action&)> = nullptr, Core::Object* = nullptr, bool checkable = false);
  129. template<typename Callback>
  130. void for_each_toolbar_button(Callback);
  131. template<typename Callback>
  132. void for_each_menu_item(Callback);
  133. String m_text;
  134. RefPtr<Gfx::Bitmap> m_icon;
  135. Shortcut m_shortcut;
  136. bool m_enabled { true };
  137. bool m_checkable { false };
  138. bool m_checked { false };
  139. ShortcutScope m_scope { ShortcutScope::None };
  140. HashTable<Button*> m_buttons;
  141. HashTable<MenuItem*> m_menu_items;
  142. WeakPtr<ActionGroup> m_action_group;
  143. WeakPtr<Core::Object> m_activator;
  144. };
  145. }
  146. template<>
  147. inline bool Core::is<GUI::Action>(const Core::Object& object)
  148. {
  149. return object.is_action();
  150. }