Event.h 15 KB

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