Widget.cpp 36 KB

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