Event.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/StringBuilder.h>
  9. #include <AK/Vector.h>
  10. #include <Kernel/API/KeyCode.h>
  11. #include <LibCore/Event.h>
  12. #include <LibGUI/FocusSource.h>
  13. #include <LibGUI/Forward.h>
  14. #include <LibGUI/WindowType.h>
  15. #include <LibGfx/Bitmap.h>
  16. #include <LibGfx/Point.h>
  17. #include <LibGfx/Rect.h>
  18. namespace GUI {
  19. class Event : public Core::Event {
  20. public:
  21. enum Type {
  22. Show = 1000,
  23. Hide,
  24. Paint,
  25. MultiPaint,
  26. Resize,
  27. MouseMove,
  28. MouseDown,
  29. MouseDoubleClick,
  30. MouseUp,
  31. MouseWheel,
  32. Enter,
  33. Leave,
  34. KeyDown,
  35. KeyUp,
  36. WindowEntered,
  37. WindowLeft,
  38. WindowBecameInactive,
  39. WindowBecameActive,
  40. WindowInputEntered,
  41. WindowInputLeft,
  42. FocusIn,
  43. FocusOut,
  44. WindowCloseRequest,
  45. ContextMenu,
  46. EnabledChange,
  47. DragEnter,
  48. DragLeave,
  49. DragMove,
  50. Drop,
  51. ThemeChange,
  52. FontsChange,
  53. ScreenRectsChange,
  54. ActionEnter,
  55. ActionLeave,
  56. AppletAreaRectChange,
  57. __Begin_WM_Events,
  58. WM_WindowRemoved,
  59. WM_WindowStateChanged,
  60. WM_WindowRectChanged,
  61. WM_WindowIconBitmapChanged,
  62. WM_AppletAreaSizeChanged,
  63. WM_SuperKeyPressed,
  64. WM_SuperSpaceKeyPressed,
  65. WM_SuperDigitKeyPressed,
  66. WM_WorkspaceChanged,
  67. WM_KeymapChanged,
  68. __End_WM_Events,
  69. };
  70. Event() = default;
  71. explicit Event(Type type)
  72. : Core::Event(type)
  73. {
  74. }
  75. virtual ~Event() = default;
  76. bool is_key_event() const { return type() == KeyUp || type() == KeyDown; }
  77. bool is_paint_event() const { return type() == Paint; }
  78. };
  79. class WMEvent : public Event {
  80. public:
  81. WMEvent(Type type, int client_id, int window_id)
  82. : Event(type)
  83. , m_client_id(client_id)
  84. , m_window_id(window_id)
  85. {
  86. }
  87. int client_id() const { return m_client_id; }
  88. int window_id() const { return m_window_id; }
  89. private:
  90. int m_client_id { -1 };
  91. int m_window_id { -1 };
  92. };
  93. class WMSuperKeyPressedEvent : public WMEvent {
  94. public:
  95. explicit WMSuperKeyPressedEvent(int client_id)
  96. : WMEvent(Event::Type::WM_SuperKeyPressed, client_id, 0)
  97. {
  98. }
  99. };
  100. class WMSuperSpaceKeyPressedEvent : public WMEvent {
  101. public:
  102. explicit WMSuperSpaceKeyPressedEvent(int client_id)
  103. : WMEvent(Event::Type::WM_SuperSpaceKeyPressed, client_id, 0)
  104. {
  105. }
  106. };
  107. class WMSuperDigitKeyPressedEvent : public WMEvent {
  108. public:
  109. WMSuperDigitKeyPressedEvent(int client_id, u8 digit)
  110. : WMEvent(Event::Type::WM_SuperDigitKeyPressed, client_id, 0)
  111. , m_digit(digit)
  112. {
  113. }
  114. u8 digit() const { return m_digit; }
  115. private:
  116. u8 m_digit { 0 };
  117. };
  118. class WMAppletAreaSizeChangedEvent : public WMEvent {
  119. public:
  120. explicit WMAppletAreaSizeChangedEvent(Gfx::IntSize const& size)
  121. : WMEvent(Event::Type::WM_AppletAreaSizeChanged, 0, 0)
  122. , m_size(size)
  123. {
  124. }
  125. Gfx::IntSize const& size() const { return m_size; }
  126. private:
  127. Gfx::IntSize m_size;
  128. };
  129. class WMWindowRemovedEvent : public WMEvent {
  130. public:
  131. WMWindowRemovedEvent(int client_id, int window_id)
  132. : WMEvent(Event::Type::WM_WindowRemoved, client_id, window_id)
  133. {
  134. }
  135. };
  136. class WMWindowStateChangedEvent : public WMEvent {
  137. public:
  138. 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)
  139. : WMEvent(Event::Type::WM_WindowStateChanged, client_id, window_id)
  140. , m_parent_client_id(parent_client_id)
  141. , m_parent_window_id(parent_window_id)
  142. , m_title(title)
  143. , m_rect(rect)
  144. , m_window_type(window_type)
  145. , m_workspace_row(workspace_row)
  146. , m_workspace_column(workspace_column)
  147. , m_active(is_active)
  148. , m_modal(is_modal)
  149. , m_minimized(is_minimized)
  150. , m_frameless(is_frameless)
  151. , m_progress(progress)
  152. {
  153. }
  154. int parent_client_id() const { return m_parent_client_id; }
  155. int parent_window_id() const { return m_parent_window_id; }
  156. String const& title() const { return m_title; }
  157. Gfx::IntRect const& rect() const { return m_rect; }
  158. bool is_active() const { return m_active; }
  159. bool is_modal() const { return m_modal; }
  160. WindowType window_type() const { return m_window_type; }
  161. bool is_minimized() const { return m_minimized; }
  162. bool is_frameless() const { return m_frameless; }
  163. Optional<int> progress() const { return m_progress; }
  164. unsigned workspace_row() const { return m_workspace_row; }
  165. unsigned workspace_column() const { return m_workspace_column; }
  166. private:
  167. int m_parent_client_id;
  168. int m_parent_window_id;
  169. String m_title;
  170. Gfx::IntRect m_rect;
  171. WindowType m_window_type;
  172. unsigned m_workspace_row;
  173. unsigned m_workspace_column;
  174. bool m_active;
  175. bool m_modal;
  176. bool m_minimized;
  177. bool m_frameless;
  178. Optional<int> m_progress;
  179. };
  180. class WMWindowRectChangedEvent : public WMEvent {
  181. public:
  182. WMWindowRectChangedEvent(int client_id, int window_id, Gfx::IntRect const& rect)
  183. : WMEvent(Event::Type::WM_WindowRectChanged, client_id, window_id)
  184. , m_rect(rect)
  185. {
  186. }
  187. Gfx::IntRect const& rect() const { return m_rect; }
  188. private:
  189. Gfx::IntRect m_rect;
  190. };
  191. class WMWindowIconBitmapChangedEvent : public WMEvent {
  192. public:
  193. WMWindowIconBitmapChangedEvent(int client_id, int window_id, Gfx::Bitmap const* bitmap)
  194. : WMEvent(Event::Type::WM_WindowIconBitmapChanged, client_id, window_id)
  195. , m_bitmap(move(bitmap))
  196. {
  197. }
  198. Gfx::Bitmap const* bitmap() const { return m_bitmap; }
  199. private:
  200. RefPtr<Gfx::Bitmap> m_bitmap;
  201. };
  202. class WMWorkspaceChangedEvent : public WMEvent {
  203. public:
  204. explicit WMWorkspaceChangedEvent(int client_id, unsigned current_row, unsigned current_column)
  205. : WMEvent(Event::Type::WM_WorkspaceChanged, client_id, 0)
  206. , m_current_row(current_row)
  207. , m_current_column(current_column)
  208. {
  209. }
  210. unsigned current_row() const { return m_current_row; }
  211. unsigned current_column() const { return m_current_column; }
  212. private:
  213. unsigned const m_current_row;
  214. unsigned const m_current_column;
  215. };
  216. class WMKeymapChangedEvent : public WMEvent {
  217. public:
  218. explicit WMKeymapChangedEvent(int client_id, String const& keymap)
  219. : WMEvent(Event::Type::WM_KeymapChanged, client_id, 0)
  220. , m_keymap(keymap)
  221. {
  222. }
  223. String const& keymap() const { return m_keymap; }
  224. private:
  225. const String m_keymap;
  226. };
  227. class MultiPaintEvent final : public Event {
  228. public:
  229. explicit MultiPaintEvent(Vector<Gfx::IntRect, 32> rects, Gfx::IntSize const& window_size)
  230. : Event(Event::MultiPaint)
  231. , m_rects(move(rects))
  232. , m_window_size(window_size)
  233. {
  234. }
  235. Vector<Gfx::IntRect, 32> const& rects() const { return m_rects; }
  236. Gfx::IntSize const& window_size() const { return m_window_size; }
  237. private:
  238. Vector<Gfx::IntRect, 32> m_rects;
  239. Gfx::IntSize m_window_size;
  240. };
  241. class PaintEvent final : public Event {
  242. public:
  243. explicit PaintEvent(Gfx::IntRect const& rect, Gfx::IntSize const& window_size = {})
  244. : Event(Event::Paint)
  245. , m_rect(rect)
  246. , m_window_size(window_size)
  247. {
  248. }
  249. Gfx::IntRect const& rect() const { return m_rect; }
  250. Gfx::IntSize const& window_size() const { return m_window_size; }
  251. private:
  252. Gfx::IntRect m_rect;
  253. Gfx::IntSize m_window_size;
  254. };
  255. class ResizeEvent final : public Event {
  256. public:
  257. explicit ResizeEvent(Gfx::IntSize const& size)
  258. : Event(Event::Resize)
  259. , m_size(size)
  260. {
  261. }
  262. Gfx::IntSize const& size() const { return m_size; }
  263. private:
  264. Gfx::IntSize m_size;
  265. };
  266. class ContextMenuEvent final : public Event {
  267. public:
  268. explicit ContextMenuEvent(Gfx::IntPoint const& position, Gfx::IntPoint const& screen_position)
  269. : Event(Event::ContextMenu)
  270. , m_position(position)
  271. , m_screen_position(screen_position)
  272. {
  273. }
  274. Gfx::IntPoint const& position() const { return m_position; }
  275. Gfx::IntPoint const& screen_position() const { return m_screen_position; }
  276. private:
  277. Gfx::IntPoint m_position;
  278. Gfx::IntPoint m_screen_position;
  279. };
  280. class ShowEvent final : public Event {
  281. public:
  282. ShowEvent()
  283. : Event(Event::Show)
  284. {
  285. }
  286. };
  287. class HideEvent final : public Event {
  288. public:
  289. HideEvent()
  290. : Event(Event::Hide)
  291. {
  292. }
  293. };
  294. enum MouseButton : u8 {
  295. None = 0,
  296. Primary = 1,
  297. Secondary = 2,
  298. Middle = 4,
  299. Backward = 8,
  300. Forward = 16,
  301. };
  302. class KeyEvent final : public Event {
  303. public:
  304. KeyEvent(Type type, KeyCode key, u8 modifiers, u32 code_point, u32 scancode)
  305. : Event(type)
  306. , m_key(key)
  307. , m_modifiers(modifiers)
  308. , m_code_point(code_point)
  309. , m_scancode(scancode)
  310. {
  311. }
  312. KeyCode key() const { return m_key; }
  313. bool ctrl() const { return m_modifiers & Mod_Ctrl; }
  314. bool alt() const { return m_modifiers & Mod_Alt; }
  315. bool altgr() const { return m_modifiers & Mod_AltGr; }
  316. bool shift() const { return m_modifiers & Mod_Shift; }
  317. bool super() const { return m_modifiers & Mod_Super; }
  318. u8 modifiers() const { return m_modifiers; }
  319. u32 code_point() const { return m_code_point; }
  320. String text() const
  321. {
  322. StringBuilder sb;
  323. sb.append_code_point(m_code_point);
  324. return sb.to_string();
  325. }
  326. u32 scancode() const { return m_scancode; }
  327. String to_string() const;
  328. bool is_arrow_key() const
  329. {
  330. switch (m_key) {
  331. case KeyCode::Key_Up:
  332. case KeyCode::Key_Down:
  333. case KeyCode::Key_Left:
  334. case KeyCode::Key_Right:
  335. return true;
  336. default:
  337. return false;
  338. }
  339. }
  340. private:
  341. friend class ConnectionToWindowServer;
  342. KeyCode m_key { KeyCode::Key_Invalid };
  343. u8 m_modifiers { 0 };
  344. u32 m_code_point { 0 };
  345. u32 m_scancode { 0 };
  346. };
  347. class MouseEvent final : public Event {
  348. public:
  349. 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)
  350. : Event(type)
  351. , m_position(position)
  352. , m_buttons(buttons)
  353. , m_button(button)
  354. , m_modifiers(modifiers)
  355. , m_wheel_delta_x(wheel_delta_x)
  356. , m_wheel_delta_y(wheel_delta_y)
  357. , m_wheel_raw_delta_x(wheel_raw_delta_x)
  358. , m_wheel_raw_delta_y(wheel_raw_delta_y)
  359. {
  360. }
  361. Gfx::IntPoint const& position() const { return m_position; }
  362. int x() const { return m_position.x(); }
  363. int y() const { return m_position.y(); }
  364. MouseButton button() const { return m_button; }
  365. unsigned buttons() const { return m_buttons; }
  366. bool ctrl() const { return m_modifiers & Mod_Ctrl; }
  367. bool alt() const { return m_modifiers & Mod_Alt; }
  368. bool shift() const { return m_modifiers & Mod_Shift; }
  369. bool super() const { return m_modifiers & Mod_Super; }
  370. unsigned modifiers() const { return m_modifiers; }
  371. int wheel_delta_x() const { return m_wheel_delta_x; }
  372. int wheel_delta_y() const { return m_wheel_delta_y; }
  373. int wheel_raw_delta_x() const { return m_wheel_raw_delta_x; }
  374. int wheel_raw_delta_y() const { return m_wheel_raw_delta_y; }
  375. private:
  376. Gfx::IntPoint m_position;
  377. unsigned m_buttons { 0 };
  378. MouseButton m_button { MouseButton::None };
  379. unsigned m_modifiers { 0 };
  380. int m_wheel_delta_x { 0 };
  381. int m_wheel_delta_y { 0 };
  382. int m_wheel_raw_delta_x { 0 };
  383. int m_wheel_raw_delta_y { 0 };
  384. };
  385. class DragEvent final : public Event {
  386. public:
  387. DragEvent(Type type, Gfx::IntPoint const& position, Vector<String> mime_types)
  388. : Event(type)
  389. , m_position(position)
  390. , m_mime_types(move(mime_types))
  391. {
  392. }
  393. Gfx::IntPoint const& position() const { return m_position; }
  394. Vector<String> const& mime_types() const { return m_mime_types; }
  395. private:
  396. Gfx::IntPoint m_position;
  397. Vector<String> m_mime_types;
  398. };
  399. class DropEvent final : public Event {
  400. public:
  401. DropEvent(Gfx::IntPoint const&, String const& text, NonnullRefPtr<Core::MimeData> mime_data);
  402. ~DropEvent() = default;
  403. Gfx::IntPoint const& position() const { return m_position; }
  404. String const& text() const { return m_text; }
  405. Core::MimeData const& mime_data() const { return m_mime_data; }
  406. private:
  407. Gfx::IntPoint m_position;
  408. String m_text;
  409. NonnullRefPtr<Core::MimeData> m_mime_data;
  410. };
  411. class ThemeChangeEvent final : public Event {
  412. public:
  413. ThemeChangeEvent()
  414. : Event(Type::ThemeChange)
  415. {
  416. }
  417. };
  418. class FontsChangeEvent final : public Event {
  419. public:
  420. FontsChangeEvent()
  421. : Event(Type::FontsChange)
  422. {
  423. }
  424. };
  425. class ScreenRectsChangeEvent final : public Event {
  426. public:
  427. explicit ScreenRectsChangeEvent(Vector<Gfx::IntRect, 4> const& rects, size_t main_screen_index)
  428. : Event(Type::ScreenRectsChange)
  429. , m_rects(rects)
  430. , m_main_screen_index(main_screen_index)
  431. {
  432. }
  433. Vector<Gfx::IntRect, 4> const& rects() const { return m_rects; }
  434. size_t main_screen_index() const { return m_main_screen_index; }
  435. private:
  436. Vector<Gfx::IntRect, 4> m_rects;
  437. size_t m_main_screen_index;
  438. };
  439. class AppletAreaRectChangeEvent final : public Event {
  440. public:
  441. explicit AppletAreaRectChangeEvent(Gfx::IntRect rect)
  442. : Event(Type::AppletAreaRectChange)
  443. , m_rect(rect)
  444. {
  445. }
  446. Gfx::IntRect rect() const { return m_rect; }
  447. private:
  448. Gfx::IntRect const m_rect;
  449. };
  450. class FocusEvent final : public Event {
  451. public:
  452. explicit FocusEvent(Type type, FocusSource source)
  453. : Event(type)
  454. , m_source(source)
  455. {
  456. }
  457. FocusSource source() const { return m_source; }
  458. private:
  459. FocusSource m_source { FocusSource::Programmatic };
  460. };
  461. class ActionEvent final : public Event {
  462. public:
  463. ActionEvent(Type, Action&);
  464. ~ActionEvent() = default;
  465. Action const& action() const { return *m_action; }
  466. Action& action() { return *m_action; }
  467. private:
  468. NonnullRefPtr<Action> m_action;
  469. };
  470. }