Widget.cpp 28 KB

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