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