Event.h 11 KB

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