Widget.cpp 28 KB

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