summaryrefslogtreecommitdiff
path: root/Libraries/LibGUI/GAction.cpp
blob: d62ba660e9d1b2ea84ea41c541007f966cc25857 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <LibGUI/GAction.h>
#include <LibGUI/GActionGroup.h>
#include <LibGUI/GApplication.h>
#include <LibGUI/GButton.h>
#include <LibGUI/GMenuItem.h>

namespace GCommonActions {

NonnullRefPtr<GAction> make_open_action(Function<void(GAction&)> callback, GWidget* widget)
{
    return GAction::create("Open...", { Mod_Ctrl, Key_O }, GraphicsBitmap::load_from_file("/res/icons/16x16/open.png"), move(callback), widget);
}

NonnullRefPtr<GAction> make_move_to_front_action(Function<void(GAction&)> callback, GWidget* widget)
{
    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);
}

NonnullRefPtr<GAction> make_move_to_back_action(Function<void(GAction&)> callback, GWidget* widget)
{
    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);
}

NonnullRefPtr<GAction> make_undo_action(Function<void(GAction&)> callback, GWidget* widget)
{
    return GAction::create("Undo", { Mod_Ctrl, Key_Z }, GraphicsBitmap::load_from_file("/res/icons/16x16/undo.png"), move(callback), widget);
}

NonnullRefPtr<GAction> make_redo_action(Function<void(GAction&)> callback, GWidget* widget)
{
    return GAction::create("Redo", { Mod_Ctrl, Key_Y }, GraphicsBitmap::load_from_file("/res/icons/16x16/redo.png"), move(callback), widget);
}

NonnullRefPtr<GAction> make_delete_action(Function<void(GAction&)> callback, GWidget* widget)
{
    return GAction::create("Delete", { Mod_None, Key_Delete }, GraphicsBitmap::load_from_file("/res/icons/16x16/delete.png"), move(callback), widget);
}

NonnullRefPtr<GAction> make_cut_action(Function<void(GAction&)> callback, GWidget* widget)
{
    return GAction::create("Cut", { Mod_Ctrl, Key_X }, GraphicsBitmap::load_from_file("/res/icons/cut16.png"), move(callback), widget);
}

NonnullRefPtr<GAction> make_copy_action(Function<void(GAction&)> callback, GWidget* widget)
{
    return GAction::create("Copy", { Mod_Ctrl, Key_C }, GraphicsBitmap::load_from_file("/res/icons/16x16/edit-copy.png"), move(callback), widget);
}

NonnullRefPtr<GAction> make_paste_action(Function<void(GAction&)> callback, GWidget* widget)
{
    return GAction::create("Paste", { Mod_Ctrl, Key_V }, GraphicsBitmap::load_from_file("/res/icons/paste16.png"), move(callback), widget);
}

NonnullRefPtr<GAction> make_fullscreen_action(Function<void(GAction&)> callback, GWidget* widget)
{
    return GAction::create("Fullscreen", { Mod_None, Key_F11 }, move(callback), widget);
}

NonnullRefPtr<GAction> make_quit_action(Function<void(GAction&)> callback)
{
    return GAction::create("Quit", { Mod_Alt, Key_F4 }, move(callback));
}

NonnullRefPtr<GAction> make_go_back_action(Function<void(GAction&)> callback, GWidget* widget)
{
    return GAction::create("Go back", { Mod_Alt, Key_Left }, GraphicsBitmap::load_from_file("/res/icons/16x16/go-back.png"), move(callback), widget);
}

NonnullRefPtr<GAction> make_go_forward_action(Function<void(GAction&)> callback, GWidget* widget)
{
    return GAction::create("Go forward", { Mod_Alt, Key_Right }, GraphicsBitmap::load_from_file("/res/icons/16x16/go-forward.png"), move(callback), widget);
}

NonnullRefPtr<GAction> make_go_home_action(Function<void(GAction&)> callback, GWidget* widget)
{
    return GAction::create("Go home", { Mod_Alt, Key_Home }, GraphicsBitmap::load_from_file("/res/icons/16x16/go-home.png"), move(callback), widget);
}

