Widget.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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_STRING_PROPERTY("font", m_font->family, set_font_family);
  100. REGISTER_INT_PROPERTY("font_size", m_font->presentation_size, set_font_size);
  101. REGISTER_FONT_WEIGHT_PROPERTY("font_weight", m_font->weight, set_font_weight);
  102. register_property(
  103. "font_type", [this] { return m_font->is_fixed_width() ? "FixedWidth" : "Normal"; },
  104. [this](auto& value) {
  105. if (value.to_string() == "FixedWidth") {
  106. set_font_fixed_width(true);
  107. return true;
  108. }
  109. if (value.to_string() == "Normal") {
  110. set_font_fixed_width(false);
  111. return true;
  112. }
  113. return false;
  114. });
  115. register_property(
  116. "focus_policy", [this]() -> JsonValue {
  117. auto policy = focus_policy();
  118. if (policy == GUI::FocusPolicy::ClickFocus)
  119. return "ClickFocus";
  120. if (policy == GUI::FocusPolicy::NoFocus)
  121. return "NoFocus";
  122. if (policy == GUI::FocusPolicy::TabFocus)
  123. return "TabFocus";
  124. if (policy == GUI::FocusPolicy::StrongFocus)
  125. return "StrongFocus";
  126. return JsonValue(); },
  127. [this](auto& value) {
  128. if (!value.is_string())
  129. return false;
  130. if (value.as_string() == "ClickFocus") {
  131. set_focus_policy(GUI::FocusPolicy::ClickFocus);
  132. return true;
  133. }
  134. if (value.as_string() == "NoFocus") {
  135. set_focus_policy(GUI::FocusPolicy::NoFocus);
  136. return true;
  137. }
  138. if (value.as_string() == "TabFocus") {
  139. set_focus_policy(GUI::FocusPolicy::TabFocus);
  140. return true;
  141. }
  142. if (value.as_string() == "StrongFocus") {
  143. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  144. return true;
  145. }
  146. return false;
  147. });
  148. register_property(
  149. "foreground_color", [this]() -> JsonValue { return palette().color(foreground_role()).to_string(); },
  150. [this](auto& value) {
  151. auto c = Color::from_string(value.to_string());
  152. if (c.has_value()) {
  153. auto _palette = palette();
  154. _palette.set_color(foreground_role(), c.value());
  155. set_palette(_palette);
  156. return true;
  157. }
  158. return false;
  159. });
  160. register_property(
  161. "background_color", [this]() -> JsonValue { return palette().color(background_role()).to_string(); },
  162. [this](auto& value) {
  163. auto c = Color::from_string(value.to_string());
  164. if (c.has_value()) {
  165. auto _palette = palette();
  166. _palette.set_color(background_role(), c.value());
  167. set_palette(_palette);
  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() && is<Widget>(event.insertion_before_child()))
  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 handle_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::DragEnter:
  267. return drag_enter_event(static_cast<DragEvent&>(event));
  268. case Event::DragMove:
  269. return drag_move_event(static_cast<DragEvent&>(event));
  270. case Event::DragLeave:
  271. return drag_leave_event(static_cast<Event&>(event));
  272. case Event::Drop:
  273. return drop_event(static_cast<DropEvent&>(event));
  274. case Event::ThemeChange:
  275. return theme_change_event(static_cast<ThemeChangeEvent&>(event));
  276. case Event::Enter:
  277. return handle_enter_event(event);
  278. case Event::Leave:
  279. return handle_leave_event(event);
  280. case Event::EnabledChange:
  281. return change_event(static_cast<Event&>(event));
  282. default:
  283. return Core::Object::event(event);
  284. }
  285. }
  286. void Widget::handle_keydown_event(KeyEvent& event)
  287. {
  288. keydown_event(event);
  289. if (event.key() == KeyCode::Key_Menu) {
  290. ContextMenuEvent c_event(window_relative_rect().bottom_right(), screen_relative_rect().bottom_right());
  291. context_menu_event(c_event);
  292. }
  293. }
  294. void Widget::handle_paint_event(PaintEvent& event)
  295. {
  296. VERIFY(is_visible());
  297. if (fill_with_background_color()) {
  298. Painter painter(*this);
  299. painter.fill_rect(event.rect(), palette().color(background_role()));
  300. }
  301. paint_event(event);
  302. auto children_clip_rect = this->children_clip_rect();
  303. for_each_child_widget([&](auto& child) {
  304. if (!child.is_visible())
  305. return IterationDecision::Continue;
  306. if (child.relative_rect().intersects(event.rect())) {
  307. PaintEvent local_event(event.rect().intersected(children_clip_rect).intersected(child.relative_rect()).translated(-child.relative_position()));
  308. child.dispatch_event(local_event, this);
  309. }
  310. return IterationDecision::Continue;
  311. });
  312. second_paint_event(event);
  313. auto* app = Application::the();
  314. if (app && app->dnd_debugging_enabled() && has_pending_drop()) {
  315. Painter painter(*this);
  316. painter.draw_rect(rect(), Color::Blue);
  317. }
  318. if (app && app->focus_debugging_enabled() && is_focused()) {
  319. Painter painter(*this);
  320. painter.draw_rect(rect(), Color::Cyan);
  321. }
  322. if (is_being_inspected()) {
  323. Painter painter(*this);
  324. painter.draw_rect(rect(), Color::Magenta);
  325. }
  326. }
  327. void Widget::set_layout(NonnullRefPtr<Layout> layout)
  328. {
  329. if (m_layout) {
  330. m_layout->notify_disowned({}, *this);
  331. m_layout->remove_from_parent();
  332. }
  333. m_layout = move(layout);
  334. if (m_layout) {
  335. add_child(*m_layout);
  336. m_layout->notify_adopted({}, *this);
  337. do_layout();
  338. } else {
  339. update();
  340. }
  341. }
  342. void Widget::do_layout()
  343. {
  344. for_each_child_widget([&](auto& child) {
  345. child.do_layout();
  346. return IterationDecision::Continue;
  347. });
  348. custom_layout();
  349. if (!m_layout)
  350. return;
  351. m_layout->run(*this);
  352. did_layout();
  353. update();
  354. }
  355. void Widget::notify_layout_changed(Badge<Layout>)
  356. {
  357. invalidate_layout();
  358. }
  359. void Widget::handle_resize_event(ResizeEvent& event)
  360. {
  361. resize_event(event);
  362. do_layout();
  363. }
  364. void Widget::handle_mouseup_event(MouseEvent& event)
  365. {
  366. mouseup_event(event);
  367. }
  368. void Widget::handle_mousedown_event(MouseEvent& event)
  369. {
  370. if (has_flag(focus_policy(), FocusPolicy::ClickFocus))
  371. set_focus(true, FocusSource::Mouse);
  372. mousedown_event(event);
  373. if (event.button() == MouseButton::Right) {
  374. ContextMenuEvent c_event(event.position(), screen_relative_rect().location().translated(event.position()));
  375. context_menu_event(c_event);
  376. }
  377. }
  378. void Widget::handle_mousedoubleclick_event(MouseEvent& event)
  379. {
  380. doubleclick_event(event);
  381. }
  382. void Widget::handle_enter_event(Core::Event& event)
  383. {
  384. if (auto* window = this->window())
  385. window->update_cursor({});
  386. show_or_hide_tooltip();
  387. enter_event(event);
  388. }
  389. void Widget::handle_leave_event(Core::Event& event)
  390. {
  391. if (auto* window = this->window())
  392. window->update_cursor({});
  393. Application::the()->hide_tooltip();
  394. leave_event(event);
  395. }
  396. void Widget::doubleclick_event(MouseEvent&)
  397. {
  398. }
  399. void Widget::resize_event(ResizeEvent&)
  400. {
  401. }
  402. void Widget::paint_event(PaintEvent&)
  403. {
  404. }
  405. void Widget::second_paint_event(PaintEvent&)
  406. {
  407. }
  408. void Widget::show_event(ShowEvent&)
  409. {
  410. }
  411. void Widget::hide_event(HideEvent&)
  412. {
  413. }
  414. void Widget::keydown_event(KeyEvent& event)
  415. {
  416. if (!event.alt() && !event.ctrl() && !event.super()) {
  417. if (event.key() == KeyCode::Key_Tab) {
  418. if (event.shift())
  419. focus_previous_widget(FocusSource::Keyboard, false);
  420. else
  421. focus_next_widget(FocusSource::Keyboard, false);
  422. event.accept();
  423. return;
  424. }
  425. if (!event.shift() && (event.key() == KeyCode::Key_Left || event.key() == KeyCode::Key_Up)) {
  426. focus_previous_widget(FocusSource::Keyboard, true);
  427. event.accept();
  428. return;
  429. }
  430. if (!event.shift() && (event.key() == KeyCode::Key_Right || event.key() == KeyCode::Key_Down)) {
  431. focus_next_widget(FocusSource::Keyboard, true);
  432. event.accept();
  433. return;
  434. }
  435. }
  436. event.ignore();
  437. }
  438. void Widget::keyup_event(KeyEvent& event)
  439. {
  440. event.ignore();
  441. }
  442. void Widget::mousedown_event(MouseEvent&)
  443. {
  444. }
  445. void Widget::mouseup_event(MouseEvent&)
  446. {
  447. }
  448. void Widget::mousemove_event(MouseEvent&)
  449. {
  450. }
  451. void Widget::mousewheel_event(MouseEvent&)
  452. {
  453. }
  454. void Widget::context_menu_event(ContextMenuEvent&)
  455. {
  456. }
  457. void Widget::focusin_event(FocusEvent&)
  458. {
  459. }
  460. void Widget::focusout_event(FocusEvent&)
  461. {
  462. }
  463. void Widget::enter_event(Core::Event&)
  464. {
  465. }
  466. void Widget::leave_event(Core::Event&)
  467. {
  468. }
  469. void Widget::change_event(Event&)
  470. {
  471. }
  472. void Widget::drag_move_event(DragEvent&)
  473. {
  474. }
  475. void Widget::drag_enter_event(DragEvent& event)
  476. {
  477. StringBuilder builder;
  478. builder.join(',', event.mime_types());
  479. dbgln("{} {:p} DRAG ENTER @ {}, {}", class_name(), this, event.position(), builder.string_view());
  480. }
  481. void Widget::drag_leave_event(Event&)
  482. {
  483. dbgln("{} {:p} DRAG LEAVE", class_name(), this);
  484. }
  485. void Widget::drop_event(DropEvent& event)
  486. {
  487. dbgln("{} {:p} DROP @ {}, '{}'", class_name(), this, event.position(), event.text());
  488. }
  489. void Widget::theme_change_event(ThemeChangeEvent&)
  490. {
  491. }
  492. void Widget::screen_rect_change_event(ScreenRectChangeEvent&)
  493. {
  494. }
  495. void Widget::update()
  496. {
  497. if (rect().is_empty())
  498. return;
  499. update(rect());
  500. }
  501. void Widget::update(const Gfx::IntRect& rect)
  502. {
  503. if (!is_visible())
  504. return;
  505. if (!updates_enabled())
  506. return;
  507. auto bound_by_widget = rect.intersected(this->rect());
  508. if (bound_by_widget.is_empty())
  509. return;
  510. Window* window = m_window;
  511. Widget* parent = parent_widget();
  512. while (parent) {
  513. if (!parent->updates_enabled())
  514. return;
  515. window = parent->m_window;
  516. parent = parent->parent_widget();
  517. }
  518. if (window)
  519. window->update(bound_by_widget.translated(window_relative_rect().location()));
  520. }
  521. Gfx::IntRect Widget::window_relative_rect() const
  522. {
  523. auto rect = relative_rect();
  524. for (auto* parent = parent_widget(); parent; parent = parent->parent_widget()) {
  525. rect.move_by(parent->relative_position());
  526. }
  527. return rect;
  528. }
  529. Gfx::IntRect Widget::screen_relative_rect() const
  530. {
  531. auto window_position = window()->window_type() == WindowType::Applet
  532. ? window()->applet_rect_on_screen().location()
  533. : window()->rect().location();
  534. return window_relative_rect().translated(window_position);
  535. }
  536. Widget* Widget::child_at(const Gfx::IntPoint& point) const
  537. {
  538. for (int i = children().size() - 1; i >= 0; --i) {
  539. if (!is<Widget>(children()[i]))
  540. continue;
  541. auto& child = downcast<Widget>(children()[i]);
  542. if (!child.is_visible())
  543. continue;
  544. if (child.content_rect().contains(point))
  545. return const_cast<Widget*>(&child);
  546. }
  547. return nullptr;
  548. }
  549. Widget::HitTestResult Widget::hit_test(const Gfx::IntPoint& position, ShouldRespectGreediness should_respect_greediness)
  550. {
  551. if (should_respect_greediness == ShouldRespectGreediness::Yes && is_greedy_for_hits())
  552. return { this, position };
  553. if (auto* child = child_at(position))
  554. return child->hit_test(position - child->relative_position());
  555. return { this, position };
  556. }
  557. void Widget::set_window(Window* window)
  558. {
  559. if (m_window == window)
  560. return;
  561. m_window = window;
  562. }
  563. void Widget::set_focus_proxy(Widget* proxy)
  564. {
  565. if (m_focus_proxy == proxy)
  566. return;
  567. m_focus_proxy = proxy;
  568. }
  569. FocusPolicy Widget::focus_policy() const
  570. {
  571. if (m_focus_proxy)
  572. return m_focus_proxy->focus_policy();
  573. return m_focus_policy;
  574. }
  575. void Widget::set_focus_policy(FocusPolicy policy)
  576. {
  577. if (m_focus_proxy)
  578. return m_focus_proxy->set_focus_policy(policy);
  579. m_focus_policy = policy;
  580. }
  581. bool Widget::is_focused() const
  582. {
  583. if (m_focus_proxy)
  584. return m_focus_proxy->is_focused();
  585. auto* win = window();
  586. if (!win)
  587. return false;
  588. // Accessory windows are not active despite being the active
  589. // input window. So we can have focus if either we're the active
  590. // input window or we're the active window
  591. if (win->is_active_input() || win->is_active())
  592. return win->focused_widget() == this;
  593. return false;
  594. }
  595. void Widget::set_focus(bool focus, FocusSource source)
  596. {
  597. if (m_focus_proxy)
  598. return m_focus_proxy->set_focus(focus, source);
  599. auto* win = window();
  600. if (!win)
  601. return;
  602. if (focus) {
  603. win->set_focused_widget(this, source);
  604. } else {
  605. if (win->focused_widget() == this)
  606. win->set_focused_widget(nullptr, source);
  607. }
  608. }
  609. void Widget::set_font(const Gfx::Font* font)
  610. {
  611. if (m_font.ptr() == font)
  612. return;
  613. if (!font)
  614. m_font = Gfx::FontDatabase::default_font();
  615. else
  616. m_font = *font;
  617. did_change_font();
  618. update();
  619. }
  620. void Widget::set_font_family(const String& family)
  621. {
  622. set_font(Gfx::FontDatabase::the().get(family, m_font->presentation_size(), m_font->weight()));
  623. }
  624. void Widget::set_font_size(unsigned size)
  625. {
  626. set_font(Gfx::FontDatabase::the().get(m_font->family(), size, m_font->weight()));
  627. }
  628. void Widget::set_font_weight(unsigned weight)
  629. {
  630. set_font(Gfx::FontDatabase::the().get(m_font->family(), m_font->presentation_size(), weight));
  631. }
  632. void Widget::set_font_fixed_width(bool fixed_width)
  633. {
  634. if (fixed_width)
  635. set_font(Gfx::FontDatabase::the().get(Gfx::FontDatabase::the().default_fixed_width_font().family(), m_font->presentation_size(), m_font->weight()));
  636. else
  637. set_font(Gfx::FontDatabase::the().get(Gfx::FontDatabase::the().default_font().family(), m_font->presentation_size(), m_font->weight()));
  638. }
  639. void Widget::set_global_cursor_tracking(bool enabled)
  640. {
  641. auto* win = window();
  642. if (!win)
  643. return;
  644. win->set_global_cursor_tracking_widget(enabled ? this : nullptr);
  645. }
  646. bool Widget::global_cursor_tracking() const
  647. {
  648. auto* win = window();
  649. if (!win)
  650. return false;
  651. return win->global_cursor_tracking_widget() == this;
  652. }
  653. void Widget::set_min_size(const Gfx::IntSize& size)
  654. {
  655. if (m_min_size == size)
  656. return;
  657. m_min_size = size;
  658. invalidate_layout();
  659. }
  660. void Widget::set_max_size(const Gfx::IntSize& size)
  661. {
  662. if (m_max_size == size)
  663. return;
  664. m_max_size = size;
  665. invalidate_layout();
  666. }
  667. void Widget::invalidate_layout()
  668. {
  669. if (window())
  670. window()->schedule_relayout();
  671. }
  672. void Widget::set_visible(bool visible)
  673. {
  674. if (visible == m_visible)
  675. return;
  676. m_visible = visible;
  677. if (auto* parent = parent_widget())
  678. parent->invalidate_layout();
  679. if (m_visible)
  680. update();
  681. if (m_visible) {
  682. ShowEvent e;
  683. event(e);
  684. } else {
  685. HideEvent e;
  686. event(e);
  687. }
  688. }
  689. bool Widget::spans_entire_window_horizontally() const
  690. {
  691. auto* w = window();
  692. if (!w)
  693. return false;
  694. auto* main_widget = w->main_widget();
  695. if (!main_widget)
  696. return false;
  697. if (main_widget == this)
  698. return true;
  699. auto wrr = window_relative_rect();
  700. return wrr.left() == main_widget->rect().left() && wrr.right() == main_widget->rect().right();
  701. }
  702. void Widget::set_enabled(bool enabled)
  703. {
  704. if (m_enabled == enabled)
  705. return;
  706. m_enabled = enabled;
  707. for_each_child_widget([enabled](auto& child) {
  708. child.set_enabled(enabled);
  709. return IterationDecision::Continue;
  710. });
  711. if (!m_enabled && window() && window()->focused_widget() == this) {
  712. window()->did_disable_focused_widget({});
  713. }
  714. if (!m_enabled)
  715. set_override_cursor(Gfx::StandardCursor::None);
  716. Event e(Event::EnabledChange);
  717. event(e);
  718. update();
  719. }
  720. void Widget::move_to_front()
  721. {
  722. auto* parent = parent_widget();
  723. if (!parent)
  724. return;
  725. if (parent->children().size() == 1)
  726. return;
  727. parent->children().remove_first_matching([this](auto& entry) {
  728. return entry == this;
  729. });
  730. parent->children().append(*this);
  731. parent->update();
  732. }
  733. void Widget::move_to_back()
  734. {
  735. auto* parent = parent_widget();
  736. if (!parent)
  737. return;
  738. if (parent->children().size() == 1)
  739. return;
  740. parent->children().remove_first_matching([this](auto& entry) {
  741. return entry == this;
  742. });
  743. parent->children().prepend(*this);
  744. parent->update();
  745. }
  746. bool Widget::is_frontmost() const
  747. {
  748. auto* parent = parent_widget();
  749. if (!parent)
  750. return true;
  751. return &parent->children().last() == this;
  752. }
  753. bool Widget::is_backmost() const
  754. {
  755. auto* parent = parent_widget();
  756. if (!parent)
  757. return true;
  758. return &parent->children().first() == this;
  759. }
  760. Action* Widget::action_for_key_event(const KeyEvent& event)
  761. {
  762. Shortcut shortcut(event.modifiers(), (KeyCode)event.key());
  763. if (!shortcut.is_valid()) {
  764. return nullptr;
  765. }
  766. Action* found_action = nullptr;
  767. for_each_child_of_type<Action>([&](auto& action) {
  768. if (action.shortcut() == shortcut) {
  769. found_action = &action;
  770. return IterationDecision::Break;
  771. }
  772. return IterationDecision::Continue;
  773. });
  774. return found_action;
  775. }
  776. void Widget::set_updates_enabled(bool enabled)
  777. {
  778. if (m_updates_enabled == enabled)
  779. return;
  780. m_updates_enabled = enabled;
  781. if (enabled)
  782. update();
  783. }
  784. void Widget::focus_previous_widget(FocusSource source, bool siblings_only)
  785. {
  786. auto focusable_widgets = window()->focusable_widgets(source);
  787. if (siblings_only)
  788. focusable_widgets.remove_all_matching([this](auto& entry) { return entry->parent() != parent(); });
  789. for (int i = focusable_widgets.size() - 1; i >= 0; --i) {
  790. if (focusable_widgets[i] != this)
  791. continue;
  792. if (i > 0)
  793. focusable_widgets[i - 1]->set_focus(true, source);
  794. else
  795. focusable_widgets.last()->set_focus(true, source);
  796. }
  797. }
  798. void Widget::focus_next_widget(FocusSource source, bool siblings_only)
  799. {
  800. auto focusable_widgets = window()->focusable_widgets(source);
  801. if (siblings_only)
  802. focusable_widgets.remove_all_matching([this](auto& entry) { return entry->parent() != parent(); });
  803. for (size_t i = 0; i < focusable_widgets.size(); ++i) {
  804. if (focusable_widgets[i] != this)
  805. continue;
  806. if (i < focusable_widgets.size() - 1)
  807. focusable_widgets[i + 1]->set_focus(true, source);
  808. else
  809. focusable_widgets.first()->set_focus(true, source);
  810. }
  811. }
  812. Vector<Widget*> Widget::child_widgets() const
  813. {
  814. Vector<Widget*> widgets;
  815. widgets.ensure_capacity(children().size());
  816. for (auto& child : const_cast<Widget*>(this)->children()) {
  817. if (is<Widget>(child))
  818. widgets.append(static_cast<Widget*>(&child));
  819. }
  820. return widgets;
  821. }
  822. void Widget::set_palette(const Palette& palette)
  823. {
  824. m_palette = palette.impl();
  825. }
  826. void Widget::set_background_role(ColorRole role)
  827. {
  828. m_background_role = role;
  829. }
  830. void Widget::set_foreground_role(ColorRole role)
  831. {
  832. m_foreground_role = role;
  833. }
  834. Gfx::Palette Widget::palette() const
  835. {
  836. return Gfx::Palette(*m_palette);
  837. }
  838. void Widget::did_begin_inspection()
  839. {
  840. update();
  841. }
  842. void Widget::did_end_inspection()
  843. {
  844. update();
  845. }
  846. void Widget::set_content_margins(const Margins& margins)
  847. {
  848. if (m_content_margins == margins)
  849. return;
  850. m_content_margins = margins;
  851. invalidate_layout();
  852. }
  853. Gfx::IntRect Widget::content_rect() const
  854. {
  855. auto rect = relative_rect();
  856. rect.move_by(m_content_margins.left(), m_content_margins.top());
  857. rect.set_width(rect.width() - (m_content_margins.left() + m_content_margins.right()));
  858. rect.set_height(rect.height() - (m_content_margins.top() + m_content_margins.bottom()));
  859. return rect;
  860. }
  861. void Widget::set_tooltip(const StringView& tooltip)
  862. {
  863. m_tooltip = tooltip;
  864. if (Application::the()->tooltip_source_widget() == this)
  865. show_or_hide_tooltip();
  866. }
  867. void Widget::show_or_hide_tooltip()
  868. {
  869. if (has_tooltip())
  870. Application::the()->show_tooltip(m_tooltip, this);
  871. else
  872. Application::the()->hide_tooltip();
  873. }
  874. Gfx::IntRect Widget::children_clip_rect() const
  875. {
  876. return rect();
  877. }
  878. void Widget::set_override_cursor(Gfx::StandardCursor cursor)
  879. {
  880. if (m_override_cursor == cursor)
  881. return;
  882. m_override_cursor = cursor;
  883. if (auto* window = this->window())
  884. window->update_cursor({});
  885. }
  886. bool Widget::load_from_gml(const StringView& gml_string)
  887. {
  888. return load_from_gml(gml_string, [](const String& class_name) -> RefPtr<Widget> {
  889. dbgln("Class '{}' not registered", class_name);
  890. return nullptr;
  891. });
  892. }
  893. bool Widget::load_from_gml(const StringView& gml_string, RefPtr<Widget> (*unregistered_child_handler)(const String&))
  894. {
  895. auto value = parse_gml(gml_string);
  896. if (!value.is_object())
  897. return false;
  898. return load_from_json(value.as_object(), unregistered_child_handler);
  899. }
  900. bool Widget::load_from_json(const JsonObject& json, RefPtr<Widget> (*unregistered_child_handler)(const String&))
  901. {
  902. json.for_each_member([&](auto& key, auto& value) {
  903. set_property(key, value);
  904. });
  905. auto layout_value = json.get("layout");
  906. if (!layout_value.is_null() && !layout_value.is_object()) {
  907. dbgln("layout is not an object");
  908. return false;
  909. }
  910. if (layout_value.is_object()) {
  911. auto& layout = layout_value.as_object();
  912. auto class_name = layout.get("class");
  913. if (class_name.is_null()) {
  914. dbgln("Invalid layout class name");
  915. return false;
  916. }
  917. if (class_name.to_string() == "GUI::VerticalBoxLayout") {
  918. set_layout<GUI::VerticalBoxLayout>();
  919. } else if (class_name.to_string() == "GUI::HorizontalBoxLayout") {
  920. set_layout<GUI::HorizontalBoxLayout>();
  921. } else {
  922. dbgln("Unknown layout class: '{}'", class_name.to_string());
  923. return false;
  924. }
  925. layout.for_each_member([&](auto& key, auto& value) {
  926. this->layout()->set_property(key, value);
  927. });
  928. }
  929. auto children = json.get("children");
  930. if (children.is_array()) {
  931. for (auto& child_json_value : children.as_array().values()) {
  932. if (!child_json_value.is_object())
  933. return false;
  934. auto& child_json = child_json_value.as_object();
  935. auto class_name = child_json.get("class");
  936. if (!class_name.is_string()) {
  937. dbgln("No class name in entry");
  938. return false;
  939. }
  940. RefPtr<Widget> child_widget;
  941. if (auto* registration = WidgetClassRegistration::find(class_name.as_string())) {
  942. child_widget = registration->construct();
  943. } else {
  944. child_widget = unregistered_child_handler(class_name.as_string());
  945. if (!child_widget)
  946. return false;
  947. }
  948. add_child(*child_widget);
  949. child_widget->load_from_json(child_json, unregistered_child_handler);
  950. }
  951. }
  952. return true;
  953. }
  954. bool Widget::has_focus_within() const
  955. {
  956. auto* window = this->window();
  957. if (!window)
  958. return false;
  959. if (!window->focused_widget())
  960. return false;
  961. auto& effective_focus_widget = focus_proxy() ? *focus_proxy() : *this;
  962. return window->focused_widget() == &effective_focus_widget || is_ancestor_of(*window->focused_widget());
  963. }
  964. void Widget::set_shrink_to_fit(bool b)
  965. {
  966. if (m_shrink_to_fit == b)
  967. return;
  968. m_shrink_to_fit = b;
  969. invalidate_layout();
  970. }
  971. bool Widget::has_pending_drop() const
  972. {
  973. return Application::the()->pending_drop_widget() == this;
  974. }
  975. }