Widget.cpp 33 KB

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