Event.h 13 KB

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