Widget.cpp 26 KB

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