GWidget.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 <LibGUI/GPainter.h>
  9. #include <LibGUI/GApplication.h>
  10. #include <unistd.h>
  11. GWidget::GWidget(GWidget* parent)
  12. : CObject(parent)
  13. {
  14. set_font(nullptr);
  15. m_background_color = Color::LightGray;
  16. m_foreground_color = Color::Black;
  17. }
  18. GWidget::~GWidget()
  19. {
  20. }
  21. void GWidget::child_event(CChildEvent& event)
  22. {
  23. if (event.type() == GEvent::ChildAdded) {
  24. if (event.child() && event.child()->is_widget() && layout())
  25. layout()->add_widget(static_cast<GWidget&>(*event.child()));
  26. }
  27. if (event.type() == GEvent::ChildRemoved) {
  28. if (layout()) {
  29. if (event.child() && event.child()->is_widget())
  30. layout()->remove_widget(static_cast<GWidget&>(*event.child()));
  31. else
  32. invalidate_layout();
  33. }
  34. }
  35. return CObject::child_event(event);
  36. }
  37. void GWidget::set_relative_rect(const Rect& rect)
  38. {
  39. if (rect == m_relative_rect)
  40. return;
  41. bool size_changed = m_relative_rect.size() != rect.size();
  42. m_relative_rect = rect;
  43. if (size_changed) {
  44. GResizeEvent resize_event(m_relative_rect.size(), rect.size());
  45. event(resize_event);
  46. }
  47. update();
  48. }
  49. void GWidget::event(CEvent& event)
  50. {
  51. switch (event.type()) {
  52. case GEvent::Paint:
  53. return handle_paint_event(static_cast<GPaintEvent&>(event));
  54. case GEvent::Resize:
  55. return handle_resize_event(static_cast<GResizeEvent&>(event));
  56. case GEvent::FocusIn:
  57. return focusin_event(event);
  58. case GEvent::FocusOut:
  59. return focusout_event(event);
  60. case GEvent::Show:
  61. return show_event(static_cast<GShowEvent&>(event));
  62. case GEvent::Hide:
  63. return hide_event(static_cast<GHideEvent&>(event));
  64. case GEvent::KeyDown:
  65. return keydown_event(static_cast<GKeyEvent&>(event));
  66. case GEvent::KeyUp:
  67. return keyup_event(static_cast<GKeyEvent&>(event));
  68. case GEvent::MouseMove:
  69. return mousemove_event(static_cast<GMouseEvent&>(event));
  70. case GEvent::MouseDown:
  71. return handle_mousedown_event(static_cast<GMouseEvent&>(event));
  72. case GEvent::MouseUp:
  73. return handle_mouseup_event(static_cast<GMouseEvent&>(event));
  74. case GEvent::Enter:
  75. return handle_enter_event(event);
  76. case GEvent::Leave:
  77. return handle_leave_event(event);
  78. default:
  79. return CObject::event(event);
  80. }
  81. }
  82. void GWidget::handle_paint_event(GPaintEvent& event)
  83. {
  84. ASSERT(is_visible());
  85. if (fill_with_background_color()) {
  86. GPainter painter(*this);
  87. painter.fill_rect(event.rect(), background_color());
  88. } else {
  89. #ifdef DEBUG_WIDGET_UNDERDRAW
  90. // FIXME: This is a bit broken.
  91. // If the widget is not opaque, let's not mess it up with debugging color.
  92. GPainter painter(*this);
  93. painter.fill_rect(rect(), Color::Red);
  94. #endif
  95. }
  96. paint_event(event);
  97. for (auto* ch : children()) {
  98. auto* child = (GWidget*)ch;
  99. if (!child->is_visible())
  100. continue;
  101. if (child->relative_rect().intersects(event.rect())) {
  102. auto local_rect = event.rect();
  103. local_rect.intersect(child->relative_rect());
  104. local_rect.move_by(-child->relative_rect().x(), -child->relative_rect().y());
  105. GPaintEvent local_event(local_rect);
  106. child->event(local_event);
  107. }
  108. }
  109. second_paint_event(event);
  110. }
  111. void GWidget::set_layout(OwnPtr<GLayout>&& layout)
  112. {
  113. if (m_layout.ptr() == layout.ptr())
  114. return;
  115. if (m_layout)
  116. m_layout->notify_disowned(Badge<GWidget>(), *this);
  117. m_layout = move(layout);
  118. if (m_layout) {
  119. m_layout->notify_adopted(Badge<GWidget>(), *this);
  120. do_layout();
  121. } else {
  122. update();
  123. }
  124. }
  125. void GWidget::do_layout()
  126. {
  127. if (!m_layout)
  128. return;
  129. m_layout->run(*this);
  130. update();
  131. }
  132. void GWidget::notify_layout_changed(Badge<GLayout>)
  133. {
  134. do_layout();
  135. }
  136. void GWidget::handle_resize_event(GResizeEvent& event)
  137. {
  138. if (layout())
  139. do_layout();
  140. return resize_event(event);
  141. }
  142. void GWidget::handle_mouseup_event(GMouseEvent& event)
  143. {
  144. mouseup_event(event);
  145. if (!rect().contains(event.position()))
  146. return;
  147. // It's a click.. but is it a doubleclick?
  148. // FIXME: This needs improvement.
  149. int elapsed_since_last_click = m_click_clock.elapsed();
  150. dbgprintf("Click clock elapsed: %d\n", m_click_clock.elapsed());
  151. if (elapsed_since_last_click < 250) {
  152. doubleclick_event(event);
  153. } else {
  154. m_click_clock.start();
  155. }
  156. }
  157. void GWidget::handle_mousedown_event(GMouseEvent& event)
  158. {
  159. if (accepts_focus())
  160. set_focus(true);
  161. if (!m_click_clock.is_valid())
  162. m_click_clock.start();
  163. mousedown_event(event);
  164. }
  165. void GWidget::handle_enter_event(CEvent& event)
  166. {
  167. if (has_tooltip())
  168. GApplication::the().show_tooltip(m_tooltip, screen_relative_rect().center().translated(0, height() / 2));
  169. enter_event(event);
  170. }
  171. void GWidget::handle_leave_event(CEvent& event)
  172. {
  173. GApplication::the().hide_tooltip();
  174. leave_event(event);
  175. }
  176. void GWidget::click_event(GMouseEvent&)
  177. {
  178. }
  179. void GWidget::doubleclick_event(GMouseEvent&)
  180. {
  181. }
  182. void GWidget::resize_event(GResizeEvent&)
  183. {
  184. }
  185. void GWidget::paint_event(GPaintEvent&)
  186. {
  187. }
  188. void GWidget::second_paint_event(GPaintEvent&)
  189. {
  190. }
  191. void GWidget::show_event(GShowEvent&)
  192. {
  193. }
  194. void GWidget::hide_event(GHideEvent&)
  195. {
  196. }
  197. void GWidget::keydown_event(GKeyEvent&)
  198. {
  199. }
  200. void GWidget::keyup_event(GKeyEvent&)
  201. {
  202. }
  203. void GWidget::mousedown_event(GMouseEvent&)
  204. {
  205. }
  206. void GWidget::mouseup_event(GMouseEvent&)
  207. {
  208. }
  209. void GWidget::mousemove_event(GMouseEvent&)
  210. {
  211. }
  212. void GWidget::focusin_event(CEvent&)
  213. {
  214. }
  215. void GWidget::focusout_event(CEvent&)
  216. {
  217. }
  218. void GWidget::enter_event(CEvent&)
  219. {
  220. }
  221. void GWidget::leave_event(CEvent&)
  222. {
  223. }
  224. void GWidget::update()
  225. {
  226. update(rect());
  227. }
  228. void GWidget::update(const Rect& rect)
  229. {
  230. if (!is_visible())
  231. return;
  232. auto* w = window();
  233. if (!w)
  234. return;
  235. w->update(rect.translated(window_relative_rect().location()));
  236. }
  237. Rect GWidget::window_relative_rect() const
  238. {
  239. auto rect = relative_rect();
  240. for (auto* parent = parent_widget(); parent; parent = parent->parent_widget()) {
  241. rect.move_by(parent->relative_position());
  242. }
  243. return rect;
  244. }
  245. Rect GWidget::screen_relative_rect() const
  246. {
  247. return window_relative_rect().translated(window()->position());
  248. }
  249. GWidget::HitTestResult GWidget::hit_test(int x, int y)
  250. {
  251. if (is_greedy_for_hits())
  252. return { this, x, y };
  253. for (int i = children().size() - 1; i >= 0; --i) {
  254. if (!children()[i]->is_widget())
  255. continue;
  256. auto& child = *(GWidget*)children()[i];
  257. if (!child.is_visible())
  258. continue;
  259. if (child.relative_rect().contains(x, y))
  260. return child.hit_test(x - child.relative_rect().x(), y - child.relative_rect().y());
  261. }
  262. return { this, x, y };
  263. }
  264. void GWidget::set_window(GWindow* window)
  265. {
  266. if (m_window == window)
  267. return;
  268. m_window = window;
  269. }
  270. bool GWidget::is_focused() const
  271. {
  272. auto* win = window();
  273. if (!win)
  274. return false;
  275. if (!win->is_active())
  276. return false;
  277. return win->focused_widget() == this;
  278. }
  279. void GWidget::set_focus(bool focus)
  280. {
  281. auto* win = window();
  282. if (!win)
  283. return;
  284. if (focus) {
  285. win->set_focused_widget(this);
  286. } else {
  287. if (win->focused_widget() == this)
  288. win->set_focused_widget(nullptr);
  289. }
  290. }
  291. void GWidget::set_font(RetainPtr<Font>&& font)
  292. {
  293. if (!font)
  294. m_font = Font::default_font();
  295. else
  296. m_font = move(font);
  297. update();
  298. }
  299. void GWidget::set_global_cursor_tracking(bool enabled)
  300. {
  301. auto* win = window();
  302. if (!win)
  303. return;
  304. win->set_global_cursor_tracking_widget(enabled ? this : nullptr);
  305. }
  306. bool GWidget::global_cursor_tracking() const
  307. {
  308. auto* win = window();
  309. if (!win)
  310. return false;
  311. return win->global_cursor_tracking_widget() == this;
  312. }
  313. void GWidget::set_preferred_size(const Size& size)
  314. {
  315. if (m_preferred_size == size)
  316. return;
  317. m_preferred_size = size;
  318. invalidate_layout();
  319. }
  320. void GWidget::set_size_policy(SizePolicy horizontal_policy, SizePolicy vertical_policy)
  321. {
  322. if (m_horizontal_size_policy == horizontal_policy && m_vertical_size_policy == vertical_policy)
  323. return;
  324. m_horizontal_size_policy = horizontal_policy;
  325. m_vertical_size_policy = vertical_policy;
  326. invalidate_layout();
  327. }
  328. void GWidget::invalidate_layout()
  329. {
  330. auto* w = window();
  331. if (!w)
  332. return;
  333. if (!w->main_widget())
  334. return;
  335. do_layout();
  336. w->main_widget()->do_layout();
  337. }
  338. void GWidget::set_visible(bool visible)
  339. {
  340. if (visible == m_visible)
  341. return;
  342. m_visible = visible;
  343. if (auto* parent = parent_widget())
  344. parent->invalidate_layout();
  345. if (m_visible)
  346. update();
  347. }
  348. bool GWidget::spans_entire_window_horizontally() const
  349. {
  350. auto* w = window();
  351. if (!w)
  352. return false;
  353. auto* main_widget = w->main_widget();
  354. if (!main_widget)
  355. return false;
  356. if (main_widget == this)
  357. return true;
  358. auto wrr = window_relative_rect();
  359. return wrr.left() == main_widget->rect().left() && wrr.right() == main_widget->rect().right();
  360. }
  361. void GWidget::set_enabled(bool enabled)
  362. {
  363. if (m_enabled == enabled)
  364. return;
  365. m_enabled = enabled;
  366. update();
  367. }