GWidget.cpp 15 KB

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