GWidget.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include "GWidget.h"
  2. #include "GEvent.h"
  3. #include "GEventLoop.h"
  4. #include "GWindow.h"
  5. #include <AK/Assertions.h>
  6. #include <SharedGraphics/GraphicsBitmap.h>
  7. #include <SharedGraphics/Painter.h>
  8. GWidget::GWidget(GWidget* parent)
  9. : GObject(parent)
  10. {
  11. set_font(nullptr);
  12. m_background_color = Color::LightGray;
  13. m_foreground_color = Color::Black;
  14. }
  15. GWidget::~GWidget()
  16. {
  17. }
  18. void GWidget::set_relative_rect(const Rect& rect)
  19. {
  20. if (rect == m_relative_rect)
  21. return;
  22. if (m_relative_rect.size() != rect.size()) {
  23. auto event = make<GResizeEvent>(m_relative_rect.size(), rect.size());
  24. GEventLoop::main().post_event(this, move(event));
  25. }
  26. m_relative_rect = rect;
  27. update();
  28. }
  29. void GWidget::repaint(const Rect& rect)
  30. {
  31. // FIXME: Implement.
  32. }
  33. void GWidget::event(GEvent& event)
  34. {
  35. switch (event.type()) {
  36. case GEvent::Paint:
  37. m_has_pending_paint_event = false;
  38. return handle_paint_event(static_cast<GPaintEvent&>(event));
  39. case GEvent::Resize:
  40. return resize_event(static_cast<GResizeEvent&>(event));
  41. case GEvent::FocusIn:
  42. return focusin_event(event);
  43. case GEvent::FocusOut:
  44. return focusout_event(event);
  45. case GEvent::Show:
  46. return show_event(static_cast<GShowEvent&>(event));
  47. case GEvent::Hide:
  48. return hide_event(static_cast<GHideEvent&>(event));
  49. case GEvent::KeyDown:
  50. return keydown_event(static_cast<GKeyEvent&>(event));
  51. case GEvent::KeyUp:
  52. return keyup_event(static_cast<GKeyEvent&>(event));
  53. case GEvent::MouseMove:
  54. return mousemove_event(static_cast<GMouseEvent&>(event));
  55. case GEvent::MouseDown:
  56. if (accepts_focus())
  57. set_focus(true);
  58. return mousedown_event(static_cast<GMouseEvent&>(event));
  59. case GEvent::MouseUp:
  60. return mouseup_event(static_cast<GMouseEvent&>(event));
  61. default:
  62. return GObject::event(event);
  63. }
  64. }
  65. void GWidget::handle_paint_event(GPaintEvent& event)
  66. {
  67. if (fill_with_background_color()) {
  68. Painter painter(*this);
  69. painter.fill_rect(event.rect(), background_color());
  70. }
  71. paint_event(event);
  72. for (auto* ch : children()) {
  73. auto* child = (GWidget*)ch;
  74. if (child->relative_rect().intersects(event.rect())) {
  75. auto local_rect = event.rect();
  76. local_rect.intersect(child->relative_rect());
  77. local_rect.move_by(-child->relative_rect().x(), -child->relative_rect().y());
  78. GPaintEvent local_event(local_rect);
  79. child->event(local_event);
  80. }
  81. }
  82. }
  83. void GWidget::resize_event(GResizeEvent&)
  84. {
  85. }
  86. void GWidget::paint_event(GPaintEvent&)
  87. {
  88. }
  89. void GWidget::show_event(GShowEvent&)
  90. {
  91. }
  92. void GWidget::hide_event(GHideEvent&)
  93. {
  94. }
  95. void GWidget::keydown_event(GKeyEvent&)
  96. {
  97. }
  98. void GWidget::keyup_event(GKeyEvent&)
  99. {
  100. }
  101. void GWidget::mousedown_event(GMouseEvent&)
  102. {
  103. }
  104. void GWidget::mouseup_event(GMouseEvent&)
  105. {
  106. }
  107. void GWidget::mousemove_event(GMouseEvent&)
  108. {
  109. }
  110. void GWidget::focusin_event(GEvent&)
  111. {
  112. }
  113. void GWidget::focusout_event(GEvent&)
  114. {
  115. }
  116. void GWidget::update()
  117. {
  118. auto* w = window();
  119. if (!w)
  120. return;
  121. if (m_has_pending_paint_event)
  122. return;
  123. m_has_pending_paint_event = true;
  124. w->update(window_relative_rect());
  125. }
  126. Rect GWidget::window_relative_rect() const
  127. {
  128. auto rect = relative_rect();
  129. for (auto* parent = parent_widget(); parent; parent = parent->parent_widget()) {
  130. rect.move_by(parent->relative_position());
  131. }
  132. return rect;
  133. }
  134. GWidget::HitTestResult GWidget::hit_test(int x, int y)
  135. {
  136. // FIXME: Care about z-order.
  137. for (auto* ch : children()) {
  138. auto* child = (GWidget*)ch;
  139. if (child->relative_rect().contains(x, y)) {
  140. return child->hit_test(x - child->relative_rect().x(), y - child->relative_rect().y());
  141. }
  142. }
  143. return { this, x, y };
  144. }
  145. void GWidget::set_window(GWindow* window)
  146. {
  147. if (m_window == window)
  148. return;
  149. m_window = window;
  150. }
  151. bool GWidget::is_focused() const
  152. {
  153. auto* win = window();
  154. if (!win)
  155. return false;
  156. if (!win->is_active())
  157. return false;
  158. return win->focused_widget() == this;
  159. }
  160. void GWidget::set_focus(bool focus)
  161. {
  162. auto* win = window();
  163. if (!win)
  164. return;
  165. if (focus) {
  166. win->set_focused_widget(this);
  167. } else {
  168. if (win->focused_widget() == this)
  169. win->set_focused_widget(nullptr);
  170. }
  171. }
  172. void GWidget::set_font(RetainPtr<Font>&& font)
  173. {
  174. if (!font)
  175. m_font = Font::default_font();
  176. else
  177. m_font = move(font);
  178. }
  179. void GWidget::set_global_cursor_tracking(bool enabled)
  180. {
  181. auto* win = window();
  182. if (!win)
  183. return;
  184. win->set_global_cursor_tracking_widget(enabled ? this : nullptr);
  185. }
  186. bool GWidget::global_cursor_tracking() const
  187. {
  188. auto* win = window();
  189. if (!win)
  190. return false;
  191. return win->global_cursor_tracking_widget() == this;
  192. }