GWidget.cpp 15 KB

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