Widget.cpp 28 KB

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