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