Widget.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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/BoxLayout.h>
  31. #include <LibGUI/Button.h>
  32. #include <LibGUI/CheckBox.h>
  33. #include <LibGUI/Event.h>
  34. #include <LibGUI/GroupBox.h>
  35. #include <LibGUI/Label.h>
  36. #include <LibGUI/Layout.h>
  37. #include <LibGUI/Menu.h>
  38. #include <LibGUI/Painter.h>
  39. #include <LibGUI/RadioButton.h>
  40. #include <LibGUI/ScrollBar.h>
  41. #include <LibGUI/Slider.h>
  42. #include <LibGUI/SpinBox.h>
  43. #include <LibGUI/Splitter.h>
  44. #include <LibGUI/StatusBar.h>
  45. #include <LibGUI/TextBox.h>
  46. #include <LibGUI/ToolBar.h>
  47. #include <LibGUI/ToolBarContainer.h>
  48. #include <LibGUI/Widget.h>
  49. #include <LibGUI/Window.h>
  50. #include <LibGUI/WindowServerConnection.h>
  51. #include <LibGfx/Bitmap.h>
  52. #include <LibGfx/Font.h>
  53. #include <LibGfx/Palette.h>
  54. #include <unistd.h>
  55. namespace GUI {
  56. REGISTER_WIDGET(GUI, Button)
  57. REGISTER_WIDGET(GUI, CheckBox)
  58. REGISTER_WIDGET(GUI, GroupBox)
  59. REGISTER_WIDGET(GUI, HorizontalSplitter)
  60. REGISTER_WIDGET(GUI, Label)
  61. REGISTER_WIDGET(GUI, RadioButton)
  62. REGISTER_WIDGET(GUI, ScrollBar)
  63. REGISTER_WIDGET(GUI, Slider)
  64. REGISTER_WIDGET(GUI, SpinBox)
  65. REGISTER_WIDGET(GUI, StatusBar)
  66. REGISTER_WIDGET(GUI, TextBox)
  67. REGISTER_WIDGET(GUI, TextEditor)
  68. REGISTER_WIDGET(GUI, ToolBar)
  69. REGISTER_WIDGET(GUI, ToolBarContainer)
  70. REGISTER_WIDGET(GUI, Widget)
  71. static HashMap<String, WidgetClassRegistration*>& widget_classes()
  72. {
  73. static HashMap<String, WidgetClassRegistration*>* map;
  74. if (!map)
  75. map = new HashMap<String, WidgetClassRegistration*>;
  76. return *map;
  77. }
  78. WidgetClassRegistration::WidgetClassRegistration(const String& class_name, Function<NonnullRefPtr<Widget>()> factory)
  79. : m_class_name(class_name)
  80. , m_factory(move(factory))
  81. {
  82. widget_classes().set(class_name, this);
  83. }
  84. WidgetClassRegistration::~WidgetClassRegistration()
  85. {
  86. }
  87. void WidgetClassRegistration::for_each(Function<void(const WidgetClassRegistration&)> callback)
  88. {
  89. for (auto& it : widget_classes()) {
  90. callback(*it.value);
  91. }
  92. }
  93. const WidgetClassRegistration* WidgetClassRegistration::find(const String& class_name)
  94. {
  95. return widget_classes().get(class_name).value_or(nullptr);
  96. }
  97. Widget::Widget()
  98. : Core::Object(nullptr, true)
  99. , m_background_role(Gfx::ColorRole::Window)
  100. , m_foreground_role(Gfx::ColorRole::WindowText)
  101. , m_font(Gfx::Font::default_font())
  102. , m_palette(Application::the()->palette().impl())
  103. {
  104. }
  105. Widget::~Widget()
  106. {
  107. }
  108. void Widget::child_event(Core::ChildEvent& event)
  109. {
  110. if (event.type() == Event::ChildAdded) {
  111. if (event.child() && is<Widget>(*event.child()) && layout()) {
  112. if (event.insertion_before_child() && event.insertion_before_child()->is_widget())
  113. layout()->insert_widget_before(downcast<Widget>(*event.child()), downcast<Widget>(*event.insertion_before_child()));
  114. else
  115. layout()->add_widget(downcast<Widget>(*event.child()));
  116. }
  117. if (window() && event.child() && is<Widget>(*event.child()))
  118. window()->did_add_widget({}, downcast<Widget>(*event.child()));
  119. }
  120. if (event.type() == Event::ChildRemoved) {
  121. if (layout()) {
  122. if (event.child() && is<Widget>(*event.child()))
  123. layout()->remove_widget(downcast<Widget>(*event.child()));
  124. else
  125. invalidate_layout();
  126. }
  127. if (window() && event.child() && is<Widget>(*event.child()))
  128. window()->did_remove_widget({}, downcast<Widget>(*event.child()));
  129. update();
  130. }
  131. return Core::Object::child_event(event);
  132. }
  133. void Widget::set_relative_rect(const Gfx::IntRect& a_rect)
  134. {
  135. // Get rid of negative width/height values.
  136. Gfx::IntRect rect = {
  137. a_rect.x(),
  138. a_rect.y(),
  139. max(a_rect.width(), 0),
  140. max(a_rect.height(), 0)
  141. };
  142. if (rect == m_relative_rect)
  143. return;
  144. auto old_rect = m_relative_rect;
  145. bool size_changed = m_relative_rect.size() != rect.size();
  146. m_relative_rect = rect;
  147. if (size_changed) {
  148. ResizeEvent resize_event(rect.size());
  149. event(resize_event);
  150. }
  151. if (auto* parent = parent_widget())
  152. parent->update(old_rect);
  153. update();
  154. }
  155. void Widget::event(Core::Event& event)
  156. {
  157. if (!is_enabled()) {
  158. switch (event.type()) {
  159. case Event::MouseUp:
  160. case Event::MouseDown:
  161. case Event::MouseMove:
  162. case Event::MouseWheel:
  163. case Event::MouseDoubleClick:
  164. case Event::KeyUp:
  165. case Event::KeyDown:
  166. return;
  167. default:
  168. break;
  169. }
  170. }
  171. switch (event.type()) {
  172. case Event::Paint:
  173. return handle_paint_event(static_cast<PaintEvent&>(event));
  174. case Event::Resize:
  175. return handle_resize_event(static_cast<ResizeEvent&>(event));
  176. case Event::FocusIn:
  177. return focusin_event(static_cast<FocusEvent&>(event));
  178. case Event::FocusOut:
  179. return focusout_event(static_cast<FocusEvent&>(event));
  180. case Event::Show:
  181. return show_event(static_cast<ShowEvent&>(event));
  182. case Event::Hide:
  183. return hide_event(static_cast<HideEvent&>(event));
  184. case Event::KeyDown:
  185. return keydown_event(static_cast<KeyEvent&>(event));
  186. case Event::KeyUp:
  187. return keyup_event(static_cast<KeyEvent&>(event));
  188. case Event::MouseMove:
  189. return mousemove_event(static_cast<MouseEvent&>(event));
  190. case Event::MouseDown:
  191. return handle_mousedown_event(static_cast<MouseEvent&>(event));
  192. case Event::MouseDoubleClick:
  193. return handle_mousedoubleclick_event(static_cast<MouseEvent&>(event));
  194. case Event::MouseUp:
  195. return handle_mouseup_event(static_cast<MouseEvent&>(event));
  196. case Event::MouseWheel:
  197. return mousewheel_event(static_cast<MouseEvent&>(event));
  198. case Event::DragMove:
  199. return drag_move_event(static_cast<DragEvent&>(event));
  200. case Event::Drop:
  201. return drop_event(static_cast<DropEvent&>(event));
  202. case Event::ThemeChange:
  203. return theme_change_event(static_cast<ThemeChangeEvent&>(event));
  204. case Event::Enter:
  205. return handle_enter_event(event);
  206. case Event::Leave:
  207. return handle_leave_event(event);
  208. case Event::EnabledChange:
  209. return change_event(static_cast<Event&>(event));
  210. default:
  211. return Core::Object::event(event);
  212. }
  213. }
  214. void Widget::handle_paint_event(PaintEvent& event)
  215. {
  216. ASSERT(is_visible());
  217. if (fill_with_background_color()) {
  218. Painter painter(*this);
  219. painter.fill_rect(event.rect(), palette().color(background_role()));
  220. }
  221. paint_event(event);
  222. auto children_clip_rect = this->children_clip_rect();
  223. for_each_child_widget([&](auto& child) {
  224. if (!child.is_visible())
  225. return IterationDecision::Continue;
  226. if (child.relative_rect().intersects(event.rect())) {
  227. PaintEvent local_event(event.rect().intersected(children_clip_rect).intersected(child.relative_rect()).translated(-child.relative_position()));
  228. child.dispatch_event(local_event, this);
  229. }
  230. return IterationDecision::Continue;
  231. });
  232. second_paint_event(event);
  233. if (is_being_inspected()) {
  234. Painter painter(*this);
  235. painter.draw_rect(rect(), Color::Magenta);
  236. }
  237. if (Application::the()->focus_debugging_enabled()) {
  238. if (is_focused()) {
  239. Painter painter(*this);
  240. painter.draw_rect(rect(), Color::Cyan);
  241. }
  242. }
  243. }
  244. void Widget::set_layout(NonnullRefPtr<Layout> layout)
  245. {
  246. if (m_layout) {
  247. m_layout->notify_disowned({}, *this);
  248. m_layout->remove_from_parent();
  249. }
  250. m_layout = move(layout);
  251. if (m_layout) {
  252. add_child(*m_layout);
  253. m_layout->notify_adopted({}, *this);
  254. do_layout();
  255. } else {
  256. update();
  257. }
  258. }
  259. void Widget::do_layout()
  260. {
  261. for_each_child_widget([&](auto& child) {
  262. child.do_layout();
  263. return IterationDecision::Continue;
  264. });
  265. custom_layout();
  266. if (!m_layout)
  267. return;
  268. m_layout->run(*this);
  269. did_layout();
  270. update();
  271. }
  272. void Widget::notify_layout_changed(Badge<Layout>)
  273. {
  274. invalidate_layout();
  275. }
  276. void Widget::handle_resize_event(ResizeEvent& event)
  277. {
  278. resize_event(event);
  279. do_layout();
  280. }
  281. void Widget::handle_mouseup_event(MouseEvent& event)
  282. {
  283. mouseup_event(event);
  284. }
  285. void Widget::handle_mousedown_event(MouseEvent& event)
  286. {
  287. if (accepts_focus())
  288. set_focus(true, FocusSource::Mouse);
  289. mousedown_event(event);
  290. if (event.button() == MouseButton::Right) {
  291. ContextMenuEvent c_event(event.position(), screen_relative_rect().location().translated(event.position()));
  292. context_menu_event(c_event);
  293. }
  294. }
  295. void Widget::handle_mousedoubleclick_event(MouseEvent& event)
  296. {
  297. doubleclick_event(event);
  298. }
  299. void Widget::handle_enter_event(Core::Event& event)
  300. {
  301. window()->update_cursor({});
  302. show_tooltip();
  303. enter_event(event);
  304. }
  305. void Widget::handle_leave_event(Core::Event& event)
  306. {
  307. window()->update_cursor({});
  308. Application::the()->hide_tooltip();
  309. leave_event(event);
  310. }
  311. void Widget::doubleclick_event(MouseEvent&)
  312. {
  313. }
  314. void Widget::resize_event(ResizeEvent&)
  315. {
  316. }
  317. void Widget::paint_event(PaintEvent&)
  318. {
  319. }
  320. void Widget::second_paint_event(PaintEvent&)
  321. {
  322. }
  323. void Widget::show_event(ShowEvent&)
  324. {
  325. }
  326. void Widget::hide_event(HideEvent&)
  327. {
  328. }
  329. void Widget::keydown_event(KeyEvent& event)
  330. {
  331. if (!event.alt() && !event.ctrl() && !event.logo() && event.key() == KeyCode::Key_Tab) {
  332. if (event.shift())
  333. focus_previous_widget(FocusSource::Keyboard);
  334. else
  335. focus_next_widget(FocusSource::Keyboard);
  336. event.accept();
  337. return;
  338. }
  339. event.ignore();
  340. }
  341. void Widget::keyup_event(KeyEvent& event)
  342. {
  343. event.ignore();
  344. }
  345. void Widget::mousedown_event(MouseEvent&)
  346. {
  347. }
  348. void Widget::mouseup_event(MouseEvent&)
  349. {
  350. }
  351. void Widget::mousemove_event(MouseEvent&)
  352. {
  353. }
  354. void Widget::mousewheel_event(MouseEvent&)
  355. {
  356. }
  357. void Widget::context_menu_event(ContextMenuEvent&)
  358. {
  359. }
  360. void Widget::focusin_event(FocusEvent&)
  361. {
  362. }
  363. void Widget::focusout_event(FocusEvent&)
  364. {
  365. }
  366. void Widget::enter_event(Core::Event&)
  367. {
  368. }
  369. void Widget::leave_event(Core::Event&)
  370. {
  371. }
  372. void Widget::change_event(Event&)
  373. {
  374. }
  375. void Widget::drag_move_event(DragEvent& event)
  376. {
  377. dbg() << class_name() << "{" << this << "} DRAG MOVE position: " << event.position() << ", data_type: '" << event.data_type() << "'";
  378. event.ignore();
  379. }
  380. void Widget::drop_event(DropEvent& event)
  381. {
  382. dbg() << class_name() << "{" << this << "} DROP position: " << event.position() << ", text: '" << event.text() << "'";
  383. event.ignore();
  384. }
  385. void Widget::theme_change_event(ThemeChangeEvent&)
  386. {
  387. }
  388. void Widget::update()
  389. {
  390. if (rect().is_empty())
  391. return;
  392. update(rect());
  393. }
  394. void Widget::update(const Gfx::IntRect& rect)
  395. {
  396. if (!is_visible())
  397. return;
  398. if (!updates_enabled())
  399. return;
  400. Window* window = m_window;
  401. Widget* parent = parent_widget();
  402. while (parent) {
  403. if (!parent->updates_enabled())
  404. return;
  405. window = parent->m_window;
  406. parent = parent->parent_widget();
  407. }
  408. if (window)
  409. window->update(rect.translated(window_relative_rect().location()));
  410. }
  411. Gfx::IntRect Widget::window_relative_rect() const
  412. {
  413. auto rect = relative_rect();
  414. for (auto* parent = parent_widget(); parent; parent = parent->parent_widget()) {
  415. rect.move_by(parent->relative_position());
  416. }
  417. return rect;
  418. }
  419. Gfx::IntRect Widget::screen_relative_rect() const
  420. {
  421. auto window_position = window()->window_type() == WindowType::MenuApplet
  422. ? window()->rect_in_menubar().location()
  423. : window()->rect().location();
  424. return window_relative_rect().translated(window_position);
  425. }
  426. Widget* Widget::child_at(const Gfx::IntPoint& point) const
  427. {
  428. for (int i = children().size() - 1; i >= 0; --i) {
  429. if (!is<Widget>(children()[i]))
  430. continue;
  431. auto& child = downcast<Widget>(children()[i]);
  432. if (!child.is_visible())
  433. continue;
  434. if (child.content_rect().contains(point))
  435. return const_cast<Widget*>(&child);
  436. }
  437. return nullptr;
  438. }
  439. Widget::HitTestResult Widget::hit_test(const Gfx::IntPoint& position, ShouldRespectGreediness should_respect_greediness)
  440. {
  441. if (should_respect_greediness == ShouldRespectGreediness::Yes && is_greedy_for_hits())
  442. return { this, position };
  443. if (auto* child = child_at(position))
  444. return child->hit_test(position - child->relative_position());
  445. return { this, position };
  446. }
  447. void Widget::set_window(Window* window)
  448. {
  449. if (m_window == window)
  450. return;
  451. m_window = window;
  452. }
  453. void Widget::set_focus_proxy(Widget* proxy)
  454. {
  455. if (m_focus_proxy == proxy)
  456. return;
  457. if (proxy)
  458. m_focus_proxy = proxy->make_weak_ptr();
  459. else
  460. m_focus_proxy = nullptr;
  461. }
  462. bool Widget::is_focused() const
  463. {
  464. if (m_focus_proxy)
  465. return m_focus_proxy->is_focused();
  466. auto* win = window();
  467. if (!win)
  468. return false;
  469. // Accessory windows are not active despite being the active
  470. // input window. So we can have focus if either we're the active
  471. // input window or we're the active window
  472. if (win->is_active_input() || win->is_active())
  473. return win->focused_widget() == this;
  474. return false;
  475. }
  476. void Widget::set_focus(bool focus, FocusSource source)
  477. {
  478. if (m_focus_proxy)
  479. return m_focus_proxy->set_focus(focus, source);
  480. auto* win = window();
  481. if (!win)
  482. return;
  483. if (focus) {
  484. win->set_focused_widget(this, source);
  485. } else {
  486. if (win->focused_widget() == this)
  487. win->set_focused_widget(nullptr, source);
  488. }
  489. }
  490. void Widget::set_font(const Gfx::Font* font)
  491. {
  492. if (m_font.ptr() == font)
  493. return;
  494. if (!font)
  495. m_font = Gfx::Font::default_font();
  496. else
  497. m_font = *font;
  498. did_change_font();
  499. update();
  500. }
  501. void Widget::set_global_cursor_tracking(bool enabled)
  502. {
  503. auto* win = window();
  504. if (!win)
  505. return;
  506. win->set_global_cursor_tracking_widget(enabled ? this : nullptr);
  507. }
  508. bool Widget::global_cursor_tracking() const
  509. {
  510. auto* win = window();
  511. if (!win)
  512. return false;
  513. return win->global_cursor_tracking_widget() == this;
  514. }
  515. void Widget::set_preferred_size(const Gfx::IntSize& size)
  516. {
  517. if (m_preferred_size == size)
  518. return;
  519. m_preferred_size = size;
  520. invalidate_layout();
  521. }
  522. void Widget::set_size_policy(Orientation orientation, SizePolicy policy)
  523. {
  524. if (orientation == Orientation::Horizontal)
  525. set_size_policy(policy, m_vertical_size_policy);
  526. else
  527. set_size_policy(m_horizontal_size_policy, policy);
  528. }
  529. void Widget::set_size_policy(SizePolicy horizontal_policy, SizePolicy vertical_policy)
  530. {
  531. if (m_horizontal_size_policy == horizontal_policy && m_vertical_size_policy == vertical_policy)
  532. return;
  533. m_horizontal_size_policy = horizontal_policy;
  534. m_vertical_size_policy = vertical_policy;
  535. invalidate_layout();
  536. }
  537. void Widget::invalidate_layout()
  538. {
  539. if (window())
  540. window()->schedule_relayout();
  541. }
  542. void Widget::set_visible(bool visible)
  543. {
  544. if (visible == m_visible)
  545. return;
  546. m_visible = visible;
  547. if (auto* parent = parent_widget())
  548. parent->invalidate_layout();
  549. if (m_visible)
  550. update();
  551. if (m_visible) {
  552. ShowEvent e;
  553. event(e);
  554. } else {
  555. HideEvent e;
  556. event(e);
  557. }
  558. }
  559. bool Widget::spans_entire_window_horizontally() const
  560. {
  561. auto* w = window();
  562. if (!w)
  563. return false;
  564. auto* main_widget = w->main_widget();
  565. if (!main_widget)
  566. return false;
  567. if (main_widget == this)
  568. return true;
  569. auto wrr = window_relative_rect();
  570. return wrr.left() == main_widget->rect().left() && wrr.right() == main_widget->rect().right();
  571. }
  572. void Widget::set_enabled(bool enabled)
  573. {
  574. if (m_enabled == enabled)
  575. return;
  576. m_enabled = enabled;
  577. for_each_child_widget([enabled](auto& child) {
  578. child.set_enabled(enabled);
  579. return IterationDecision::Continue;
  580. });
  581. Event e(Event::EnabledChange);
  582. event(e);
  583. update();
  584. }
  585. void Widget::move_to_front()
  586. {
  587. auto* parent = parent_widget();
  588. if (!parent)
  589. return;
  590. if (parent->children().size() == 1)
  591. return;
  592. parent->children().remove_first_matching([this](auto& entry) {
  593. return entry == this;
  594. });
  595. parent->children().append(*this);
  596. parent->update();
  597. }
  598. void Widget::move_to_back()
  599. {
  600. auto* parent = parent_widget();
  601. if (!parent)
  602. return;
  603. if (parent->children().size() == 1)
  604. return;
  605. parent->children().remove_first_matching([this](auto& entry) {
  606. return entry == this;
  607. });
  608. parent->children().prepend(*this);
  609. parent->update();
  610. }
  611. bool Widget::is_frontmost() const
  612. {
  613. auto* parent = parent_widget();
  614. if (!parent)
  615. return true;
  616. return &parent->children().last() == this;
  617. }
  618. bool Widget::is_backmost() const
  619. {
  620. auto* parent = parent_widget();
  621. if (!parent)
  622. return true;
  623. return &parent->children().first() == this;
  624. }
  625. Action* Widget::action_for_key_event(const KeyEvent& event)
  626. {
  627. Shortcut shortcut(event.modifiers(), (KeyCode)event.key());
  628. Action* found_action = nullptr;
  629. for_each_child_of_type<Action>([&](auto& action) {
  630. if (action.shortcut() == shortcut) {
  631. found_action = &action;
  632. return IterationDecision::Break;
  633. }
  634. return IterationDecision::Continue;
  635. });
  636. return found_action;
  637. }
  638. void Widget::set_updates_enabled(bool enabled)
  639. {
  640. if (m_updates_enabled == enabled)
  641. return;
  642. m_updates_enabled = enabled;
  643. if (enabled)
  644. update();
  645. }
  646. void Widget::focus_previous_widget(FocusSource source)
  647. {
  648. auto focusable_widgets = window()->focusable_widgets();
  649. for (int i = focusable_widgets.size() - 1; i >= 0; --i) {
  650. if (focusable_widgets[i] != this)
  651. continue;
  652. if (i > 0)
  653. focusable_widgets[i - 1]->set_focus(true, source);
  654. else
  655. focusable_widgets.last()->set_focus(true, source);
  656. }
  657. }
  658. void Widget::focus_next_widget(FocusSource source)
  659. {
  660. auto focusable_widgets = window()->focusable_widgets();
  661. for (size_t i = 0; i < focusable_widgets.size(); ++i) {
  662. if (focusable_widgets[i] != this)
  663. continue;
  664. if (i < focusable_widgets.size() - 1)
  665. focusable_widgets[i + 1]->set_focus(true, source);
  666. else
  667. focusable_widgets.first()->set_focus(true, source);
  668. }
  669. }
  670. void Widget::set_backcolor(const StringView& color_string)
  671. {
  672. auto color = Color::from_string(color_string);
  673. if (!color.has_value())
  674. return;
  675. set_background_color(color.value());
  676. }
  677. void Widget::set_forecolor(const StringView& color_string)
  678. {
  679. auto color = Color::from_string(color_string);
  680. if (!color.has_value())
  681. return;
  682. set_foreground_color(color.value());
  683. }
  684. void Widget::save_to(AK::JsonObject& json)
  685. {
  686. json.set("relative_rect", relative_rect().to_string());
  687. json.set("fill_with_background_color", fill_with_background_color());
  688. json.set("tooltip", tooltip());
  689. json.set("visible", is_visible());
  690. json.set("focused", is_focused());
  691. json.set("enabled", is_enabled());
  692. json.set("background_color", background_color().to_string());
  693. json.set("foreground_color", foreground_color().to_string());
  694. json.set("preferred_size", preferred_size().to_string());
  695. json.set("size_policy", String::format("[%s,%s]", to_string(horizontal_size_policy()), to_string(vertical_size_policy())));
  696. Core::Object::save_to(json);
  697. }
  698. bool Widget::set_property(const StringView& name, const JsonValue& value)
  699. {
  700. if (name == "fill_with_background_color") {
  701. set_fill_with_background_color(value.to_bool());
  702. return true;
  703. }
  704. if (name == "tooltip") {
  705. set_tooltip(value.to_string());
  706. return true;
  707. }
  708. if (name == "enable") {
  709. set_enabled(value.to_bool());
  710. return true;
  711. }
  712. if (name == "focused") {
  713. set_focus(value.to_bool());
  714. return true;
  715. }
  716. if (name == "visible") {
  717. set_visible(value.to_bool());
  718. return true;
  719. }
  720. return Core::Object::set_property(name, value);
  721. }
  722. Vector<Widget*> Widget::child_widgets() const
  723. {
  724. Vector<Widget*> widgets;
  725. widgets.ensure_capacity(children().size());
  726. for (auto& child : const_cast<Widget*>(this)->children()) {
  727. if (child.is_widget())
  728. widgets.append(static_cast<Widget*>(&child));
  729. }
  730. return widgets;
  731. }
  732. void Widget::set_palette(const Palette& palette)
  733. {
  734. m_palette = palette.impl();
  735. }
  736. void Widget::set_background_role(ColorRole role)
  737. {
  738. m_background_role = role;
  739. }
  740. void Widget::set_foreground_role(ColorRole role)
  741. {
  742. m_foreground_role = role;
  743. }
  744. Gfx::Palette Widget::palette() const
  745. {
  746. return Gfx::Palette(*m_palette);
  747. }
  748. void Widget::did_begin_inspection()
  749. {
  750. update();
  751. }
  752. void Widget::did_end_inspection()
  753. {
  754. update();
  755. }
  756. void Widget::set_content_margins(const Margins& margins)
  757. {
  758. if (m_content_margins == margins)
  759. return;
  760. m_content_margins = margins;
  761. invalidate_layout();
  762. }
  763. Gfx::IntRect Widget::content_rect() const
  764. {
  765. auto rect = relative_rect();
  766. rect.move_by(m_content_margins.left(), m_content_margins.top());
  767. rect.set_width(rect.width() - (m_content_margins.left() + m_content_margins.right()));
  768. rect.set_height(rect.height() - (m_content_margins.top() + m_content_margins.bottom()));
  769. return rect;
  770. }
  771. void Widget::set_tooltip(const StringView& tooltip)
  772. {
  773. m_tooltip = tooltip;
  774. if (GUI::Application::the()->tooltip_source_widget() == this)
  775. show_tooltip();
  776. }
  777. void Widget::show_tooltip()
  778. {
  779. if (has_tooltip())
  780. Application::the()->show_tooltip(m_tooltip, screen_relative_rect().center().translated(0, height() / 2), this);
  781. }
  782. Gfx::IntRect Widget::children_clip_rect() const
  783. {
  784. return rect();
  785. }
  786. void Widget::set_override_cursor(Gfx::StandardCursor cursor)
  787. {
  788. if (m_override_cursor == cursor)
  789. return;
  790. m_override_cursor = cursor;
  791. if (auto* window = this->window())
  792. window->update_cursor({});
  793. }
  794. bool Widget::load_from_json(const StringView& json_string)
  795. {
  796. auto json_value = JsonValue::from_string(json_string);
  797. if (!json_value.has_value()) {
  798. dbg() << "load_from_json parse failed: _" << json_string << "_";
  799. return false;
  800. }
  801. if (!json_value.value().is_object()) {
  802. dbg() << "load_from_json parse non-object";
  803. return false;
  804. }
  805. return load_from_json(json_value.value().as_object());
  806. }
  807. bool Widget::load_from_json(const JsonObject& json)
  808. {
  809. auto name = json.get("name");
  810. if (name.is_string())
  811. this->set_name(name.as_string());
  812. auto horizontal_size_policy = json.get("horizontal_size_policy");
  813. if (horizontal_size_policy.is_string()) {
  814. if (horizontal_size_policy.as_string() == "Fill")
  815. set_size_policy(Gfx::Orientation::Horizontal, SizePolicy::Fill);
  816. else if (horizontal_size_policy.as_string() == "Fixed")
  817. set_size_policy(Gfx::Orientation::Horizontal, SizePolicy::Fixed);
  818. }
  819. auto vertical_size_policy = json.get("vertical_size_policy");
  820. if (vertical_size_policy.is_string()) {
  821. if (vertical_size_policy.as_string() == "Fill")
  822. set_size_policy(Gfx::Orientation::Vertical, SizePolicy::Fill);
  823. else if (vertical_size_policy.as_string() == "Fixed")
  824. set_size_policy(Gfx::Orientation::Vertical, SizePolicy::Fixed);
  825. }
  826. auto fill_with_background_color = json.get("fill_with_background_color");
  827. if (fill_with_background_color.is_bool())
  828. set_fill_with_background_color(fill_with_background_color.to_bool());
  829. auto preferred_height = json.get("preferred_height");
  830. if (preferred_height.is_number())
  831. set_preferred_size(preferred_size().width(), preferred_height.to_i32());
  832. auto preferred_width = json.get("preferred_width");
  833. if (preferred_width.is_number())
  834. set_preferred_size(preferred_width.to_i32(), preferred_size().height());
  835. auto layout_value = json.get("layout");
  836. if (!layout_value.is_null() && !layout_value.is_object()) {
  837. dbg() << "layout is not an object";
  838. return false;
  839. }
  840. if (layout_value.is_object()) {
  841. auto& layout = layout_value.as_object();
  842. auto class_name = layout.get("class");
  843. if (class_name.is_null()) {
  844. dbg() << "Invalid layout class name";
  845. return false;
  846. }
  847. if (class_name.to_string() == "GUI::VerticalBoxLayout") {
  848. set_layout<GUI::VerticalBoxLayout>();
  849. } else if (class_name.to_string() == "GUI::HorizontalBoxLayout") {
  850. set_layout<GUI::HorizontalBoxLayout>();
  851. } else {
  852. dbg() << "Unknown layout class: '" << class_name.to_string() << "'";
  853. return false;
  854. }
  855. auto spacing = layout.get("spacing");
  856. if (spacing.is_number())
  857. this->layout()->set_spacing(spacing.to_i32());
  858. auto margins = layout.get("margins");
  859. if (margins.is_array()) {
  860. if (margins.as_array().size() != 4) {
  861. dbg() << "margins array needs 4 entries";
  862. return false;
  863. }
  864. int m[4];
  865. for (size_t i = 0; i < 4; ++i)
  866. m[i] = margins.as_array().at(i).to_i32();
  867. dbg() << "setting margins " << m[0] << "," << m[1] << "," << m[2] << "," << m[3];
  868. this->layout()->set_margins({ m[0], m[1], m[2], m[3] });
  869. }
  870. }
  871. auto children = json.get("children");
  872. if (children.is_array()) {
  873. for (auto& child_json_value : children.as_array().values()) {
  874. if (!child_json_value.is_object())
  875. return false;
  876. auto& child_json = child_json_value.as_object();
  877. auto class_name = child_json.get("class");
  878. if (!class_name.is_string()) {
  879. dbg() << "No class name in entry";
  880. return false;
  881. }
  882. auto* registration = WidgetClassRegistration::find(class_name.as_string());
  883. if (!registration) {
  884. dbg() << "Class '" << class_name.as_string() << "' not registered";
  885. return false;
  886. }
  887. auto child_widget = registration->construct();
  888. add_child(*child_widget);
  889. child_widget->load_from_json(child_json);
  890. }
  891. }
  892. return true;
  893. }
  894. Widget* Widget::find_child_by_name(const String& name)
  895. {
  896. Widget* found_widget = nullptr;
  897. for_each_child_widget([&](auto& child) {
  898. if (child.name() == name) {
  899. found_widget = &child;
  900. return IterationDecision::Break;
  901. }
  902. return IterationDecision::Continue;
  903. });
  904. return found_widget;
  905. }
  906. Widget* Widget::find_descendant_by_name(const String& name)
  907. {
  908. Widget* found_widget = nullptr;
  909. if (this->name() == name)
  910. return this;
  911. for_each_child_widget([&](auto& child) {
  912. found_widget = child.find_descendant_by_name(name);
  913. if (found_widget)
  914. return IterationDecision::Break;
  915. return IterationDecision::Continue;
  916. });
  917. return found_widget;
  918. }
  919. }