Widget.cpp 26 KB

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