GEvent.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #pragma once
  2. #include <Kernel/KeyCode.h>
  3. #include <LibCore/CEvent.h>
  4. #include <LibDraw/Point.h>
  5. #include <LibDraw/Rect.h>
  6. #include <LibGUI/GWindowType.h>
  7. class CObject;
  8. class GEvent : public CEvent {
  9. public:
  10. enum Type {
  11. Show = 1000,
  12. Hide,
  13. Paint,
  14. MultiPaint,
  15. Resize,
  16. MouseMove,
  17. MouseDown,
  18. MouseDoubleClick,
  19. MouseUp,
  20. MouseWheel,
  21. Enter,
  22. Leave,
  23. KeyDown,
  24. KeyUp,
  25. WindowEntered,
  26. WindowLeft,
  27. WindowBecameInactive,
  28. WindowBecameActive,
  29. FocusIn,
  30. FocusOut,
  31. WindowCloseRequest,
  32. ContextMenu,
  33. EnabledChange,
  34. Drop,
  35. __Begin_WM_Events,
  36. WM_WindowRemoved,
  37. WM_WindowStateChanged,
  38. WM_WindowRectChanged,
  39. WM_WindowIconBitmapChanged,
  40. __End_WM_Events,
  41. };
  42. GEvent() {}
  43. explicit GEvent(Type type)
  44. : CEvent(type)
  45. {
  46. }
  47. virtual ~GEvent() {}
  48. bool is_key_event() const { return type() == KeyUp || type() == KeyDown; }
  49. bool is_paint_event() const { return type() == Paint; }
  50. };
  51. class GWMEvent : public GEvent {
  52. public:
  53. GWMEvent(Type type, int client_id, int window_id)
  54. : GEvent(type)
  55. , m_client_id(client_id)
  56. , m_window_id(window_id)
  57. {
  58. }
  59. int client_id() const { return m_client_id; }
  60. int window_id() const { return m_window_id; }
  61. private:
  62. int m_client_id { -1 };
  63. int m_window_id { -1 };
  64. };
  65. class GWMWindowRemovedEvent : public GWMEvent {
  66. public:
  67. GWMWindowRemovedEvent(int client_id, int window_id)
  68. : GWMEvent(GEvent::Type::WM_WindowRemoved, client_id, window_id)
  69. {
  70. }
  71. };
  72. class GWMWindowStateChangedEvent : public GWMEvent {
  73. public:
  74. GWMWindowStateChangedEvent(int client_id, int window_id, const StringView& title, const Rect& rect, bool is_active, GWindowType window_type, bool is_minimized)
  75. : GWMEvent(GEvent::Type::WM_WindowStateChanged, client_id, window_id)
  76. , m_title(title)
  77. , m_rect(rect)
  78. , m_window_type(window_type)
  79. , m_active(is_active)
  80. , m_minimized(is_minimized)
  81. {
  82. }
  83. String title() const { return m_title; }
  84. Rect rect() const { return m_rect; }
  85. bool is_active() const { return m_active; }
  86. GWindowType window_type() const { return m_window_type; }
  87. bool is_minimized() const { return m_minimized; }
  88. private:
  89. String m_title;
  90. Rect m_rect;
  91. GWindowType m_window_type;
  92. bool m_active;
  93. bool m_minimized;
  94. };
  95. class GWMWindowRectChangedEvent : public GWMEvent {
  96. public:
  97. GWMWindowRectChangedEvent(int client_id, int window_id, const Rect& rect)
  98. : GWMEvent(GEvent::Type::WM_WindowRectChanged, client_id, window_id)
  99. , m_rect(rect)
  100. {
  101. }
  102. Rect rect() const { return m_rect; }
  103. private:
  104. Rect m_rect;
  105. };
  106. class GWMWindowIconBitmapChangedEvent : public GWMEvent {
  107. public:
  108. GWMWindowIconBitmapChangedEvent(int client_id, int window_id, int icon_buffer_id, const Size& icon_size)
  109. : GWMEvent(GEvent::Type::WM_WindowIconBitmapChanged, client_id, window_id)
  110. , m_icon_buffer_id(icon_buffer_id)
  111. , m_icon_size(icon_size)
  112. {
  113. }
  114. int icon_buffer_id() const { return m_icon_buffer_id; }
  115. const Size& icon_size() const { return m_icon_size; }
  116. private:
  117. int m_icon_buffer_id;
  118. Size m_icon_size;
  119. };
  120. class GMultiPaintEvent final : public GEvent {
  121. public:
  122. explicit GMultiPaintEvent(const Vector<Rect, 32>& rects, const Size& window_size)
  123. : GEvent(GEvent::MultiPaint)
  124. , m_rects(rects)
  125. , m_window_size(window_size)
  126. {
  127. }
  128. const Vector<Rect, 32>& rects() const { return m_rects; }
  129. Size window_size() const { return m_window_size; }
  130. private:
  131. Vector<Rect, 32> m_rects;
  132. Size m_window_size;
  133. };
  134. class GPaintEvent final : public GEvent {
  135. public:
  136. explicit GPaintEvent(const Rect& rect, const Size& window_size = Size())
  137. : GEvent(GEvent::Paint)
  138. , m_rect(rect)
  139. , m_window_size(window_size)
  140. {
  141. }
  142. Rect rect() const { return m_rect; }
  143. Size window_size() const { return m_window_size; }
  144. private:
  145. Rect m_rect;
  146. Size m_window_size;
  147. };
  148. class GResizeEvent final : public GEvent {
  149. public:
  150. explicit GResizeEvent(const Size& old_size, const Size& size)
  151. : GEvent(GEvent::Resize)
  152. , m_old_size(old_size)
  153. , m_size(size)
  154. {
  155. }
  156. const Size& old_size() const { return m_old_size; }
  157. const Size& size() const { return m_size; }
  158. private:
  159. Size m_old_size;
  160. Size m_size;
  161. };
  162. class GContextMenuEvent final : public GEvent {
  163. public:
  164. explicit GContextMenuEvent(const Point& position, const Point& screen_position)
  165. : GEvent(GEvent::ContextMenu)
  166. , m_position(position)
  167. , m_screen_position(screen_position)
  168. {
  169. }
  170. const Point& position() const { return m_position; }
  171. const Point& screen_position() const { return m_screen_position; }
  172. private:
  173. Point m_position;
  174. Point m_screen_position;
  175. };
  176. class GShowEvent final : public GEvent {
  177. public:
  178. GShowEvent()
  179. : GEvent(GEvent::Show)
  180. {
  181. }
  182. };
  183. class GHideEvent final : public GEvent {
  184. public:
  185. GHideEvent()
  186. : GEvent(GEvent::Hide)
  187. {
  188. }
  189. };
  190. enum GMouseButton : u8 {
  191. None = 0,
  192. Left = 1,
  193. Right = 2,
  194. Middle = 4,
  195. };
  196. class GKeyEvent final : public GEvent {
  197. public:
  198. GKeyEvent(Type type, int key, u8 modifiers)
  199. : GEvent(type)
  200. , m_key(key)
  201. , m_modifiers(modifiers)
  202. {
  203. }
  204. int key() const { return m_key; }
  205. bool ctrl() const { return m_modifiers & Mod_Ctrl; }
  206. bool alt() const { return m_modifiers & Mod_Alt; }
  207. bool shift() const { return m_modifiers & Mod_Shift; }
  208. bool logo() const { return m_modifiers & Mod_Logo; }
  209. u8 modifiers() const { return m_modifiers; }
  210. String text() const { return m_text; }
  211. private:
  212. friend class GWindowServerConnection;
  213. int m_key { 0 };
  214. u8 m_modifiers { 0 };
  215. String m_text;
  216. };
  217. class GMouseEvent final : public GEvent {
  218. public:
  219. GMouseEvent(Type type, const Point& position, unsigned buttons, GMouseButton button, unsigned modifiers, int wheel_delta)
  220. : GEvent(type)
  221. , m_position(position)
  222. , m_buttons(buttons)
  223. , m_button(button)
  224. , m_modifiers(modifiers)
  225. , m_wheel_delta(wheel_delta)
  226. {
  227. }
  228. Point position() const { return m_position; }
  229. int x() const { return m_position.x(); }
  230. int y() const { return m_position.y(); }
  231. GMouseButton button() const { return m_button; }
  232. unsigned buttons() const { return m_buttons; }
  233. unsigned modifiers() const { return m_modifiers; }
  234. int wheel_delta() const { return m_wheel_delta; }
  235. private:
  236. Point m_position;
  237. unsigned m_buttons { 0 };
  238. GMouseButton m_button { GMouseButton::None };
  239. unsigned m_modifiers { 0 };
  240. int m_wheel_delta { 0 };
  241. };
  242. class GDropEvent final : public GEvent {
  243. public:
  244. GDropEvent(const Point& position, const String& text, const String& data_type, const String& data)
  245. : GEvent(GEvent::Drop)
  246. , m_position(position)
  247. , m_text(text)
  248. , m_data_type(data_type)
  249. , m_data(data)
  250. {
  251. }
  252. const Point& position() const { return m_position; }
  253. const String& text() const { return m_text; }
  254. const String& data_type() const { return m_data_type; }
  255. const String& data() const { return m_data; }
  256. private:
  257. Point m_position;
  258. String m_text;
  259. String m_data_type;
  260. String m_data;
  261. };