Widget.cpp 32 KB

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