GWidget.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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.dispatch_event(local_event, this);
  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. custom_layout();
  136. if (!m_layout)
  137. return;
  138. m_layout->run(*this);
  139. update();
  140. }
  141. void GWidget::notify_layout_changed(Badge<GLayout>)
  142. {
  143. invalidate_layout();
  144. }
  145. void GWidget::handle_resize_event(GResizeEvent& event)
  146. {
  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. event.accept();
  208. return;
  209. }
  210. event.ignore();
  211. }
  212. void GWidget::keyup_event(GKeyEvent&)
  213. {
  214. }
  215. void GWidget::mousedown_event(GMouseEvent&)
  216. {
  217. }
  218. void GWidget::mouseup_event(GMouseEvent&)
  219. {
  220. }
  221. void GWidget::mousemove_event(GMouseEvent&)
  222. {
  223. }
  224. void GWidget::mousewheel_event(GMouseEvent&)
  225. {
  226. }
  227. void GWidget::context_menu_event(GContextMenuEvent&)
  228. {
  229. }
  230. void GWidget::focusin_event(CEvent&)
  231. {
  232. }
  233. void GWidget::focusout_event(CEvent&)
  234. {
  235. }
  236. void GWidget::enter_event(CEvent&)
  237. {
  238. }
  239. void GWidget::leave_event(CEvent&)
  240. {
  241. }
  242. void GWidget::change_event(GEvent&)
  243. {
  244. }
  245. void GWidget::update()
  246. {
  247. if (rect().is_empty())
  248. return;
  249. update(rect());
  250. }
  251. void GWidget::update(const Rect& rect)
  252. {
  253. if (!is_visible())
  254. return;
  255. if (!updates_enabled())
  256. return;
  257. GWindow* window = m_window;
  258. GWidget* parent = parent_widget();
  259. while (parent) {
  260. if (!parent->updates_enabled())
  261. return;
  262. window = parent->m_window;
  263. parent = parent->parent_widget();
  264. }
  265. if (window)
  266. window->update(rect.translated(window_relative_rect().location()));
  267. }
  268. Rect GWidget::window_relative_rect() const
  269. {
  270. auto rect = relative_rect();
  271. for (auto* parent = parent_widget(); parent; parent = parent->parent_widget()) {
  272. rect.move_by(parent->relative_position());
  273. }
  274. return rect;
  275. }
  276. Rect GWidget::screen_relative_rect() const
  277. {
  278. return window_relative_rect().translated(window()->position());
  279. }
  280. GWidget* GWidget::child_at(const Point& point) const
  281. {
  282. for (int i = children().size() - 1; i >= 0; --i) {
  283. if (!is<GWidget>(*children()[i]))
  284. continue;
  285. auto& child = to<GWidget>(*children()[i]);
  286. if (!child.is_visible())
  287. continue;
  288. if (child.relative_rect().contains(point))
  289. return &child;
  290. }
  291. return nullptr;
  292. }
  293. GWidget::HitTestResult GWidget::hit_test(const Point& position, ShouldRespectGreediness should_respect_greediness)
  294. {
  295. if (should_respect_greediness == ShouldRespectGreediness::Yes && is_greedy_for_hits())
  296. return { this, position };
  297. if (auto* child = child_at(position))
  298. return child->hit_test(position - child->relative_position());
  299. return { this, position };
  300. }
  301. void GWidget::set_window(GWindow* window)
  302. {
  303. if (m_window == window)
  304. return;
  305. m_window = window;
  306. }
  307. bool GWidget::is_focused() const
  308. {
  309. auto* win = window();
  310. if (!win)
  311. return false;
  312. if (!win->is_active())
  313. return false;
  314. return win->focused_widget() == this;
  315. }
  316. void GWidget::set_focus(bool focus)
  317. {
  318. auto* win = window();
  319. if (!win)
  320. return;
  321. if (focus) {
  322. win->set_focused_widget(this);
  323. } else {
  324. if (win->focused_widget() == this)
  325. win->set_focused_widget(nullptr);
  326. }
  327. }
  328. void GWidget::set_font(const Font* font)
  329. {
  330. if (m_font.ptr() == font)
  331. return;
  332. if (!font)
  333. m_font = Font::default_font();
  334. else
  335. m_font = *font;
  336. did_change_font();
  337. update();
  338. }
  339. void GWidget::set_global_cursor_tracking(bool enabled)
  340. {
  341. auto* win = window();
  342. if (!win)
  343. return;
  344. win->set_global_cursor_tracking_widget(enabled ? this : nullptr);
  345. }
  346. bool GWidget::global_cursor_tracking() const
  347. {
  348. auto* win = window();
  349. if (!win)
  350. return false;
  351. return win->global_cursor_tracking_widget() == this;
  352. }
  353. void GWidget::set_preferred_size(const Size& size)
  354. {
  355. if (m_preferred_size == size)
  356. return;
  357. m_preferred_size = size;
  358. invalidate_layout();
  359. }
  360. void GWidget::set_size_policy(Orientation orientation, SizePolicy policy)
  361. {
  362. if (orientation == Orientation::Horizontal)
  363. set_size_policy(policy, m_vertical_size_policy);
  364. else
  365. set_size_policy(m_horizontal_size_policy, policy);
  366. }
  367. void GWidget::set_size_policy(SizePolicy horizontal_policy, SizePolicy vertical_policy)
  368. {
  369. if (m_horizontal_size_policy == horizontal_policy && m_vertical_size_policy == vertical_policy)
  370. return;
  371. m_horizontal_size_policy = horizontal_policy;
  372. m_vertical_size_policy = vertical_policy;
  373. invalidate_layout();
  374. }
  375. void GWidget::invalidate_layout()
  376. {
  377. if (m_layout_dirty)
  378. return;
  379. m_layout_dirty = true;
  380. deferred_invoke([this](auto&) {
  381. m_layout_dirty = false;
  382. auto* w = window();
  383. if (!w)
  384. return;
  385. if (!w->main_widget())
  386. return;
  387. do_layout();
  388. w->main_widget()->do_layout();
  389. });
  390. }
  391. void GWidget::set_visible(bool visible)
  392. {
  393. if (visible == m_visible)
  394. return;
  395. m_visible = visible;
  396. if (auto* parent = parent_widget())
  397. parent->invalidate_layout();
  398. if (m_visible)
  399. update();
  400. }
  401. bool GWidget::spans_entire_window_horizontally() const
  402. {
  403. auto* w = window();
  404. if (!w)
  405. return false;
  406. auto* main_widget = w->main_widget();
  407. if (!main_widget)
  408. return false;
  409. if (main_widget == this)
  410. return true;
  411. auto wrr = window_relative_rect();
  412. return wrr.left() == main_widget->rect().left() && wrr.right() == main_widget->rect().right();
  413. }
  414. void GWidget::set_enabled(bool enabled)
  415. {
  416. if (m_enabled == enabled)
  417. return;
  418. m_enabled = enabled;
  419. GEvent e(GEvent::EnabledChange);
  420. event(e);
  421. update();
  422. }
  423. void GWidget::move_to_front()
  424. {
  425. auto* parent = parent_widget();
  426. if (!parent)
  427. return;
  428. if (parent->children().size() == 1)
  429. return;
  430. parent->children().remove_first_matching([this](auto& entry) {
  431. return entry == this;
  432. });
  433. parent->children().append(this);
  434. parent->update();
  435. }
  436. void GWidget::move_to_back()
  437. {
  438. auto* parent = parent_widget();
  439. if (!parent)
  440. return;
  441. if (parent->children().size() == 1)
  442. return;
  443. parent->children().remove_first_matching([this](auto& entry) {
  444. return entry == this;
  445. });
  446. parent->children().prepend(this);
  447. parent->update();
  448. }
  449. bool GWidget::is_frontmost() const
  450. {
  451. auto* parent = parent_widget();
  452. if (!parent)
  453. return true;
  454. return parent->children().last() == this;
  455. }
  456. bool GWidget::is_backmost() const
  457. {
  458. auto* parent = parent_widget();
  459. if (!parent)
  460. return true;
  461. return parent->children().first() == this;
  462. }
  463. GAction* GWidget::action_for_key_event(const GKeyEvent& event)
  464. {
  465. auto it = m_local_shortcut_actions.find(GShortcut(event.modifiers(), (KeyCode)event.key()));
  466. if (it == m_local_shortcut_actions.end())
  467. return nullptr;
  468. return (*it).value;
  469. }
  470. void GWidget::register_local_shortcut_action(Badge<GAction>, GAction& action)
  471. {
  472. m_local_shortcut_actions.set(action.shortcut(), &action);
  473. }
  474. void GWidget::unregister_local_shortcut_action(Badge<GAction>, GAction& action)
  475. {
  476. m_local_shortcut_actions.remove(action.shortcut());
  477. }
  478. void GWidget::set_updates_enabled(bool enabled)
  479. {
  480. if (m_updates_enabled == enabled)
  481. return;
  482. m_updates_enabled = enabled;
  483. if (enabled)
  484. update();
  485. }
  486. void GWidget::focus_previous_widget()
  487. {
  488. auto focusable_widgets = window()->focusable_widgets();
  489. for (int i = focusable_widgets.size() - 1; i >= 0; --i) {
  490. if (focusable_widgets[i] != this)
  491. continue;
  492. if (i > 0)
  493. focusable_widgets[i - 1]->set_focus(true);
  494. else
  495. focusable_widgets.last()->set_focus(true);
  496. }
  497. }
  498. void GWidget::focus_next_widget()
  499. {
  500. auto focusable_widgets = window()->focusable_widgets();
  501. for (int i = 0; i < focusable_widgets.size(); ++i) {
  502. if (focusable_widgets[i] != this)
  503. continue;
  504. if (i < focusable_widgets.size() - 1)
  505. focusable_widgets[i + 1]->set_focus(true);
  506. else
  507. focusable_widgets.first()->set_focus(true);
  508. }
  509. }
  510. void GWidget::set_backcolor(const StringView& color_string)
  511. {
  512. auto color = Color::from_string(color_string);
  513. if (!color.has_value())
  514. return;
  515. set_background_color(color.value());
  516. }
  517. void GWidget::set_forecolor(const StringView& color_string)
  518. {
  519. auto color = Color::from_string(color_string);
  520. if (!color.has_value())
  521. return;
  522. set_foreground_color(color.value());
  523. }
  524. void GWidget::save_to(AK::JsonObject& json)
  525. {
  526. json.set("relative_rect", relative_rect().to_string());
  527. json.set("fill_with_background_color", fill_with_background_color());
  528. json.set("tooltip", tooltip());
  529. json.set("visible", is_visible());
  530. json.set("focused", is_focused());
  531. json.set("enabled", is_enabled());
  532. json.set("background_color", background_color().to_string());
  533. json.set("foreground_color", foreground_color().to_string());
  534. json.set("preferred_size", preferred_size().to_string());
  535. json.set("size_policy", String::format("[%s,%s]", to_string(horizontal_size_policy()), to_string(vertical_size_policy())));
  536. CObject::save_to(json);
  537. }