summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/Event.h
blob: a0b013b0c101a47b28d8bb2ee3fcf0e943efa710 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/StringBuilder.h>
#include <AK/Vector.h>
#include <Kernel/API/KeyCode.h>
#include <LibCore/Event.h>
#include <LibGUI/FocusSource.h>
#include <LibGUI/Forward.h>
#include <LibGUI/WindowType.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Point.h>
#include <LibGfx/Rect.h>

namespace GUI {

class Event : public Core::Event {
public:
    enum Type {
        Show = 1000,
        Hide,
        Paint,
        MultiPaint,
        Resize,
        MouseMove,
        MouseDown,
        MouseDoubleClick,
        MouseUp,
        MouseWheel,
        Enter,
        Leave,
        KeyDown,
        KeyUp,
        WindowEntered,
        WindowLeft,
        WindowBecameInactive,
        WindowBecameActive,
        WindowInputEntered,
        WindowInputLeft,
        FocusIn,
        FocusOut,
        WindowCloseRequest,
        ContextMenu,
        EnabledChange,
        DragEnter,
        DragLeave,
        DragMove,
        Drop,
        ThemeChange,
        FontsChange,
        ScreenRectsChange,
        ActionEnter,
        ActionLeave,
        AppletAreaRectChange,

        __Begin_WM_Events,
        WM_WindowRemoved,
        WM_WindowStateChanged,
        WM_WindowRectChanged,
        WM_WindowIconBitmapChanged,
        WM_AppletAreaSizeChanged,
        WM_SuperKeyPressed,
        WM_SuperSpaceKeyPressed,
        WM_SuperDigitKeyPressed,
        WM_WorkspaceChanged,
        WM_KeymapChanged,
        __End_WM_Events,
    };

    Event() = default;
    explicit Event(Type type)
        : Core::Event(type)
    {
    }
    virtual ~Event() = default;

    bool is_key_event() const { return type() == KeyUp || type() == KeyDown; }
    bool is_paint_event() const { return type() == Paint; }
};

class WMEvent : public Event {
public:
    WMEvent(Type type, int client_id, int window_id)
        : Event(type)
        , m_client_id(client_id)
        , m_window_id(window_id)
    {
    }

    int client_id() const { return m_client_id; }
    int window_id() const { return m_window_id; }

private:
    int m_client_id { -1 };
    int m_window_id { -1 };
};

class WMSuperKeyPressedEvent : public WMEvent {
public:
    explicit WMSuperKeyPressedEvent(int client_id)
        : WMEvent(Event::Type::WM_SuperKeyPressed, client_id, 0)
    {
    }
};

class WMSuperSpaceKeyPressedEvent : public WMEvent {
public:
    explicit WMSuperSpaceKeyPressedEvent(int client_id)
        : WMEvent(Event::Type::WM_SuperSpaceKeyPressed, client_id, 0)
    {
    }
};

class WMSuperDigitKeyPressedEvent : public WMEvent {
public:
    WMSuperDigitKeyPressedEvent(int client_id, u8 digit)
        : WMEvent(Event::Type::WM_SuperDigitKeyPressed, client_id, 0)
        , m_digit(digit)
    {
    }

    u8 digit() const { return m_digit; }

private:
    u8 m_digit { 0 };
};

class WMAppletAreaSizeChangedEvent : public WMEvent {
public:
    explicit WMAppletAreaSizeChangedEvent(Gfx::IntSize const& size)
        : WMEvent(Event::Type::WM_AppletAreaSizeChanged, 0, 0)
        , m_size(size)
    {
    }

