GWidget.cpp 14 KB

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