GWidget.cpp 6.2 KB

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