Widget.cpp 29 KB

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