Event.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/StringBuilder.h>
  28. #include <AK/Vector.h>
  29. #include <Kernel/API/KeyCode.h>
  30. #include <LibCore/Event.h>
  31. #include <LibGUI/FocusSource.h>
  32. #include <LibGUI/WindowType.h>
  33. #include <LibGfx/Point.h>
  34. #include <LibGfx/Rect.h>
  35. namespace GUI {
  36. class Event : public Core::Event {
  37. public:
  38. enum Type {
  39. Show = 1000,
  40. Hide,
  41. Paint,
  42. MultiPaint,
  43. Resize,
  44. MouseMove,
  45. MouseDown,
  46. MouseDoubleClick,
  47. MouseUp,
  48. MouseWheel,
  49. Enter,
  50. Leave,
  51. KeyDown,
  52. KeyUp,
  53. WindowEntered,
  54. WindowLeft,
  55. WindowBecameInactive,
  56. WindowBecameActive,
  57. WindowInputEntered,
  58. WindowInputLeft,
  59. FocusIn,
  60. FocusOut,
  61. WindowCloseRequest,
  62. ContextMenu,
  63. EnabledChange,
  64. DragMove,
  65. Drop,
  66. ThemeChange,
  67. __Begin_WM_Events,
  68. WM_WindowRemoved,
  69. WM_WindowStateChanged,
  70. WM_WindowRectChanged,
  71. WM_WindowIconBitmapChanged,
  72. __End_WM_Events,
  73. };
  74. Event() { }
  75. explicit Event(Type type)
  76. : Core::Event(type)
  77. {
  78. }
  79. virtual ~Event() { }
  80. bool is_key_event() const { return type() == KeyUp || type() == KeyDown; }
  81. bool is_paint_event() const { return type() == Paint; }
  82. };
  83. class WMEvent : public Event {
  84. public:
  85. WMEvent(Type type, int client_id, int window_id)
  86. : Event(type)
  87. , m_client_id(client_id)
  88. , m_window_id(window_id)
  89. {
  90. }
  91. int client_id() const { return m_client_id; }
  92. int window_id() const { return m_window_id; }
  93. private:
  94. int m_client_id { -1 };
  95. int m_window_id { -1 };
  96. };
  97. class WMWindowRemovedEvent : public WMEvent {
  98. public:
  99. WMWindowRemovedEvent(int client_id, int window_id)
  100. : WMEvent(Event::Type::WM_WindowRemoved, client_id, window_id)
  101. {
  102. }
  103. };
  104. class WMWindowStateChangedEvent : public WMEvent {
  105. public:
  106. 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, int progress)
  107. : WMEvent(Event::Type::WM_WindowStateChanged, client_id, window_id)
  108. , m_parent_client_id(parent_client_id)
  109. , m_parent_window_id(parent_window_id)
  110. , m_title(title)
  111. , m_rect(rect)
  112. , m_window_type(window_type)
  113. , m_active(is_active)
  114. , m_modal(is_modal)
  115. , m_minimized(is_minimized)
  116. , m_frameless(is_frameless)
  117. , m_progress(progress)
  118. {
  119. }
  120. int parent_client_id() const { return m_parent_client_id; }
  121. int parent_window_id() const { return m_parent_window_id; }
  122. const String& title() const { return m_title; }
  123. const Gfx::IntRect& rect() const { return m_rect; }
  124. bool is_active() const { return m_active; }
  125. bool is_modal() const { return m_modal; }
  126. WindowType window_type() const { return m_window_type; }
  127. bool is_minimized() const { return m_minimized; }
  128. bool is_frameless() const { return m_frameless; }
  129. int progress() const { return m_progress; }
  130. private:
  131. int m_parent_client_id;
  132. int m_parent_window_id;
  133. String m_title;
  134. Gfx::IntRect m_rect;
  135. WindowType m_window_type;
  136. bool m_active;
  137. bool m_modal;
  138. bool m_minimized;
  139. bool m_frameless;
  140. int m_progress;
  141. };
  142. class WMWindowRectChangedEvent : public WMEvent {
  143. public:
  144. WMWindowRectChangedEvent(int client_id, int window_id, const Gfx::IntRect& rect)
  145. : WMEvent(Event::Type::WM_WindowRectChanged, client_id, window_id)
  146. , m_rect(rect)
  147. {
  148. }
  149. const Gfx::IntRect& rect() const { return m_rect; }
  150. private:
  151. Gfx::IntRect m_rect;
  152. };
  153. class WMWindowIconBitmapChangedEvent : public WMEvent {
  154. public:
  155. WMWindowIconBitmapChangedEvent(int client_id, int window_id, int icon_buffer_id, const Gfx::IntSize& icon_size)
  156. : WMEvent(Event::Type::WM_WindowIconBitmapChanged, client_id, window_id)
  157. , m_icon_buffer_id(icon_buffer_id)
  158. , m_icon_size(icon_size)
  159. {
  160. }
  161. int icon_buffer_id() const { return m_icon_buffer_id; }
  162. const Gfx::IntSize& icon_size() const { return m_icon_size; }
  163. private:
  164. int m_icon_buffer_id;
  165. Gfx::IntSize m_icon_size;
  166. };
  167. class MultiPaintEvent final : public Event {
  168. public:
  169. explicit MultiPaintEvent(const Vector<Gfx::IntRect, 32>& rects, const Gfx::IntSize& window_size)
  170. : Event(Event::MultiPaint)
  171. , m_rects(rects)
  172. , m_window_size(window_size)
  173. {
  174. }
  175. const Vector<Gfx::IntRect, 32>& rects() const { return m_rects; }
  176. const Gfx::IntSize& window_size() const { return m_window_size; }
  177. private:
  178. Vector<Gfx::IntRect, 32> m_rects;
  179. Gfx::IntSize m_window_size;
  180. };
  181. class PaintEvent final : public Event {
  182. public:
  183. explicit PaintEvent(const Gfx::IntRect& rect, const Gfx::IntSize& window_size = {})
  184. : Event(Event::Paint)
  185. , m_rect(rect)
  186. , m_window_size(window_size)
  187. {
  188. }
  189. const Gfx::IntRect& rect() const { return m_rect; }
  190. const Gfx::IntSize& window_size() const { return m_window_size; }
  191. private:
  192. Gfx::IntRect m_rect;
  193. Gfx::IntSize m_window_size;
  194. };
  195. class ResizeEvent final : public Event {
  196. public:
  197. explicit ResizeEvent(const Gfx::IntSize& size)
  198. : Event(Event::Resize)
  199. , m_size(size)
  200. {
  201. }
  202. const Gfx::IntSize& size() const { return m_size; }
  203. private:
  204. Gfx::IntSize m_size;
  205. };
  206. class ContextMenuEvent final : public Event {
  207. public:
  208. explicit ContextMenuEvent(const Gfx::IntPoint& position, const Gfx::IntPoint& screen_position)
  209. : Event(Event::ContextMenu)
  210. , m_position(position)
  211. , m_screen_position(screen_position)
  212. {
  213. }
  214. const Gfx::IntPoint& position() const { return m_position; }
  215. const Gfx::IntPoint& screen_position() const { return m_screen_position; }
  216. private:
  217. Gfx::IntPoint m_position;
  218. Gfx::IntPoint m_screen_position;
  219. };
  220. class ShowEvent final : public Event {
  221. public:
  222. ShowEvent()
  223. : Event(Event::Show)
  224. {
  225. }
  226. };
  227. class HideEvent final : public Event {
  228. public:
  229. HideEvent()
  230. : Event(Event::Hide)
  231. {
  232. }
  233. };
  234. enum MouseButton : u8 {
  235. None = 0,
  236. Left = 1,
  237. Right = 2,
  238. Middle = 4,
  239. Back = 8,
  240. Forward = 16,
  241. };
  242. class KeyEvent final : public Event {
  243. public:
  244. KeyEvent(Type type, KeyCode key, u8 modifiers, u32 code_point, u32 scancode)
  245. : Event(type)
  246. , m_key(key)
  247. , m_modifiers(modifiers)
  248. , m_code_point(code_point)
  249. , m_scancode(scancode)
  250. {
  251. }
  252. KeyCode key() const { return m_key; }
  253. bool ctrl() const { return m_modifiers & Mod_Ctrl; }
  254. bool alt() const { return m_modifiers & Mod_Alt; }
  255. bool shift() const { return m_modifiers & Mod_Shift; }
  256. bool logo() const { return m_modifiers & Mod_Logo; }
  257. u8 modifiers() const { return m_modifiers; }
  258. u32 code_point() const { return m_code_point; }
  259. String text() const
  260. {
  261. StringBuilder sb;
  262. sb.append_code_point(m_code_point);
  263. return sb.to_string();
  264. }
  265. u32 scancode() const { return m_scancode; }
  266. String to_string() const;
  267. private:
  268. friend class WindowServerConnection;
  269. KeyCode m_key { KeyCode::Key_Invalid };
  270. u8 m_modifiers { 0 };
  271. u32 m_code_point { 0 };
  272. u32 m_scancode { 0 };
  273. };
  274. class MouseEvent final : public Event {
  275. public:
  276. MouseEvent(Type type, const Gfx::IntPoint& position, unsigned buttons, MouseButton button, unsigned modifiers, int wheel_delta)
  277. : Event(type)
  278. , m_position(position)
  279. , m_buttons(buttons)
  280. , m_button(button)
  281. , m_modifiers(modifiers)
  282. , m_wheel_delta(wheel_delta)
  283. {
  284. }
  285. const Gfx::IntPoint& position() const { return m_position; }
  286. int x() const { return m_position.x(); }
  287. int y() const { return m_position.y(); }
  288. MouseButton button() const { return m_button; }
  289. unsigned buttons() const { return m_buttons; }
  290. bool ctrl() const { return m_modifiers & Mod_Ctrl; }
  291. bool alt() const { return m_modifiers & Mod_Alt; }
  292. bool shift() const { return m_modifiers & Mod_Shift; }
  293. bool logo() const { return m_modifiers & Mod_Logo; }
  294. unsigned modifiers() const { return m_modifiers; }
  295. int wheel_delta() const { return m_wheel_delta; }
  296. private:
  297. Gfx::IntPoint m_position;
  298. unsigned m_buttons { 0 };
  299. MouseButton m_button { MouseButton::None };
  300. unsigned m_modifiers { 0 };
  301. int m_wheel_delta { 0 };
  302. };
  303. class DragEvent final : public Event {
  304. public:
  305. DragEvent(Type type, const Gfx::IntPoint& position, const String& data_type)
  306. : Event(type)
  307. , m_position(position)
  308. , m_data_type(data_type)
  309. {
  310. }
  311. const Gfx::IntPoint& position() const { return m_position; }
  312. const String& data_type() const { return m_data_type; }
  313. private:
  314. Gfx::IntPoint m_position;
  315. String m_data_type;
  316. };
  317. class DropEvent final : public Event {
  318. public:
  319. DropEvent(const Gfx::IntPoint&, const String& text, NonnullRefPtr<Core::MimeData> mime_data);
  320. ~DropEvent();
  321. const Gfx::IntPoint& position() const { return m_position; }
  322. const String& text() const { return m_text; }
  323. const Core::MimeData& mime_data() const { return m_mime_data; }
  324. private:
  325. Gfx::IntPoint m_position;
  326. String m_text;
  327. NonnullRefPtr<Core::MimeData> m_mime_data;
  328. };
  329. class ThemeChangeEvent final : public Event {
  330. public:
  331. ThemeChangeEvent()
  332. : Event(Type::ThemeChange)
  333. {
  334. }
  335. };
  336. class FocusEvent final : public Event {
  337. public:
  338. explicit FocusEvent(Type type, FocusSource source)
  339. : Event(type)
  340. , m_source(source)
  341. {
  342. }
  343. FocusSource source() const { return m_source; }
  344. private:
  345. FocusSource m_source { FocusSource::Programmatic };
  346. };
  347. }