GEvent.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #pragma once
  2. #include <SharedGraphics/Point.h>
  3. #include <SharedGraphics/Rect.h>
  4. #include <AK/AKString.h>
  5. #include <AK/Types.h>
  6. #include <AK/WeakPtr.h>
  7. #include <Kernel/KeyCode.h>
  8. #include <LibGUI/GWindowType.h>
  9. class GObject;
  10. class GEvent {
  11. public:
  12. enum Type {
  13. Invalid = 0,
  14. Quit,
  15. Show,
  16. Hide,
  17. Paint,
  18. Resize,
  19. MouseMove,
  20. MouseDown,
  21. MouseUp,
  22. Enter,
  23. Leave,
  24. KeyDown,
  25. KeyUp,
  26. Timer,
  27. DeferredDestroy,
  28. WindowEntered,
  29. WindowLeft,
  30. WindowBecameInactive,
  31. WindowBecameActive,
  32. FocusIn,
  33. FocusOut,
  34. WindowCloseRequest,
  35. ChildAdded,
  36. ChildRemoved,
  37. WM_WindowRemoved,
  38. WM_WindowStateChanged,
  39. };
  40. GEvent() { }
  41. explicit GEvent(Type type) : m_type(type) { }
  42. virtual ~GEvent() { }
  43. Type type() const { return m_type; }
  44. bool is_mouse_event() const { return m_type == MouseMove || m_type == MouseDown || m_type == MouseUp; }
  45. bool is_key_event() const { return m_type == KeyUp || m_type == KeyDown; }
  46. bool is_paint_event() const { return m_type == Paint; }
  47. private:
  48. Type m_type { Invalid };
  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 String& title, const Rect& rect, bool is_active, GWindowType window_type)
  74. : GWMEvent(GEvent::Type::WM_WindowStateChanged, client_id, window_id)
  75. , m_title(title)
  76. , m_rect(rect)
  77. , m_active(is_active)
  78. , m_window_type(window_type)
  79. {
  80. }
  81. String title() const { return m_title; }
  82. Rect rect() const { return m_rect; }
  83. bool is_active() const { return m_active; }
  84. GWindowType window_type() const { return m_window_type; }
  85. private:
  86. String m_title;
  87. Rect m_rect;
  88. bool m_active;
  89. GWindowType m_window_type;
  90. };
  91. class QuitEvent final : public GEvent {
  92. public:
  93. QuitEvent()
  94. : GEvent(GEvent::Quit)
  95. {
  96. }
  97. };
  98. class GPaintEvent final : public GEvent {
  99. public:
  100. explicit GPaintEvent(const Rect& rect, const Size& window_size = Size())
  101. : GEvent(GEvent::Paint)
  102. , m_rect(rect)
  103. , m_window_size(window_size)
  104. {
  105. }
  106. Rect rect() const { return m_rect; }
  107. Size window_size() const { return m_window_size; }
  108. private:
  109. Rect m_rect;
  110. Size m_window_size;
  111. };
  112. class GResizeEvent final : public GEvent {
  113. public:
  114. explicit GResizeEvent(const Size& old_size, const Size& size)
  115. : GEvent(GEvent::Resize)
  116. , m_old_size(old_size)
  117. , m_size(size)
  118. {
  119. }
  120. const Size& old_size() const { return m_old_size; }
  121. const Size& size() const { return m_size; }
  122. private:
  123. Size m_old_size;
  124. Size m_size;
  125. };
  126. class GShowEvent final : public GEvent {
  127. public:
  128. GShowEvent()
  129. : GEvent(GEvent::Show)
  130. {
  131. }
  132. };
  133. class GHideEvent final : public GEvent {
  134. public:
  135. GHideEvent()
  136. : GEvent(GEvent::Hide)
  137. {
  138. }
  139. };
  140. enum GMouseButton : byte {
  141. None = 0,
  142. Left = 1,
  143. Right = 2,
  144. Middle = 4,
  145. };
  146. class GKeyEvent final : public GEvent {
  147. public:
  148. GKeyEvent(Type type, int key, byte modifiers)
  149. : GEvent(type)
  150. , m_key(key)
  151. , m_modifiers(modifiers)
  152. {
  153. }
  154. int key() const { return m_key; }
  155. bool ctrl() const { return m_modifiers & Mod_Ctrl; }
  156. bool alt() const { return m_modifiers & Mod_Alt; }
  157. bool shift() const { return m_modifiers & Mod_Shift; }
  158. bool logo() const { return m_modifiers & Mod_Logo; }
  159. byte modifiers() const { return m_modifiers; }
  160. String text() const { return m_text; }
  161. private:
  162. friend class GEventLoop;
  163. int m_key { 0 };
  164. byte m_modifiers { 0 };
  165. String m_text;
  166. };
  167. class GMouseEvent final : public GEvent {
  168. public:
  169. GMouseEvent(Type type, const Point& position, unsigned buttons, GMouseButton button, unsigned modifiers)
  170. : GEvent(type)
  171. , m_position(position)
  172. , m_buttons(buttons)
  173. , m_button(button)
  174. , m_modifiers(modifiers)
  175. {
  176. }
  177. Point position() const { return m_position; }
  178. int x() const { return m_position.x(); }
  179. int y() const { return m_position.y(); }
  180. GMouseButton button() const { return m_button; }
  181. unsigned buttons() const { return m_buttons; }
  182. unsigned modifiers() const { return m_modifiers; }
  183. private:
  184. Point m_position;
  185. unsigned m_buttons { 0 };
  186. GMouseButton m_button { GMouseButton::None };
  187. unsigned m_modifiers { 0 };
  188. };
  189. class GTimerEvent final : public GEvent {
  190. public:
  191. explicit GTimerEvent(int timer_id) : GEvent(GEvent::Timer), m_timer_id(timer_id) { }
  192. ~GTimerEvent() { }
  193. int timer_id() const { return m_timer_id; }
  194. private:
  195. int m_timer_id;
  196. };
  197. class GChildEvent final : public GEvent {
  198. public:
  199. GChildEvent(Type, GObject& child);
  200. ~GChildEvent();
  201. GObject* child() { return m_child.ptr(); }
  202. const GObject* child() const { return m_child.ptr(); }
  203. private:
  204. WeakPtr<GObject> m_child;
  205. };