Event.h 9.1 KB

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