Action.cpp 9.6 KB

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