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