Widget.cpp 31 KB

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