Event.h 13 KB

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