Serenity Operating System
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#pragma once
28
29#include <Kernel/KeyCode.h>
30#include <LibCore/Event.h>
31#include <LibGUI/WindowType.h>
32#include <LibGfx/Point.h>
33#include <LibGfx/Rect.h>
34
35namespace GUI {
36
37class Event : public Core::Event {
38public:
39 enum Type {
40 Show = 1000,
41 Hide,
42 Paint,
43 MultiPaint,
44 Resize,
45 MouseMove,
46 MouseDown,
47 MouseDoubleClick,
48 MouseUp,
49 MouseWheel,
50 Enter,
51 Leave,
52 KeyDown,
53 KeyUp,
54 WindowEntered,
55 WindowLeft,
56 WindowBecameInactive,
57 WindowBecameActive,
58 FocusIn,
59 FocusOut,
60 WindowCloseRequest,
61 ContextMenu,
62 EnabledChange,
63 DragMove,
64 Drop,
65 ThemeChange,
66
67 __Begin_WM_Events,
68 WM_WindowRemoved,
69 WM_WindowStateChanged,
70 WM_WindowRectChanged,
71 WM_WindowIconBitmapChanged,
72 __End_WM_Events,
73 };
74
75 Event() {}
76 explicit Event(Type type)
77 : Core::Event(type)
78 {
79 }
80 virtual ~Event() {}
81
82 bool is_key_event() const { return type() == KeyUp || type() == KeyDown; }
83 bool is_paint_event() const { return type() == Paint; }
84};
85
86class WMEvent : public Event {
87public:
88 WMEvent(Type type, int client_id, int window_id)
89 : Event(type)
90 , m_client_id(client_id)
91 , m_window_id(window_id)
92 {
93 }
94
95 int client_id() const { return m_client_id; }
96 int window_id() const { return m_window_id; }
97
98private:
99 int m_client_id { -1 };
100 int m_window_id { -1 };
101};
102
103class WMWindowRemovedEvent : public WMEvent {
104public:
105 WMWindowRemovedEvent(int client_id, int window_id)
106 : WMEvent(Event::Type::WM_WindowRemoved, client_id, window_id)
107 {
108 }
109};
110
111class WMWindowStateChangedEvent : public WMEvent {
112public:
113 WMWindowStateChangedEvent(int client_id, int window_id, const StringView& title, const Gfx::Rect& rect, bool is_active, WindowType window_type, bool is_minimized)
114 : WMEvent(Event::Type::WM_WindowStateChanged, client_id, window_id)
115 , m_title(title)
116 , m_rect(rect)
117 , m_window_type(window_type)
118 , m_active(is_active)
119 , m_minimized(is_minimized)
120 {
121 }
122
123 String title() const { return m_title; }
124 Gfx::Rect rect() const { return m_rect; }
125 bool is_active() const { return m_active; }
126 WindowType window_type() const { return m_window_type; }
127 bool is_minimized() const { return m_minimized; }
128
129private:
130 String m_title;
131 Gfx::Rect m_rect;
132 WindowType m_window_type;
133 bool m_active;
134 bool m_minimized;
135};
136
137class WMWindowRectChangedEvent : public WMEvent {
138public:
139 WMWindowRectChangedEvent(int client_id, int window_id, const Gfx::Rect& rect)
140 : WMEvent(Event::Type::WM_WindowRectChanged, client_id, window_id)
141 , m_rect(rect)
142 {
143 }
144
145 Gfx::Rect rect() const { return m_rect; }
146
147private:
148 Gfx::Rect m_rect;
149};
150
151class WMWindowIconBitmapChangedEvent : public WMEvent {
152public:
153 WMWindowIconBitmapChangedEvent(int client_id, int window_id, int icon_buffer_id, const Gfx::Size& icon_size)
154 : WMEvent(Event::Type::WM_WindowIconBitmapChanged, client_id, window_id)
155 , m_icon_buffer_id(icon_buffer_id)
156 , m_icon_size(icon_size)
157 {
158 }
159
160 int icon_buffer_id() const { return m_icon_buffer_id; }
161 const Gfx::Size& icon_size() const { return m_icon_size; }
162
163private:
164 int m_icon_buffer_id;
165 Gfx::Size m_icon_size;
166};
167
168class MultiPaintEvent final : public Event {
169public:
170 explicit MultiPaintEvent(const Vector<Gfx::Rect, 32>& rects, const Gfx::Size& window_size)
171 : Event(Event::MultiPaint)
172 , m_rects(rects)
173 , m_window_size(window_size)
174 {
175 }
176
177 const Vector<Gfx::Rect, 32>& rects() const { return m_rects; }
178 Gfx::Size window_size() const { return m_window_size; }
179
180private:
181 Vector<Gfx::Rect, 32> m_rects;
182 Gfx::Size m_window_size;
183};
184
185class PaintEvent final : public Event {
186public:
187 explicit PaintEvent(const Gfx::Rect& rect, const Gfx::Size& window_size = {})
188 : Event(Event::Paint)
189 , m_rect(rect)
190 , m_window_size(window_size)
191 {
192 }
193
194 Gfx::Rect rect() const { return m_rect; }
195 Gfx::Size window_size() const { return m_window_size; }
196
197private:
198 Gfx::Rect m_rect;
199 Gfx::Size m_window_size;
200};
201
202class ResizeEvent final : public Event {
203public:
204 explicit ResizeEvent(const Gfx::Size& old_size, const Gfx::Size& size)
205 : Event(Event::Resize)
206 , m_old_size(old_size)
207 , m_size(size)
208 {
209 }
210
211 const Gfx::Size& old_size() const { return m_old_size; }
212 const Gfx::Size& size() const { return m_size; }
213
214private:
215 Gfx::Size m_old_size;
216 Gfx::Size m_size;
217};
218
219class ContextMenuEvent final : public Event {
220public:
221 explicit ContextMenuEvent(const Gfx::Point& position, const Gfx::Point& screen_position)
222 : Event(Event::ContextMenu)
223 , m_position(position)
224 , m_screen_position(screen_position)
225 {
226 }
227
228 const Gfx::Point& position() const { return m_position; }
229 const Gfx::Point& screen_position() const { return m_screen_position; }
230
231private:
232 Gfx::Point m_position;
233 Gfx::Point m_screen_position;
234};
235
236class ShowEvent final : public Event {
237public:
238 ShowEvent()
239 : Event(Event::Show)
240 {
241 }
242};
243
244class HideEvent final : public Event {
245public:
246 HideEvent()
247 : Event(Event::Hide)
248 {
249 }
250};
251
252enum MouseButton : u8 {
253 None = 0,
254 Left = 1,
255 Right = 2,
256 Middle = 4,
257};
258
259class KeyEvent final : public Event {
260public:
261 KeyEvent(Type type, int key, u8 modifiers)
262 : Event(type)
263 , m_key(key)
264 , m_modifiers(modifiers)
265 {
266 }
267
268 int key() const { return m_key; }
269 bool ctrl() const { return m_modifiers & Mod_Ctrl; }
270 bool alt() const { return m_modifiers & Mod_Alt; }
271 bool shift() const { return m_modifiers & Mod_Shift; }
272 bool logo() const { return m_modifiers & Mod_Logo; }
273 u8 modifiers() const { return m_modifiers; }
274 String text() const { return m_text; }
275
276private:
277 friend class WindowServerConnection;
278 int m_key { 0 };
279 u8 m_modifiers { 0 };
280 String m_text;
281};
282
283class MouseEvent final : public Event {
284public:
285 MouseEvent(Type type, const Gfx::Point& position, unsigned buttons, MouseButton button, unsigned modifiers, int wheel_delta)
286 : Event(type)
287 , m_position(position)
288 , m_buttons(buttons)
289 , m_button(button)
290 , m_modifiers(modifiers)
291 , m_wheel_delta(wheel_delta)
292 {
293 }
294
295 Gfx::Point position() const { return m_position; }
296 int x() const { return m_position.x(); }
297 int y() const { return m_position.y(); }
298 MouseButton button() const { return m_button; }
299 unsigned buttons() const { return m_buttons; }
300 unsigned modifiers() const { return m_modifiers; }
301 int wheel_delta() const { return m_wheel_delta; }
302
303private:
304 Gfx::Point m_position;
305 unsigned m_buttons { 0 };
306 MouseButton m_button { MouseButton::None };
307 unsigned m_modifiers { 0 };
308 int m_wheel_delta { 0 };
309};
310
311class DragEvent final : public Event {
312public:
313 DragEvent(Type type, const Gfx::Point& position, const String& data_type)
314 : Event(type)
315 , m_position(position)
316 , m_data_type(data_type)
317 {
318 }
319
320 const Gfx::Point& position() const { return m_position; }
321 const String& data_type() const { return m_data_type; }
322
323private:
324 Gfx::Point m_position;
325 String m_data_type;
326};
327
328class DropEvent final : public Event {
329public:
330 DropEvent(const Gfx::Point&, const String& text, NonnullRefPtr<Core::MimeData> mime_data);
331
332 ~DropEvent();
333
334 const Gfx::Point& position() const { return m_position; }
335 const String& text() const { return m_text; }
336 const Core::MimeData& mime_data() const { return m_mime_data; }
337
338private:
339 Gfx::Point m_position;
340 String m_text;
341 NonnullRefPtr<Core::MimeData> m_mime_data;
342};
343
344class ThemeChangeEvent final : public Event {
345public:
346 ThemeChangeEvent()
347 : Event(Type::ThemeChange)
348 {
349 }
350};
351
352}