    Gfx::IntSize const& size() const { return m_size; }

private:
    Gfx::IntSize m_size;
};

class WMWindowRemovedEvent : public WMEvent {
public:
    WMWindowRemovedEvent(int client_id, int window_id)
        : WMEvent(Event::Type::WM_WindowRemoved, client_id, window_id)
    {
    }
};

class WMWindowStateChangedEvent : public WMEvent {
public:
    WMWindowStateChangedEvent(int client_id, int window_id, int parent_client_id, int parent_window_id, StringView title, Gfx::IntRect const& rect, unsigned workspace_row, unsigned workspace_column, bool is_active, bool is_modal, WindowType window_type, bool is_minimized, bool is_frameless, Optional<int> progress)
        : WMEvent(Event::Type::WM_WindowStateChanged, client_id, window_id)
        , m_parent_client_id(parent_client_id)
        , m_parent_window_id(parent_window_id)
        , m_title(title)
        , m_rect(rect)
        , m_window_type(window_type)
        , m_workspace_row(workspace_row)
        , m_workspace_column(workspace_column)
        , m_active(is_active)
        , m_modal(is_modal)
        , m_minimized(is_minimized)
        , m_frameless(is_frameless)
        , m_progress(progress)
    {
    }

    int parent_client_id() const { return m_parent_client_id; }
    int parent_window_id() const { return m_parent_window_id; }
    String const& title() const { return m_title; }
    Gfx::IntRect const& rect() const { return m_rect; }
    bool is_active() const { return m_active; }
    bool is_modal() const { return m_modal; }
    WindowType window_type() const { return m_window_type; }
    bool is_minimized() const { return m_minimized; }
    bool is_frameless() const { return m_frameless; }
    Optional<int> progress() const { return m_progress; }
    unsigned workspace_row() const { return m_workspace_row; }
    unsigned workspace_column() const { return m_workspace_column; }

private:
    int m_parent_client_id;
    int m_parent_window_id;
    String m_title;
    Gfx::IntRect m_rect;
    WindowType m_window_type;
    unsigned m_workspace_row;
    unsigned m_workspace_column;
    bool m_active;
    bool m_modal;
    bool m_minimized;
    bool m_frameless;
    Optional<int> m_progress;
};

class WMWindowRectChangedEvent : public WMEvent {
public:
    WMWindowRectChangedEvent(int client_id, int window_id, Gfx::IntRect const& rect)
        : WMEvent(Event::Type::WM_WindowRectChanged, client_id, window_id)
        , m_rect(rect)
    {
    }

    Gfx::IntRect const& rect() const { return m_rect; }

private:
    Gfx::IntRect m_rect;
};

class WMWindowIconBitmapChangedEvent : public WMEvent {
public:
    WMWindowIconBitmapChangedEvent(int client_id, int window_id, Gfx::Bitmap const* bitmap)
        : WMEvent(Event::Type::WM_WindowIconBitmapChanged, client_id, window_id)
        , m_bitmap(move(bitmap))
    {
    }

    Gfx::Bitmap const* bitmap() const { return m_bitmap; }

private:
    RefPtr<Gfx::Bitmap> m_bitmap;
};

class WMWorkspaceChangedEvent : public WMEvent {
public:
    explicit WMWorkspaceChangedEvent(int client_id, unsigned current_row, unsigned current_column)
        : WMEvent(Event::Type::WM_WorkspaceChanged, client_id, 0)
        , m_current_row(current_row)
        , m_current_column(current_column)
    {
    }

    unsigned current_row() const { return m_current_row; }
    unsigned current_column() const { return m_current_column; }

private:
    unsigned const m_current_row;
    unsigned const m_current_column;
};

class WMKeymapChangedEvent : public WMEvent {
public:
    explicit WMKeymapChangedEvent(int client_id, String const& keymap)
        : WMEvent(Event::Type::WM_KeymapChanged, client_id, 0)
        , m_keymap(keymap)
    {
    }

    String const& keymap() const { return m_keymap; }

private:
    const String m_keymap;
};

class MultiPaintEvent final : public Event {
public:
    explicit MultiPaintEvent(Vector<Gfx::IntRect, 32> rects, Gfx::IntSize const& window_size)
        : Event(Event::MultiPaint)
        , m_rects(move(rects))
        , m_window_size(window_size)
    {
    }

