Action.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. #include <LibGUI/Action.h>
  27. #include <LibGUI/ActionGroup.h>
  28. #include <LibGUI/Application.h>
  29. #include <LibGUI/Button.h>
  30. #include <LibGUI/MenuItem.h>
  31. namespace GUI {
  32. namespace CommonActions {
  33. NonnullRefPtr<Action> make_open_action(Function<void(Action&)> callback, Core::Object* parent)
  34. {
  35. return Action::create("Open...", { Mod_Ctrl, Key_O }, Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"), move(callback), parent);
  36. }
  37. NonnullRefPtr<Action> make_move_to_front_action(Function<void(Action&)> callback, Core::Object* parent)
  38. {
  39. return Action::create("Move to front", { Mod_Ctrl | Mod_Shift, Key_Up }, Gfx::Bitmap::load_from_file("/res/icons/16x16/move-to-front.png"), move(callback), parent);
  40. }
  41. NonnullRefPtr<Action> make_move_to_back_action(Function<void(Action&)> callback, Core::Object* parent)
  42. {
  43. return Action::create("Move to back", { Mod_Ctrl | Mod_Shift, Key_Down }, Gfx::Bitmap::load_from_file("/res/icons/16x16/move-to-back.png"), move(callback), parent);
  44. }
  45. NonnullRefPtr<Action> make_undo_action(Function<void(Action&)> callback, Core::Object* parent)
  46. {
  47. return Action::create("Undo", { Mod_Ctrl, Key_Z }, Gfx::Bitmap::load_from_file("/res/icons/16x16/undo.png"), move(callback), parent);
  48. }
  49. NonnullRefPtr<Action> make_redo_action(Function<void(Action&)> callback, Core::Object* parent)
  50. {
  51. return Action::create("Redo", { Mod_Ctrl, Key_Y }, Gfx::Bitmap::load_from_file("/res/icons/16x16/redo.png"), move(callback), parent);
  52. }
  53. NonnullRefPtr<Action> make_delete_action(Function<void(Action&)> callback, Core::Object* parent)
  54. {
  55. return Action::create("Delete", { Mod_None, Key_Delete }, Gfx::Bitmap::load_from_file("/res/icons/16x16/delete.png"), move(callback), parent);
  56. }
  57. NonnullRefPtr<Action> make_cut_action(Function<void(Action&)> callback, Core::Object* parent)
  58. {
  59. return Action::create("Cut", { Mod_Ctrl, Key_X }, Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-cut.png"), move(callback), parent);
  60. }
  61. NonnullRefPtr<Action> make_copy_action(Function<void(Action&)> callback, Core::Object* parent)
  62. {
  63. return Action::create("Copy", { Mod_Ctrl, Key_C }, Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-copy.png"), move(callback), parent);
  64. }
  65. NonnullRefPtr<Action> make_paste_action(Function<void(Action&)> callback, Core::Object* parent)
  66. {
  67. return Action::create("Paste", { Mod_Ctrl, Key_V }, Gfx::Bitmap::load_from_file("/res/icons/paste16.png"), move(callback), parent);
  68. }
  69. NonnullRefPtr<Action> make_fullscreen_action(Function<void(Action&)> callback, Core::Object* parent)
  70. {
  71. return Action::create("Fullscreen", { Mod_None, Key_F11 }, move(callback), parent);
  72. }
  73. NonnullRefPtr<Action> make_quit_action(Function<void(Action&)> callback)
  74. {
  75. return Action::create("Quit", { Mod_Alt, Key_F4 }, move(callback));
  76. }
  77. NonnullRefPtr<Action> make_go_back_action(Function<void(Action&)> callback, Core::Object* parent)
  78. {
  79. return Action::create("Go back", { Mod_Alt, Key_Left }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-back.png"), move(callback), parent);
  80. }
  81. NonnullRefPtr<Action> make_go_forward_action(Function<void(Action&)> callback, Core::Object* parent)
  82. {
  83. return Action::create("Go forward", { Mod_Alt, Key_Right }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"), move(callback), parent);
  84. }
  85. NonnullRefPtr<Action> make_go_home_action(Function<void(Action&)> callback, Core::Object* parent)
  86. {
  87. return Action::create("Go home", { Mod_Alt, Key_Home }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-home.png"), move(callback), parent);
  88. }
  89. NonnullRefPtr<Action> make_reload_action(Function<void(Action&)> callback, Core::Object* parent)
  90. {
  91. return Action::create("Reload", { Mod_Ctrl, Key_R }, Gfx::Bitmap::load_from_file("/res/icons/16x16/reload.png"), move(callback), parent);
  92. }
  93. }
  94. Action::Action(const StringView& text, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  95. : Core::Object(parent)
  96. , on_activation(move(on_activation_callback))
  97. , m_text(text)
  98. , m_checkable(checkable)
  99. {
  100. }
  101. Action::Action(const StringView& text, RefPtr<Gfx::Bitmap>&& icon, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  102. : Core::Object(parent)
  103. , on_activation(move(on_activation_callback))
  104. , m_text(text)
  105. , m_icon(move(icon))
  106. , m_checkable(checkable)
  107. {
  108. }
  109. Action::Action(const StringView& text, const Shortcut& shortcut, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  110. : Action(text, shortcut, nullptr, move(on_activation_callback), parent, checkable)
  111. {
  112. }
  113. Action::Action(const StringView& text, const Shortcut& shortcut, RefPtr<Gfx::Bitmap>&& icon, Function<void(Action&)> on_activation_callback, Core::Object* parent, bool checkable)
  114. : Core::Object(parent)
  115. , on_activation(move(on_activation_callback))
  116. , m_text(text)
  117. , m_icon(move(icon))
  118. , m_shortcut(shortcut)
  119. , m_checkable(checkable)
  120. {
  121. if (parent && Core::is<Widget>(*parent)) {
  122. m_scope = ShortcutScope::WidgetLocal;
  123. } else if (parent && Core::is<Window>(*parent)) {
  124. m_scope = ShortcutScope::WindowLocal;
  125. } else {
  126. m_scope = ShortcutScope::ApplicationGlobal;
  127. Application::the().register_global_shortcut_action({}, *this);
  128. }
  129. }
  130. Action::~Action()
  131. {
  132. if (m_shortcut.is_valid() && m_scope == ShortcutScope::ApplicationGlobal)
  133. Application::the().unregister_global_shortcut_action({}, *this);
  134. }
  135. void Action::activate(Core::Object* activator)
  136. {
  137. if (!on_activation)
  138. return;
  139. if (activator)
  140. m_activator = activator->make_weak_ptr();
  141. if (is_checkable()) {
  142. if (m_action_group) {
  143. if (m_action_group->is_unchecking_allowed())
  144. set_checked(!is_checked());
  145. else
  146. set_checked(true);
  147. } else {
  148. set_checked(!is_checked());
  149. }
  150. }
  151. on_activation(*this);
  152. m_activator = nullptr;
  153. }
  154. void Action::register_button(Badge<Button>, Button& button)
  155. {
  156. m_buttons.set(&button);
  157. }
  158. void Action::unregister_button(Badge<Button>, Button& button)
  159. {
  160. m_buttons.remove(&button);
  161. }
  162. void Action::register_menu_item(Badge<MenuItem>, MenuItem& menu_item)
  163. {
  164. m_menu_items.set(&menu_item);
  165. }
  166. void Action::unregister_menu_item(Badge<MenuItem>, MenuItem& menu_item)
  167. {
  168. m_menu_items.remove(&menu_item);
  169. }
  170. template<typename Callback>
  171. void Action::for_each_toolbar_button(Callback callback)
  172. {
  173. for (auto& it : m_buttons)
  174. callback(*it);
  175. }
  176. template<typename Callback>
  177. void Action::for_each_menu_item(Callback callback)
  178. {
  179. for (auto& it : m_menu_items)
  180. callback(*it);
  181. }
  182. void Action::set_enabled(bool enabled)
  183. {
  184. if (m_enabled == enabled)
  185. return;
  186. m_enabled = enabled;
  187. for_each_toolbar_button([enabled](auto& button) {
  188. button.set_enabled(enabled);
  189. });
  190. for_each_menu_item([enabled](auto& item) {
  191. item.set_enabled(enabled);
  192. });
  193. }
  194. void Action::set_checked(bool checked)
  195. {
  196. if (m_checked == checked)
  197. return;
  198. m_checked = checked;
  199. if (m_checked && m_action_group) {
  200. m_action_group->for_each_action([this](auto& other_action) {
  201. if (this == &other_action)
  202. return IterationDecision::Continue;
  203. if (other_action.is_checkable())
  204. other_action.set_checked(false);
  205. return IterationDecision::Continue;
  206. });
  207. }
  208. for_each_toolbar_button([checked](auto& button) {
  209. button.set_checked(checked);
  210. });
  211. for_each_menu_item([checked](MenuItem& item) {
  212. item.set_checked(checked);
  213. });
  214. }
  215. void Action::set_group(Badge<ActionGroup>, ActionGroup* group)
  216. {
  217. m_action_group = group ? group->make_weak_ptr() : nullptr;
  218. }
  219. void Action::set_icon(const Gfx::Bitmap* icon)
  220. {
  221. m_icon = icon;
  222. }
  223. }