Action.cpp 9.6 KB

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