GWidget.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #include "GWidget.h"
  2. #include "GEvent.h"
  3. #include "GEventLoop.h"
  4. #include "GWindow.h"
  5. #include <LibGUI/GLayout.h>
  6. #include <AK/Assertions.h>
  7. #include <SharedGraphics/GraphicsBitmap.h>
  8. #include <SharedGraphics/Painter.h>
  9. GWidget::GWidget(GWidget* parent)
  10. : GObject(parent)
  11. {
  12. set_font(nullptr);
  13. m_background_color = Color::LightGray;
  14. m_foreground_color = Color::Black;
  15. if (parent && parent->layout())
  16. parent->layout()->add_widget(*this);
  17. }
  18. GWidget::~GWidget()
  19. {
  20. }
  21. void GWidget::set_relative_rect(const Rect& rect)
  22. {
  23. if (rect == m_relative_rect)
  24. return;
  25. if (m_relative_rect.size() != rect.size()) {
  26. auto event = make<GResizeEvent>(m_relative_rect.size(), rect.size());
  27. GEventLoop::main().post_event(this, move(event));
  28. }
  29. m_relative_rect = rect;
  30. update();
  31. }
  32. void GWidget::repaint(const Rect& rect)
  33. {
  34. // FIXME: Implement.
  35. }
  36. void GWidget::event(GEvent& event)
  37. {
  38. switch (event.type()) {
  39. case GEvent::Paint:
  40. m_has_pending_paint_event = false;
  41. return handle_paint_event(static_cast<GPaintEvent&>(event));
  42. case GEvent::Resize:
  43. return handle_resize_event(static_cast<GResizeEvent&>(event));
  44. case GEvent::FocusIn:
  45. return focusin_event(event);
  46. case GEvent::FocusOut:
  47. return focusout_event(event);
  48. case GEvent::Show:
  49. return show_event(static_cast<GShowEvent&>(event));
  50. case GEvent::Hide:
  51. return hide_event(static_cast<GHideEvent&>(event));
  52. case GEvent::KeyDown:
  53. return keydown_event(static_cast<GKeyEvent&>(event));
  54. case GEvent::KeyUp:
  55. return keyup_event(static_cast<GKeyEvent&>(event));
  56. case GEvent::MouseMove:
  57. return mousemove_event(static_cast<GMouseEvent&>(event));
  58. case GEvent::MouseDown:
  59. if (accepts_focus())
  60. set_focus(true);
  61. return mousedown_event(static_cast<GMouseEvent&>(event));
  62. case GEvent::MouseUp:
  63. return mouseup_event(static_cast<GMouseEvent&>(event));
  64. default:
  65. return GObject::event(event);
  66. }
  67. }
  68. void GWidget::handle_paint_event(GPaintEvent& event)
  69. {
  70. if (fill_with_background_color()) {
  71. Painter painter(*this);
  72. painter.fill_rect(event.rect(), background_color());
  73. }
  74. paint_event(event);
  75. for (auto* ch : children()) {
  76. auto* child = (GWidget*)ch;
  77. if (child->relative_rect().intersects(event.rect())) {
  78. auto local_rect = event.rect();
  79. local_rect.intersect(child->relative_rect());
  80. local_rect.move_by(-child->relative_rect().x(), -child->relative_rect().y());
  81. GPaintEvent local_event(local_rect);
  82. child->event(local_event);
  83. }
  84. }
  85. }
  86. void GWidget::set_layout(OwnPtr<GLayout>&& layout)
  87. {
  88. if (m_layout.ptr() == layout.ptr())
  89. return;
  90. if (m_layout)
  91. m_layout->notify_disowned(Badge<GWidget>(), *this);
  92. m_layout = move(layout);
  93. if (m_layout) {
  94. m_layout->notify_adopted(Badge<GWidget>(), *this);
  95. do_layout();
  96. } else {
  97. update();
  98. }
  99. }
  100. void GWidget::do_layout()
  101. {
  102. if (!m_layout)
  103. return;
  104. m_layout->run(*this);
  105. update();
  106. }
  107. void GWidget::notify_layout_changed(Badge<GLayout>)
  108. {
  109. do_layout();
  110. }
  111. void GWidget::handle_resize_event(GResizeEvent& event)
  112. {
  113. if (layout())
  114. do_layout();
  115. return resize_event(event);
  116. }
  117. void GWidget::resize_event(GResizeEvent&)
  118. {
  119. }
  120. void GWidget::paint_event(GPaintEvent&)
  121. {
  122. }
  123. void GWidget::show_event(GShowEvent&)
  124. {
  125. }
  126. void GWidget::hide_event(GHideEvent&)
  127. {
  128. }
  129. void GWidget::keydown_event(GKeyEvent&)
  130. {
  131. }
  132. void GWidget::keyup_event(GKeyEvent&)
  133. {
  134. }
  135. void GWidget::mousedown_event(GMouseEvent&)
  136. {
  137. }
  138. void GWidget::mouseup_event(GMouseEvent&)
  139. {
  140. }
  141. void GWidget::mousemove_event(GMouseEvent&)
  142. {
  143. }
  144. void GWidget::focusin_event(GEvent&)
  145. {
  146. }
  147. void GWidget::focusout_event(GEvent&)
  148. {
  149. }
  150. void GWidget::update()
  151. {
  152. auto* w = window();
  153. if (!w)
  154. return;
  155. if (m_has_pending_paint_event)
  156. return;
  157. m_has_pending_paint_event = true;
  158. w->update(window_relative_rect());
  159. }
  160. Rect GWidget::window_relative_rect() const
  161. {
  162. auto rect = relative_rect();
  163. for (auto* parent = parent_widget(); parent; parent = parent->parent_widget()) {
  164. rect.move_by(parent->relative_position());
  165. }
  166. return rect;
  167. }
  168. GWidget::HitTestResult GWidget::hit_test(int x, int y)
  169. {
  170. // FIXME: Care about z-order.
  171. for (auto* ch : children()) {
  172. auto* child = (GWidget*)ch;
  173. if (child->relative_rect().contains(x, y)) {
  174. return child->hit_test(x - child->relative_rect().x(), y - child->relative_rect().y());
  175. }
  176. }
  177. return { this, x, y };
  178. }
  179. void GWidget::set_window(GWindow* window)
  180. {
  181. if (m_window == window)
  182. return;
  183. m_window = window;
  184. }
  185. bool GWidget::is_focused() const
  186. {
  187. auto* win = window();
  188. if (!win)
  189. return false;
  190. if (!win->is_active())
  191. return false;
  192. return win->focused_widget() == this;
  193. }
  194. void GWidget::set_focus(bool focus)
  195. {
  196. auto* win = window();
  197. if (!win)
  198. return;
  199. if (focus) {
  200. win->set_focused_widget(this);
  201. } else {
  202. if (win->focused_widget() == this)
  203. win->set_focused_widget(nullptr);
  204. }
  205. }
  206. void GWidget::set_font(RetainPtr<Font>&& font)
  207. {
  208. if (!font)
  209. m_font = Font::default_font();
  210. else
  211. m_font = move(font);
  212. }
  213. void GWidget::set_global_cursor_tracking(bool enabled)
  214. {
  215. auto* win = window();
  216. if (!win)
  217. return;
  218. win->set_global_cursor_tracking_widget(enabled ? this : nullptr);
  219. }
  220. bool GWidget::global_cursor_tracking() const
  221. {
  222. auto* win = window();
  223. if (!win)
  224. return false;
  225. return win->global_cursor_tracking_widget() == this;
  226. }
  227. void GWidget::set_preferred_size(const Size& size)
  228. {
  229. if (m_preferred_size == size)
  230. return;
  231. m_preferred_size = size;
  232. invalidate_layout();
  233. }
  234. void GWidget::set_size_policy(SizePolicy horizontal_policy, SizePolicy vertical_policy)
  235. {
  236. if (m_horizontal_size_policy == horizontal_policy && m_vertical_size_policy == vertical_policy)
  237. return;
  238. m_horizontal_size_policy = horizontal_policy;
  239. m_vertical_size_policy = vertical_policy;
  240. invalidate_layout();
  241. }
  242. void GWidget::invalidate_layout()
  243. {
  244. auto* w = window();
  245. if (!w)
  246. return;
  247. if (!w->main_widget())
  248. return;
  249. w->main_widget()->do_layout();
  250. }