2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2021-04-17 13:18:59 -03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-02-12 11:09:48 -02:00
|
|
|
#pragma once
|
|
|
|
|
|
2019-05-28 06:53:16 -03:00
|
|
|
#include <AK/Badge.h>
|
2019-02-12 11:09:48 -02:00
|
|
|
#include <AK/Function.h>
|
2019-05-28 06:53:16 -03:00
|
|
|
#include <AK/HashTable.h>
|
2019-06-21 13:45:35 -03:00
|
|
|
#include <AK/NonnullRefPtr.h>
|
2019-09-01 17:28:07 -03:00
|
|
|
#include <AK/RefCounted.h>
|
2020-01-10 13:06:00 -03:00
|
|
|
#include <AK/String.h>
|
2019-04-20 16:56:56 -03:00
|
|
|
#include <AK/WeakPtr.h>
|
2019-05-28 06:53:16 -03:00
|
|
|
#include <AK/Weakable.h>
|
2020-02-14 21:33:34 -03:00
|
|
|
#include <LibCore/Object.h>
|
2020-02-16 05:17:49 -03:00
|
|
|
#include <LibGUI/Forward.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/Shortcut.h>
|
2020-02-14 21:33:34 -03:00
|
|
|
#include <LibGfx/Forward.h>
|
2019-02-12 11:09:48 -02:00
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
namespace GUI {
|
|
|
|
|
|
|
|
|
|
namespace CommonActions {
|
2021-01-04 19:31:08 -03:00
|
|
|
NonnullRefPtr<Action> make_about_action(const String& app_name, const Icon& app_icon, Window* parent = nullptr);
|
2020-07-26 12:16:35 -03:00
|
|
|
NonnullRefPtr<Action> make_open_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
|
NonnullRefPtr<Action> make_save_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
2020-08-24 17:36:11 -03:00
|
|
|
NonnullRefPtr<Action> make_save_as_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
2020-07-26 12:16:35 -03:00
|
|
|
NonnullRefPtr<Action> make_undo_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
|
NonnullRefPtr<Action> make_redo_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
|
NonnullRefPtr<Action> make_cut_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
|
NonnullRefPtr<Action> make_copy_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
|
NonnullRefPtr<Action> make_paste_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
|
NonnullRefPtr<Action> make_delete_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
|
NonnullRefPtr<Action> make_move_to_front_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
|
NonnullRefPtr<Action> make_move_to_back_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
|
NonnullRefPtr<Action> make_fullscreen_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
|
NonnullRefPtr<Action> make_quit_action(Function<void(Action&)>);
|
2021-01-02 05:10:48 -03:00
|
|
|
NonnullRefPtr<Action> make_help_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
2020-07-26 12:16:35 -03:00
|
|
|
NonnullRefPtr<Action> make_go_back_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
|
NonnullRefPtr<Action> make_go_forward_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
|
NonnullRefPtr<Action> make_go_home_action(Function<void(Action&)> callback, Core::Object* parent = nullptr);
|
|
|
|
|
NonnullRefPtr<Action> make_reload_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
|
NonnullRefPtr<Action> make_select_all_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
2021-07-12 19:23:10 -03:00
|
|
|
NonnullRefPtr<Action> make_rename_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
2021-04-04 17:39:41 -03:00
|
|
|
NonnullRefPtr<Action> make_properties_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
2021-05-15 15:27:07 -03:00
|
|
|
NonnullRefPtr<Action> make_zoom_in_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
|
NonnullRefPtr<Action> make_zoom_out_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
|
NonnullRefPtr<Action> make_reset_zoom_action(Function<void(Action&)>, Core::Object* parent = nullptr);
|
|
|
|
|
|
2019-09-01 17:28:07 -03:00
|
|
|
};
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
class Action final : public Core::Object {
|
|
|
|
|
C_OBJECT(Action)
|
2019-02-12 11:09:48 -02:00
|
|
|
public:
|
2019-06-07 12:13:23 -03:00
|
|
|
enum class ShortcutScope {
|
2019-04-20 16:56:56 -03:00
|
|
|
None,
|
|
|
|
|
WidgetLocal,
|
2020-02-01 21:57:57 -03:00
|
|
|
WindowLocal,
|
|
|
|
|
ApplicationGlobal,
|
2019-04-20 16:56:56 -03:00
|
|
|
};
|
2021-02-20 08:45:55 -03:00
|
|
|
static NonnullRefPtr<Action> create(String text, Function<void(Action&)> callback, Core::Object* parent = nullptr);
|
|
|
|
|
static NonnullRefPtr<Action> create(String text, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent = nullptr);
|
|
|
|
|
static NonnullRefPtr<Action> create(String text, const Shortcut& shortcut, Function<void(Action&)> callback, Core::Object* parent = nullptr);
|
2021-06-24 05:53:47 -03:00
|
|
|
static NonnullRefPtr<Action> create(String text, const Shortcut& shortcut, const Shortcut& alternate_shortcut, Function<void(Action&)> callback, Core::Object* parent = nullptr);
|
2021-02-20 08:45:55 -03:00
|
|
|
static NonnullRefPtr<Action> create(String text, const Shortcut& shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent = nullptr);
|
2021-06-24 05:53:47 -03:00
|
|
|
static NonnullRefPtr<Action> create(String text, const Shortcut& shortcut, const Shortcut& alternate_shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent = nullptr);
|
2021-02-20 08:45:55 -03:00
|
|
|
static NonnullRefPtr<Action> create_checkable(String text, Function<void(Action&)> callback, Core::Object* parent = nullptr);
|
|
|
|
|
static NonnullRefPtr<Action> create_checkable(String text, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent = nullptr);
|
|
|
|
|
static NonnullRefPtr<Action> create_checkable(String text, const Shortcut& shortcut, Function<void(Action&)> callback, Core::Object* parent = nullptr);
|
|
|
|
|
static NonnullRefPtr<Action> create_checkable(String text, const Shortcut& shortcut, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> callback, Core::Object* parent = nullptr);
|
2020-04-21 12:19:27 -03:00
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
virtual ~Action() override;
|
2019-04-20 16:56:56 -03:00
|
|
|
|
2019-02-12 11:09:48 -02:00
|
|
|
String text() const { return m_text; }
|
2021-05-08 16:15:38 -03:00
|
|
|
void set_text(String);
|
2021-04-17 13:18:59 -03:00
|
|
|
|
2021-04-17 19:17:04 -03:00
|
|
|
String const& status_tip() const { return m_status_tip; }
|
|
|
|
|
void set_status_tip(String status_tip) { m_status_tip = move(status_tip); }
|
2021-04-17 13:18:59 -03:00
|
|
|
|
2021-06-24 12:34:44 -03:00
|
|
|
Shortcut const& shortcut() const { return m_shortcut; }
|
|
|
|
|
Shortcut const& alternate_shortcut() const { return m_alternate_shortcut; }
|
2020-02-06 07:56:38 -03:00
|
|
|
const Gfx::Bitmap* icon() const { return m_icon.ptr(); }
|
2020-02-14 19:02:47 -03:00
|
|
|
void set_icon(const Gfx::Bitmap*);
|
2019-02-12 11:09:48 -02:00
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
const Core::Object* activator() const { return m_activator.ptr(); }
|
|
|
|
|
Core::Object* activator() { return m_activator.ptr(); }
|
2019-12-09 17:25:48 -03:00
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
Function<void(Action&)> on_activation;
|
2019-02-12 11:09:48 -02:00
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
void activate(Core::Object* activator = nullptr);
|
2019-02-12 11:09:48 -02:00
|
|
|
|
2019-04-11 21:53:27 -03:00
|
|
|
bool is_enabled() const { return m_enabled; }
|
|
|
|
|
void set_enabled(bool);
|
|
|
|
|
|
2019-04-26 16:09:56 -03:00
|
|
|
bool is_checkable() const { return m_checkable; }
|
|
|
|
|
void set_checkable(bool checkable) { m_checkable = checkable; }
|
|
|
|
|
|
2019-05-28 06:53:16 -03:00
|
|
|
bool is_checked() const
|
|
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(is_checkable());
|
2019-05-28 06:53:16 -03:00
|
|
|
return m_checked;
|
|
|
|
|
}
|
2019-04-26 16:09:56 -03:00
|
|
|
void set_checked(bool);
|
|
|
|
|
|
2020-12-06 17:53:29 -03:00
|
|
|
bool swallow_key_event_when_disabled() const { return m_swallow_key_event_when_disabled; }
|
|
|
|
|
void set_swallow_key_event_when_disabled(bool swallow) { m_swallow_key_event_when_disabled = swallow; }
|
|
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
void register_button(Badge<Button>, Button&);
|
|
|
|
|
void unregister_button(Badge<Button>, Button&);
|
|
|
|
|
void register_menu_item(Badge<MenuItem>, MenuItem&);
|
|
|
|
|
void unregister_menu_item(Badge<MenuItem>, MenuItem&);
|
2019-04-11 21:53:27 -03:00
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
const ActionGroup* group() const { return m_action_group.ptr(); }
|
|
|
|
|
void set_group(Badge<ActionGroup>, ActionGroup*);
|
2019-07-09 17:10:03 -03:00
|
|
|
|
2019-02-12 11:09:48 -02:00
|
|
|
private:
|
2021-02-20 08:45:55 -03:00
|
|
|
Action(String, Function<void(Action&)> = nullptr, Core::Object* = nullptr, bool checkable = false);
|
|
|
|
|
Action(String, const Shortcut&, Function<void(Action&)> = nullptr, Core::Object* = nullptr, bool checkable = false);
|
2021-06-24 05:53:47 -03:00
|
|
|
Action(String, const Shortcut&, const Shortcut&, Function<void(Action&)> = nullptr, Core::Object* = nullptr, bool checkable = false);
|
|
|
|
|
Action(String, const Shortcut&, const Shortcut&, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> = nullptr, Core::Object* = nullptr, bool checkable = false);
|
2021-02-20 08:45:55 -03:00
|
|
|
Action(String, RefPtr<Gfx::Bitmap> icon, Function<void(Action&)> = nullptr, Core::Object* = nullptr, bool checkable = false);
|
2019-02-19 22:39:46 -03:00
|
|
|
|
2019-05-28 06:53:16 -03:00
|
|
|
template<typename Callback>
|
|
|
|
|
void for_each_toolbar_button(Callback);
|
|
|
|
|
template<typename Callback>
|
|
|
|
|
void for_each_menu_item(Callback);
|
2019-04-11 21:53:27 -03:00
|
|
|
|
2019-02-12 11:09:48 -02:00
|
|
|
String m_text;
|
2021-04-17 19:17:04 -03:00
|
|
|
String m_status_tip;
|
2020-02-06 07:56:38 -03:00
|
|
|
RefPtr<Gfx::Bitmap> m_icon;
|
2020-02-02 11:07:41 -03:00
|
|
|
Shortcut m_shortcut;
|
2021-06-24 05:53:47 -03:00
|
|
|
Shortcut m_alternate_shortcut;
|
2019-04-11 21:53:27 -03:00
|
|
|
bool m_enabled { true };
|
2019-04-26 16:09:56 -03:00
|
|
|
bool m_checkable { false };
|
|
|
|
|
bool m_checked { false };
|
2020-12-06 17:53:29 -03:00
|
|
|
bool m_swallow_key_event_when_disabled { false };
|
2019-04-20 16:56:56 -03:00
|
|
|
ShortcutScope m_scope { ShortcutScope::None };
|
2019-04-11 21:53:27 -03:00
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
HashTable<Button*> m_buttons;
|
|
|
|
|
HashTable<MenuItem*> m_menu_items;
|
|
|
|
|
WeakPtr<ActionGroup> m_action_group;
|
2020-02-02 08:34:39 -03:00
|
|
|
WeakPtr<Core::Object> m_activator;
|
2019-02-12 11:09:48 -02:00
|
|
|
};
|
2020-02-01 21:57:57 -03:00
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
}
|