Widget.cpp 21 KB

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