Event.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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, bool is_frameless)
  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. , m_frameless(is_frameless)
  109. {
  110. }
  111. String title() const { return m_title; }
  112. Gfx::Rect rect() const { return m_rect; }
  113. bool is_active() const { return m_active; }
  114. WindowType window_type() const { return m_window_type; }
  115. bool is_minimized() const { return m_minimized; }
  116. bool is_frameless() const { return m_frameless; }
  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. bool m_frameless;
  124. };
  125. class WMWindowRectChangedEvent : public WMEvent {
  126. public:
  127. WMWindowRectChangedEvent(int client_id, int window_id, const Gfx::Rect& rect)
  128. : WMEvent(Event::Type::WM_WindowRectChanged, client_id, window_id)
  129. , m_rect(rect)
  130. {
  131. }
  132. Gfx::Rect rect() const { return m_rect; }
  133. private:
  134. Gfx::Rect m_rect;
  135. };
  136. class WMWindowIconBitmapChangedEvent : public WMEvent {
  137. public:
  138. WMWindowIconBitmapChangedEvent(int client_id, int window_id, int icon_buffer_id, const Gfx::Size& icon_size)
  139. : WMEvent(Event::Type::WM_WindowIconBitmapChanged, client_id, window_id)
  140. , m_icon_buffer_id(icon_buffer_id)
  141. , m_icon_size(icon_size)
  142. {
  143. }
  144. int icon_buffer_id() const { return m_icon_buffer_id; }
  145. const Gfx::Size& icon_size() const { return m_icon_size; }
  146. private:
  147. int m_icon_buffer_id;
  148. Gfx::Size m_icon_size;
  149. };
  150. class MultiPaintEvent final : public Event {
  151. public:
  152. explicit MultiPaintEvent(const Vector<Gfx::Rect, 32>& rects, const Gfx::Size& window_size)
  153. : Event(Event::MultiPaint)
  154. , m_rects(rects)
  155. , m_window_size(window_size)
  156. {
  157. }
  158. const Vector<Gfx::Rect, 32>& rects() const { return m_rects; }
  159. Gfx::Size window_size() const { return m_window_size; }
  160. private:
  161. Vector<Gfx::Rect, 32> m_rects;
  162. Gfx::Size m_window_size;
  163. };
  164. class PaintEvent final : public Event {
  165. public:
  166. explicit PaintEvent(const Gfx::Rect& rect, const Gfx::Size& window_size = {})
  167. : Event(Event::Paint)
  168. , m_rect(rect)
  169. , m_window_size(window_size)
  170. {
  171. }
  172. Gfx::Rect rect() const { return m_rect; }
  173. Gfx::Size window_size() const { return m_window_size; }
  174. private:
  175. Gfx::Rect m_rect;
  176. Gfx::Size m_window_size;
  177. };
  178. class ResizeEvent final : public Event {
  179. public:
  180. explicit ResizeEvent(const Gfx::Size& old_size, const Gfx::Size& size)
  181. : Event(Event::Resize)
  182. , m_old_size(old_size)
  183. , m_size(size)
  184. {
  185. }
  186. const Gfx::Size& old_size() const { return m_old_size; }
  187. const Gfx::Size& size() const { return m_size; }
  188. private:
  189. Gfx::Size m_old_size;
  190. Gfx::Size m_size;
  191. };
  192. class ContextMenuEvent final : public Event {
  193. public:
  194. explicit ContextMenuEvent(const Gfx::Point& position, const Gfx::Point& screen_position)
  195. : Event(Event::ContextMenu)
  196. , m_position(position)
  197. , m_screen_position(screen_position)
  198. {
  199. }
  200. const Gfx::Point& position() const { return m_position; }
  201. const Gfx::Point& screen_position() const { return m_screen_position; }
  202. private:
  203. Gfx::Point m_position;
  204. Gfx::Point m_screen_position;
  205. };
  206. class ShowEvent final : public Event {
  207. public:
  208. ShowEvent()
  209. : Event(Event::Show)
  210. {
  211. }
  212. };
  213. class HideEvent final : public Event {
  214. public:
  215. HideEvent()
  216. : Event(Event::Hide)
  217. {
  218. }
  219. };
  220. enum MouseButton : u8 {
  221. None = 0,
  222. Left = 1,
  223. Right = 2,
  224. Middle = 4,
  225. Back = 8,
  226. Forward = 16,
  227. };
  228. class KeyEvent final : public Event {
  229. public:
  230. KeyEvent(Type type, int key, u8 modifiers)
  231. : Event(type)
  232. , m_key(key)
  233. , m_modifiers(modifiers)
  234. {
  235. }
  236. int key() const { return m_key; }
  237. bool ctrl() const { return m_modifiers & Mod_Ctrl; }
  238. bool alt() const { return m_modifiers & Mod_Alt; }
  239. bool shift() const { return m_modifiers & Mod_Shift; }
  240. bool logo() const { return m_modifiers & Mod_Logo; }
  241. u8 modifiers() const { return m_modifiers; }
  242. String text() const { return m_text; }
  243. private:
  244. friend class WindowServerConnection;
  245. int m_key { 0 };
  246. u8 m_modifiers { 0 };
  247. String m_text;
  248. };
  249. class MouseEvent final : public Event {
  250. public:
  251. MouseEvent(Type type, const Gfx::Point& position, unsigned buttons, MouseButton button, unsigned modifiers, int wheel_delta)
  252. : Event(type)
  253. , m_position(position)
  254. , m_buttons(buttons)
  255. , m_button(button)
  256. , m_modifiers(modifiers)
  257. , m_wheel_delta(wheel_delta)
  258. {
  259. }
  260. Gfx::Point position() const { return m_position; }
  261. int x() const { return m_position.x(); }
  262. int y() const { return m_position.y(); }
  263. MouseButton button() const { return m_button; }
  264. unsigned buttons() const { return m_buttons; }
  265. unsigned modifiers() const { return m_modifiers; }
  266. int wheel_delta() const { return m_wheel_delta; }
  267. private:
  268. Gfx::Point m_position;
  269. unsigned m_buttons { 0 };
  270. MouseButton m_button { MouseButton::None };
  271. unsigned m_modifiers { 0 };
  272. int m_wheel_delta { 0 };
  273. };
  274. class DragEvent final : public Event {
  275. public:
  276. DragEvent(Type type, const Gfx::Point& position, const String& data_type)
  277. : Event(type)
  278. , m_position(position)
  279. , m_data_type(data_type)
  280. {
  281. }
  282. const Gfx::Point& position() const { return m_position; }
  283. const String& data_type() const { return m_data_type; }
  284. private:
  285. Gfx::Point m_position;
  286. String m_data_type;
  287. };
  288. class DropEvent final : public Event {
  289. public:
  290. DropEvent(const Gfx::Point&, const String& text, NonnullRefPtr<Core::MimeData> mime_data);
  291. ~DropEvent();
  292. const Gfx::Point& position() const { return m_position; }
  293. const String& text() const { return m_text; }
  294. const Core::MimeData& mime_data() const { return m_mime_data; }
  295. private:
  296. Gfx::Point m_position;
  297. String m_text;
  298. NonnullRefPtr<Core::MimeData> m_mime_data;
  299. };
  300. class ThemeChangeEvent final : public Event {
  301. public:
  302. ThemeChangeEvent()
  303. : Event(Type::ThemeChange)
  304. {
  305. }
  306. };
  307. }