Event.h 12 KB

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