    Vector<Gfx::IntRect, 32> const& rects() const { return m_rects; }
    Gfx::IntSize const& window_size() const { return m_window_size; }

private:
    Vector<Gfx::IntRect, 32> m_rects;
    Gfx::IntSize m_window_size;
};

class PaintEvent final : public Event {
public:
    explicit PaintEvent(Gfx::IntRect const& rect, Gfx::IntSize const& window_size = {})
        : Event(Event::Paint)
        , m_rect(rect)
        , m_window_size(window_size)
    {
    }

    Gfx::IntRect const& rect() const { return m_rect; }
    Gfx::IntSize const& window_size() const { return m_window_size; }

private:
    Gfx::IntRect m_rect;
    Gfx::IntSize m_window_size;
};

class ResizeEvent final : public Event {
public:
    explicit ResizeEvent(Gfx::IntSize const& size)
        : Event(Event::Resize)
        , m_size(size)
    {
    }

    Gfx::IntSize const& size() const { return m_size; }

private:
    Gfx::IntSize m_size;
};

class ContextMenuEvent final : public Event {
public:
    explicit ContextMenuEvent(Gfx::IntPoint const& position, Gfx::IntPoint const& screen_position)
        : Event(Event::ContextMenu)
        , m_position(position)
        , m_screen_position(screen_position)
    {
    }

    Gfx::IntPoint const& position() const { return m_position; }
    Gfx::IntPoint const& screen_position() const { return m_screen_position; }

private:
    Gfx::IntPoint m_position;
    Gfx::IntPoint m_screen_position;
};

class ShowEvent final : public Event {
public:
    ShowEvent()
        : Event(Event::Show)
    {
    }
};

class HideEvent final : public Event {
public:
    HideEvent()
        : Event(Event::Hide)
    {
    }
};

enum MouseButton : u8 {
    None = 0,
    Primary = 1,
    Secondary = 2,
    Middle = 4,
    Backward = 8,
    Forward = 16,
};

class KeyEvent final : public Event {
public:
    KeyEvent(Type type, KeyCode key, u8 modifiers, u32 code_point, u32 scancode)
        : Event(type)
        , m_key(key)
        , m_modifiers(modifiers)
        , m_code_point(code_point)
        , m_scancode(scancode)
    {
    }

    KeyCode key() const { return m_key; }
    bool ctrl() const { return m_modifiers & Mod_Ctrl; }
    bool alt() const { return m_modifiers & Mod_Alt; }
    bool altgr() const { return m_modifiers & Mod_AltGr; }
    bool shift() const { return m_modifiers & Mod_Shift; }
    bool super() const { return m_modifiers & Mod_Super; }
    u8 modifiers() const { return m_modifiers; }
    u32 code_point() const { return m_code_point; }
    String text() const
    {
        StringBuilder sb;
        sb.append_code_point(m_code_point);
        return sb.to_string();
    }
    u32 scancode() const { return m_scancode; }

    String to_string() const;

    bool is_arrow_key() const
    {
        switch (m_key) {
        case KeyCode::Key_Up:
        case KeyCode::Key_Down:
        case KeyCode::Key_Left:
        case KeyCode::Key_Right:
            return true;
        default:
            return false;
        }
    }

private:
    friend class ConnectionToWindowServer;
    KeyCode m_key { KeyCode::Key_Invalid };
    u8 m_modifiers { 0 };
    u32 m_code_point { 0 };
    u32 m_scancode { 0 };
};

class MouseEvent final : public Event {
public:
    MouseEvent(Type type, Gfx::IntPoint const& position, unsigned buttons, MouseButton button, unsigned modifiers, int wheel_delta_x, int wheel_delta_y, int wheel_raw_delta_x, int wheel_raw_delta_y)
        : Event(type)
        , m_position(position)
        , m_buttons(buttons)
        , m_button(button)
        , m_modifiers(modifiers)
        , m_wheel_delta_x(wheel_delta_x)
        , m_wheel_delta_y(wheel_delta_y)
        , m_wheel_raw_delta_x(wheel_raw_delta_x)
        , m_wheel_raw_delta_y(wheel_raw_delta_y)
    {
    }

