GEvent.h 6.7 KB

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