Widget.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Assertions.h>
  27. #include <AK/JsonObject.h>
  28. #include <LibGUI/Action.h>
  29. #include <LibGUI/Application.h>
  30. #include <LibGUI/BoxLayout.h>
  31. #include <LibGUI/Button.h>
  32. #include <LibGUI/CheckBox.h>
  33. #include <LibGUI/ColorInput.h>
  34. #include <LibGUI/Event.h>
  35. #include <LibGUI/GroupBox.h>
  36. #include <LibGUI/Label.h>
  37. #include <LibGUI/Layout.h>
  38. #include <LibGUI/Menu.h>
  39. #include <LibGUI/Painter.h>
  40. #include <LibGUI/RadioButton.h>
  41. #include <LibGUI/ScrollBar.h>
  42. #include <LibGUI/Slider.h>
  43. #include <LibGUI/SpinBox.h>
  44. #include <LibGUI/Splitter.h>
  45. #include <LibGUI/StatusBar.h>
  46. #include <LibGUI/TextBox.h>
  47. #include <LibGUI/ToolBar.h>
  48. #include <LibGUI/ToolBarContainer.h>
  49. #include <LibGUI/Widget.h>
  50. #include <LibGUI/Window.h>
  51. #include <LibGUI/WindowServerConnection.h>
  52. #include <LibGfx/Bitmap.h>
  53. #include <LibGfx/Font.h>
  54. #include <LibGfx/Palette.h>
  55. #include <unistd.h>
  56. namespace GUI {
  57. REGISTER_WIDGET(GUI, Button)
  58. REGISTER_WIDGET(GUI, CheckBox)
  59. REGISTER_WIDGET(GUI, ColorInput)
  60. REGISTER_WIDGET(GUI, Frame)
  61. REGISTER_WIDGET(GUI, GroupBox)
  62. REGISTER_WIDGET(GUI, HorizontalSplitter)
  63. REGISTER_WIDGET(GUI, Label)
  64. REGISTER_WIDGET(GUI, RadioButton)
  65. REGISTER_WIDGET(GUI, ScrollBar)
  66. REGISTER_WIDGET(GUI, Slider)
  67. REGISTER_WIDGET(GUI, SpinBox)
  68. REGISTER_WIDGET(GUI, StatusBar)
  69. REGISTER_WIDGET(GUI, TextBox)
  70. REGISTER_WIDGET(GUI, TextEditor)
  71. REGISTER_WIDGET(GUI, ToolBar)
  72. REGISTER_WIDGET(GUI, ToolBarContainer)
  73. REGISTER_WIDGET(GUI, Widget)
  74. static HashMap<String, WidgetClassRegistration*>& widget_classes()
  75. {
  76. static HashMap<String, WidgetClassRegistration*>* map;
  77. if (!map)
  78. map = new HashMap<String, WidgetClassRegistration*>;
  79. return *map;
  80. }
  81. WidgetClassRegistration::WidgetClassRegistration(const String& class_name, Function<NonnullRefPtr<Widget>()> factory)
  82. : m_class_name(class_name)
  83. , m_factory(move(factory))
  84. {
  85. widget_classes().set(class_name, this);
  86. }
  87. WidgetClassRegistration::~WidgetClassRegistration()
  88. {
  89. }
  90. void WidgetClassRegistration::for_each(Function<void(const WidgetClassRegistration&)> callback)
  91. {
  92. for (auto& it : widget_classes()) {
  93. callback(*it.value);
  94. }
  95. }
  96. const WidgetClassRegistration* WidgetClassRegistration::find(const String& class_name)
  97. {
  98. return widget_classes().get(class_name).value_or(nullptr);
  99. }
  100. Widget::Widget()
  101. : Core::Object(nullptr, true)
  102. , m_background_role(Gfx::ColorRole::Window)
  103. , m_foreground_role(Gfx::ColorRole::WindowText)
  104. , m_font(Gfx::Font::default_font())
  105. , m_palette(Application::the()->palette().impl())
  106. {
  107. REGISTER_RECT_PROPERTY("relative_rect", relative_rect, set_relative_rect);
  108. REGISTER_BOOL_PROPERTY("fill_with_background_color", fill_with_background_color, set_fill_with_background_color);
  109. REGISTER_BOOL_PROPERTY("visible", is_visible, set_visible);
  110. REGISTER_BOOL_PROPERTY("focused", is_focused, set_focus);
  111. REGISTER_BOOL_PROPERTY("enabled", is_enabled, set_enabled);
  112. REGISTER_STRING_PROPERTY("tooltip", tooltip, set_tooltip);
  113. REGISTER_SIZE_PROPERTY("preferred_size", preferred_size, set_preferred_size);
  114. REGISTER_INT_PROPERTY("preferred_width", preferred_width, set_preferred_width);
  115. REGISTER_INT_PROPERTY("preferred_height", preferred_height, set_preferred_height);
  116. REGISTER_SIZE_POLICY_PROPERTY("horizontal_size_policy", horizontal_size_policy, set_horizontal_size_policy);
  117. REGISTER_SIZE_POLICY_PROPERTY("vertical_size_policy", vertical_size_policy, set_vertical_size_policy);
  118. register_property(
  119. "focus_policy", [this]() -> JsonValue {
  120. auto policy = focus_policy();
  121. if (policy == GUI::FocusPolicy::ClickFocus)
  122. return "ClickFocus";
  123. if (policy == GUI::FocusPolicy::NoFocus)
  124. return "NoFocus";
  125. if (policy == GUI::FocusPolicy::TabFocus)
  126. return "TabFocus";
  127. if (policy == GUI::FocusPolicy::StrongFocus)
  128. return "StrongFocus";
  129. return JsonValue(); },
  130. [this](auto& value) {
  131. if (!value.is_string())
  132. return false;
  133. if (value.as_string() == "ClickFocus") {
  134. set_focus_policy(GUI::FocusPolicy::ClickFocus);
  135. return true;
  136. }
  137. if (value.as_string() == "NoFocus") {
  138. set_focus_policy(GUI::FocusPolicy::NoFocus);
  139. return true;
  140. }
  141. if (value.as_string() == "TabFocus") {
  142. set_focus_policy(GUI::FocusPolicy::TabFocus);
  143. return true;
  144. }
  145. if (value.as_string() == "StrongFocus") {
  146. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  147. return true;
  148. }
  149. return false;
  150. });
  151. }
  152. Widget::~Widget()
  153. {
  154. }
  155. void Widget::child_event(Core::ChildEvent& event)
  156. {
  157. if (event.type() == Event::ChildAdded) {
  158. if (event.child() && is<Widget>(*event.child()) && layout()) {
  159. if (event.insertion_before_child() && event.insertion_before_child()->is_widget())
  160. layout()->insert_widget_before(downcast<Widget>(*event.child()), downcast<Widget>(*event.insertion_before_child()));
  161. else
  162. layout()->add_widget(downcast<Widget>(*event.child()));
  163. }
  164. if (window() && event.child() && is<Widget>(*event.child()))
  165. window()->did_add_widget({}, downcast<Widget>(*event.child()));
  166. }
  167. if (event.type() == Event::ChildRemoved) {
  168. if (layout()) {
  169. if (event.child() && is<Widget>(*event.child()))
  170. layout()->remove_widget(downcast<Widget>(*event.child()));
  171. else
  172. invalidate_layout();
  173. }
  174. if (window() && event.child() && is<Widget>(*event.child()))
  175. window()->did_remove_widget({}, downcast<Widget>(*event.child()));
  176. update();
  177. }
  178. return Core::Object::child_event(event);
  179. }
  180. void Widget::set_relative_rect(const Gfx::IntRect& a_rect)
  181. {
  182. // Get rid of negative width/height values.
  183. Gfx::IntRect rect = {
  184. a_rect.x(),
  185. a_rect.y(),
  186. max(a_rect.width(), 0),
  187. max(a_rect.height(), 0)
  188. };
  189. if (rect == m_relative_rect)
  190. return;
  191. auto old_rect = m_relative_rect;
  192. bool size_changed = m_relative_rect.size() != rect.size();
  193. m_relative_rect = rect;
  194. if (size_changed) {
  195. ResizeEvent resize_event(rect.size());
  196. event(resize_event);
  197. }
  198. if (auto* parent = parent_widget())
  199. parent->update(old_rect);
  200. update();
  201. }
  202. void Widget::event(Core::Event& event)
  203. {
  204. if (!is_enabled()) {
  205. switch (event.type()) {
  206. case Event::MouseUp:
  207. case Event::MouseDown:
  208. case Event::MouseMove:
  209. case Event::MouseWheel:
  210. case Event::MouseDoubleClick:
  211. case Event::KeyUp:
  212. case Event::KeyDown:
  213. return;
  214. default:
  215. break;
  216. }
  217. }
  218. switch (event.type()) {
  219. case Event::Paint:
  220. return handle_paint_event(static_cast<PaintEvent&>(event));
  221. case Event::Resize:
  222. return handle_resize_event(static_cast<ResizeEvent&>(event));
  223. case Event::FocusIn:
  224. return focusin_event(static_cast<FocusEvent&>(event));
  225. case Event::FocusOut:
  226. return focusout_event(static_cast<FocusEvent&>(event));
  227. case Event::Show:
  228. return show_event(static_cast<ShowEvent&>(event));
  229. case Event::Hide:
  230. return hide_event(static_cast<HideEvent&>(event));
  231. case Event::KeyDown:
  232. return keydown_event(static_cast<KeyEvent&>(event));
  233. case Event::KeyUp:
  234. return keyup_event(static_cast<KeyEvent&>(event));
  235. case Event::MouseMove:
  236. return mousemove_event(static_cast<MouseEvent&>(event));
  237. case Event::MouseDown:
  238. return handle_mousedown_event(static_cast<MouseEvent&>(event));
  239. case Event::MouseDoubleClick:
  240. return handle_mousedoubleclick_event(static_cast<MouseEvent&>(event));
  241. case Event::MouseUp:
  242. return handle_mouseup_event(static_cast<MouseEvent&>(event));
  243. case Event::MouseWheel:
  244. return mousewheel_event(static_cast<MouseEvent&>(event));
  245. case Event::DragMove:
  246. return drag_move_event(static_cast<DragEvent&>(event));
  247. case Event::Drop:
  248. return drop_event(static_cast<DropEvent&>(event));
  249. case Event::ThemeChange:
  250. return theme_change_event(static_cast<ThemeChangeEvent&>(event));
  251. case Event::Enter:
  252. return handle_enter_event(event);
  253. case Event::Leave:
  254. return handle_leave_event(event);
  255. case Event::EnabledChange:
  256. return change_event(static_cast<Event&>(event));
  257. default:
  258. return Core::Object::event(event);
  259. }
  260. }
  261. void Widget::handle_paint_event(PaintEvent& event)
  262. {
  263. ASSERT(is_visible());
  264. if (fill_with_background_color()) {
  265. Painter painter(*this);
  266. painter.fill_rect(event.rect(), palette().color(background_role()));
  267. }
  268. paint_event(event);
  269. auto children_clip_rect = this->children_clip_rect();
  270. for_each_child_widget([&](auto& child) {
  271. if (!child.is_visible())
  272. return IterationDecision::Continue;
  273. if (child.relative_rect().intersects(event.rect())) {
  274. PaintEvent local_event(event.rect().intersected(children_clip_rect).intersected(child.relative_rect()).translated(-child.relative_position()));
  275. child.dispatch_event(local_event, this);
  276. }
  277. return IterationDecision::Continue;
  278. });
  279. second_paint_event(event);
  280. if (is_being_inspected()) {
  281. Painter painter(*this);
  282. painter.draw_rect(rect(), Color::Magenta);
  283. }
  284. if (Application::the()->focus_debugging_enabled()) {
  285. if (is_focused()) {
  286. Painter painter(*this);
  287. painter.draw_rect(rect(), Color::Cyan);
  288. }
  289. }
  290. }
  291. void Widget::set_layout(NonnullRefPtr<Layout> layout)
  292. {
  293. if (m_layout) {
  294. m_layout->notify_disowned({}, *this);
  295. m_layout->remove_from_parent();
  296. }
  297. m_layout = move(layout);
  298. if (m_layout) {
  299. add_child(*m_layout);
  300. m_layout->notify_adopted({}, *this);
  301. do_layout();
  302. } else {
  303. update();
  304. }
  305. }
  306. void Widget::do_layout()
  307. {
  308. for_each_child_widget([&](auto& child) {
  309. child.do_layout();
  310. return IterationDecision::Continue;
  311. });
  312. custom_layout();
  313. if (!m_layout)
  314. return;
  315. m_layout->run(*this);
  316. did_layout();
  317. update();
  318. }
  319. void Widget::notify_layout_changed(Badge<Layout>)
  320. {
  321. invalidate_layout();
  322. }
  323. void Widget::handle_resize_event(ResizeEvent& event)
  324. {
  325. resize_event(event);
  326. do_layout();
  327. }
  328. void Widget::handle_mouseup_event(MouseEvent& event)
  329. {
  330. mouseup_event(event);
  331. }
  332. void Widget::handle_mousedown_event(MouseEvent& event)
  333. {
  334. if (((unsigned)focus_policy() & (unsigned)FocusPolicy::ClickFocus))
  335. set_focus(true, FocusSource::Mouse);
  336. mousedown_event(event);
  337. if (event.button() == MouseButton::Right) {
  338. ContextMenuEvent c_event(event.position(), screen_relative_rect().location().translated(event.position()));
  339. context_menu_event(c_event);
  340. }
  341. }
  342. void Widget::handle_mousedoubleclick_event(MouseEvent& event)
  343. {
  344. doubleclick_event(event);
  345. }
  346. void Widget::handle_enter_event(Core::Event& event)
  347. {
  348. if (auto* window = this->window())
  349. window->update_cursor({});
  350. show_tooltip();
  351. enter_event(event);
  352. }
  353. void Widget::handle_leave_event(Core::Event& event)
  354. {
  355. if (auto* window = this->window())
  356. window->update_cursor({});
  357. Application::the()->hide_tooltip();
  358. leave_event(event);
  359. }
  360. void Widget::doubleclick_event(MouseEvent&)
  361. {
  362. }
  363. void Widget::resize_event(ResizeEvent&)
  364. {
  365. }
  366. void Widget::paint_event(PaintEvent&)
  367. {
  368. }
  369. void Widget::second_paint_event(PaintEvent&)
  370. {
  371. }
  372. void Widget::show_event(ShowEvent&)
  373. {
  374. }
  375. void Widget::hide_event(HideEvent&)
  376. {
  377. }
  378. void Widget::keydown_event(KeyEvent& event)
  379. {
  380. if (!event.alt() && !event.ctrl() && !event.logo() && event.key() == KeyCode::Key_Tab) {
  381. if (event.shift())
  382. focus_previous_widget(FocusSource::Keyboard);
  383. else
  384. focus_next_widget(FocusSource::Keyboard);
  385. event.accept();
  386. return;
  387. }
  388. event.ignore();
  389. }
  390. void Widget::keyup_event(KeyEvent& event)
  391. {
  392. event.ignore();
  393. }
  394. void Widget::mousedown_event(MouseEvent&)
  395. {
  396. }
  397. void Widget::mouseup_event(MouseEvent&)
  398. {
  399. }
  400. void Widget::mousemove_event(MouseEvent&)
  401. {
  402. }
  403. void Widget::mousewheel_event(MouseEvent&)
  404. {
  405. }
  406. void Widget::context_menu_event(ContextMenuEvent&)
  407. {
  408. }
  409. void Widget::focusin_event(FocusEvent&)
  410. {
  411. }
  412. void Widget::focusout_event(FocusEvent&)
  413. {
  414. }
  415. void Widget::enter_event(Core::Event&)
  416. {
  417. }
  418. void Widget::leave_event(Core::Event&)
  419. {
  420. }
  421. void Widget::change_event(Event&)
  422. {
  423. }
  424. void Widget::drag_move_event(DragEvent& event)
  425. {
  426. dbg() << class_name() << "{" << this << "} DRAG MOVE position: " << event.position() << ", data_type: '" << event.data_type() << "'";
  427. event.ignore();
  428. }
  429. void Widget::drop_event(DropEvent& event)
  430. {
  431. dbg() << class_name() << "{" << this << "} DROP position: " << event.position() << ", text: '" << event.text() << "'";
  432. event.ignore();
  433. }
  434. void Widget::theme_change_event(ThemeChangeEvent&)
  435. {
  436. }
  437. void Widget::update()
  438. {
  439. if (rect().is_empty())
  440. return;
  441. update(rect());
  442. }
  443. void Widget::update(const Gfx::IntRect& rect)
  444. {
  445. if (!is_visible())
  446. return;
  447. if (!updates_enabled())
  448. return;
  449. Window* window = m_window;
  450. Widget* parent = parent_widget();
  451. while (parent) {
  452. if (!parent->updates_enabled())
  453. return;
  454. window = parent->m_window;
  455. parent = parent->parent_widget();
  456. }
  457. if (window)
  458. window->update(rect.translated(window_relative_rect().location()));
  459. }
  460. Gfx::IntRect Widget::window_relative_rect() const
  461. {
  462. auto rect = relative_rect();
  463. for (auto* parent = parent_widget(); parent; parent = parent->parent_widget()) {
  464. rect.move_by(parent->relative_position());
  465. }
  466. return rect;
  467. }
  468. Gfx::IntRect Widget::screen_relative_rect() const
  469. {
  470. auto window_position = window()->window_type() == WindowType::MenuApplet
  471. ? window()->rect_in_menubar().location()
  472. : window()->rect().location();
  473. return window_relative_rect().translated(window_position);
  474. }
  475. Widget* Widget::child_at(const Gfx::IntPoint& point) const
  476. {
  477. for (int i = children().size() - 1; i >= 0; --i) {
  478. if (!is<Widget>(children()[i]))
  479. continue;
  480. auto& child = downcast<Widget>(children()[i]);
  481. if (!child.is_visible())
  482. continue;
  483. if (child.content_rect().contains(point))
  484. return const_cast<Widget*>(&child);
  485. }
  486. return nullptr;
  487. }
  488. Widget::HitTestResult Widget::hit_test(const Gfx::IntPoint& position, ShouldRespectGreediness should_respect_greediness)
  489. {
  490. if (should_respect_greediness == ShouldRespectGreediness::Yes && is_greedy_for_hits())
  491. return { this, position };
  492. if (auto* child = child_at(position))
  493. return child->hit_test(position - child->relative_position());
  494. return { this, position };
  495. }
  496. void Widget::set_window(Window* window)
  497. {
  498. if (m_window == window)
  499. return;
  500. m_window = window;
  501. }
  502. void Widget::set_focus_proxy(Widget* proxy)
  503. {
  504. if (m_focus_proxy == proxy)
  505. return;
  506. m_focus_proxy = proxy;
  507. }
  508. FocusPolicy Widget::focus_policy() const
  509. {
  510. if (m_focus_proxy)
  511. return m_focus_proxy->focus_policy();
  512. return m_focus_policy;
  513. }
  514. void Widget::set_focus_policy(FocusPolicy policy)
  515. {
  516. if (m_focus_proxy)
  517. return m_focus_proxy->set_focus_policy(policy);
  518. m_focus_policy = policy;
  519. }
  520. bool Widget::is_focused() const
  521. {
  522. if (m_focus_proxy)
  523. return m_focus_proxy->is_focused();
  524. auto* win = window();
  525. if (!win)
  526. return false;
  527. // Accessory windows are not active despite being the active
  528. // input window. So we can have focus if either we're the active
  529. // input window or we're the active window
  530. if (win->is_active_input() || win->is_active())
  531. return win->focused_widget() == this;
  532. return false;
  533. }
  534. void Widget::set_focus(bool focus, FocusSource source)
  535. {
  536. if (m_focus_proxy)
  537. return m_focus_proxy->set_focus(focus, source);
  538. auto* win = window();
  539. if (!win)
  540. return;
  541. if (focus) {
  542. win->set_focused_widget(this, source);
  543. } else {
  544. if (win->focused_widget() == this)
  545. win->set_focused_widget(nullptr, source);
  546. }
  547. }
  548. void Widget::set_font(const Gfx::Font* font)
  549. {
  550. if (m_font.ptr() == font)
  551. return;
  552. if (!font)
  553. m_font = Gfx::Font::default_font();
  554. else
  555. m_font = *font;
  556. did_change_font();
  557. update();
  558. }
  559. void Widget::set_global_cursor_tracking(bool enabled)
  560. {
  561. auto* win = window();
  562. if (!win)
  563. return;
  564. win->set_global_cursor_tracking_widget(enabled ? this : nullptr);
  565. }
  566. bool Widget::global_cursor_tracking() const
  567. {
  568. auto* win = window();
  569. if (!win)
  570. return false;
  571. return win->global_cursor_tracking_widget() == this;
  572. }
  573. void Widget::set_preferred_size(const Gfx::IntSize& size)
  574. {
  575. if (m_preferred_size == size)
  576. return;
  577. m_preferred_size = size;
  578. invalidate_layout();
  579. }
  580. void Widget::set_size_policy(Orientation orientation, SizePolicy policy)
  581. {
  582. if (orientation == Orientation::Horizontal)
  583. set_size_policy(policy, m_vertical_size_policy);
  584. else
  585. set_size_policy(m_horizontal_size_policy, policy);
  586. }
  587. void Widget::set_size_policy(SizePolicy horizontal_policy, SizePolicy vertical_policy)
  588. {
  589. if (m_horizontal_size_policy == horizontal_policy && m_vertical_size_policy == vertical_policy)
  590. return;
  591. m_horizontal_size_policy = horizontal_policy;
  592. m_vertical_size_policy = vertical_policy;
  593. invalidate_layout();
  594. }
  595. void Widget::invalidate_layout()
  596. {
  597. if (window())
  598. window()->schedule_relayout();
  599. }
  600. void Widget::set_visible(bool visible)
  601. {
  602. if (visible == m_visible)
  603. return;
  604. m_visible = visible;
  605. if (auto* parent = parent_widget())
  606. parent->invalidate_layout();
  607. if (m_visible)
  608. update();
  609. if (m_visible) {
  610. ShowEvent e;
  611. event(e);
  612. } else {
  613. HideEvent e;
  614. event(e);
  615. }
  616. }
  617. bool Widget::spans_entire_window_horizontally() const
  618. {
  619. auto* w = window();
  620. if (!w)
  621. return false;
  622. auto* main_widget = w->main_widget();
  623. if (!main_widget)
  624. return false;
  625. if (main_widget == this)
  626. return true;
  627. auto wrr = window_relative_rect();
  628. return wrr.left() == main_widget->rect().left() && wrr.right() == main_widget->rect().right();
  629. }
  630. void Widget::set_enabled(bool enabled)
  631. {
  632. if (m_enabled == enabled)
  633. return;
  634. m_enabled = enabled;
  635. for_each_child_widget([enabled](auto& child) {
  636. child.set_enabled(enabled);
  637. return IterationDecision::Continue;
  638. });
  639. Event e(Event::EnabledChange);
  640. event(e);
  641. update();
  642. }
  643. void Widget::move_to_front()
  644. {
  645. auto* parent = parent_widget();
  646. if (!parent)
  647. return;
  648. if (parent->children().size() == 1)
  649. return;
  650. parent->children().remove_first_matching([this](auto& entry) {
  651. return entry == this;
  652. });
  653. parent->children().append(*this);
  654. parent->update();
  655. }
  656. void Widget::move_to_back()
  657. {
  658. auto* parent = parent_widget();
  659. if (!parent)
  660. return;
  661. if (parent->children().size() == 1)
  662. return;
  663. parent->children().remove_first_matching([this](auto& entry) {
  664. return entry == this;
  665. });
  666. parent->children().prepend(*this);
  667. parent->update();
  668. }
  669. bool Widget::is_frontmost() const
  670. {
  671. auto* parent = parent_widget();
  672. if (!parent)
  673. return true;
  674. return &parent->children().last() == this;
  675. }
  676. bool Widget::is_backmost() const
  677. {
  678. auto* parent = parent_widget();
  679. if (!parent)
  680. return true;
  681. return &parent->children().first() == this;
  682. }
  683. Action* Widget::action_for_key_event(const KeyEvent& event)
  684. {
  685. Shortcut shortcut(event.modifiers(), (KeyCode)event.key());
  686. if (!shortcut.is_valid()) {
  687. return nullptr;
  688. }
  689. Action* found_action = nullptr;
  690. for_each_child_of_type<Action>([&](auto& action) {
  691. if (action.shortcut() == shortcut) {
  692. found_action = &action;
  693. return IterationDecision::Break;
  694. }
  695. return IterationDecision::Continue;
  696. });
  697. return found_action;
  698. }
  699. void Widget::set_updates_enabled(bool enabled)
  700. {
  701. if (m_updates_enabled == enabled)
  702. return;
  703. m_updates_enabled = enabled;
  704. if (enabled)
  705. update();
  706. }
  707. void Widget::focus_previous_widget(FocusSource source)
  708. {
  709. auto focusable_widgets = window()->focusable_widgets(source);
  710. for (int i = focusable_widgets.size() - 1; i >= 0; --i) {
  711. if (focusable_widgets[i] != this)
  712. continue;
  713. if (i > 0)
  714. focusable_widgets[i - 1]->set_focus(true, source);
  715. else
  716. focusable_widgets.last()->set_focus(true, source);
  717. }
  718. }
  719. void Widget::focus_next_widget(FocusSource source)
  720. {
  721. auto focusable_widgets = window()->focusable_widgets(source);
  722. for (size_t i = 0; i < focusable_widgets.size(); ++i) {
  723. if (focusable_widgets[i] != this)
  724. continue;
  725. if (i < focusable_widgets.size() - 1)
  726. focusable_widgets[i + 1]->set_focus(true, source);
  727. else
  728. focusable_widgets.first()->set_focus(true, source);
  729. }
  730. }
  731. void Widget::set_backcolor(const StringView& color_string)
  732. {
  733. auto color = Color::from_string(color_string);
  734. if (!color.has_value())
  735. return;
  736. set_background_color(color.value());
  737. }
  738. void Widget::set_forecolor(const StringView& color_string)
  739. {
  740. auto color = Color::from_string(color_string);
  741. if (!color.has_value())
  742. return;
  743. set_foreground_color(color.value());
  744. }
  745. Vector<Widget*> Widget::child_widgets() const
  746. {
  747. Vector<Widget*> widgets;
  748. widgets.ensure_capacity(children().size());
  749. for (auto& child : const_cast<Widget*>(this)->children()) {
  750. if (child.is_widget())
  751. widgets.append(static_cast<Widget*>(&child));
  752. }
  753. return widgets;
  754. }
  755. void Widget::set_palette(const Palette& palette)
  756. {
  757. m_palette = palette.impl();
  758. }
  759. void Widget::set_background_role(ColorRole role)
  760. {
  761. m_background_role = role;
  762. }
  763. void Widget::set_foreground_role(ColorRole role)
  764. {
  765. m_foreground_role = role;
  766. }
  767. Gfx::Palette Widget::palette() const
  768. {
  769. return Gfx::Palette(*m_palette);
  770. }
  771. void Widget::did_begin_inspection()
  772. {
  773. update();
  774. }
  775. void Widget::did_end_inspection()
  776. {
  777. update();
  778. }
  779. void Widget::set_content_margins(const Margins& margins)
  780. {
  781. if (m_content_margins == margins)
  782. return;
  783. m_content_margins = margins;
  784. invalidate_layout();
  785. }
  786. Gfx::IntRect Widget::content_rect() const
  787. {
  788. auto rect = relative_rect();
  789. rect.move_by(m_content_margins.left(), m_content_margins.top());
  790. rect.set_width(rect.width() - (m_content_margins.left() + m_content_margins.right()));
  791. rect.set_height(rect.height() - (m_content_margins.top() + m_content_margins.bottom()));
  792. return rect;
  793. }
  794. void Widget::set_tooltip(const StringView& tooltip)
  795. {
  796. m_tooltip = tooltip;
  797. if (GUI::Application::the()->tooltip_source_widget() == this)
  798. show_tooltip();
  799. }
  800. void Widget::show_tooltip()
  801. {
  802. if (has_tooltip())
  803. Application::the()->show_tooltip(m_tooltip, screen_relative_rect().center().translated(0, height() / 2), this);
  804. }
  805. Gfx::IntRect Widget::children_clip_rect() const
  806. {
  807. return rect();
  808. }
  809. void Widget::set_override_cursor(Gfx::StandardCursor cursor)
  810. {
  811. if (m_override_cursor == cursor)
  812. return;
  813. m_override_cursor = cursor;
  814. if (auto* window = this->window())
  815. window->update_cursor({});
  816. }
  817. bool Widget::load_from_json(const StringView& json_string)
  818. {
  819. auto json_value = JsonValue::from_string(json_string);
  820. if (!json_value.has_value()) {
  821. dbg() << "load_from_json parse failed: _" << json_string << "_";
  822. return false;
  823. }
  824. if (!json_value.value().is_object()) {
  825. dbg() << "load_from_json parse non-object";
  826. return false;
  827. }
  828. return load_from_json(json_value.value().as_object());
  829. }
  830. bool Widget::load_from_json(const JsonObject& json)
  831. {
  832. json.for_each_member([&](auto& key, auto& value) {
  833. set_property(key, value);
  834. });
  835. auto layout_value = json.get("layout");
  836. if (!layout_value.is_null() && !layout_value.is_object()) {
  837. dbg() << "layout is not an object";
  838. return false;
  839. }
  840. if (layout_value.is_object()) {
  841. auto& layout = layout_value.as_object();
  842. auto class_name = layout.get("class");
  843. if (class_name.is_null()) {
  844. dbg() << "Invalid layout class name";
  845. return false;
  846. }
  847. if (class_name.to_string() == "GUI::VerticalBoxLayout") {
  848. set_layout<GUI::VerticalBoxLayout>();
  849. } else if (class_name.to_string() == "GUI::HorizontalBoxLayout") {
  850. set_layout<GUI::HorizontalBoxLayout>();
  851. } else {
  852. dbg() << "Unknown layout class: '" << class_name.to_string() << "'";
  853. return false;
  854. }
  855. layout.for_each_member([&](auto& key, auto& value) {
  856. this->layout()->set_property(key, value);
  857. });
  858. }
  859. auto children = json.get("children");
  860. if (children.is_array()) {
  861. for (auto& child_json_value : children.as_array().values()) {
  862. if (!child_json_value.is_object())
  863. return false;
  864. auto& child_json = child_json_value.as_object();
  865. auto class_name = child_json.get("class");
  866. if (!class_name.is_string()) {
  867. dbg() << "No class name in entry";
  868. return false;
  869. }
  870. auto* registration = WidgetClassRegistration::find(class_name.as_string());
  871. if (!registration) {
  872. dbg() << "Class '" << class_name.as_string() << "' not registered";
  873. return false;
  874. }
  875. auto child_widget = registration->construct();
  876. add_child(*child_widget);
  877. child_widget->load_from_json(child_json);
  878. }
  879. }
  880. return true;
  881. }
  882. Widget* Widget::find_child_by_name(const String& name)
  883. {
  884. Widget* found_widget = nullptr;
  885. for_each_child_widget([&](auto& child) {
  886. if (child.name() == name) {
  887. found_widget = &child;
  888. return IterationDecision::Break;
  889. }
  890. return IterationDecision::Continue;
  891. });
  892. return found_widget;
  893. }
  894. Widget* Widget::find_descendant_by_name(const String& name)
  895. {
  896. Widget* found_widget = nullptr;
  897. if (this->name() == name)
  898. return this;
  899. for_each_child_widget([&](auto& child) {
  900. found_widget = child.find_descendant_by_name(name);
  901. if (found_widget)
  902. return IterationDecision::Break;
  903. return IterationDecision::Continue;
  904. });
  905. return found_widget;
  906. }
  907. bool Widget::has_focus_within() const
  908. {
  909. auto* window = this->window();
  910. if (!window)
  911. return false;
  912. if (!window->focused_widget())
  913. return false;
  914. auto& effective_focus_widget = focus_proxy() ? *focus_proxy() : *this;
  915. return window->focused_widget() == &effective_focus_widget || is_ancestor_of(*window->focused_widget());
  916. }
  917. }