    Gfx::IntPoint const& position() const { return m_position; }
    int x() const { return m_position.x(); }
    int y() const { return m_position.y(); }
    MouseButton button() const { return m_button; }
    unsigned buttons() const { return m_buttons; }
    bool ctrl() const { return m_modifiers & Mod_Ctrl; }
    bool alt() const { return m_modifiers & Mod_Alt; }
    bool shift() const { return m_modifiers & Mod_Shift; }
    bool super() const { return m_modifiers & Mod_Super; }
    unsigned modifiers() const { return m_modifiers; }
    int wheel_delta_x() const { return m_wheel_delta_x; }
    int wheel_delta_y() const { return m_wheel_delta_y; }
    int wheel_raw_delta_x() const { return m_wheel_raw_delta_x; }
    int wheel_raw_delta_y() const { return m_wheel_raw_delta_y; }

private:
    Gfx::IntPoint m_position;
    unsigned m_buttons { 0 };
    MouseButton m_button { MouseButton::None };
    unsigned m_modifiers { 0 };
    int m_wheel_delta_x { 0 };
    int m_wheel_delta_y { 0 };
    int m_wheel_raw_delta_x { 0 };
    int m_wheel_raw_delta_y { 0 };
};

class DragEvent final : public Event {
public:
    DragEvent(Type type, Gfx::IntPoint const& position, Vector<String> mime_types)
        : Event(type)
        , m_position(position)
        , m_mime_types(move(mime_types))
    {
    }

    Gfx::IntPoint const& position() const { return m_position; }
    Vector<String> const& mime_types() const { return m_mime_types; }

private:
    Gfx::IntPoint m_position;
    Vector<String> m_mime_types;
};

class DropEvent final : public Event {
public:
    DropEvent(Gfx::IntPoint const&, String const& text, NonnullRefPtr<Core::MimeData> mime_data);

    ~DropEvent() = default;

    Gfx::IntPoint const& position() const { return m_position; }
    String const& text() const { return m_text; }
    Core::MimeData const& mime_data() const { return m_mime_data; }

private:
    Gfx::IntPoint m_position;
    String m_text;
    NonnullRefPtr<Core::MimeData> m_mime_data;
};

class ThemeChangeEvent final : public Event {
public:
    ThemeChangeEvent()
        : Event(Type::ThemeChange)
    {
    }
};

class FontsChangeEvent final : public Event {
public:
    FontsChangeEvent()
        : Event(Type::FontsChange)
    {
    }
};

class ScreenRectsChangeEvent final : public Event {
public:
    explicit ScreenRectsChangeEvent(Vector<Gfx::IntRect, 4> const& rects, size_t main_screen_index)
        : Event(Type::ScreenRectsChange)
        , m_rects(rects)
        , m_main_screen_index(main_screen_index)
    {
    }

    Vector<Gfx::IntRect, 4> const& rects() const { return m_rects; }
    size_t main_screen_index() const { return m_main_screen_index; }

private:
    Vector<Gfx::IntRect, 4> m_rects;
    size_t m_main_screen_index;
};

class AppletAreaRectChangeEvent final : public Event {
public:
    explicit AppletAreaRectChangeEvent(Gfx::IntRect rect)
        : Event(Type::AppletAreaRectChange)
        , m_rect(rect)
    {
    }

    Gfx::IntRect rect() const { return m_rect; }

private:
    Gfx::IntRect const m_rect;
};

class FocusEvent final : public Event {
public:
    explicit FocusEvent(Type type, FocusSource source)
        : Event(type)
        , m_source(source)
    {
    }

    FocusSource source() const { return m_source; }

private:
    FocusSource m_source { FocusSource::Programmatic };
};

class ActionEvent final : public Event {
public:
    ActionEvent(Type, Action&);
    ~ActionEvent() = default;

    Action const& action() const { return *m_action; }
    Action& action() { return *m_action; }

private:
    NonnullRefPtr<Action> m_action;
};

}