Event.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 <Kernel/KeyCode.h>
  28. #include <LibCore/Event.h>
  29. #include <LibGUI/WindowType.h>
  30. #include <LibGfx/Point.h>
  31. #include <LibGfx/Rect.h>
  32. namespace GUI {
  33. class Event : public Core::Event {
  34. public:
  35. enum Type {
  36. Show = 1000,
  37. Hide,
  38. Paint,
  39. MultiPaint,
  40. Resize,
  41. MouseMove,
  42. MouseDown,
  43. MouseDoubleClick,
  44. MouseUp,
  45. MouseWheel,
  46. Enter,
  47. Leave,
  48. KeyDown,
  49. KeyUp,
  50. WindowEntered,
  51. WindowLeft,
  52. WindowBecameInactive,
  53. WindowBecameActive,
  54. FocusIn,
  55. FocusOut,
  56. WindowCloseRequest,
  57. ContextMenu,
  58. EnabledChange,
  59. DragMove,
  60. Drop,
  61. ThemeChange,
  62. __Begin_WM_Events,
  63. WM_WindowRemoved,
  64. WM_WindowStateChanged,
  65. WM_WindowRectChanged,
  66. WM_WindowIconBitmapChanged,
  67. __End_WM_Events,
  68. };
  69. Event() {}
  70. explicit Event(Type type)
  71. : Core::Event(type)
  72. {
  73. }
  74. virtual ~Event() {}
  75. bool is_key_event() const { return type() == KeyUp || type() == KeyDown; }
  76. bool is_paint_event() const { return type() == Paint; }
  77. };
  78. class WMEvent : public Event {
  79. public:
  80. WMEvent(Type type, int client_id, int window_id)
  81. : Event(type)
  82. , m_client_id(client_id)
  83. , m_window_id(window_id)
  84. {
  85. }
  86. int client_id() const { return m_client_id; }
  87. int window_id() const { return m_window_id; }
  88. private:
  89. int m_client_id { -1 };
  90. int m_window_id { -1 };
  91. };
  92. class WMWindowRemovedEvent : public WMEvent {
  93. public:
  94. WMWindowRemovedEvent(int client_id, int window_id)
  95. : WMEvent(Event::Type::WM_WindowRemoved, client_id, window_id)
  96. {
  97. }
  98. };
  99. class WMWindowStateChangedEvent : public WMEvent {
  100. public:
  101. WMWindowStateChangedEvent(int client_id, int window_id, const StringView& title, const Gfx::Rect& rect, bool is_active, WindowType window_type, bool is_minimized)
  102. : WMEvent(Event::Type::WM_WindowStateChanged, client_id, window_id)
  103. , m_title(title)
  104. , m_rect(rect)
  105. , m_window_type(window_type)
  106. , m_active(is_active)
  107. , m_minimized(is_minimized)
  108. {
  109. }
  110. String title() const { return m_title; }
  111. Gfx::Rect rect() const { return m_rect; }
  112. bool is_active() const { return m_active; }
  113. WindowType window_type() const { return m_window_type; }
  114. bool is_minimized() const { return m_minimized; }
  115. private:
  116. String m_title;
  117. Gfx::Rect m_rect;
  118. WindowType m_window_type;
  119. bool m_active;
  120. bool m_minimized;
  121. };
  122. class WMWindowRectChangedEvent : public WMEvent {
  123. public:
  124. WMWindowRectChangedEvent(int client_id, int window_id, const Gfx::Rect& rect)
  125. : WMEvent(Event::Type::WM_WindowRectChanged, client_id, window_id)
  126. , m_rect(rect)
  127. {
  128. }
  129. Gfx::Rect rect() const { return m_rect; }
  130. private:
  131. Gfx::Rect m_rect;
  132. };
  133. class WMWindowIconBitmapChangedEvent : public WMEvent {
  134. public:
  135. WMWindowIconBitmapChangedEvent(int client_id, int window_id, int icon_buffer_id, const Gfx::Size& icon_size)
  136. : WMEvent(Event::Type::WM_WindowIconBitmapChanged, client_id, window_id)
  137. , m_icon_buffer_id(icon_buffer_id)
  138. , m_icon_size(icon_size)
  139. {
  140. }
  141. int icon_buffer_id() const { return m_icon_buffer_id; }
  142. const Gfx::Size& icon_size() const { return m_icon_size; }
  143. private:
  144. int m_icon_buffer_id;
  145. Gfx::Size m_icon_size;
  146. };
  147. class MultiPaintEvent final : public Event {
  148. public:
  149. explicit MultiPaintEvent(const Vector<Gfx::Rect, 32>& rects, const Gfx::Size& window_size)
  150. : Event(Event::MultiPaint)
  151. , m_rects(rects)
  152. , m_window_size(window_size)
  153. {
  154. }
  155. const Vector<Gfx::Rect, 32>& rects() const { return m_rects; }
  156. Gfx::Size window_size() const { return m_window_size; }
  157. private:
  158. Vector<Gfx::Rect, 32> m_rects;
  159. Gfx::Size m_window_size;
  160. };
  161. class PaintEvent final : public Event {
  162. public:
  163. explicit PaintEvent(const Gfx::Rect& rect, const Gfx::Size& window_size = {})
  164. : Event(Event::Paint)
  165. , m_rect(rect)
  166. , m_window_size(window_size)
  167. {
  168. }
  169. Gfx::Rect rect() const { return m_rect; }
  170. Gfx::Size window_size() const { return m_window_size; }
  171. private:
  172. Gfx::Rect m_rect;
  173. Gfx::Size m_window_size;
  174. };
  175. class ResizeEvent final : public Event {
  176. public:
  177. explicit ResizeEvent(const Gfx::Size& old_size, const Gfx::Size& size)
  178. : Event(Event::Resize)
  179. , m_old_size(old_size)
  180. , m_size(size)
  181. {
  182. }
  183. const Gfx::Size& old_size() const { return m_old_size; }
  184. const Gfx::Size& size() const { return m_size; }
  185. private:
  186. Gfx::Size m_old_size;
  187. Gfx::Size m_size;
  188. };
  189. class ContextMenuEvent final : public Event {
  190. public:
  191. explicit ContextMenuEvent(const Gfx::Point& position, const Gfx::Point& screen_position)
  192. : Event(Event::ContextMenu)
  193. , m_position(position)
  194. , m_screen_position(screen_position)
  195. {
  196. }
  197. const Gfx::Point& position() const { return m_position; }
  198. const Gfx::Point& screen_position() const { return m_screen_position; }
  199. private:
  200. Gfx::Point m_position;
  201. Gfx::Point m_screen_position;
  202. };
  203. class ShowEvent final : public Event {
  204. public:
  205. ShowEvent()
  206. : Event(Event::Show)
  207. {
  208. }
  209. };
  210. class HideEvent final : public Event {
  211. public:
  212. HideEvent()
  213. : Event(Event::Hide)
  214. {
  215. }
  216. };
  217. enum MouseButton : u8 {
  218. None = 0,
  219. Left = 1,
  220. Right = 2,
  221. Middle = 4,
  222. };
  223. class KeyEvent final : public Event {
  224. public:
  225. KeyEvent(Type type, int key, u8 modifiers)
  226. : Event(type)
  227. , m_key(key)
  228. , m_modifiers(modifiers)
  229. {
  230. }
  231. int key() const { return m_key; }
  232. bool ctrl() const { return m_modifiers & Mod_Ctrl; }
  233. bool alt() const { return m_modifiers & Mod_Alt; }
  234. bool shift() const { return m_modifiers & Mod_Shift; }
  235. bool logo() const { return m_modifiers & Mod_Logo; }
  236. u8 modifiers() const { return m_modifiers; }
  237. String text() const { return m_text; }
  238. private:
  239. friend class WindowServerConnection;
  240. int m_key { 0 };
  241. u8 m_modifiers { 0 };
  242. String m_text;
  243. };
  244. class MouseEvent final : public Event {
  245. public:
  246. MouseEvent(Type type, const Gfx::Point& position, unsigned buttons, MouseButton button, unsigned modifiers, int wheel_delta)
  247. : Event(type)
  248. , m_position(position)
  249. , m_buttons(buttons)
  250. , m_button(button)
  251. , m_modifiers(modifiers)
  252. , m_wheel_delta(wheel_delta)
  253. {
  254. }
  255. Gfx::Point position() const { return m_position; }
  256. int x() const { return m_position.x(); }
  257. int y() const { return m_position.y(); }
  258. MouseButton button() const { return m_button; }
  259. unsigned buttons() const { return m_buttons; }
  260. unsigned modifiers() const { return m_modifiers; }
  261. int wheel_delta() const { return m_wheel_delta; }
  262. private:
  263. Gfx::Point m_position;
  264. unsigned m_buttons { 0 };
  265. MouseButton m_button { MouseButton::None };
  266. unsigned m_modifiers { 0 };
  267. int m_wheel_delta { 0 };
  268. };
  269. class DragEvent final : public Event {
  270. public:
  271. DragEvent(Type type, const Gfx::Point& position, const String& data_type)
  272. : Event(type)
  273. , m_position(position)
  274. , m_data_type(data_type)
  275. {
  276. }
  277. const Gfx::Point& position() const { return m_position; }
  278. const String& data_type() const { return m_data_type; }
  279. private:
  280. Gfx::Point m_position;
  281. String m_data_type;
  282. };
  283. class DropEvent final : public Event {
  284. public:
  285. DropEvent(const Gfx::Point&, const String& text, NonnullRefPtr<Core::MimeData> mime_data);
  286. ~DropEvent();
  287. const Gfx::Point& position() const { return m_position; }
  288. const String& text() const { return m_text; }
  289. const Core::MimeData& mime_data() const { return m_mime_data; }
  290. private:
  291. Gfx::Point m_position;
  292. String m_text;
  293. NonnullRefPtr<Core::MimeData> m_mime_data;
  294. };
  295. class ThemeChangeEvent final : public Event {
  296. public:
  297. ThemeChangeEvent()
  298. : Event(Type::ThemeChange)
  299. {
  300. }
  301. };
  302. }