Widget.cpp 32 KB

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