GWidget.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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::White;
  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. // FIXME: Make some kind of event loop driven ResizeEvent?
  23. m_relative_rect = rect;
  24. update();
  25. }
  26. void GWidget::repaint(const Rect& rect)
  27. {
  28. // FIXME: Implement.
  29. }
  30. void GWidget::event(GEvent& event)
  31. {
  32. switch (event.type()) {
  33. case GEvent::Paint:
  34. m_has_pending_paint_event = false;
  35. return paint_event(static_cast<GPaintEvent&>(event));
  36. case GEvent::FocusIn:
  37. return focusin_event(event);
  38. case GEvent::FocusOut:
  39. return focusout_event(event);
  40. case GEvent::Show:
  41. return show_event(static_cast<GShowEvent&>(event));
  42. case GEvent::Hide:
  43. return hide_event(static_cast<GHideEvent&>(event));
  44. case GEvent::KeyDown:
  45. return keydown_event(static_cast<GKeyEvent&>(event));
  46. case GEvent::KeyUp:
  47. return keyup_event(static_cast<GKeyEvent&>(event));
  48. case GEvent::MouseMove:
  49. return mousemove_event(static_cast<GMouseEvent&>(event));
  50. case GEvent::MouseDown:
  51. if (accepts_focus())
  52. set_focus(true);
  53. return mousedown_event(static_cast<GMouseEvent&>(event));
  54. case GEvent::MouseUp:
  55. return mouseup_event(static_cast<GMouseEvent&>(event));
  56. default:
  57. return GObject::event(event);
  58. }
  59. }
  60. void GWidget::paint_event(GPaintEvent& event)
  61. {
  62. if (fill_with_background_color()) {
  63. Painter painter(*this);
  64. painter.fill_rect(event.rect(), background_color());
  65. }
  66. for (auto* ch : children()) {
  67. auto* child = (GWidget*)ch;
  68. if (child->relative_rect().intersects(event.rect())) {
  69. // FIXME: Pass localized rect?
  70. child->event(event);
  71. }
  72. }
  73. }
  74. void GWidget::show_event(GShowEvent&)
  75. {
  76. }
  77. void GWidget::hide_event(GHideEvent&)
  78. {
  79. }
  80. void GWidget::keydown_event(GKeyEvent&)
  81. {
  82. }
  83. void GWidget::keyup_event(GKeyEvent&)
  84. {
  85. }
  86. void GWidget::mousedown_event(GMouseEvent&)
  87. {
  88. }
  89. void GWidget::mouseup_event(GMouseEvent&)
  90. {
  91. }
  92. void GWidget::mousemove_event(GMouseEvent&)
  93. {
  94. }
  95. void GWidget::focusin_event(GEvent&)
  96. {
  97. }
  98. void GWidget::focusout_event(GEvent&)
  99. {
  100. }
  101. void GWidget::update()
  102. {
  103. auto* w = window();
  104. if (!w)
  105. return;
  106. if (m_has_pending_paint_event)
  107. return;
  108. m_has_pending_paint_event = true;
  109. w->update(relative_rect());
  110. }
  111. GWidget::HitTestResult GWidget::hit_test(int x, int y)
  112. {
  113. // FIXME: Care about z-order.
  114. for (auto* ch : children()) {
  115. auto* child = (GWidget*)ch;
  116. if (child->relative_rect().contains(x, y)) {
  117. return child->hit_test(x - child->relative_rect().x(), y - child->relative_rect().y());
  118. }
  119. }
  120. return { this, x, y };
  121. }
  122. void GWidget::set_window(GWindow* window)
  123. {
  124. if (m_window == window)
  125. return;
  126. m_window = window;
  127. }
  128. bool GWidget::is_focused() const
  129. {
  130. auto* win = window();
  131. if (!win)
  132. return false;
  133. if (!win->is_active())
  134. return false;
  135. return win->focused_widget() == this;
  136. }
  137. void GWidget::set_focus(bool focus)
  138. {
  139. auto* win = window();
  140. if (!win)
  141. return;
  142. if (focus) {
  143. win->set_focused_widget(this);
  144. } else {
  145. if (win->focused_widget() == this)
  146. win->set_focused_widget(nullptr);
  147. }
  148. }
  149. void GWidget::set_font(RetainPtr<Font>&& font)
  150. {
  151. if (!font)
  152. m_font = Font::default_font();
  153. else
  154. m_font = move(font);
  155. }
  156. void GWidget::set_global_cursor_tracking(bool enabled)
  157. {
  158. auto* win = window();
  159. if (!win)
  160. return;
  161. win->set_global_cursor_tracking_widget(enabled ? this : nullptr);
  162. }
  163. bool GWidget::global_cursor_tracking() const
  164. {
  165. auto* win = window();
  166. if (!win)
  167. return false;
  168. return win->global_cursor_tracking_widget() == this;
  169. }