Widget.cpp 29 KB

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