GAction.cpp 8.7 KB

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