NonnullRefPtr<GAction> make_reload_action(Function<void(GAction&)> callback, GWidget* widget)
{
    return GAction::create("Reload", { Mod_Ctrl, Key_R }, GraphicsBitmap::load_from_file("/res/icons/16x16/reload.png"), move(callback), widget);
}

}

GAction::GAction(const StringView& text, Function<void(GAction&)> on_activation_callback, GWidget* widget)
    : on_activation(move(on_activation_callback))
    , m_text(text)
    , m_widget(widget ? widget->make_weak_ptr() : nullptr)
{
}

GAction::GAction(const StringView& text, RefPtr<GraphicsBitmap>&& icon, Function<void(GAction&)> on_activation_callback, GWidget* widget)
    : on_activation(move(on_activation_callback))
    , m_text(text)
    , m_icon(move(icon))
    , m_widget(widget ? widget->make_weak_ptr() : nullptr)
{
}

GAction::GAction(const StringView& text, const GShortcut& shortcut, Function<void(GAction&)> on_activation_callback, GWidget* widget)
    : GAction(text, shortcut, nullptr, move(on_activation_callback), widget)
{
}

GAction::GAction(const StringView& text, const GShortcut& shortcut, RefPtr<GraphicsBitmap>&& icon, Function<void(GAction&)> on_activation_callback, GWidget* widget)
    : on_activation(move(on_activation_callback))
    , m_text(text)
    , m_icon(move(icon))
    , m_shortcut(shortcut)
    , m_widget(widget ? widget->make_weak_ptr() : nullptr)
{
    if (m_widget) {
        m_scope = ShortcutScope::WidgetLocal;
        m_widget->register_local_shortcut_action({}, *this);
    } else {
        m_scope = ShortcutScope::ApplicationGlobal;
        GApplication::the().register_global_shortcut_action({}, *this);
    }
}

GAction::~GAction()
{
    if (m_shortcut.is_valid() && m_scope == ShortcutScope::ApplicationGlobal)
        GApplication::the().unregister_global_shortcut_action({}, *this);
    if (m_widget && m_scope == ShortcutScope::WidgetLocal)
        m_widget->unregister_local_shortcut_action({}, *this);
}

void GAction::activate(CObject* activator)
{
    if (activator)
        m_activator = activator->make_weak_ptr();
    if (on_activation)
        on_activation(*this);
    m_activator = nullptr;
}

void GAction::register_button(Badge<GButton>, GButton& button)
{
    m_buttons.set(&button);
}

void GAction::unregister_button(Badge<GButton>, GButton& button)
{
    m_buttons.remove(&button);
}

void GAction::register_menu_item(Badge<GMenuItem>, GMenuItem& menu_item)
{
    m_menu_items.set(&menu_item);
}

void GAction::unregister_menu_item(Badge<GMenuItem>, GMenuItem& menu_item)
{
    m_menu_items.remove(&menu_item);
}

template<typename Callback>
void GAction::for_each_toolbar_button(Callback callback)
{
    for (auto& it : m_buttons)
        callback(*it);
}

template<typename Callback>
void GAction::for_each_menu_item(Callback callback)
{
    for (auto& it : m_menu_items)
        callback(*it);
}

void GAction::set_enabled(bool enabled)
{
    if (m_enabled == enabled)
        return;
    m_enabled = enabled;
    for_each_toolbar_button([enabled](GButton& button) {
        button.set_enabled(enabled);
    });
    for_each_menu_item([enabled](GMenuItem& item) {
        item.set_enabled(enabled);
    });
}

void GAction::set_checked(bool checked)
{
    if (m_checked == checked)
        return;
    m_checked = checked;

    if (m_checked && m_action_group) {
        m_action_group->for_each_action([this](auto& other_action) {
            if (this == &other_action)
                return IterationDecision::Continue;
            if (other_action.is_checkable())
                other_action.set_checked(false);
            return IterationDecision::Continue;
        });
    }

    for_each_toolbar_button([checked](GButton& button) {
        button.set_checked(checked);
    });
    for_each_menu_item([checked](GMenuItem& item) {
        item.set_checked(checked);
    });
}

void GAction::set_group(Badge<GActionGroup>, GActionGroup* group)
{
    m_action_group = group ? group->make_weak_ptr() : nullptr;
}