Widget.cpp 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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 (((unsigned)focus_policy() & (unsigned)FocusPolicy::ClickFocus))
  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. if (auto* window = this->window())
  316. window->update_cursor({});
  317. show_tooltip();
  318. enter_event(event);
  319. }
  320. void Widget::handle_leave_event(Core::Event& event)
  321. {
  322. if (auto* window = this->window())
  323. window->update_cursor({});
  324. Application::the()->hide_tooltip();
  325. leave_event(event);
  326. }
  327. void Widget::doubleclick_event(MouseEvent&)
  328. {
  329. }
  330. void Widget::resize_event(ResizeEvent&)
  331. {
  332. }
  333. void Widget::paint_event(PaintEvent&)
  334. {
  335. }
  336. void Widget::second_paint_event(PaintEvent&)
  337. {
  338. }
  339. void Widget::show_event(ShowEvent&)
  340. {
  341. }
  342. void Widget::hide_event(HideEvent&)
  343. {
  344. }
  345. void Widget::keydown_event(KeyEvent& event)
  346. {
  347. if (!event.alt() && !event.ctrl() && !event.logo() && event.key() == KeyCode::Key_Tab) {
  348. if (event.shift())
  349. focus_previous_widget(FocusSource::Keyboard);
  350. else
  351. focus_next_widget(FocusSource::Keyboard);
  352. event.accept();
  353. return;
  354. }
  355. event.ignore();
  356. }
  357. void Widget::keyup_event(KeyEvent& event)
  358. {
  359. event.ignore();
  360. }
  361. void Widget::mousedown_event(MouseEvent&)
  362. {
  363. }
  364. void Widget::mouseup_event(MouseEvent&)
  365. {
  366. }
  367. void Widget::mousemove_event(MouseEvent&)
  368. {
  369. }
  370. void Widget::mousewheel_event(MouseEvent&)
  371. {
  372. }
  373. void Widget::context_menu_event(ContextMenuEvent&)
  374. {
  375. }
  376. void Widget::focusin_event(FocusEvent&)
  377. {
  378. }
  379. void Widget::focusout_event(FocusEvent&)
  380. {
  381. }
  382. void Widget::enter_event(Core::Event&)
  383. {
  384. }
  385. void Widget::leave_event(Core::Event&)
  386. {
  387. }
  388. void Widget::change_event(Event&)
  389. {
  390. }
  391. void Widget::drag_move_event(DragEvent& event)
  392. {
  393. dbg() << class_name() << "{" << this << "} DRAG MOVE position: " << event.position() << ", data_type: '" << event.data_type() << "'";
  394. event.ignore();
  395. }
  396. void Widget::drop_event(DropEvent& event)
  397. {
  398. dbg() << class_name() << "{" << this << "} DROP position: " << event.position() << ", text: '" << event.text() << "'";
  399. event.ignore();
  400. }
  401. void Widget::theme_change_event(ThemeChangeEvent&)
  402. {
  403. }
  404. void Widget::update()
  405. {
  406. if (rect().is_empty())
  407. return;
  408. update(rect());
  409. }
  410. void Widget::update(const Gfx::IntRect& rect)
  411. {
  412. if (!is_visible())
  413. return;
  414. if (!updates_enabled())
  415. return;
  416. Window* window = m_window;
  417. Widget* parent = parent_widget();
  418. while (parent) {
  419. if (!parent->updates_enabled())
  420. return;
  421. window = parent->m_window;
  422. parent = parent->parent_widget();
  423. }
  424. if (window)
  425. window->update(rect.translated(window_relative_rect().location()));
  426. }
  427. Gfx::IntRect Widget::window_relative_rect() const
  428. {
  429. auto rect = relative_rect();
  430. for (auto* parent = parent_widget(); parent; parent = parent->parent_widget()) {
  431. rect.move_by(parent->relative_position());
  432. }
  433. return rect;
  434. }
  435. Gfx::IntRect Widget::screen_relative_rect() const
  436. {
  437. auto window_position = window()->window_type() == WindowType::MenuApplet
  438. ? window()->rect_in_menubar().location()
  439. : window()->rect().location();
  440. return window_relative_rect().translated(window_position);
  441. }
  442. Widget* Widget::child_at(const Gfx::IntPoint& point) const
  443. {
  444. for (int i = children().size() - 1; i >= 0; --i) {
  445. if (!is<Widget>(children()[i]))
  446. continue;
  447. auto& child = downcast<Widget>(children()[i]);
  448. if (!child.is_visible())
  449. continue;
  450. if (child.content_rect().contains(point))
  451. return const_cast<Widget*>(&child);
  452. }
  453. return nullptr;
  454. }
  455. Widget::HitTestResult Widget::hit_test(const Gfx::IntPoint& position, ShouldRespectGreediness should_respect_greediness)
  456. {
  457. if (should_respect_greediness == ShouldRespectGreediness::Yes && is_greedy_for_hits())
  458. return { this, position };
  459. if (auto* child = child_at(position))
  460. return child->hit_test(position - child->relative_position());
  461. return { this, position };
  462. }
  463. void Widget::set_window(Window* window)
  464. {
  465. if (m_window == window)
  466. return;
  467. m_window = window;
  468. }
  469. void Widget::set_focus_proxy(Widget* proxy)
  470. {
  471. if (m_focus_proxy == proxy)
  472. return;
  473. if (proxy)
  474. m_focus_proxy = proxy->make_weak_ptr();
  475. else
  476. m_focus_proxy = nullptr;
  477. }
  478. FocusPolicy Widget::focus_policy() const
  479. {
  480. if (m_focus_proxy)
  481. return m_focus_proxy->focus_policy();
  482. return m_focus_policy;
  483. }
  484. void Widget::set_focus_policy(FocusPolicy policy)
  485. {
  486. if (m_focus_proxy)
  487. return m_focus_proxy->set_focus_policy(policy);
  488. m_focus_policy = policy;
  489. }
  490. bool Widget::is_focused() const
  491. {
  492. if (m_focus_proxy)
  493. return m_focus_proxy->is_focused();
  494. auto* win = window();
  495. if (!win)
  496. return false;
  497. // Accessory windows are not active despite being the active
  498. // input window. So we can have focus if either we're the active
  499. // input window or we're the active window
  500. if (win->is_active_input() || win->is_active())
  501. return win->focused_widget() == this;
  502. return false;
  503. }
  504. void Widget::set_focus(bool focus, FocusSource source)
  505. {
  506. if (m_focus_proxy)
  507. return m_focus_proxy->set_focus(focus, source);
  508. auto* win = window();
  509. if (!win)
  510. return;
  511. if (focus) {
  512. win->set_focused_widget(this, source);
  513. } else {
  514. if (win->focused_widget() == this)
  515. win->set_focused_widget(nullptr, source);
  516. }
  517. }
  518. void Widget::set_font(const Gfx::Font* font)
  519. {
  520. if (m_font.ptr() == font)
  521. return;
  522. if (!font)
  523. m_font = Gfx::Font::default_font();
  524. else
  525. m_font = *font;
  526. did_change_font();
  527. update();
  528. }
  529. void Widget::set_global_cursor_tracking(bool enabled)
  530. {
  531. auto* win = window();
  532. if (!win)
  533. return;
  534. win->set_global_cursor_tracking_widget(enabled ? this : nullptr);
  535. }
  536. bool Widget::global_cursor_tracking() const
  537. {
  538. auto* win = window();
  539. if (!win)
  540. return false;
  541. return win->global_cursor_tracking_widget() == this;
  542. }
  543. void Widget::set_preferred_size(const Gfx::IntSize& size)
  544. {
  545. if (m_preferred_size == size)
  546. return;
  547. m_preferred_size = size;
  548. invalidate_layout();
  549. }
  550. void Widget::set_size_policy(Orientation orientation, SizePolicy policy)
  551. {
  552. if (orientation == Orientation::Horizontal)
  553. set_size_policy(policy, m_vertical_size_policy);
  554. else
  555. set_size_policy(m_horizontal_size_policy, policy);
  556. }
  557. void Widget::set_size_policy(SizePolicy horizontal_policy, SizePolicy vertical_policy)
  558. {
  559. if (m_horizontal_size_policy == horizontal_policy && m_vertical_size_policy == vertical_policy)
  560. return;
  561. m_horizontal_size_policy = horizontal_policy;
  562. m_vertical_size_policy = vertical_policy;
  563. invalidate_layout();
  564. }
  565. void Widget::invalidate_layout()
  566. {
  567. if (window())
  568. window()->schedule_relayout();
  569. }
  570. void Widget::set_visible(bool visible)
  571. {
  572. if (visible == m_visible)
  573. return;
  574. m_visible = visible;
  575. if (auto* parent = parent_widget())
  576. parent->invalidate_layout();
  577. if (m_visible)
  578. update();
  579. if (m_visible) {
  580. ShowEvent e;
  581. event(e);
  582. } else {
  583. HideEvent e;
  584. event(e);
  585. }
  586. }
  587. bool Widget::spans_entire_window_horizontally() const
  588. {
  589. auto* w = window();
  590. if (!w)
  591. return false;
  592. auto* main_widget = w->main_widget();
  593. if (!main_widget)
  594. return false;
  595. if (main_widget == this)
  596. return true;
  597. auto wrr = window_relative_rect();
  598. return wrr.left() == main_widget->rect().left() && wrr.right() == main_widget->rect().right();
  599. }
  600. void Widget::set_enabled(bool enabled)
  601. {
  602. if (m_enabled == enabled)
  603. return;
  604. m_enabled = enabled;
  605. for_each_child_widget([enabled](auto& child) {
  606. child.set_enabled(enabled);
  607. return IterationDecision::Continue;
  608. });
  609. Event e(Event::EnabledChange);
  610. event(e);
  611. update();
  612. }
  613. void Widget::move_to_front()
  614. {
  615. auto* parent = parent_widget();
  616. if (!parent)
  617. return;
  618. if (parent->children().size() == 1)
  619. return;
  620. parent->children().remove_first_matching([this](auto& entry) {
  621. return entry == this;
  622. });
  623. parent->children().append(*this);
  624. parent->update();
  625. }
  626. void Widget::move_to_back()
  627. {
  628. auto* parent = parent_widget();
  629. if (!parent)
  630. return;
  631. if (parent->children().size() == 1)
  632. return;
  633. parent->children().remove_first_matching([this](auto& entry) {
  634. return entry == this;
  635. });
  636. parent->children().prepend(*this);
  637. parent->update();
  638. }
  639. bool Widget::is_frontmost() const
  640. {
  641. auto* parent = parent_widget();
  642. if (!parent)
  643. return true;
  644. return &parent->children().last() == this;
  645. }
  646. bool Widget::is_backmost() const
  647. {
  648. auto* parent = parent_widget();
  649. if (!parent)
  650. return true;
  651. return &parent->children().first() == this;
  652. }
  653. Action* Widget::action_for_key_event(const KeyEvent& event)
  654. {
  655. Shortcut shortcut(event.modifiers(), (KeyCode)event.key());
  656. Action* found_action = nullptr;
  657. for_each_child_of_type<Action>([&](auto& action) {
  658. if (action.shortcut() == shortcut) {
  659. found_action = &action;
  660. return IterationDecision::Break;
  661. }
  662. return IterationDecision::Continue;
  663. });
  664. return found_action;
  665. }
  666. void Widget::set_updates_enabled(bool enabled)
  667. {
  668. if (m_updates_enabled == enabled)
  669. return;
  670. m_updates_enabled = enabled;
  671. if (enabled)
  672. update();
  673. }
  674. void Widget::focus_previous_widget(FocusSource source)
  675. {
  676. auto focusable_widgets = window()->focusable_widgets(source);
  677. for (int i = focusable_widgets.size() - 1; i >= 0; --i) {
  678. if (focusable_widgets[i] != this)
  679. continue;
  680. if (i > 0)
  681. focusable_widgets[i - 1]->set_focus(true, source);
  682. else
  683. focusable_widgets.last()->set_focus(true, source);
  684. }
  685. }
  686. void Widget::focus_next_widget(FocusSource source)
  687. {
  688. auto focusable_widgets = window()->focusable_widgets(source);
  689. for (size_t i = 0; i < focusable_widgets.size(); ++i) {
  690. if (focusable_widgets[i] != this)
  691. continue;
  692. if (i < focusable_widgets.size() - 1)
  693. focusable_widgets[i + 1]->set_focus(true, source);
  694. else
  695. focusable_widgets.first()->set_focus(true, source);
  696. }
  697. }
  698. void Widget::set_backcolor(const StringView& color_string)
  699. {
  700. auto color = Color::from_string(color_string);
  701. if (!color.has_value())
  702. return;
  703. set_background_color(color.value());
  704. }
  705. void Widget::set_forecolor(const StringView& color_string)
  706. {
  707. auto color = Color::from_string(color_string);
  708. if (!color.has_value())
  709. return;
  710. set_foreground_color(color.value());
  711. }
  712. Vector<Widget*> Widget::child_widgets() const
  713. {
  714. Vector<Widget*> widgets;
  715. widgets.ensure_capacity(children().size());
  716. for (auto& child : const_cast<Widget*>(this)->children()) {
  717. if (child.is_widget())
  718. widgets.append(static_cast<Widget*>(&child));
  719. }
  720. return widgets;
  721. }
  722. void Widget::set_palette(const Palette& palette)
  723. {
  724. m_palette = palette.impl();
  725. }
  726. void Widget::set_background_role(ColorRole role)
  727. {
  728. m_background_role = role;
  729. }
  730. void Widget::set_foreground_role(ColorRole role)
  731. {
  732. m_foreground_role = role;
  733. }
  734. Gfx::Palette Widget::palette() const
  735. {
  736. return Gfx::Palette(*m_palette);
  737. }
  738. void Widget::did_begin_inspection()
  739. {
  740. update();
  741. }
  742. void Widget::did_end_inspection()
  743. {
  744. update();
  745. }
  746. void Widget::set_content_margins(const Margins& margins)
  747. {
  748. if (m_content_margins == margins)
  749. return;
  750. m_content_margins = margins;
  751. invalidate_layout();
  752. }
  753. Gfx::IntRect Widget::content_rect() const
  754. {
  755. auto rect = relative_rect();
  756. rect.move_by(m_content_margins.left(), m_content_margins.top());
  757. rect.set_width(rect.width() - (m_content_margins.left() + m_content_margins.right()));
  758. rect.set_height(rect.height() - (m_content_margins.top() + m_content_margins.bottom()));
  759. return rect;
  760. }
  761. void Widget::set_tooltip(const StringView& tooltip)
  762. {
  763. m_tooltip = tooltip;
  764. if (GUI::Application::the()->tooltip_source_widget() == this)
  765. show_tooltip();
  766. }
  767. void Widget::show_tooltip()
  768. {
  769. if (has_tooltip())
  770. Application::the()->show_tooltip(m_tooltip, screen_relative_rect().center().translated(0, height() / 2), this);
  771. }
  772. Gfx::IntRect Widget::children_clip_rect() const
  773. {
  774. return rect();
  775. }
  776. void Widget::set_override_cursor(Gfx::StandardCursor cursor)
  777. {
  778. if (m_override_cursor == cursor)
  779. return;
  780. m_override_cursor = cursor;
  781. if (auto* window = this->window())
  782. window->update_cursor({});
  783. }
  784. bool Widget::load_from_json(const StringView& json_string)
  785. {
  786. auto json_value = JsonValue::from_string(json_string);
  787. if (!json_value.has_value()) {
  788. dbg() << "load_from_json parse failed: _" << json_string << "_";
  789. return false;
  790. }
  791. if (!json_value.value().is_object()) {
  792. dbg() << "load_from_json parse non-object";
  793. return false;
  794. }
  795. return load_from_json(json_value.value().as_object());
  796. }
  797. bool Widget::load_from_json(const JsonObject& json)
  798. {
  799. json.for_each_member([&](auto& key, auto& value) {
  800. set_property(key, value);
  801. });
  802. auto layout_value = json.get("layout");
  803. if (!layout_value.is_null() && !layout_value.is_object()) {
  804. dbg() << "layout is not an object";
  805. return false;
  806. }
  807. if (layout_value.is_object()) {
  808. auto& layout = layout_value.as_object();
  809. auto class_name = layout.get("class");
  810. if (class_name.is_null()) {
  811. dbg() << "Invalid layout class name";
  812. return false;
  813. }
  814. if (class_name.to_string() == "GUI::VerticalBoxLayout") {
  815. set_layout<GUI::VerticalBoxLayout>();
  816. } else if (class_name.to_string() == "GUI::HorizontalBoxLayout") {
  817. set_layout<GUI::HorizontalBoxLayout>();
  818. } else {
  819. dbg() << "Unknown layout class: '" << class_name.to_string() << "'";
  820. return false;
  821. }
  822. layout.for_each_member([&](auto& key, auto& value) {
  823. this->layout()->set_property(key, value);
  824. });
  825. }
  826. auto children = json.get("children");
  827. if (children.is_array()) {
  828. for (auto& child_json_value : children.as_array().values()) {
  829. if (!child_json_value.is_object())
  830. return false;
  831. auto& child_json = child_json_value.as_object();
  832. auto class_name = child_json.get("class");
  833. if (!class_name.is_string()) {
  834. dbg() << "No class name in entry";
  835. return false;
  836. }
  837. auto* registration = WidgetClassRegistration::find(class_name.as_string());
  838. if (!registration) {
  839. dbg() << "Class '" << class_name.as_string() << "' not registered";
  840. return false;
  841. }
  842. auto child_widget = registration->construct();
  843. add_child(*child_widget);
  844. child_widget->load_from_json(child_json);
  845. }
  846. }
  847. return true;
  848. }
  849. Widget* Widget::find_child_by_name(const String& name)
  850. {
  851. Widget* found_widget = nullptr;
  852. for_each_child_widget([&](auto& child) {
  853. if (child.name() == name) {
  854. found_widget = &child;
  855. return IterationDecision::Break;
  856. }
  857. return IterationDecision::Continue;
  858. });
  859. return found_widget;
  860. }
  861. Widget* Widget::find_descendant_by_name(const String& name)
  862. {
  863. Widget* found_widget = nullptr;
  864. if (this->name() == name)
  865. return this;
  866. for_each_child_widget([&](auto& child) {
  867. found_widget = child.find_descendant_by_name(name);
  868. if (found_widget)
  869. return IterationDecision::Break;
  870. return IterationDecision::Continue;
  871. });
  872. return found_widget;
  873. }
  874. bool Widget::has_focus_within() const
  875. {
  876. auto* window = this->window();
  877. if (!window)
  878. return false;
  879. if (!window->focused_widget())
  880. return false;
  881. auto& effective_focus_widget = focus_proxy() ? *focus_proxy() : *this;
  882. return window->focused_widget() == &effective_focus_widget || is_ancestor_of(*window->focused_widget());
  883. }
  884. }