GEvent.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #pragma once
  2. #include <SharedGraphics/Point.h>
  3. #include <SharedGraphics/Rect.h>
  4. #include <AK/AKString.h>
  5. #include <AK/Types.h>
  6. class GEvent {
  7. public:
  8. enum Type {
  9. Invalid = 0,
  10. Quit,
  11. Show,
  12. Hide,
  13. Paint,
  14. Resize,
  15. MouseMove,
  16. MouseDown,
  17. MouseUp,
  18. Enter,
  19. Leave,
  20. KeyDown,
  21. KeyUp,
  22. Timer,
  23. DeferredDestroy,
  24. WindowEntered,
  25. WindowLeft,
  26. WindowBecameInactive,
  27. WindowBecameActive,
  28. FocusIn,
  29. FocusOut,
  30. WindowCloseRequest,
  31. };
  32. GEvent() { }
  33. explicit GEvent(Type type) : m_type(type) { }
  34. virtual ~GEvent() { }
  35. Type type() const { return m_type; }
  36. bool is_mouse_event() const { return m_type == MouseMove || m_type == MouseDown || m_type == MouseUp; }
  37. bool is_key_event() const { return m_type == KeyUp || m_type == KeyDown; }
  38. bool is_paint_event() const { return m_type == Paint; }
  39. private:
  40. Type m_type { Invalid };
  41. };
  42. class QuitEvent final : public GEvent {
  43. public:
  44. QuitEvent()
  45. : GEvent(GEvent::Quit)
  46. {
  47. }
  48. };
  49. class GPaintEvent final : public GEvent {
  50. public:
  51. explicit GPaintEvent(const Rect& rect = Rect())
  52. : GEvent(GEvent::Paint)
  53. , m_rect(rect)
  54. {
  55. }
  56. const Rect& rect() const { return m_rect; }
  57. private:
  58. Rect m_rect;
  59. };
  60. class GResizeEvent final : public GEvent {
  61. public:
  62. explicit GResizeEvent(const Size& old_size, const Size& size)
  63. : GEvent(GEvent::Resize)
  64. , m_old_size(old_size)
  65. , m_size(size)
  66. {
  67. }
  68. const Size& old_size() const { return m_old_size; }
  69. const Size& size() const { return m_size; }
  70. private:
  71. Size m_old_size;
  72. Size m_size;
  73. };
  74. class GShowEvent final : public GEvent {
  75. public:
  76. GShowEvent()
  77. : GEvent(GEvent::Show)
  78. {
  79. }
  80. };
  81. class GHideEvent final : public GEvent {
  82. public:
  83. GHideEvent()
  84. : GEvent(GEvent::Hide)
  85. {
  86. }
  87. };
  88. enum GMouseButton : byte {
  89. None = 0,
  90. Left = 1,
  91. Right = 2,
  92. Middle = 4,
  93. };
  94. class GKeyEvent final : public GEvent {
  95. public:
  96. GKeyEvent(Type type, int key)
  97. : GEvent(type)
  98. , m_key(key)
  99. {
  100. }
  101. int key() const { return m_key; }
  102. bool ctrl() const { return m_ctrl; }
  103. bool alt() const { return m_alt; }
  104. bool shift() const { return m_shift; }
  105. String text() const { return m_text; }
  106. private:
  107. friend class GEventLoop;
  108. int m_key { 0 };
  109. bool m_ctrl { false };
  110. bool m_alt { false };
  111. bool m_shift { false };
  112. String m_text;
  113. };
  114. class GMouseEvent final : public GEvent {
  115. public:
  116. GMouseEvent(Type type, const Point& position, unsigned buttons, GMouseButton button = GMouseButton::None)
  117. : GEvent(type)
  118. , m_position(position)
  119. , m_buttons(buttons)
  120. , m_button(button)
  121. {
  122. }
  123. Point position() const { return m_position; }
  124. int x() const { return m_position.x(); }
  125. int y() const { return m_position.y(); }
  126. GMouseButton button() const { return m_button; }
  127. unsigned buttons() const { return m_buttons; }
  128. private:
  129. Point m_position;
  130. unsigned m_buttons { 0 };
  131. GMouseButton m_button { GMouseButton::None };
  132. };
  133. class GTimerEvent final : public GEvent {
  134. public:
  135. explicit GTimerEvent(int timer_id) : GEvent(GEvent::Timer), m_timer_id(timer_id) { }
  136. ~GTimerEvent() { }
  137. int timer_id() const { return m_timer_id; }
  138. private:
  139. int m_timer_id;
  140. };