Widget.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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(downcast<Widget>(*event.child()), downcast<Widget>(*event.insertion_before_child()));
  136. else
  137. layout()->add_widget(downcast<Widget>(*event.child()));
  138. }
  139. if (window() && event.child() && is<Widget>(*event.child()))
  140. window()->did_add_widget({}, downcast<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(downcast<Widget>(*event.child()));
  146. else
  147. invalidate_layout();
  148. }
  149. if (window() && event.child() && is<Widget>(*event.child()))
  150. window()->did_remove_widget({}, downcast<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_rect_change_event(ScreenRectChangeEvent&)
  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 = downcast<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) {
  637. ShowEvent e;
  638. event(e);
  639. } else {
  640. HideEvent e;
  641. event(e);
  642. }
  643. }
  644. bool Widget::spans_entire_window_horizontally() const
  645. {
  646. auto* w = window();
  647. if (!w)
  648. return false;
  649. auto* main_widget = w->main_widget();
  650. if (!main_widget)
  651. return false;
  652. if (main_widget == this)
  653. return true;
  654. auto wrr = window_relative_rect();
  655. return wrr.left() == main_widget->rect().left() && wrr.right() == main_widget->rect().right();
  656. }
  657. void Widget::set_enabled(bool enabled)
  658. {
  659. if (m_enabled == enabled)
  660. return;
  661. m_enabled = enabled;
  662. for_each_child_widget([enabled](auto& child) {
  663. child.set_enabled(enabled);
  664. return IterationDecision::Continue;
  665. });
  666. if (!m_enabled && window() && window()->focused_widget() == this) {
  667. window()->did_disable_focused_widget({});
  668. }
  669. if (!m_enabled)
  670. set_override_cursor(Gfx::StandardCursor::None);
  671. Event e(Event::EnabledChange);
  672. event(e);
  673. update();
  674. }
  675. void Widget::move_to_front()
  676. {
  677. auto* parent = parent_widget();
  678. if (!parent)
  679. return;
  680. if (parent->children().size() == 1)
  681. return;
  682. parent->children().remove_first_matching([this](auto& entry) {
  683. return entry == this;
  684. });
  685. parent->children().append(*this);
  686. parent->update();
  687. }
  688. void Widget::move_to_back()
  689. {
  690. auto* parent = parent_widget();
  691. if (!parent)
  692. return;
  693. if (parent->children().size() == 1)
  694. return;
  695. parent->children().remove_first_matching([this](auto& entry) {
  696. return entry == this;
  697. });
  698. parent->children().prepend(*this);
  699. parent->update();
  700. }
  701. bool Widget::is_frontmost() const
  702. {
  703. auto* parent = parent_widget();
  704. if (!parent)
  705. return true;
  706. return &parent->children().last() == this;
  707. }
  708. bool Widget::is_backmost() const
  709. {
  710. auto* parent = parent_widget();
  711. if (!parent)
  712. return true;
  713. return &parent->children().first() == this;
  714. }
  715. Action* Widget::action_for_key_event(const KeyEvent& event)
  716. {
  717. Shortcut shortcut(event.modifiers(), (KeyCode)event.key());
  718. if (!shortcut.is_valid()) {
  719. return nullptr;
  720. }
  721. Action* found_action = nullptr;
  722. for_each_child_of_type<Action>([&](auto& action) {
  723. if (action.shortcut() == shortcut) {
  724. found_action = &action;
  725. return IterationDecision::Break;
  726. }
  727. return IterationDecision::Continue;
  728. });
  729. return found_action;
  730. }
  731. void Widget::set_updates_enabled(bool enabled)
  732. {
  733. if (m_updates_enabled == enabled)
  734. return;
  735. m_updates_enabled = enabled;
  736. if (enabled)
  737. update();
  738. }
  739. void Widget::focus_previous_widget(FocusSource source, bool siblings_only)
  740. {
  741. auto focusable_widgets = window()->focusable_widgets(source);
  742. if (siblings_only)
  743. focusable_widgets.remove_all_matching([this](auto& entry) { return entry->parent() != parent(); });
  744. for (int i = focusable_widgets.size() - 1; i >= 0; --i) {
  745. if (focusable_widgets[i] != this)
  746. continue;
  747. if (i > 0)
  748. focusable_widgets[i - 1]->set_focus(true, source);
  749. else
  750. focusable_widgets.last()->set_focus(true, source);
  751. }
  752. }
  753. void Widget::focus_next_widget(FocusSource source, bool siblings_only)
  754. {
  755. auto focusable_widgets = window()->focusable_widgets(source);
  756. if (siblings_only)
  757. focusable_widgets.remove_all_matching([this](auto& entry) { return entry->parent() != parent(); });
  758. for (size_t i = 0; i < focusable_widgets.size(); ++i) {
  759. if (focusable_widgets[i] != this)
  760. continue;
  761. if (i < focusable_widgets.size() - 1)
  762. focusable_widgets[i + 1]->set_focus(true, source);
  763. else
  764. focusable_widgets.first()->set_focus(true, source);
  765. }
  766. }
  767. Vector<Widget*> Widget::child_widgets() const
  768. {
  769. Vector<Widget*> widgets;
  770. widgets.ensure_capacity(children().size());
  771. for (auto& child : const_cast<Widget*>(this)->children()) {
  772. if (is<Widget>(child))
  773. widgets.append(static_cast<Widget*>(&child));
  774. }
  775. return widgets;
  776. }
  777. void Widget::set_palette(const Palette& palette)
  778. {
  779. m_palette = palette.impl();
  780. }
  781. void Widget::set_background_role(ColorRole role)
  782. {
  783. m_background_role = role;
  784. }
  785. void Widget::set_foreground_role(ColorRole role)
  786. {
  787. m_foreground_role = role;
  788. }
  789. Gfx::Palette Widget::palette() const
  790. {
  791. return Gfx::Palette(*m_palette);
  792. }
  793. void Widget::did_begin_inspection()
  794. {
  795. update();
  796. }
  797. void Widget::did_end_inspection()
  798. {
  799. update();
  800. }
  801. void Widget::set_content_margins(const Margins& margins)
  802. {
  803. if (m_content_margins == margins)
  804. return;
  805. m_content_margins = margins;
  806. invalidate_layout();
  807. }
  808. Gfx::IntRect Widget::content_rect() const
  809. {
  810. auto rect = relative_rect();
  811. rect.translate_by(m_content_margins.left(), m_content_margins.top());
  812. rect.set_width(rect.width() - (m_content_margins.left() + m_content_margins.right()));
  813. rect.set_height(rect.height() - (m_content_margins.top() + m_content_margins.bottom()));
  814. return rect;
  815. }
  816. void Widget::set_tooltip(String tooltip)
  817. {
  818. m_tooltip = move(tooltip);
  819. if (Application::the()->tooltip_source_widget() == this)
  820. show_or_hide_tooltip();
  821. }
  822. void Widget::show_or_hide_tooltip()
  823. {
  824. if (has_tooltip())
  825. Application::the()->show_tooltip(m_tooltip, this);
  826. else
  827. Application::the()->hide_tooltip();
  828. }
  829. Gfx::IntRect Widget::children_clip_rect() const
  830. {
  831. return rect();
  832. }
  833. void Widget::set_override_cursor(Gfx::StandardCursor cursor)
  834. {
  835. if (m_override_cursor == cursor)
  836. return;
  837. m_override_cursor = cursor;
  838. if (auto* window = this->window())
  839. window->update_cursor({});
  840. }
  841. bool Widget::load_from_gml(const StringView& gml_string)
  842. {
  843. return load_from_gml(gml_string, [](const String& class_name) -> RefPtr<Core::Object> {
  844. dbgln("Class '{}' not registered", class_name);
  845. return nullptr;
  846. });
  847. }
  848. bool Widget::load_from_gml(const StringView& gml_string, RefPtr<Core::Object> (*unregistered_child_handler)(const String&))
  849. {
  850. auto value = parse_gml(gml_string);
  851. if (!value.is_object())
  852. return false;
  853. return load_from_json(value.as_object(), unregistered_child_handler);
  854. }
  855. bool Widget::load_from_json(const JsonObject& json, RefPtr<Core::Object> (*unregistered_child_handler)(const String&))
  856. {
  857. json.for_each_member([&](auto& key, auto& value) {
  858. set_property(key, value);
  859. });
  860. auto layout_value = json.get("layout");
  861. if (!layout_value.is_null() && !layout_value.is_object()) {
  862. dbgln("layout is not an object");
  863. return false;
  864. }
  865. if (layout_value.is_object()) {
  866. auto& layout = layout_value.as_object();
  867. auto class_name = layout.get("class");
  868. if (class_name.is_null()) {
  869. dbgln("Invalid layout class name");
  870. return false;
  871. }
  872. if (class_name.to_string() == "GUI::VerticalBoxLayout") {
  873. set_layout<GUI::VerticalBoxLayout>();
  874. } else if (class_name.to_string() == "GUI::HorizontalBoxLayout") {
  875. set_layout<GUI::HorizontalBoxLayout>();
  876. } else {
  877. dbgln("Unknown layout class: '{}'", class_name.to_string());
  878. return false;
  879. }
  880. layout.for_each_member([&](auto& key, auto& value) {
  881. this->layout()->set_property(key, value);
  882. });
  883. }
  884. auto children = json.get("children");
  885. if (children.is_array()) {
  886. for (auto& child_json_value : children.as_array().values()) {
  887. if (!child_json_value.is_object())
  888. return false;
  889. auto& child_json = child_json_value.as_object();
  890. auto class_name = child_json.get("class");
  891. if (!class_name.is_string()) {
  892. dbgln("No class name in entry");
  893. return false;
  894. }
  895. RefPtr<Core::Object> child;
  896. if (auto* registration = Core::ObjectClassRegistration::find(class_name.as_string())) {
  897. child = registration->construct();
  898. } else {
  899. child = unregistered_child_handler(class_name.as_string());
  900. }
  901. if (!child)
  902. return false;
  903. add_child(*child);
  904. child->load_from_json(child_json, unregistered_child_handler);
  905. }
  906. }
  907. return true;
  908. }
  909. bool Widget::has_focus_within() const
  910. {
  911. auto* window = this->window();
  912. if (!window)
  913. return false;
  914. if (!window->focused_widget())
  915. return false;
  916. auto& effective_focus_widget = focus_proxy() ? *focus_proxy() : *this;
  917. return window->focused_widget() == &effective_focus_widget || is_ancestor_of(*window->focused_widget());
  918. }
  919. void Widget::set_shrink_to_fit(bool b)
  920. {
  921. if (m_shrink_to_fit == b)
  922. return;
  923. m_shrink_to_fit = b;
  924. invalidate_layout();
  925. }
  926. bool Widget::has_pending_drop() const
  927. {
  928. return Application::the()->pending_drop_widget() == this;
  929. }
  930. }