Widget.cpp 22 KB

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