GWidget.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. #include "GWidget.h"
  2. #include "GEvent.h"
  3. #include "GEventLoop.h"
  4. #include "GWindow.h"
  5. #include <AK/Assertions.h>
  6. #include <LibGUI/GAction.h>
  7. #include <LibGUI/GApplication.h>
  8. #include <LibGUI/GLayout.h>
  9. #include <LibGUI/GMenu.h>
  10. #include <LibGUI/GPainter.h>
  11. #include <LibDraw/GraphicsBitmap.h>
  12. #include <unistd.h>
  13. GWidget::GWidget(GWidget* parent)
  14. : CObject(parent, true)
  15. {
  16. set_font(nullptr);
  17. m_background_color = Color::WarmGray;
  18. m_foreground_color = Color::Black;
  19. }
  20. GWidget::~GWidget()
  21. {
  22. }
  23. void GWidget::child_event(CChildEvent& event)
  24. {
  25. if (event.type() == GEvent::ChildAdded) {
  26. if (event.child() && is<GWidget>(*event.child()) && layout())
  27. layout()->add_widget(to<GWidget>(*event.child()));
  28. }
  29. if (event.type() == GEvent::ChildRemoved) {
  30. if (layout()) {
  31. if (event.child() && is<GWidget>(*event.child()))
  32. layout()->remove_widget(to<GWidget>(*event.child()));
  33. else
  34. invalidate_layout();
  35. }
  36. update();
  37. }
  38. return CObject::child_event(event);
  39. }
  40. void GWidget::set_relative_rect(const Rect& rect)
  41. {
  42. if (rect == m_relative_rect)
  43. return;
  44. auto old_rect = m_relative_rect;
  45. bool size_changed = m_relative_rect.size() != rect.size();
  46. m_relative_rect = rect;
  47. if (size_changed) {
  48. GResizeEvent resize_event(m_relative_rect.size(), rect.size());
  49. event(resize_event);
  50. }
  51. if (auto* parent = parent_widget())
  52. parent->update(old_rect);
  53. update();
  54. }
  55. void GWidget::event(CEvent& event)
  56. {
  57. switch (event.type()) {
  58. case GEvent::Paint:
  59. return handle_paint_event(static_cast<GPaintEvent&>(event));
  60. case GEvent::Resize:
  61. return handle_resize_event(static_cast<GResizeEvent&>(event));
  62. case GEvent::FocusIn:
  63. return focusin_event(event);
  64. case GEvent::FocusOut:
  65. return focusout_event(event);
  66. case GEvent::Show:
  67. return show_event(static_cast<GShowEvent&>(event));
  68. case GEvent::Hide:
  69. return hide_event(static_cast<GHideEvent&>(event));
  70. case GEvent::KeyDown:
  71. return keydown_event(static_cast<GKeyEvent&>(event));
  72. case GEvent::KeyUp:
  73. return keyup_event(static_cast<GKeyEvent&>(event));
  74. case GEvent::MouseMove:
  75. return mousemove_event(static_cast<GMouseEvent&>(event));
  76. case GEvent::MouseDown:
  77. return handle_mousedown_event(static_cast<GMouseEvent&>(event));
  78. case GEvent::MouseDoubleClick:
  79. return handle_mousedoubleclick_event(static_cast<GMouseEvent&>(event));
  80. case GEvent::MouseUp:
  81. return handle_mouseup_event(static_cast<GMouseEvent&>(event));
  82. case GEvent::MouseWheel:
  83. return mousewheel_event(static_cast<GMouseEvent&>(event));
  84. case GEvent::Enter:
  85. return handle_enter_event(event);
  86. case GEvent::Leave:
  87. return handle_leave_event(event);
  88. case GEvent::EnabledChange:
  89. return change_event(static_cast<GEvent&>(event));
  90. default:
  91. return CObject::event(event);
  92. }
  93. }
  94. void GWidget::handle_paint_event(GPaintEvent& event)
  95. {
  96. ASSERT(is_visible());
  97. if (fill_with_background_color()) {
  98. GPainter painter(*this);
  99. painter.fill_rect(event.rect(), background_color());
  100. } else {
  101. #ifdef DEBUG_WIDGET_UNDERDRAW
  102. // FIXME: This is a bit broken.
  103. // If the widget is not opaque, let's not mess it up with debugging color.
  104. GPainter painter(*this);
  105. painter.fill_rect(rect(), Color::Red);
  106. #endif
  107. }
  108. paint_event(event);
  109. for_each_child_widget([&](auto& child) {
  110. if (!child.is_visible())
  111. return IterationDecision::Continue;
  112. if (child.relative_rect().intersects(event.rect())) {
  113. GPaintEvent local_event(event.rect().intersected(child.relative_rect()).translated(-child.relative_position()));
  114. child.event(local_event);
  115. }
  116. return IterationDecision::Continue;
  117. });
  118. second_paint_event(event);
  119. }
  120. void GWidget::set_layout(OwnPtr<GLayout>&& layout)
  121. {
  122. if (m_layout)
  123. m_layout->notify_disowned({}, *this);
  124. m_layout = move(layout);
  125. if (m_layout) {
  126. m_layout->notify_adopted({}, *this);
  127. do_layout();
  128. } else {
  129. update();
  130. }
  131. }
  132. void GWidget::do_layout()
  133. {
  134. if (!m_layout)
  135. return;
  136. m_layout->run(*this);
  137. update();
  138. }
  139. void GWidget::notify_layout_changed(Badge<GLayout>)
  140. {
  141. invalidate_layout();
  142. }
  143. void GWidget::handle_resize_event(GResizeEvent& event)
  144. {
  145. if (layout())
  146. do_layout();
  147. return resize_event(event);
  148. }
  149. void GWidget::handle_mouseup_event(GMouseEvent& event)
  150. {
  151. mouseup_event(event);
  152. }
  153. void GWidget::handle_mousedown_event(GMouseEvent& event)
  154. {
  155. if (accepts_focus())
  156. set_focus(true);
  157. mousedown_event(event);
  158. if (event.button() == GMouseButton::Right) {
  159. GContextMenuEvent c_event(event.position(), screen_relative_rect().location().translated(event.position()));
  160. context_menu_event(c_event);
  161. }
  162. }
  163. void GWidget::handle_mousedoubleclick_event(GMouseEvent& event)
  164. {
  165. doubleclick_event(event);
  166. }
  167. void GWidget::handle_enter_event(CEvent& event)
  168. {
  169. if (has_tooltip())
  170. GApplication::the().show_tooltip(m_tooltip, screen_relative_rect().center().translated(0, height() / 2));
  171. enter_event(event);
  172. }
  173. void GWidget::handle_leave_event(CEvent& event)
  174. {
  175. GApplication::the().hide_tooltip();
  176. leave_event(event);
  177. }
  178. void GWidget::click_event(GMouseEvent&)
  179. {
  180. }
  181. void GWidget::doubleclick_event(GMouseEvent&)
  182. {
  183. }
  184. void GWidget::resize_event(GResizeEvent&)
  185. {
  186. }
  187. void GWidget::paint_event(GPaintEvent&)
  188. {
  189. }
  190. void GWidget::second_paint_event(GPaintEvent&)
  191. {
  192. }
  193. void GWidget::show_event(GShowEvent&)
  194. {
  195. }
  196. void GWidget::hide_event(GHideEvent&)
  197. {
  198. }
  199. void GWidget::keydown_event(GKeyEvent& event)
  200. {
  201. if (!event.alt() && !event.ctrl() && !event.logo() && event.key() == KeyCode::Key_Tab) {
  202. if (event.shift())
  203. focus_previous_widget();
  204. else
  205. focus_next_widget();
  206. }
  207. }
  208. void GWidget::keyup_event(GKeyEvent&)
  209. {
  210. }
  211. void GWidget::mousedown_event(GMouseEvent&)
  212. {
  213. }
  214. void GWidget::mouseup_event(GMouseEvent&)
  215. {
  216. }
  217. void GWidget::mousemove_event(GMouseEvent&)
  218. {
  219. }
  220. void GWidget::mousewheel_event(GMouseEvent&)
  221. {
  222. }
  223. void GWidget::context_menu_event(GContextMenuEvent&)
  224. {
  225. }
  226. void GWidget::focusin_event(CEvent&)
  227. {
  228. }
  229. void GWidget::focusout_event(CEvent&)
  230. {
  231. }
  232. void GWidget::enter_event(CEvent&)
  233. {
  234. }
  235. void GWidget::leave_event(CEvent&)
  236. {
  237. }
  238. void GWidget::change_event(GEvent&)
  239. {
  240. }
  241. void GWidget::update()
  242. {
  243. if (rect().is_empty())
  244. return;
  245. update(rect());
  246. }
  247. void GWidget::update(const Rect& rect)
  248. {
  249. if (!is_visible())
  250. return;
  251. if (!updates_enabled())
  252. return;
  253. GWindow* window = m_window;
  254. GWidget* parent = parent_widget();
  255. while (parent) {
  256. if (!parent->updates_enabled())
  257. return;
  258. window = parent->m_window;
  259. parent = parent->parent_widget();
  260. }
  261. if (window)
  262. window->update(rect.translated(window_relative_rect().location()));
  263. }
  264. Rect GWidget::window_relative_rect() const
  265. {
  266. auto rect = relative_rect();
  267. for (auto* parent = parent_widget(); parent; parent = parent->parent_widget()) {
  268. rect.move_by(parent->relative_position());
  269. }
  270. return rect;
  271. }
  272. Rect GWidget::screen_relative_rect() const
  273. {
  274. return window_relative_rect().translated(window()->position());
  275. }
  276. GWidget* GWidget::child_at(const Point& point) const
  277. {
  278. for (int i = children().size() - 1; i >= 0; --i) {
  279. if (!is<GWidget>(*children()[i]))
  280. continue;
  281. auto& child = to<GWidget>(*children()[i]);
  282. if (!child.is_visible())
  283. continue;
  284. if (child.relative_rect().contains(point))
  285. return &child;
  286. }
  287. return nullptr;
  288. }
  289. GWidget::HitTestResult GWidget::hit_test(const Point& position)
  290. {
  291. if (is_greedy_for_hits())
  292. return { this, position };
  293. if (auto* child = child_at(position))
  294. return child->hit_test(position - child->relative_position());
  295. return { this, position };
  296. }
  297. void GWidget::set_window(GWindow* window)
  298. {
  299. if (m_window == window)
  300. return;
  301. m_window = window;
  302. }
  303. bool GWidget::is_focused() const
  304. {
  305. auto* win = window();
  306. if (!win)
  307. return false;
  308. if (!win->is_active())
  309. return false;
  310. return win->focused_widget() == this;
  311. }
  312. void GWidget::set_focus(bool focus)
  313. {
  314. auto* win = window();
  315. if (!win)
  316. return;
  317. if (focus) {
  318. win->set_focused_widget(this);
  319. } else {
  320. if (win->focused_widget() == this)
  321. win->set_focused_widget(nullptr);
  322. }
  323. }
  324. void GWidget::set_font(Font* font)
  325. {
  326. if (!font)
  327. m_font = Font::default_font();
  328. else
  329. m_font = font;
  330. update();
  331. }
  332. void GWidget::set_global_cursor_tracking(bool enabled)
  333. {
  334. auto* win = window();
  335. if (!win)
  336. return;
  337. win->set_global_cursor_tracking_widget(enabled ? this : nullptr);
  338. }
  339. bool GWidget::global_cursor_tracking() const
  340. {
  341. auto* win = window();
  342. if (!win)
  343. return false;
  344. return win->global_cursor_tracking_widget() == this;
  345. }
  346. void GWidget::set_preferred_size(const Size& size)
  347. {
  348. if (m_preferred_size == size)
  349. return;
  350. m_preferred_size = size;
  351. invalidate_layout();
  352. }
  353. void GWidget::set_size_policy(SizePolicy horizontal_policy, SizePolicy vertical_policy)
  354. {
  355. if (m_horizontal_size_policy == horizontal_policy && m_vertical_size_policy == vertical_policy)
  356. return;
  357. m_horizontal_size_policy = horizontal_policy;
  358. m_vertical_size_policy = vertical_policy;
  359. invalidate_layout();
  360. }
  361. void GWidget::invalidate_layout()
  362. {
  363. if (m_layout_dirty)
  364. return;
  365. m_layout_dirty = true;
  366. deferred_invoke([this](auto&) {
  367. m_layout_dirty = false;
  368. auto* w = window();
  369. if (!w)
  370. return;
  371. if (!w->main_widget())
  372. return;
  373. do_layout();
  374. w->main_widget()->do_layout();
  375. });
  376. }
  377. void GWidget::set_visible(bool visible)
  378. {
  379. if (visible == m_visible)
  380. return;
  381. m_visible = visible;
  382. if (auto* parent = parent_widget())
  383. parent->invalidate_layout();
  384. if (m_visible)
  385. update();
  386. }
  387. bool GWidget::spans_entire_window_horizontally() const
  388. {
  389. auto* w = window();
  390. if (!w)
  391. return false;
  392. auto* main_widget = w->main_widget();
  393. if (!main_widget)
  394. return false;
  395. if (main_widget == this)
  396. return true;
  397. auto wrr = window_relative_rect();
  398. return wrr.left() == main_widget->rect().left() && wrr.right() == main_widget->rect().right();
  399. }
  400. void GWidget::set_enabled(bool enabled)
  401. {
  402. if (m_enabled == enabled)
  403. return;
  404. m_enabled = enabled;
  405. GEvent e(GEvent::EnabledChange);
  406. event(e);
  407. update();
  408. }
  409. void GWidget::move_to_front()
  410. {
  411. auto* parent = parent_widget();
  412. if (!parent)
  413. return;
  414. if (parent->children().size() == 1)
  415. return;
  416. parent->children().remove_first_matching([this](auto& entry) {
  417. return entry == this;
  418. });
  419. parent->children().append(this);
  420. parent->update();
  421. }
  422. void GWidget::move_to_back()
  423. {
  424. auto* parent = parent_widget();
  425. if (!parent)
  426. return;
  427. if (parent->children().size() == 1)
  428. return;
  429. parent->children().remove_first_matching([this](auto& entry) {
  430. return entry == this;
  431. });
  432. parent->children().prepend(this);
  433. parent->update();
  434. }
  435. bool GWidget::is_frontmost() const
  436. {
  437. auto* parent = parent_widget();
  438. if (!parent)
  439. return true;
  440. return parent->children().last() == this;
  441. }
  442. bool GWidget::is_backmost() const
  443. {
  444. auto* parent = parent_widget();
  445. if (!parent)
  446. return true;
  447. return parent->children().first() == this;
  448. }
  449. GAction* GWidget::action_for_key_event(const GKeyEvent& event)
  450. {
  451. auto it = m_local_shortcut_actions.find(GShortcut(event.modifiers(), (KeyCode)event.key()));
  452. if (it == m_local_shortcut_actions.end())
  453. return nullptr;
  454. return (*it).value;
  455. }
  456. void GWidget::register_local_shortcut_action(Badge<GAction>, GAction& action)
  457. {
  458. m_local_shortcut_actions.set(action.shortcut(), &action);
  459. }
  460. void GWidget::unregister_local_shortcut_action(Badge<GAction>, GAction& action)
  461. {
  462. m_local_shortcut_actions.remove(action.shortcut());
  463. }
  464. void GWidget::set_updates_enabled(bool enabled)
  465. {
  466. if (m_updates_enabled == enabled)
  467. return;
  468. m_updates_enabled = enabled;
  469. if (enabled)
  470. update();
  471. }
  472. void GWidget::focus_previous_widget()
  473. {
  474. auto focusable_widgets = window()->focusable_widgets();
  475. for (int i = focusable_widgets.size() - 1; i >= 0; --i) {
  476. if (focusable_widgets[i] != this)
  477. continue;
  478. if (i > 0)
  479. focusable_widgets[i - 1]->set_focus(true);
  480. else
  481. focusable_widgets.last()->set_focus(true);
  482. }
  483. }
  484. void GWidget::focus_next_widget()
  485. {
  486. auto focusable_widgets = window()->focusable_widgets();
  487. for (int i = 0; i < focusable_widgets.size(); ++i) {
  488. if (focusable_widgets[i] != this)
  489. continue;
  490. if (i < focusable_widgets.size() - 1)
  491. focusable_widgets[i + 1]->set_focus(true);
  492. else
  493. focusable_widgets.first()->set_focus(true);
  494. }
  495. }