Widget.cpp 20 KB

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