Widget.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Assertions.h>
  27. #include <AK/JsonObject.h>
  28. #include <LibGUI/Action.h>
  29. #include <LibGUI/Application.h>
  30. #include <LibGUI/Button.h>
  31. #include <LibGUI/CheckBox.h>
  32. #include <LibGUI/Event.h>
  33. #include <LibGUI/GroupBox.h>
  34. #include <LibGUI/Label.h>
  35. #include <LibGUI/Layout.h>
  36. #include <LibGUI/Menu.h>
  37. #include <LibGUI/Painter.h>
  38. #include <LibGUI/RadioButton.h>
  39. #include <LibGUI/ScrollBar.h>
  40. #include <LibGUI/Slider.h>
  41. #include <LibGUI/SpinBox.h>
  42. #include <LibGUI/TextBox.h>
  43. #include <LibGUI/Widget.h>
  44. #include <LibGUI/Window.h>
  45. #include <LibGUI/WindowServerConnection.h>
  46. #include <LibGfx/Bitmap.h>
  47. #include <LibGfx/Palette.h>
  48. #include <unistd.h>
  49. namespace GUI {
  50. REGISTER_GWIDGET(Button)
  51. REGISTER_GWIDGET(CheckBox)
  52. REGISTER_GWIDGET(GroupBox)
  53. REGISTER_GWIDGET(Label)
  54. REGISTER_GWIDGET(RadioButton)
  55. REGISTER_GWIDGET(ScrollBar)
  56. REGISTER_GWIDGET(Slider)
  57. REGISTER_GWIDGET(SpinBox)
  58. REGISTER_GWIDGET(TextBox)
  59. REGISTER_GWIDGET(Widget)
  60. static HashMap<String, WidgetClassRegistration*>& widget_classes()
  61. {
  62. static HashMap<String, WidgetClassRegistration*>* map;
  63. if (!map)
  64. map = new HashMap<String, WidgetClassRegistration*>;
  65. return *map;
  66. }
  67. WidgetClassRegistration::WidgetClassRegistration(const String& class_name, Function<NonnullRefPtr<Widget>(Widget*)> factory)
  68. : m_class_name(class_name)
  69. , m_factory(move(factory))
  70. {
  71. widget_classes().set(class_name, this);
  72. }
  73. WidgetClassRegistration::~WidgetClassRegistration()
  74. {
  75. }
  76. void WidgetClassRegistration::for_each(Function<void(const WidgetClassRegistration&)> callback)
  77. {
  78. for (auto& it : widget_classes()) {
  79. callback(*it.value);
  80. }
  81. }
  82. const WidgetClassRegistration* WidgetClassRegistration::find(const String& class_name)
  83. {
  84. return widget_classes().get(class_name).value_or(nullptr);
  85. }
  86. Widget::Widget(Widget* parent)
  87. : Core::Object(parent, true)
  88. , m_background_role(Gfx::ColorRole::Window)
  89. , m_foreground_role(Gfx::ColorRole::WindowText)
  90. , m_font(Gfx::Font::default_font())
  91. , m_palette(Application::the().palette().impl())
  92. {
  93. }
  94. Widget::~Widget()
  95. {
  96. }
  97. void Widget::child_event(Core::ChildEvent& event)
  98. {
  99. if (event.type() == Event::ChildAdded) {
  100. if (event.child() && Core::is<Widget>(*event.child()) && layout()) {
  101. if (event.insertion_before_child() && event.insertion_before_child()->is_widget())
  102. layout()->insert_widget_before(Core::to<Widget>(*event.child()), Core::to<Widget>(*event.insertion_before_child()));
  103. else
  104. layout()->add_widget(Core::to<Widget>(*event.child()));
  105. }
  106. }
  107. if (event.type() == Event::ChildRemoved) {
  108. if (layout()) {
  109. if (event.child() && Core::is<Widget>(*event.child()))
  110. layout()->remove_widget(Core::to<Widget>(*event.child()));
  111. else
  112. invalidate_layout();
  113. }
  114. update();
  115. }
  116. return Core::Object::child_event(event);
  117. }
  118. void Widget::set_relative_rect(const Gfx::Rect& a_rect)
  119. {
  120. // Get rid of negative width/height values.
  121. Gfx::Rect rect = {
  122. a_rect.x(),
  123. a_rect.y(),
  124. max(a_rect.width(), 0),
  125. max(a_rect.height(), 0)
  126. };
  127. if (rect == m_relative_rect)
  128. return;
  129. auto old_rect = m_relative_rect;
  130. bool size_changed = m_relative_rect.size() != rect.size();
  131. m_relative_rect = rect;
  132. if (size_changed) {
  133. ResizeEvent resize_event(m_relative_rect.size(), rect.size());
  134. event(resize_event);
  135. }
  136. if (auto* parent = parent_widget())
  137. parent->update(old_rect);
  138. update();
  139. }
  140. void Widget::event(Core::Event& event)
  141. {
  142. switch (event.type()) {
  143. case Event::Paint:
  144. return handle_paint_event(static_cast<PaintEvent&>(event));
  145. case Event::Resize:
  146. return handle_resize_event(static_cast<ResizeEvent&>(event));
  147. case Event::FocusIn:
  148. return focusin_event(event);
  149. case Event::FocusOut:
  150. return focusout_event(event);
  151. case Event::Show:
  152. return show_event(static_cast<ShowEvent&>(event));
  153. case Event::Hide:
  154. return hide_event(static_cast<HideEvent&>(event));
  155. case Event::KeyDown:
  156. return keydown_event(static_cast<KeyEvent&>(event));
  157. case Event::KeyUp:
  158. return keyup_event(static_cast<KeyEvent&>(event));
  159. case Event::MouseMove:
  160. return mousemove_event(static_cast<MouseEvent&>(event));
  161. case Event::MouseDown:
  162. return handle_mousedown_event(static_cast<MouseEvent&>(event));
  163. case Event::MouseDoubleClick:
  164. return handle_mousedoubleclick_event(static_cast<MouseEvent&>(event));
  165. case Event::MouseUp:
  166. return handle_mouseup_event(static_cast<MouseEvent&>(event));
  167. case Event::MouseWheel:
  168. return mousewheel_event(static_cast<MouseEvent&>(event));
  169. case Event::DragMove:
  170. return drag_move_event(static_cast<DragEvent&>(event));
  171. case Event::Drop:
  172. return drop_event(static_cast<DropEvent&>(event));
  173. case Event::Enter:
  174. return handle_enter_event(event);
  175. case Event::Leave:
  176. return handle_leave_event(event);
  177. case Event::EnabledChange:
  178. return change_event(static_cast<Event&>(event));
  179. default:
  180. return Core::Object::event(event);
  181. }
  182. }
  183. void Widget::handle_paint_event(PaintEvent& event)
  184. {
  185. ASSERT(is_visible());
  186. if (fill_with_background_color()) {
  187. Painter painter(*this);
  188. painter.fill_rect(event.rect(), palette().color(background_role()));
  189. } else {
  190. #ifdef DEBUG_WIDGET_UNDERDRAW
  191. // FIXME: This is a bit broken.
  192. // If the widget is not opaque, let's not mess it up with debugging color.
  193. Painter painter(*this);
  194. painter.fill_rect(rect(), Color::Red);
  195. #endif
  196. }
  197. paint_event(event);
  198. for_each_child_widget([&](auto& child) {
  199. if (!child.is_visible())
  200. return IterationDecision::Continue;
  201. if (child.relative_rect().intersects(event.rect())) {
  202. PaintEvent local_event(event.rect().intersected(child.relative_rect()).translated(-child.relative_position()));
  203. child.dispatch_event(local_event, this);
  204. }
  205. return IterationDecision::Continue;
  206. });
  207. second_paint_event(event);
  208. }
  209. void Widget::set_layout(OwnPtr<Layout>&& layout)
  210. {
  211. if (m_layout)
  212. m_layout->notify_disowned({}, *this);
  213. m_layout = move(layout);
  214. if (m_layout) {
  215. m_layout->notify_adopted({}, *this);
  216. do_layout();
  217. } else {
  218. update();
  219. }
  220. }
  221. void Widget::do_layout()
  222. {
  223. for_each_child_widget([&](auto& child) {
  224. child.do_layout();
  225. return IterationDecision::Continue;
  226. });
  227. custom_layout();
  228. if (!m_layout)
  229. return;
  230. m_layout->run(*this);
  231. did_layout();
  232. update();
  233. }
  234. void Widget::notify_layout_changed(Badge<Layout>)
  235. {
  236. invalidate_layout();
  237. }
  238. void Widget::handle_resize_event(ResizeEvent& event)
  239. {
  240. resize_event(event);
  241. do_layout();
  242. }
  243. void Widget::handle_mouseup_event(MouseEvent& event)
  244. {
  245. mouseup_event(event);
  246. }
  247. void Widget::handle_mousedown_event(MouseEvent& event)
  248. {
  249. if (accepts_focus())
  250. set_focus(true);
  251. mousedown_event(event);
  252. if (event.button() == MouseButton::Right) {
  253. ContextMenuEvent c_event(event.position(), screen_relative_rect().location().translated(event.position()));
  254. context_menu_event(c_event);
  255. }
  256. }
  257. void Widget::handle_mousedoubleclick_event(MouseEvent& event)
  258. {
  259. doubleclick_event(event);
  260. }
  261. void Widget::handle_enter_event(Core::Event& event)
  262. {
  263. if (has_tooltip())
  264. Application::the().show_tooltip(m_tooltip, screen_relative_rect().center().translated(0, height() / 2));
  265. enter_event(event);
  266. }
  267. void Widget::handle_leave_event(Core::Event& event)
  268. {
  269. Application::the().hide_tooltip();
  270. leave_event(event);
  271. }
  272. void Widget::click_event(MouseEvent&)
  273. {
  274. }
  275. void Widget::doubleclick_event(MouseEvent&)
  276. {
  277. }
  278. void Widget::resize_event(ResizeEvent&)
  279. {
  280. }
  281. void Widget::paint_event(PaintEvent&)
  282. {
  283. }
  284. void Widget::second_paint_event(PaintEvent&)
  285. {
  286. }
  287. void Widget::show_event(ShowEvent&)
  288. {
  289. }
  290. void Widget::hide_event(HideEvent&)
  291. {
  292. }
  293. void Widget::keydown_event(KeyEvent& event)
  294. {
  295. if (!event.alt() && !event.ctrl() && !event.logo() && event.key() == KeyCode::Key_Tab) {
  296. if (event.shift())
  297. focus_previous_widget();
  298. else
  299. focus_next_widget();
  300. event.accept();
  301. return;
  302. }
  303. event.ignore();
  304. }
  305. void Widget::keyup_event(KeyEvent&)
  306. {
  307. }
  308. void Widget::mousedown_event(MouseEvent&)
  309. {
  310. }
  311. void Widget::mouseup_event(MouseEvent&)
  312. {
  313. }
  314. void Widget::mousemove_event(MouseEvent&)
  315. {
  316. }
  317. void Widget::mousewheel_event(MouseEvent&)
  318. {
  319. }
  320. void Widget::context_menu_event(ContextMenuEvent&)
  321. {
  322. }
  323. void Widget::focusin_event(Core::Event&)
  324. {
  325. }
  326. void Widget::focusout_event(Core::Event&)
  327. {
  328. }
  329. void Widget::enter_event(Core::Event&)
  330. {
  331. }
  332. void Widget::leave_event(Core::Event&)
  333. {
  334. }
  335. void Widget::change_event(Event&)
  336. {
  337. }
  338. void Widget::drag_move_event(DragEvent& event)
  339. {
  340. dbg() << class_name() << "{" << this << "} DRAG MOVE position: " << event.position() << ", data_type: '" << event.data_type() << "'";
  341. event.ignore();
  342. }
  343. void Widget::drop_event(DropEvent& event)
  344. {
  345. dbg() << class_name() << "{" << this << "} DROP position: " << event.position() << ", text: '" << event.text() << "'";
  346. event.ignore();
  347. }
  348. void Widget::update()
  349. {
  350. if (rect().is_empty())
  351. return;
  352. update(rect());
  353. }
  354. void Widget::update(const Gfx::Rect& rect)
  355. {
  356. if (!is_visible())
  357. return;
  358. if (!updates_enabled())
  359. return;
  360. Window* window = m_window;
  361. Widget* parent = parent_widget();
  362. while (parent) {
  363. if (!parent->updates_enabled())
  364. return;
  365. window = parent->m_window;
  366. parent = parent->parent_widget();
  367. }
  368. if (window)
  369. window->update(rect.translated(window_relative_rect().location()));
  370. }
  371. Gfx::Rect Widget::window_relative_rect() const
  372. {
  373. auto rect = relative_rect();
  374. for (auto* parent = parent_widget(); parent; parent = parent->parent_widget()) {
  375. rect.move_by(parent->relative_position());
  376. }
  377. return rect;
  378. }
  379. Gfx::Rect Widget::screen_relative_rect() const
  380. {
  381. return window_relative_rect().translated(window()->position());
  382. }
  383. Widget* Widget::child_at(const Gfx::Point& point) const
  384. {
  385. for (int i = children().size() - 1; i >= 0; --i) {
  386. if (!Core::is<Widget>(children()[i]))
  387. continue;
  388. auto& child = Core::to<Widget>(children()[i]);
  389. if (!child.is_visible())
  390. continue;
  391. if (child.relative_rect().contains(point))
  392. return const_cast<Widget*>(&child);
  393. }
  394. return nullptr;
  395. }
  396. Widget::HitTestResult Widget::hit_test(const Gfx::Point& position, ShouldRespectGreediness should_respect_greediness)
  397. {
  398. if (should_respect_greediness == ShouldRespectGreediness::Yes && is_greedy_for_hits())
  399. return { this, position };
  400. if (auto* child = child_at(position))
  401. return child->hit_test(position - child->relative_position());
  402. return { this, position };
  403. }
  404. void Widget::set_window(Window* window)
  405. {
  406. if (m_window == window)
  407. return;
  408. m_window = window;
  409. }
  410. bool Widget::is_focused() const
  411. {
  412. auto* win = window();
  413. if (!win)
  414. return false;
  415. if (!win->is_active())
  416. return false;
  417. return win->focused_widget() == this;
  418. }
  419. void Widget::set_focus(bool focus)
  420. {
  421. auto* win = window();
  422. if (!win)
  423. return;
  424. if (focus) {
  425. win->set_focused_widget(this);
  426. } else {
  427. if (win->focused_widget() == this)
  428. win->set_focused_widget(nullptr);
  429. }
  430. }
  431. void Widget::set_font(const Gfx::Font* font)
  432. {
  433. if (m_font.ptr() == font)
  434. return;
  435. if (!font)
  436. m_font = Gfx::Font::default_font();
  437. else
  438. m_font = *font;
  439. did_change_font();
  440. update();
  441. }
  442. void Widget::set_global_cursor_tracking(bool enabled)
  443. {
  444. auto* win = window();
  445. if (!win)
  446. return;
  447. win->set_global_cursor_tracking_widget(enabled ? this : nullptr);
  448. }
  449. bool Widget::global_cursor_tracking() const
  450. {
  451. auto* win = window();
  452. if (!win)
  453. return false;
  454. return win->global_cursor_tracking_widget() == this;
  455. }
  456. void Widget::set_preferred_size(const Gfx::Size& size)
  457. {
  458. if (m_preferred_size == size)
  459. return;
  460. m_preferred_size = size;
  461. invalidate_layout();
  462. }
  463. void Widget::set_size_policy(Orientation orientation, SizePolicy policy)
  464. {
  465. if (orientation == Orientation::Horizontal)
  466. set_size_policy(policy, m_vertical_size_policy);
  467. else
  468. set_size_policy(m_horizontal_size_policy, policy);
  469. }
  470. void Widget::set_size_policy(SizePolicy horizontal_policy, SizePolicy vertical_policy)
  471. {
  472. if (m_horizontal_size_policy == horizontal_policy && m_vertical_size_policy == vertical_policy)
  473. return;
  474. m_horizontal_size_policy = horizontal_policy;
  475. m_vertical_size_policy = vertical_policy;
  476. invalidate_layout();
  477. }
  478. void Widget::invalidate_layout()
  479. {
  480. if (window())
  481. window()->schedule_relayout();
  482. }
  483. void Widget::set_visible(bool visible)
  484. {
  485. if (visible == m_visible)
  486. return;
  487. m_visible = visible;
  488. if (auto* parent = parent_widget())
  489. parent->invalidate_layout();
  490. if (m_visible)
  491. update();
  492. if (m_visible) {
  493. ShowEvent e;
  494. event(e);
  495. } else {
  496. HideEvent e;
  497. event(e);
  498. }
  499. }
  500. bool Widget::spans_entire_window_horizontally() const
  501. {
  502. auto* w = window();
  503. if (!w)
  504. return false;
  505. auto* main_widget = w->main_widget();
  506. if (!main_widget)
  507. return false;
  508. if (main_widget == this)
  509. return true;
  510. auto wrr = window_relative_rect();
  511. return wrr.left() == main_widget->rect().left() && wrr.right() == main_widget->rect().right();
  512. }
  513. void Widget::set_enabled(bool enabled)
  514. {
  515. if (m_enabled == enabled)
  516. return;
  517. m_enabled = enabled;
  518. Event e(Event::EnabledChange);
  519. event(e);
  520. update();
  521. }
  522. void Widget::move_to_front()
  523. {
  524. auto* parent = parent_widget();
  525. if (!parent)
  526. return;
  527. if (parent->children().size() == 1)
  528. return;
  529. parent->children().remove_first_matching([this](auto& entry) {
  530. return entry == this;
  531. });
  532. parent->children().append(*this);
  533. parent->update();
  534. }
  535. void Widget::move_to_back()
  536. {
  537. auto* parent = parent_widget();
  538. if (!parent)
  539. return;
  540. if (parent->children().size() == 1)
  541. return;
  542. parent->children().remove_first_matching([this](auto& entry) {
  543. return entry == this;
  544. });
  545. parent->children().prepend(*this);
  546. parent->update();
  547. }
  548. bool Widget::is_frontmost() const
  549. {
  550. auto* parent = parent_widget();
  551. if (!parent)
  552. return true;
  553. return &parent->children().last() == this;
  554. }
  555. bool Widget::is_backmost() const
  556. {
  557. auto* parent = parent_widget();
  558. if (!parent)
  559. return true;
  560. return &parent->children().first() == this;
  561. }
  562. Action* Widget::action_for_key_event(const KeyEvent& event)
  563. {
  564. Shortcut shortcut(event.modifiers(), (KeyCode)event.key());
  565. Action* found_action = nullptr;
  566. for_each_child_of_type<Action>([&](auto& action) {
  567. if (action.shortcut() == shortcut) {
  568. found_action = &action;
  569. return IterationDecision::Break;
  570. }
  571. return IterationDecision::Continue;
  572. });
  573. return found_action;
  574. }
  575. void Widget::set_updates_enabled(bool enabled)
  576. {
  577. if (m_updates_enabled == enabled)
  578. return;
  579. m_updates_enabled = enabled;
  580. if (enabled)
  581. update();
  582. }
  583. void Widget::focus_previous_widget()
  584. {
  585. auto focusable_widgets = window()->focusable_widgets();
  586. for (int i = focusable_widgets.size() - 1; i >= 0; --i) {
  587. if (focusable_widgets[i] != this)
  588. continue;
  589. if (i > 0)
  590. focusable_widgets[i - 1]->set_focus(true);
  591. else
  592. focusable_widgets.last()->set_focus(true);
  593. }
  594. }
  595. void Widget::focus_next_widget()
  596. {
  597. auto focusable_widgets = window()->focusable_widgets();
  598. for (int i = 0; i < focusable_widgets.size(); ++i) {
  599. if (focusable_widgets[i] != this)
  600. continue;
  601. if (i < focusable_widgets.size() - 1)
  602. focusable_widgets[i + 1]->set_focus(true);
  603. else
  604. focusable_widgets.first()->set_focus(true);
  605. }
  606. }
  607. void Widget::set_backcolor(const StringView& color_string)
  608. {
  609. auto color = Color::from_string(color_string);
  610. if (!color.has_value())
  611. return;
  612. set_background_color(color.value());
  613. }
  614. void Widget::set_forecolor(const StringView& color_string)
  615. {
  616. auto color = Color::from_string(color_string);
  617. if (!color.has_value())
  618. return;
  619. set_foreground_color(color.value());
  620. }
  621. void Widget::save_to(AK::JsonObject& json)
  622. {
  623. json.set("relative_rect", relative_rect().to_string());
  624. json.set("fill_with_background_color", fill_with_background_color());
  625. json.set("tooltip", tooltip());
  626. json.set("visible", is_visible());
  627. json.set("focused", is_focused());
  628. json.set("enabled", is_enabled());
  629. json.set("background_color", background_color().to_string());
  630. json.set("foreground_color", foreground_color().to_string());
  631. json.set("preferred_size", preferred_size().to_string());
  632. json.set("size_policy", String::format("[%s,%s]", to_string(horizontal_size_policy()), to_string(vertical_size_policy())));
  633. Core::Object::save_to(json);
  634. }
  635. Vector<Widget*> Widget::child_widgets() const
  636. {
  637. Vector<Widget*> widgets;
  638. widgets.ensure_capacity(children().size());
  639. for (auto& child : const_cast<Widget*>(this)->children()) {
  640. if (child.is_widget())
  641. widgets.append(static_cast<Widget*>(&child));
  642. }
  643. return widgets;
  644. }
  645. void Widget::set_palette(const Palette& palette)
  646. {
  647. m_palette = palette.impl();
  648. }
  649. void Widget::set_background_role(ColorRole role)
  650. {
  651. m_background_role = role;
  652. }
  653. void Widget::set_foreground_role(ColorRole role)
  654. {
  655. m_foreground_role = role;
  656. }
  657. Gfx::Palette Widget::palette() const
  658. {
  659. return Gfx::Palette(*m_palette);
  660. }
  661. }