Widget.cpp 29 KB

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