/* * Copyright (c) 2018-2020, Andreas Kling * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include #include #include #include #include namespace GUI { namespace CommonActions { NonnullRefPtr make_about_action(const String& app_name, const Icon& app_icon, Window* parent) { auto weak_parent = AK::try_make_weak_ptr(parent); auto action = Action::create(String::formatted("&About {}", app_name), app_icon.bitmap_for_size(16), [=](auto&) { AboutDialog::show(app_name, app_icon.bitmap_for_size(32), weak_parent.ptr()); }); action->set_status_tip("Show application about box"); return action; } NonnullRefPtr make_open_action(Function callback, Core::Object* parent) { auto action = Action::create("&Open...", { Mod_Ctrl, Key_O }, Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"), move(callback), parent); action->set_status_tip("Open an existing file"); return action; } NonnullRefPtr make_save_action(Function callback, Core::Object* parent) { auto action = Action::create("&Save", { Mod_Ctrl, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), move(callback), parent); action->set_status_tip("Save the current file"); return action; } NonnullRefPtr make_save_as_action(Function callback, Core::Object* parent) { auto action = Action::create("Save &As...", { Mod_Ctrl | Mod_Shift, Key_S }, Gfx::Bitmap::load_from_file("/res/icons/16x16/save.png"), move(callback), parent); action->set_status_tip("Save the current file with a new name"); return action; } NonnullRefPtr make_move_to_front_action(Function callback, Core::Object* parent) { auto action = 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); action->set_status_tip("Move to the top of the stack"); return action; } NonnullRefPtr make_move_to_back_action(Function callback, Core::Object* parent) { auto action = 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); action->set_status_tip("Move to the bottom of the stack"); return action; } NonnullRefPtr make_undo_action(Function callback, Core::Object* parent) { return Action::create("&Undo", { Mod_Ctrl, Key_Z }, Gfx::Bitmap::load_from_file("/res/icons/16x16/undo.png"), move(callback), parent); } NonnullRefPtr make_redo_action(Function callback, Core::Object* parent) { return Action::create("&Redo", { Mod_Ctrl, Key_Y }, Gfx::Bitmap::load_from_file("/res/icons/16x16/redo.png"), move(callback), parent); } NonnullRefPtr make_delete_action(Function callback, Core::Object* parent) { return Action::create("&Delete", { Mod_None, Key_Delete }, Gfx::Bitmap::load_from_file("/res/icons/16x16/delete.png"), move(callback), parent); } NonnullRefPtr make_cut_action(Function callback, Core::Object* parent) { auto action = Action::create("Cu&t", { Mod_Ctrl, Key_X }, Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-cut.png"), move(callback), parent); action->set_status_tip("Cut to clipboard"); return action; } NonnullRefPtr make_copy_action(Function callback, Core::Object* parent) { auto action = Action::create("&Copy", { Mod_Ctrl, Key_C }, Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-copy.png"), move(callback), parent); action->set_status_tip("Copy to clipboard"); return action; } NonnullRefPtr make_paste_action(Function callback, Core::Object* parent) { auto action = Action::create("&Paste", { Mod_Ctrl, Key_V }, Gfx::Bitmap::load_from_file("/res/icons/16x16/paste.png"), move(callback), parent); action->set_status_tip("Paste from clipboard"); return action; } NonnullRefPtr make_fullscreen_action(Function callback, Core::Object* parent) { auto action = Action::create("&Fullscreen", { Mod_None, Key_F11 }, move(callback), parent); action->set_status_tip("Enter fullscreen mode"); return action; } NonnullRefPtr make_quit_action(Function callback) { auto action = Action::create("&Quit", { Mod_Alt, Key_F4 }, move(callback)); action->set_status_tip("Quit the application"); return action; } NonnullRefPtr make_help_action(Function callback, Core::Object* parent) { auto action = Action::create("&Contents", { Mod_None, Key_F1 }, Gfx::Bitmap::load_from_file("/res/icons/16x16/app-help.png"), move(callback), parent); action->set_status_tip("Show help contents"); return action; } NonnullRefPtr make_go_back_action(Function callback, Core::Object* parent) { auto action = Action::create("Go &Back", { Mod_Alt, Key_Left }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-back.png"), move(callback), parent); action->set_status_tip("Move one step backward in history"); return action; } NonnullRefPtr make_go_forward_action(Function callback, Core::Object* parent) { auto action = Action::create("Go &Forward", { Mod_Alt, Key_Right }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"), move(callback), parent); action->set_status_tip("Move one step forward in history"); return action; } NonnullRefPtr make_go_home_action(Function callback, Core::Object* parent) { return Action::create("Go &Home", { Mod_Alt, Key_Home }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-home.png"), move(callback), parent); } NonnullRefPtr make_reload_action(Function callback, Core::Object* parent) { return Action::create("&Reload", { Mod_Ctrl, Key_R }, Gfx::Bitmap::load_from_file("/res/icons/16x16/reload.png"), move(callback), parent); } NonnullRefPtr make_select_all_action(Function callback, Core::Object* parent) { return Action::create("Select &All", { Mod_Ctrl, Key_A }, Gfx::Bitmap::load_from_file("/res/icons/16x16/select-all.png"), move(callback), parent); } NonnullRefPtr make_properties_action(Function callback, Core::Object* parent) { return Action::create("&Properties", { Mod_Alt, Key_Return }, Gfx::Bitmap::load_from_file("/res/icons/16x16/properties.png"), move(callback), parent); } } NonnullRefPtr Action::create(String text, Function callback, Core::Object* parent) { return adopt(*new Action(move(text), move(callback), parent)); } NonnullRefPtr Action::create(String text, RefPtr icon, Function callback, Core::Object* parent) { return adopt(*new Action(move(text), move(icon), move(callback), parent)); } NonnullRefPtr Action::create(String text, const Shortcut& shortcut, Function callback, Core::Object* parent) { return adopt(*new Action(move(text), shortcut, move(callback), parent)); } NonnullRefPtr Action::create(String text, const Shortcut& shortcut, RefPtr icon, Function callback, Core::Object* parent) { return adopt(*new Action(move(text), shortcut, move(icon), move(callback), parent)); } NonnullRefPtr Action::create_checkable(String text, Function callback, Core::Object* parent) { return adopt(*new Action(move(text), move(callback), parent, true)); } NonnullRefPtr Action::create_checkable(String text, RefPtr icon, Function callback, Core::Object* parent) { return adopt(*new Action(move(text), move(icon), move(callback), parent, true)); } NonnullRefPtr Action::create_checkable(String text, const Shortcut& shortcut, Function callback, Core::Object* parent) { return adopt(*new Action(move(text), shortcut, move(callback), parent, true)); } NonnullRefPtr Action::create_checkable(String text, const Shortcut& shortcut, RefPtr icon, Function callback, Core::Object* parent) { return adopt(*new Action(move(text), shortcut, move(icon), move(callback), parent, true)); } Action::Action(String text, Function on_activation_callback, Core::Object* parent, bool checkable) : Action(move(text), Shortcut {}, nullptr, move(on_activation_callback), parent, checkable) { } Action::Action(String text, RefPtr icon, Function on_activation_callback, Core::Object* parent, bool checkable) : Action(move(text), Shortcut {}, move(icon), move(on_activation_callback), parent, checkable) { } Action::Action(String text, const Shortcut& shortcut, Function on_activation_callback, Core::Object* parent, bool checkable) : Action(move(text), shortcut, nullptr, move(on_activation_callback), parent, checkable) { } Action::Action(String text, const Shortcut& shortcut, RefPtr icon, Function on_activation_callback, Core::Object* parent, bool checkable) : Core::Object(parent) , on_activation(move(on_activation_callback)) , m_text(move(text)) , m_icon(move(icon)) , m_shortcut(shortcut) , m_checkable(checkable) { if (parent && is(*parent)) { m_scope = ShortcutScope::WidgetLocal; } else if (parent && is(*parent)) { m_scope = ShortcutScope::WindowLocal; } else { m_scope = ShortcutScope::ApplicationGlobal; if (auto* app = Application::the()) { app->register_global_shortcut_action({}, *this); } } } Action::~Action() { if (m_shortcut.is_valid() && m_scope == ShortcutScope::ApplicationGlobal) { if (auto* app = Application::the()) app->unregister_global_shortcut_action({}, *this); } } void Action::activate(Core::Object* activator) { if (!on_activation) return; if (activator) m_activator = activator->make_weak_ptr(); if (is_checkable()) { if (m_action_group) { if (m_action_group->is_unchecking_allowed()) set_checked(!is_checked()); else set_checked(true); } else { set_checked(!is_checked()); } } on_activation(*this); m_activator = nullptr; } void Action::register_button(Badge