Action.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2018-2021, 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_about_action(const String& app_name, const Icon& app_icon, Window* parent = nullptr);
  42. NonnullRefPtr<Action> make_open_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  43. NonnullRefPtr<Action> make_save_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  44. NonnullRefPtr<Action> make_save_as_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  45. NonnullRefPtr<Action> make_undo_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  46. NonnullRefPtr<Action> make_redo_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  47. NonnullRefPtr<Action> make_cut_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  48. NonnullRefPtr<Action> make_copy_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  49. NonnullRefPtr<Action> make_paste_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  50. NonnullRefPtr<Action> make_delete_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  51. NonnullRefPtr<Action> make_move_to_front_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  52. NonnullRefPtr<Action> make_move_to_back_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  53. NonnullRefPtr<Action> make_fullscreen_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  54. NonnullRefPtr<Action> make_quit_action(Function<void(Action&)>);
  55. NonnullRefPtr<Action> make_help_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  56. NonnullRefPtr<Action> make_go_back_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  57. NonnullRefPtr<Action> make_go_forward_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  58. NonnullRefPtr<Action> make_go_home_action(Function<void(Action&)> callback, Core::Object* parent = nullptr);
  59. NonnullRefPtr<Action> make_reload_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  60. NonnullRefPtr<Action> make_select_all_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  61. NonnullRefPtr<Action> make_properties_action(Function<void(Action&)>, Core::Object* parent = nullptr);
  62. };
  63. class Action final : public Core::Object {
  64. C_OBJECT(Action)
  65. public:
  66. enum class ShortcutScope {
  67. None,
  68. WidgetLocal,
  69. WindowLocal,
  70. ApplicationGlobal,
  71. };
  72. static NonnullRefPtr<Action> create(String text, Function<void(Action&)> callback, Core::Object* parent = nullptr);
  73. static NonnullRefPtr<Action> create(String text, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent = nullptr);
  74. static NonnullRefPtr<Action> create(String text, const Shortcut& shortcut, Function<void(Action&)> callback, Core::Object* parent = nullptr);
  75. static NonnullRefPtr<Action> create(String text, const Shortcut& shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent = nullptr);
  76. static NonnullRefPtr<Action> create_checkable(String text, Function<void(Action&)> callback, Core::Object* parent = nullptr);
  77. static NonnullRefPtr<Action> create_checkable(String text, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent = nullptr);
  78. static NonnullRefPtr<Action> create_checkable(String text, const Shortcut& shortcut, Function<void(Action&)> callback, Core::Object* parent = nullptr);
  79. static NonnullRefPtr<Action> create_checkable(String text, const Shortcut& shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent = nullptr);
  80. virtual ~Action() override;
  81. String text() const { return m_text; }
  82. void set_text(String text) { m_text = move(text); }
  83. String const& status_tip() const { return m_status_tip; }
  84. void set_status_tip(String status_tip) { m_status_tip = move(status_tip); }
  85. Shortcut shortcut() const { return m_shortcut; }
  86. const Gfx::Bitmap* icon() const { return m_icon.ptr(); }
  87. void set_icon(const Gfx::Bitmap*);
  88. const Core::Object* activator() const { return m_activator.ptr(); }
  89. Core::Object* activator() { return m_activator.ptr(); }
  90. Function<void(Action&)> on_activation;
  91. void activate(Core::Object* activator = nullptr);
  92. bool is_enabled() const { return m_enabled; }
  93. void set_enabled(bool);
  94. bool is_checkable() const { return m_checkable; }
  95. void set_checkable(bool checkable) { m_checkable = checkable; }
  96. bool is_checked() const
  97. {
  98. VERIFY(is_checkable());
  99. return m_checked;
  100. }
  101. void set_checked(bool);
  102. bool swallow_key_event_when_disabled() const { return m_swallow_key_event_when_disabled; }
  103. void set_swallow_key_event_when_disabled(bool swallow) { m_swallow_key_event_when_disabled = swallow; }
  104. void register_button(Badge<Button>, Button&);
  105. void unregister_button(Badge<Button>, Button&);
  106. void register_menu_item(Badge<MenuItem>, MenuItem&);
  107. void unregister_menu_item(Badge<MenuItem>, MenuItem&);
  108. const ActionGroup* group() const { return m_action_group.ptr(); }
  109. void set_group(Badge<ActionGroup>, ActionGroup*);
  110. private:
  111. Action(String, Function<void(Action&)> = nullptr, Core::Object* = nullptr, bool checkable = false);
  112. Action(String, const Shortcut&, Function<void(Action&)> = nullptr, Core::Object* = nullptr, bool checkable = false);
  113. Action(String, const Shortcut&, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> = nullptr, Core::Object* = nullptr, bool checkable = false);
  114. Action(String, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> = nullptr, Core::Object* = nullptr, bool checkable = false);
  115. template<typename Callback>
  116. void for_each_toolbar_button(Callback);
  117. template<typename Callback>
  118. void for_each_menu_item(Callback);
  119. String m_text;
  120. String m_status_tip;
  121. RefPtr<Gfx::Bitmap> m_icon;
  122. Shortcut m_shortcut;
  123. bool m_enabled { true };
  124. bool m_checkable { false };
  125. bool m_checked { false };
  126. bool m_swallow_key_event_when_disabled { false };
  127. ShortcutScope m_scope { ShortcutScope::None };
  128. HashTable<Button*> m_buttons;
  129. HashTable<MenuItem*> m_menu_items;
  130. WeakPtr<ActionGroup> m_action_group;
  131. WeakPtr<Core::Object> m_activator;
  132. };
  133. }