GWidget.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. #include <unistd.h>
  10. GWidget::GWidget(GWidget* parent)
  11. : GObject(parent)
  12. {
  13. set_font(nullptr);
  14. m_background_color = Color::LightGray;
  15. m_foreground_color = Color::Black;
  16. if (parent && parent->layout())
  17. parent->layout()->add_widget(*this);
  18. }
  19. GWidget::~GWidget()
  20. {
  21. }
  22. void GWidget::set_relative_rect(const Rect& rect)
  23. {
  24. if (rect == m_relative_rect)
  25. return;
  26. bool size_changed = m_relative_rect.size() != rect.size();
  27. m_relative_rect = rect;
  28. if (size_changed) {
  29. GResizeEvent resize_event(m_relative_rect.size(), rect.size());
  30. event(resize_event);
  31. }
  32. update();
  33. }
  34. void GWidget::event(GEvent& event)
  35. {
  36. switch (event.type()) {
  37. case GEvent::Paint:
  38. return handle_paint_event(static_cast<GPaintEvent&>(event));
  39. case GEvent::Resize:
  40. return handle_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. case GEvent::Enter:
  62. return enter_event(event);
  63. case GEvent::Leave:
  64. return leave_event(event);
  65. default:
  66. return GObject::event(event);
  67. }
  68. }
  69. void GWidget::handle_paint_event(GPaintEvent& event)
  70. {
  71. if (fill_with_background_color()) {
  72. Painter painter(*this);
  73. painter.fill_rect(event.rect(), background_color());
  74. } else {
  75. #ifdef DEBUG_WIDGET_UNDERDRAW
  76. // FIXME: This is a bit broken.
  77. // If the widget is not opaque, let's not mess it up with debugging color.
  78. Painter painter(*this);
  79. painter.fill_rect(rect(), Color::Red);
  80. #endif
  81. }
  82. paint_event(event);
  83. for (auto* ch : children()) {
  84. auto* child = (GWidget*)ch;
  85. if (child->relative_rect().intersects(event.rect())) {
  86. auto local_rect = event.rect();
  87. local_rect.intersect(child->relative_rect());
  88. local_rect.move_by(-child->relative_rect().x(), -child->relative_rect().y());
  89. GPaintEvent local_event(local_rect);
  90. child->event(local_event);
  91. }
  92. }
  93. }
  94. void GWidget::set_layout(OwnPtr<GLayout>&& layout)
  95. {
  96. if (m_layout.ptr() == layout.ptr())
  97. return;
  98. if (m_layout)
  99. m_layout->notify_disowned(Badge<GWidget>(), *this);
  100. m_layout = move(layout);
  101. if (m_layout) {
  102. m_layout->notify_adopted(Badge<GWidget>(), *this);
  103. do_layout();
  104. } else {
  105. update();
  106. }
  107. }
  108. void GWidget::do_layout()
  109. {
  110. if (!m_layout)
  111. return;
  112. m_layout->run(*this);
  113. update();
  114. }
  115. void GWidget::notify_layout_changed(Badge<GLayout>)
  116. {
  117. do_layout();
  118. }
  119. void GWidget::handle_resize_event(GResizeEvent& event)
  120. {
  121. if (layout())
  122. do_layout();
  123. return resize_event(event);
  124. }
  125. void GWidget::resize_event(GResizeEvent&)
  126. {
  127. }
  128. void GWidget::paint_event(GPaintEvent&)
  129. {
  130. }
  131. void GWidget::show_event(GShowEvent&)
  132. {
  133. }
  134. void GWidget::hide_event(GHideEvent&)
  135. {
  136. }
  137. void GWidget::keydown_event(GKeyEvent&)
  138. {
  139. }
  140. void GWidget::keyup_event(GKeyEvent&)
  141. {
  142. }
  143. void GWidget::mousedown_event(GMouseEvent&)
  144. {
  145. }
  146. void GWidget::mouseup_event(GMouseEvent&)
  147. {
  148. }
  149. void GWidget::mousemove_event(GMouseEvent&)
  150. {
  151. }
  152. void GWidget::focusin_event(GEvent&)
  153. {
  154. }
  155. void GWidget::focusout_event(GEvent&)
  156. {
  157. }
  158. void GWidget::enter_event(GEvent&)
  159. {
  160. }
  161. void GWidget::leave_event(GEvent&)
  162. {
  163. }
  164. void GWidget::update()
  165. {
  166. update(rect());
  167. }
  168. void GWidget::update(const Rect& rect)
  169. {
  170. auto* w = window();
  171. if (!w)
  172. return;
  173. w->update(rect.translated(window_relative_rect().location()));
  174. }
  175. Rect GWidget::window_relative_rect() const
  176. {
  177. auto rect = relative_rect();
  178. for (auto* parent = parent_widget(); parent; parent = parent->parent_widget()) {
  179. rect.move_by(parent->relative_position());
  180. }
  181. return rect;
  182. }
  183. GWidget::HitTestResult GWidget::hit_test(int x, int y)
  184. {
  185. // FIXME: Care about z-order.
  186. for (auto* ch : children()) {
  187. auto* child = (GWidget*)ch;
  188. if (child->relative_rect().contains(x, y)) {
  189. return child->hit_test(x - child->relative_rect().x(), y - child->relative_rect().y());
  190. }
  191. }
  192. return { this, x, y };
  193. }
  194. void GWidget::set_window(GWindow* window)
  195. {
  196. if (m_window == window)
  197. return;
  198. m_window = window;
  199. }
  200. bool GWidget::is_focused() const
  201. {
  202. auto* win = window();
  203. if (!win)
  204. return false;
  205. if (!win->is_active())
  206. return false;
  207. return win->focused_widget() == this;
  208. }
  209. void GWidget::set_focus(bool focus)
  210. {
  211. auto* win = window();
  212. if (!win)
  213. return;
  214. if (focus) {
  215. win->set_focused_widget(this);
  216. } else {
  217. if (win->focused_widget() == this)
  218. win->set_focused_widget(nullptr);
  219. }
  220. }
  221. void GWidget::set_font(RetainPtr<Font>&& font)
  222. {
  223. if (!font)
  224. m_font = Font::default_font();
  225. else
  226. m_font = move(font);
  227. update();
  228. }
  229. void GWidget::set_global_cursor_tracking(bool enabled)
  230. {
  231. auto* win = window();
  232. if (!win)
  233. return;
  234. win->set_global_cursor_tracking_widget(enabled ? this : nullptr);
  235. }
  236. bool GWidget::global_cursor_tracking() const
  237. {
  238. auto* win = window();
  239. if (!win)
  240. return false;
  241. return win->global_cursor_tracking_widget() == this;
  242. }
  243. void GWidget::set_preferred_size(const Size& size)
  244. {
  245. if (m_preferred_size == size)
  246. return;
  247. m_preferred_size = size;
  248. invalidate_layout();
  249. }
  250. void GWidget::set_size_policy(SizePolicy horizontal_policy, SizePolicy vertical_policy)
  251. {
  252. if (m_horizontal_size_policy == horizontal_policy && m_vertical_size_policy == vertical_policy)
  253. return;
  254. m_horizontal_size_policy = horizontal_policy;
  255. m_vertical_size_policy = vertical_policy;
  256. invalidate_layout();
  257. }
  258. void GWidget::invalidate_layout()
  259. {
  260. auto* w = window();
  261. if (!w)
  262. return;
  263. if (!w->main_widget())
  264. return;
  265. w->main_widget()->do_layout();
  266. }