Widget.cpp 30 KB

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