Widget.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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/Event.h>
  32. #include <LibGUI/GMLParser.h>
  33. #include <LibGUI/Layout.h>
  34. #include <LibGUI/Menu.h>
  35. #include <LibGUI/Painter.h>
  36. #include <LibGUI/Widget.h>
  37. #include <LibGUI/Window.h>
  38. #include <LibGUI/WindowServerConnection.h>
  39. #include <LibGfx/Bitmap.h>
  40. #include <LibGfx/Font.h>
  41. #include <LibGfx/FontDatabase.h>
  42. #include <LibGfx/Palette.h>
  43. #include <unistd.h>
  44. REGISTER_WIDGET(GUI, Widget)
  45. namespace GUI {
  46. static HashMap<String, WidgetClassRegistration*>& widget_classes()
  47. {
  48. static HashMap<String, WidgetClassRegistration*>* map;
  49. if (!map)
  50. map = new HashMap<String, WidgetClassRegistration*>;
  51. return *map;
  52. }
  53. WidgetClassRegistration::WidgetClassRegistration(const String& class_name, Function<NonnullRefPtr<Widget>()> factory)
  54. : m_class_name(class_name)
  55. , m_factory(move(factory))
  56. {
  57. widget_classes().set(class_name, this);
  58. }
  59. WidgetClassRegistration::~WidgetClassRegistration()
  60. {
  61. }
  62. void WidgetClassRegistration::for_each(Function<void(const WidgetClassRegistration&)> callback)
  63. {
  64. for (auto& it : widget_classes()) {
  65. callback(*it.value);
  66. }
  67. }
  68. const WidgetClassRegistration* WidgetClassRegistration::find(const String& class_name)
  69. {
  70. return widget_classes().get(class_name).value_or(nullptr);
  71. }
  72. Widget::Widget()
  73. : Core::Object(nullptr)
  74. , m_background_role(Gfx::ColorRole::Window)
  75. , m_foreground_role(Gfx::ColorRole::WindowText)
  76. , m_font(Gfx::FontDatabase::default_font())
  77. , m_palette(Application::the()->palette().impl())
  78. {
  79. REGISTER_RECT_PROPERTY("relative_rect", relative_rect, set_relative_rect);
  80. REGISTER_BOOL_PROPERTY("fill_with_background_color", fill_with_background_color, set_fill_with_background_color);
  81. REGISTER_BOOL_PROPERTY("visible", is_visible, set_visible);
  82. REGISTER_BOOL_PROPERTY("focused", is_focused, set_focus);
  83. REGISTER_BOOL_PROPERTY("enabled", is_enabled, set_enabled);
  84. REGISTER_STRING_PROPERTY("tooltip", tooltip, set_tooltip);
  85. REGISTER_SIZE_PROPERTY("min_size", min_size, set_min_size);
  86. REGISTER_SIZE_PROPERTY("max_size", max_size, set_max_size);
  87. REGISTER_INT_PROPERTY("width", width, set_width);
  88. REGISTER_INT_PROPERTY("min_width", min_width, set_min_width);
  89. REGISTER_INT_PROPERTY("max_width", max_width, set_max_width);
  90. REGISTER_INT_PROPERTY("min_height", min_height, set_min_height);
  91. REGISTER_INT_PROPERTY("height", height, set_height);
  92. REGISTER_INT_PROPERTY("max_height", max_height, set_max_height);
  93. REGISTER_INT_PROPERTY("fixed_width", dummy_fixed_width, set_fixed_width);
  94. REGISTER_INT_PROPERTY("fixed_height", dummy_fixed_height, set_fixed_height);
  95. REGISTER_SIZE_PROPERTY("fixed_size", dummy_fixed_size, set_fixed_size);
  96. REGISTER_BOOL_PROPERTY("shrink_to_fit", is_shrink_to_fit, set_shrink_to_fit);
  97. REGISTER_INT_PROPERTY("x", x, set_x);
  98. REGISTER_INT_PROPERTY("y", y, set_y);
  99. register_property(
  100. "focus_policy", [this]() -> JsonValue {
  101. auto policy = focus_policy();
  102. if (policy == GUI::FocusPolicy::ClickFocus)
  103. return "ClickFocus";
  104. if (policy == GUI::FocusPolicy::NoFocus)
  105. return "NoFocus";
  106. if (policy == GUI::FocusPolicy::TabFocus)
  107. return "TabFocus";
  108. if (policy == GUI::FocusPolicy::StrongFocus)
  109. return "StrongFocus";
  110. return JsonValue(); },
  111. [this](auto& value) {
  112. if (!value.is_string())
  113. return false;
  114. if (value.as_string() == "ClickFocus") {
  115. set_focus_policy(GUI::FocusPolicy::ClickFocus);
  116. return true;
  117. }
  118. if (value.as_string() == "NoFocus") {
  119. set_focus_policy(GUI::FocusPolicy::NoFocus);
  120. return true;
  121. }
  122. if (value.as_string() == "TabFocus") {
  123. set_focus_policy(GUI::FocusPolicy::TabFocus);
  124. return true;
  125. }
  126. if (value.as_string() == "StrongFocus") {
  127. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  128. return true;
  129. }
  130. return false;
  131. });
  132. register_property(
  133. "foreground_color", [this]() -> JsonValue { return palette().color(foreground_role()).to_string(); },
  134. [this](auto& value) {
  135. auto c = Color::from_string(value.to_string());
  136. if (c.has_value()) {
  137. auto _palette = palette();
  138. _palette.set_color(foreground_role(), c.value());
  139. set_palette(_palette);
  140. return true;
  141. }
  142. return false;
  143. });
  144. register_property(
  145. "background_color", [this]() -> JsonValue { return palette().color(background_role()).to_string(); },
  146. [this](auto& value) {
  147. auto c = Color::from_string(value.to_string());
  148. if (c.has_value()) {
  149. auto _palette = palette();
  150. _palette.set_color(background_role(), c.value());
  151. set_palette(_palette);
  152. return true;
  153. }
  154. return false;
  155. });
  156. }
  157. Widget::~Widget()
  158. {
  159. }
  160. void Widget::child_event(Core::ChildEvent& event)
  161. {
  162. if (event.type() == Event::ChildAdded) {
  163. if (event.child() && is<Widget>(*event.child()) && layout()) {
  164. if (event.insertion_before_child() && is<Widget>(event.insertion_before_child()))
  165. layout()->insert_widget_before(downcast<Widget>(*event.child()), downcast<Widget>(*event.insertion_before_child()));
  166. else
  167. layout()->add_widget(downcast<Widget>(*event.child()));
  168. }
  169. if (window() && event.child() && is<Widget>(*event.child()))
  170. window()->did_add_widget({}, downcast<Widget>(*event.child()));
  171. }
  172. if (event.type() == Event::ChildRemoved) {
  173. if (layout()) {
  174. if (event.child() && is<Widget>(*event.child()))
  175. layout()->remove_widget(downcast<Widget>(*event.child()));
  176. else
  177. invalidate_layout();
  178. }
  179. if (window() && event.child() && is<Widget>(*event.child()))
  180. window()->did_remove_widget({}, downcast<Widget>(*event.child()));
  181. update();
  182. }
  183. return Core::Object::child_event(event);
  184. }
  185. void Widget::set_relative_rect(const Gfx::IntRect& a_rect)
  186. {
  187. // Get rid of negative width/height values.
  188. Gfx::IntRect rect = {
  189. a_rect.x(),
  190. a_rect.y(),
  191. max(a_rect.width(), 0),
  192. max(a_rect.height(), 0)
  193. };
  194. if (rect == m_relative_rect)
  195. return;
  196. auto old_rect = m_relative_rect;
  197. bool size_changed = m_relative_rect.size() != rect.size();
  198. m_relative_rect = rect;
  199. if (size_changed) {
  200. ResizeEvent resize_event(rect.size());
  201. event(resize_event);
  202. }
  203. if (auto* parent = parent_widget())
  204. parent->update(old_rect);
  205. update();
  206. }
  207. void Widget::event(Core::Event& event)
  208. {
  209. if (!is_enabled()) {
  210. switch (event.type()) {
  211. case Event::MouseUp:
  212. case Event::MouseDown:
  213. case Event::MouseMove:
  214. case Event::MouseWheel:
  215. case Event::MouseDoubleClick:
  216. case Event::KeyUp:
  217. case Event::KeyDown:
  218. return;
  219. default:
  220. break;
  221. }
  222. }
  223. switch (event.type()) {
  224. case Event::Paint:
  225. return handle_paint_event(static_cast<PaintEvent&>(event));
  226. case Event::Resize:
  227. return handle_resize_event(static_cast<ResizeEvent&>(event));
  228. case Event::FocusIn:
  229. return focusin_event(static_cast<FocusEvent&>(event));
  230. case Event::FocusOut:
  231. return focusout_event(static_cast<FocusEvent&>(event));
  232. case Event::Show:
  233. return show_event(static_cast<ShowEvent&>(event));
  234. case Event::Hide:
  235. return hide_event(static_cast<HideEvent&>(event));
  236. case Event::KeyDown:
  237. return handle_keydown_event(static_cast<KeyEvent&>(event));
  238. case Event::KeyUp:
  239. return keyup_event(static_cast<KeyEvent&>(event));
  240. case Event::MouseMove:
  241. return mousemove_event(static_cast<MouseEvent&>(event));
  242. case Event::MouseDown:
  243. return handle_mousedown_event(static_cast<MouseEvent&>(event));
  244. case Event::MouseDoubleClick:
  245. return handle_mousedoubleclick_event(static_cast<MouseEvent&>(event));
  246. case Event::MouseUp:
  247. return handle_mouseup_event(static_cast<MouseEvent&>(event));
  248. case Event::MouseWheel:
  249. return mousewheel_event(static_cast<MouseEvent&>(event));
  250. case Event::DragEnter:
  251. return drag_enter_event(static_cast<DragEvent&>(event));
  252. case Event::DragMove:
  253. return drag_move_event(static_cast<DragEvent&>(event));
  254. case Event::DragLeave:
  255. return drag_leave_event(static_cast<Event&>(event));
  256. case Event::Drop:
  257. return drop_event(static_cast<DropEvent&>(event));
  258. case Event::ThemeChange:
  259. return theme_change_event(static_cast<ThemeChangeEvent&>(event));
  260. case Event::Enter:
  261. return handle_enter_event(event);
  262. case Event::Leave:
  263. return handle_leave_event(event);
  264. case Event::EnabledChange:
  265. return change_event(static_cast<Event&>(event));
  266. default:
  267. return Core::Object::event(event);
  268. }
  269. }
  270. void Widget::handle_keydown_event(KeyEvent& event)
  271. {
  272. keydown_event(event);
  273. if (event.key() == KeyCode::Key_Menu) {
  274. ContextMenuEvent c_event(window_relative_rect().bottom_right(), screen_relative_rect().bottom_right());
  275. context_menu_event(c_event);
  276. }
  277. }
  278. void Widget::handle_paint_event(PaintEvent& event)
  279. {
  280. ASSERT(is_visible());
  281. if (fill_with_background_color()) {
  282. Painter painter(*this);
  283. painter.fill_rect(event.rect(), palette().color(background_role()));
  284. }
  285. paint_event(event);
  286. auto children_clip_rect = this->children_clip_rect();
  287. for_each_child_widget([&](auto& child) {
  288. if (!child.is_visible())
  289. return IterationDecision::Continue;
  290. if (child.relative_rect().intersects(event.rect())) {
  291. PaintEvent local_event(event.rect().intersected(children_clip_rect).intersected(child.relative_rect()).translated(-child.relative_position()));
  292. child.dispatch_event(local_event, this);
  293. }
  294. return IterationDecision::Continue;
  295. });
  296. second_paint_event(event);
  297. auto* app = Application::the();
  298. if (app && app->dnd_debugging_enabled() && has_pending_drop()) {
  299. Painter painter(*this);
  300. painter.draw_rect(rect(), Color::Blue);
  301. }
  302. if (app && app->focus_debugging_enabled() && is_focused()) {
  303. Painter painter(*this);
  304. painter.draw_rect(rect(), Color::Cyan);
  305. }
  306. if (is_being_inspected()) {
  307. Painter painter(*this);
  308. painter.draw_rect(rect(), Color::Magenta);
  309. }
  310. }
  311. void Widget::set_layout(NonnullRefPtr<Layout> layout)
  312. {
  313. if (m_layout) {
  314. m_layout->notify_disowned({}, *this);
  315. m_layout->remove_from_parent();
  316. }
  317. m_layout = move(layout);
  318. if (m_layout) {
  319. add_child(*m_layout);
  320. m_layout->notify_adopted({}, *this);
  321. do_layout();
  322. } else {
  323. update();
  324. }
  325. }
  326. void Widget::do_layout()
  327. {
  328. for_each_child_widget([&](auto& child) {
  329. child.do_layout();
  330. return IterationDecision::Continue;
  331. });
  332. custom_layout();
  333. if (!m_layout)
  334. return;
  335. m_layout->run(*this);
  336. did_layout();
  337. update();
  338. }
  339. void Widget::notify_layout_changed(Badge<Layout>)
  340. {
  341. invalidate_layout();
  342. }
  343. void Widget::handle_resize_event(ResizeEvent& event)
  344. {
  345. resize_event(event);
  346. do_layout();
  347. }
  348. void Widget::handle_mouseup_event(MouseEvent& event)
  349. {
  350. mouseup_event(event);
  351. }
  352. void Widget::handle_mousedown_event(MouseEvent& event)
  353. {
  354. if (((unsigned)focus_policy() & (unsigned)FocusPolicy::ClickFocus))
  355. set_focus(true, FocusSource::Mouse);
  356. mousedown_event(event);
  357. if (event.button() == MouseButton::Right) {
  358. ContextMenuEvent c_event(event.position(), screen_relative_rect().location().translated(event.position()));
  359. context_menu_event(c_event);
  360. }
  361. }
  362. void Widget::handle_mousedoubleclick_event(MouseEvent& event)
  363. {
  364. doubleclick_event(event);
  365. }
  366. void Widget::handle_enter_event(Core::Event& event)
  367. {
  368. if (auto* window = this->window())
  369. window->update_cursor({});
  370. show_or_hide_tooltip();
  371. enter_event(event);
  372. }
  373. void Widget::handle_leave_event(Core::Event& event)
  374. {
  375. if (auto* window = this->window())
  376. window->update_cursor({});
  377. Application::the()->hide_tooltip();
  378. leave_event(event);
  379. }
  380. void Widget::doubleclick_event(MouseEvent&)
  381. {
  382. }
  383. void Widget::resize_event(ResizeEvent&)
  384. {
  385. }
  386. void Widget::paint_event(PaintEvent&)
  387. {
  388. }
  389. void Widget::second_paint_event(PaintEvent&)
  390. {
  391. }
  392. void Widget::show_event(ShowEvent&)
  393. {
  394. }
  395. void Widget::hide_event(HideEvent&)
  396. {
  397. }
  398. void Widget::keydown_event(KeyEvent& event)
  399. {
  400. if (!event.alt() && !event.ctrl() && !event.logo()) {
  401. if (event.key() == KeyCode::Key_Tab) {
  402. if (event.shift())
  403. focus_previous_widget(FocusSource::Keyboard, false);
  404. else
  405. focus_next_widget(FocusSource::Keyboard, false);
  406. event.accept();
  407. return;
  408. }
  409. if (!event.shift() && (event.key() == KeyCode::Key_Left || event.key() == KeyCode::Key_Up)) {
  410. focus_previous_widget(FocusSource::Keyboard, true);
  411. event.accept();
  412. return;
  413. }
  414. if (!event.shift() && (event.key() == KeyCode::Key_Right || event.key() == KeyCode::Key_Down)) {
  415. focus_next_widget(FocusSource::Keyboard, true);
  416. event.accept();
  417. return;
  418. }
  419. }
  420. event.ignore();
  421. }
  422. void Widget::keyup_event(KeyEvent& event)
  423. {
  424. event.ignore();
  425. }
  426. void Widget::mousedown_event(MouseEvent&)
  427. {
  428. }
  429. void Widget::mouseup_event(MouseEvent&)
  430. {
  431. }
  432. void Widget::mousemove_event(MouseEvent&)
  433. {
  434. }
  435. void Widget::mousewheel_event(MouseEvent&)
  436. {
  437. }
  438. void Widget::context_menu_event(ContextMenuEvent&)
  439. {
  440. }
  441. void Widget::focusin_event(FocusEvent&)
  442. {
  443. }
  444. void Widget::focusout_event(FocusEvent&)
  445. {
  446. }
  447. void Widget::enter_event(Core::Event&)
  448. {
  449. }
  450. void Widget::leave_event(Core::Event&)
  451. {
  452. }
  453. void Widget::change_event(Event&)
  454. {
  455. }
  456. void Widget::drag_move_event(DragEvent&)
  457. {
  458. }
  459. void Widget::drag_enter_event(DragEvent& event)
  460. {
  461. StringBuilder builder;
  462. builder.join(',', event.mime_types());
  463. dbgln("{} {:p} DRAG ENTER @ {}, {}", class_name(), this, event.position(), builder.string_view());
  464. }
  465. void Widget::drag_leave_event(Event&)
  466. {
  467. dbgln("{} {:p} DRAG LEAVE", class_name(), this);
  468. }
  469. void Widget::drop_event(DropEvent& event)
  470. {
  471. dbgln("{} {:p} DROP @ {}, '{}'", class_name(), this, event.position(), event.text());
  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 (Application::the()->tooltip_source_widget() == this)
  822. show_or_hide_tooltip();
  823. }
  824. void Widget::show_or_hide_tooltip()
  825. {
  826. if (has_tooltip())
  827. Application::the()->show_tooltip(m_tooltip, this);
  828. else
  829. Application::the()->hide_tooltip();
  830. }
  831. Gfx::IntRect Widget::children_clip_rect() const
  832. {
  833. return rect();
  834. }
  835. void Widget::set_override_cursor(Gfx::StandardCursor cursor)
  836. {
  837. if (m_override_cursor == cursor)
  838. return;
  839. m_override_cursor = cursor;
  840. if (auto* window = this->window())
  841. window->update_cursor({});
  842. }
  843. bool Widget::load_from_gml(const StringView& gml_string)
  844. {
  845. return load_from_gml(gml_string, [](const String& class_name) -> RefPtr<Widget> {
  846. dbgln("Class '{}' not registered", class_name);
  847. return nullptr;
  848. });
  849. }
  850. bool Widget::load_from_gml(const StringView& gml_string, RefPtr<Widget> (*unregistered_child_handler)(const String&))
  851. {
  852. auto value = parse_gml(gml_string);
  853. if (!value.is_object())
  854. return false;
  855. return load_from_json(value.as_object(), unregistered_child_handler);
  856. }
  857. bool Widget::load_from_json(const JsonObject& json, RefPtr<Widget> (*unregistered_child_handler)(const String&))
  858. {
  859. json.for_each_member([&](auto& key, auto& value) {
  860. set_property(key, value);
  861. });
  862. auto layout_value = json.get("layout");
  863. if (!layout_value.is_null() && !layout_value.is_object()) {
  864. dbgln("layout is not an object");
  865. return false;
  866. }
  867. if (layout_value.is_object()) {
  868. auto& layout = layout_value.as_object();
  869. auto class_name = layout.get("class");
  870. if (class_name.is_null()) {
  871. dbgln("Invalid layout class name");
  872. return false;
  873. }
  874. if (class_name.to_string() == "GUI::VerticalBoxLayout") {
  875. set_layout<GUI::VerticalBoxLayout>();
  876. } else if (class_name.to_string() == "GUI::HorizontalBoxLayout") {
  877. set_layout<GUI::HorizontalBoxLayout>();
  878. } else {
  879. dbgln("Unknown layout class: '{}'", class_name.to_string());
  880. return false;
  881. }
  882. layout.for_each_member([&](auto& key, auto& value) {
  883. this->layout()->set_property(key, value);
  884. });
  885. }
  886. auto children = json.get("children");
  887. if (children.is_array()) {
  888. for (auto& child_json_value : children.as_array().values()) {
  889. if (!child_json_value.is_object())
  890. return false;
  891. auto& child_json = child_json_value.as_object();
  892. auto class_name = child_json.get("class");
  893. if (!class_name.is_string()) {
  894. dbgln("No class name in entry");
  895. return false;
  896. }
  897. RefPtr<Widget> child_widget;
  898. if (auto* registration = WidgetClassRegistration::find(class_name.as_string())) {
  899. child_widget = registration->construct();
  900. } else {
  901. child_widget = unregistered_child_handler(class_name.as_string());
  902. if (!child_widget)
  903. return false;
  904. }
  905. add_child(*child_widget);
  906. child_widget->load_from_json(child_json, unregistered_child_handler);
  907. }
  908. }
  909. return true;
  910. }
  911. bool Widget::has_focus_within() const
  912. {
  913. auto* window = this->window();
  914. if (!window)
  915. return false;
  916. if (!window->focused_widget())
  917. return false;
  918. auto& effective_focus_widget = focus_proxy() ? *focus_proxy() : *this;
  919. return window->focused_widget() == &effective_focus_widget || is_ancestor_of(*window->focused_widget());
  920. }
  921. void Widget::set_shrink_to_fit(bool b)
  922. {
  923. if (m_shrink_to_fit == b)
  924. return;
  925. m_shrink_to_fit = b;
  926. invalidate_layout();
  927. }
  928. bool Widget::has_pending_drop() const
  929. {
  930. return Application::the()->pending_drop_widget() == this;
  931. }
  932. }