Widget.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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 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_paint_event(PaintEvent& event)
  271. {
  272. ASSERT(is_visible());
  273. if (fill_with_background_color()) {
  274. Painter painter(*this);
  275. painter.fill_rect(event.rect(), palette().color(background_role()));
  276. }
  277. paint_event(event);
  278. auto children_clip_rect = this->children_clip_rect();
  279. for_each_child_widget([&](auto& child) {
  280. if (!child.is_visible())
  281. return IterationDecision::Continue;
  282. if (child.relative_rect().intersects(event.rect())) {
  283. PaintEvent local_event(event.rect().intersected(children_clip_rect).intersected(child.relative_rect()).translated(-child.relative_position()));
  284. child.dispatch_event(local_event, this);
  285. }
  286. return IterationDecision::Continue;
  287. });
  288. second_paint_event(event);
  289. auto* app = Application::the();
  290. if (app && app->dnd_debugging_enabled() && has_pending_drop()) {
  291. Painter painter(*this);
  292. painter.draw_rect(rect(), Color::Blue);
  293. }
  294. if (app && app->focus_debugging_enabled() && is_focused()) {
  295. Painter painter(*this);
  296. painter.draw_rect(rect(), Color::Cyan);
  297. }
  298. if (is_being_inspected()) {
  299. Painter painter(*this);
  300. painter.draw_rect(rect(), Color::Magenta);
  301. }
  302. }
  303. void Widget::set_layout(NonnullRefPtr<Layout> layout)
  304. {
  305. if (m_layout) {
  306. m_layout->notify_disowned({}, *this);
  307. m_layout->remove_from_parent();
  308. }
  309. m_layout = move(layout);
  310. if (m_layout) {
  311. add_child(*m_layout);
  312. m_layout->notify_adopted({}, *this);
  313. do_layout();
  314. } else {
  315. update();
  316. }
  317. }
  318. void Widget::do_layout()
  319. {
  320. for_each_child_widget([&](auto& child) {
  321. child.do_layout();
  322. return IterationDecision::Continue;
  323. });
  324. custom_layout();
  325. if (!m_layout)
  326. return;
  327. m_layout->run(*this);
  328. did_layout();
  329. update();
  330. }
  331. void Widget::notify_layout_changed(Badge<Layout>)
  332. {
  333. invalidate_layout();
  334. }
  335. void Widget::handle_resize_event(ResizeEvent& event)
  336. {
  337. resize_event(event);
  338. do_layout();
  339. }
  340. void Widget::handle_mouseup_event(MouseEvent& event)
  341. {
  342. mouseup_event(event);
  343. }
  344. void Widget::handle_mousedown_event(MouseEvent& event)
  345. {
  346. if (((unsigned)focus_policy() & (unsigned)FocusPolicy::ClickFocus))
  347. set_focus(true, FocusSource::Mouse);
  348. mousedown_event(event);
  349. if (event.button() == MouseButton::Right) {
  350. ContextMenuEvent c_event(event.position(), screen_relative_rect().location().translated(event.position()));
  351. context_menu_event(c_event);
  352. }
  353. }
  354. void Widget::handle_mousedoubleclick_event(MouseEvent& event)
  355. {
  356. doubleclick_event(event);
  357. }
  358. void Widget::handle_enter_event(Core::Event& event)
  359. {
  360. if (auto* window = this->window())
  361. window->update_cursor({});
  362. show_or_hide_tooltip();
  363. enter_event(event);
  364. }
  365. void Widget::handle_leave_event(Core::Event& event)
  366. {
  367. if (auto* window = this->window())
  368. window->update_cursor({});
  369. Application::the()->hide_tooltip();
  370. leave_event(event);
  371. }
  372. void Widget::doubleclick_event(MouseEvent&)
  373. {
  374. }
  375. void Widget::resize_event(ResizeEvent&)
  376. {
  377. }
  378. void Widget::paint_event(PaintEvent&)
  379. {
  380. }
  381. void Widget::second_paint_event(PaintEvent&)
  382. {
  383. }
  384. void Widget::show_event(ShowEvent&)
  385. {
  386. }
  387. void Widget::hide_event(HideEvent&)
  388. {
  389. }
  390. void Widget::keydown_event(KeyEvent& event)
  391. {
  392. if (!event.alt() && !event.ctrl() && !event.logo()) {
  393. if (event.key() == KeyCode::Key_Tab) {
  394. if (event.shift())
  395. focus_previous_widget(FocusSource::Keyboard, false);
  396. else
  397. focus_next_widget(FocusSource::Keyboard, false);
  398. event.accept();
  399. return;
  400. }
  401. if (!event.shift() && (event.key() == KeyCode::Key_Left || event.key() == KeyCode::Key_Up)) {
  402. focus_previous_widget(FocusSource::Keyboard, true);
  403. event.accept();
  404. return;
  405. }
  406. if (!event.shift() && (event.key() == KeyCode::Key_Right || event.key() == KeyCode::Key_Down)) {
  407. focus_next_widget(FocusSource::Keyboard, true);
  408. event.accept();
  409. return;
  410. }
  411. }
  412. event.ignore();
  413. }
  414. void Widget::keyup_event(KeyEvent& event)
  415. {
  416. event.ignore();
  417. }
  418. void Widget::mousedown_event(MouseEvent&)
  419. {
  420. }
  421. void Widget::mouseup_event(MouseEvent&)
  422. {
  423. }
  424. void Widget::mousemove_event(MouseEvent&)
  425. {
  426. }
  427. void Widget::mousewheel_event(MouseEvent&)
  428. {
  429. }
  430. void Widget::context_menu_event(ContextMenuEvent&)
  431. {
  432. }
  433. void Widget::focusin_event(FocusEvent&)
  434. {
  435. }
  436. void Widget::focusout_event(FocusEvent&)
  437. {
  438. }
  439. void Widget::enter_event(Core::Event&)
  440. {
  441. }
  442. void Widget::leave_event(Core::Event&)
  443. {
  444. }
  445. void Widget::change_event(Event&)
  446. {
  447. }
  448. void Widget::drag_move_event(DragEvent&)
  449. {
  450. }
  451. void Widget::drag_enter_event(DragEvent& event)
  452. {
  453. StringBuilder builder;
  454. builder.join(',', event.mime_types());
  455. dbgln("{} {:p} DRAG ENTER @ {}, {}", class_name(), this, event.position(), builder.string_view());
  456. }
  457. void Widget::drag_leave_event(Event&)
  458. {
  459. dbgln("{} {:p} DRAG LEAVE", class_name(), this);
  460. }
  461. void Widget::drop_event(DropEvent& event)
  462. {
  463. dbgln("{} {:p} DROP @ {}, '{}'", class_name(), this, event.position(), event.text());
  464. }
  465. void Widget::theme_change_event(ThemeChangeEvent&)
  466. {
  467. }
  468. void Widget::update()
  469. {
  470. if (rect().is_empty())
  471. return;
  472. update(rect());
  473. }
  474. void Widget::update(const Gfx::IntRect& rect)
  475. {
  476. if (!is_visible())
  477. return;
  478. if (!updates_enabled())
  479. return;
  480. Window* window = m_window;
  481. Widget* parent = parent_widget();
  482. while (parent) {
  483. if (!parent->updates_enabled())
  484. return;
  485. window = parent->m_window;
  486. parent = parent->parent_widget();
  487. }
  488. if (window)
  489. window->update(rect.translated(window_relative_rect().location()));
  490. }
  491. Gfx::IntRect Widget::window_relative_rect() const
  492. {
  493. auto rect = relative_rect();
  494. for (auto* parent = parent_widget(); parent; parent = parent->parent_widget()) {
  495. rect.move_by(parent->relative_position());
  496. }
  497. return rect;
  498. }
  499. Gfx::IntRect Widget::screen_relative_rect() const
  500. {
  501. auto window_position = window()->window_type() == WindowType::MenuApplet
  502. ? window()->rect_in_menubar().location()
  503. : window()->rect().location();
  504. return window_relative_rect().translated(window_position);
  505. }
  506. Widget* Widget::child_at(const Gfx::IntPoint& point) const
  507. {
  508. for (int i = children().size() - 1; i >= 0; --i) {
  509. if (!is<Widget>(children()[i]))
  510. continue;
  511. auto& child = downcast<Widget>(children()[i]);
  512. if (!child.is_visible())
  513. continue;
  514. if (child.content_rect().contains(point))
  515. return const_cast<Widget*>(&child);
  516. }
  517. return nullptr;
  518. }
  519. Widget::HitTestResult Widget::hit_test(const Gfx::IntPoint& position, ShouldRespectGreediness should_respect_greediness)
  520. {
  521. if (should_respect_greediness == ShouldRespectGreediness::Yes && is_greedy_for_hits())
  522. return { this, position };
  523. if (auto* child = child_at(position))
  524. return child->hit_test(position - child->relative_position());
  525. return { this, position };
  526. }
  527. void Widget::set_window(Window* window)
  528. {
  529. if (m_window == window)
  530. return;
  531. m_window = window;
  532. }
  533. void Widget::set_focus_proxy(Widget* proxy)
  534. {
  535. if (m_focus_proxy == proxy)
  536. return;
  537. m_focus_proxy = proxy;
  538. }
  539. FocusPolicy Widget::focus_policy() const
  540. {
  541. if (m_focus_proxy)
  542. return m_focus_proxy->focus_policy();
  543. return m_focus_policy;
  544. }
  545. void Widget::set_focus_policy(FocusPolicy policy)
  546. {
  547. if (m_focus_proxy)
  548. return m_focus_proxy->set_focus_policy(policy);
  549. m_focus_policy = policy;
  550. }
  551. bool Widget::is_focused() const
  552. {
  553. if (m_focus_proxy)
  554. return m_focus_proxy->is_focused();
  555. auto* win = window();
  556. if (!win)
  557. return false;
  558. // Accessory windows are not active despite being the active
  559. // input window. So we can have focus if either we're the active
  560. // input window or we're the active window
  561. if (win->is_active_input() || win->is_active())
  562. return win->focused_widget() == this;
  563. return false;
  564. }
  565. void Widget::set_focus(bool focus, FocusSource source)
  566. {
  567. if (m_focus_proxy)
  568. return m_focus_proxy->set_focus(focus, source);
  569. auto* win = window();
  570. if (!win)
  571. return;
  572. if (focus) {
  573. win->set_focused_widget(this, source);
  574. } else {
  575. if (win->focused_widget() == this)
  576. win->set_focused_widget(nullptr, source);
  577. }
  578. }
  579. void Widget::set_font(const Gfx::Font* font)
  580. {
  581. if (m_font.ptr() == font)
  582. return;
  583. if (!font)
  584. m_font = Gfx::FontDatabase::default_font();
  585. else
  586. m_font = *font;
  587. did_change_font();
  588. update();
  589. }
  590. void Widget::set_global_cursor_tracking(bool enabled)
  591. {
  592. auto* win = window();
  593. if (!win)
  594. return;
  595. win->set_global_cursor_tracking_widget(enabled ? this : nullptr);
  596. }
  597. bool Widget::global_cursor_tracking() const
  598. {
  599. auto* win = window();
  600. if (!win)
  601. return false;
  602. return win->global_cursor_tracking_widget() == this;
  603. }
  604. void Widget::set_min_size(const Gfx::IntSize& size)
  605. {
  606. if (m_min_size == size)
  607. return;
  608. m_min_size = size;
  609. invalidate_layout();
  610. }
  611. void Widget::set_max_size(const Gfx::IntSize& size)
  612. {
  613. if (m_max_size == size)
  614. return;
  615. m_max_size = size;
  616. invalidate_layout();
  617. }
  618. void Widget::invalidate_layout()
  619. {
  620. if (window())
  621. window()->schedule_relayout();
  622. }
  623. void Widget::set_visible(bool visible)
  624. {
  625. if (visible == m_visible)
  626. return;
  627. m_visible = visible;
  628. if (auto* parent = parent_widget())
  629. parent->invalidate_layout();
  630. if (m_visible)
  631. update();
  632. if (m_visible) {
  633. ShowEvent e;
  634. event(e);
  635. } else {
  636. HideEvent e;
  637. event(e);
  638. }
  639. }
  640. bool Widget::spans_entire_window_horizontally() const
  641. {
  642. auto* w = window();
  643. if (!w)
  644. return false;
  645. auto* main_widget = w->main_widget();
  646. if (!main_widget)
  647. return false;
  648. if (main_widget == this)
  649. return true;
  650. auto wrr = window_relative_rect();
  651. return wrr.left() == main_widget->rect().left() && wrr.right() == main_widget->rect().right();
  652. }
  653. void Widget::set_enabled(bool enabled)
  654. {
  655. if (m_enabled == enabled)
  656. return;
  657. m_enabled = enabled;
  658. for_each_child_widget([enabled](auto& child) {
  659. child.set_enabled(enabled);
  660. return IterationDecision::Continue;
  661. });
  662. if (!m_enabled && window() && window()->focused_widget() == this) {
  663. window()->did_disable_focused_widget({});
  664. }
  665. Event e(Event::EnabledChange);
  666. event(e);
  667. update();
  668. }
  669. void Widget::move_to_front()
  670. {
  671. auto* parent = parent_widget();
  672. if (!parent)
  673. return;
  674. if (parent->children().size() == 1)
  675. return;
  676. parent->children().remove_first_matching([this](auto& entry) {
  677. return entry == this;
  678. });
  679. parent->children().append(*this);
  680. parent->update();
  681. }
  682. void Widget::move_to_back()
  683. {
  684. auto* parent = parent_widget();
  685. if (!parent)
  686. return;
  687. if (parent->children().size() == 1)
  688. return;
  689. parent->children().remove_first_matching([this](auto& entry) {
  690. return entry == this;
  691. });
  692. parent->children().prepend(*this);
  693. parent->update();
  694. }
  695. bool Widget::is_frontmost() const
  696. {
  697. auto* parent = parent_widget();
  698. if (!parent)
  699. return true;
  700. return &parent->children().last() == this;
  701. }
  702. bool Widget::is_backmost() const
  703. {
  704. auto* parent = parent_widget();
  705. if (!parent)
  706. return true;
  707. return &parent->children().first() == this;
  708. }
  709. Action* Widget::action_for_key_event(const KeyEvent& event)
  710. {
  711. Shortcut shortcut(event.modifiers(), (KeyCode)event.key());
  712. if (!shortcut.is_valid()) {
  713. return nullptr;
  714. }
  715. Action* found_action = nullptr;
  716. for_each_child_of_type<Action>([&](auto& action) {
  717. if (action.shortcut() == shortcut) {
  718. found_action = &action;
  719. return IterationDecision::Break;
  720. }
  721. return IterationDecision::Continue;
  722. });
  723. return found_action;
  724. }
  725. void Widget::set_updates_enabled(bool enabled)
  726. {
  727. if (m_updates_enabled == enabled)
  728. return;
  729. m_updates_enabled = enabled;
  730. if (enabled)
  731. update();
  732. }
  733. void Widget::focus_previous_widget(FocusSource source, bool siblings_only)
  734. {
  735. auto focusable_widgets = window()->focusable_widgets(source);
  736. if (siblings_only)
  737. focusable_widgets.remove_all_matching([this](auto& entry) { return entry->parent() != parent(); });
  738. for (int i = focusable_widgets.size() - 1; i >= 0; --i) {
  739. if (focusable_widgets[i] != this)
  740. continue;
  741. if (i > 0)
  742. focusable_widgets[i - 1]->set_focus(true, source);
  743. else
  744. focusable_widgets.last()->set_focus(true, source);
  745. }
  746. }
  747. void Widget::focus_next_widget(FocusSource source, bool siblings_only)
  748. {
  749. auto focusable_widgets = window()->focusable_widgets(source);
  750. if (siblings_only)
  751. focusable_widgets.remove_all_matching([this](auto& entry) { return entry->parent() != parent(); });
  752. for (size_t i = 0; i < focusable_widgets.size(); ++i) {
  753. if (focusable_widgets[i] != this)
  754. continue;
  755. if (i < focusable_widgets.size() - 1)
  756. focusable_widgets[i + 1]->set_focus(true, source);
  757. else
  758. focusable_widgets.first()->set_focus(true, source);
  759. }
  760. }
  761. Vector<Widget*> Widget::child_widgets() const
  762. {
  763. Vector<Widget*> widgets;
  764. widgets.ensure_capacity(children().size());
  765. for (auto& child : const_cast<Widget*>(this)->children()) {
  766. if (is<Widget>(child))
  767. widgets.append(static_cast<Widget*>(&child));
  768. }
  769. return widgets;
  770. }
  771. void Widget::set_palette(const Palette& palette)
  772. {
  773. m_palette = palette.impl();
  774. }
  775. void Widget::set_background_role(ColorRole role)
  776. {
  777. m_background_role = role;
  778. }
  779. void Widget::set_foreground_role(ColorRole role)
  780. {
  781. m_foreground_role = role;
  782. }
  783. Gfx::Palette Widget::palette() const
  784. {
  785. return Gfx::Palette(*m_palette);
  786. }
  787. void Widget::did_begin_inspection()
  788. {
  789. update();
  790. }
  791. void Widget::did_end_inspection()
  792. {
  793. update();
  794. }
  795. void Widget::set_content_margins(const Margins& margins)
  796. {
  797. if (m_content_margins == margins)
  798. return;
  799. m_content_margins = margins;
  800. invalidate_layout();
  801. }
  802. Gfx::IntRect Widget::content_rect() const
  803. {
  804. auto rect = relative_rect();
  805. rect.move_by(m_content_margins.left(), m_content_margins.top());
  806. rect.set_width(rect.width() - (m_content_margins.left() + m_content_margins.right()));
  807. rect.set_height(rect.height() - (m_content_margins.top() + m_content_margins.bottom()));
  808. return rect;
  809. }
  810. void Widget::set_tooltip(const StringView& tooltip)
  811. {
  812. m_tooltip = tooltip;
  813. if (Application::the()->tooltip_source_widget() == this)
  814. show_or_hide_tooltip();
  815. }
  816. void Widget::show_or_hide_tooltip()
  817. {
  818. if (has_tooltip())
  819. Application::the()->show_tooltip(m_tooltip, this);
  820. else
  821. Application::the()->hide_tooltip();
  822. }
  823. Gfx::IntRect Widget::children_clip_rect() const
  824. {
  825. return rect();
  826. }
  827. void Widget::set_override_cursor(Gfx::StandardCursor cursor)
  828. {
  829. if (m_override_cursor == cursor)
  830. return;
  831. m_override_cursor = cursor;
  832. if (auto* window = this->window())
  833. window->update_cursor({});
  834. }
  835. bool Widget::load_from_gml(const StringView& gml_string)
  836. {
  837. return load_from_gml(gml_string, [](const String& class_name) -> RefPtr<Widget> {
  838. dbgln("Class '{}' not registered", class_name);
  839. return nullptr;
  840. });
  841. }
  842. bool Widget::load_from_gml(const StringView& gml_string, RefPtr<Widget> (*unregistered_child_handler)(const String&))
  843. {
  844. auto value = parse_gml(gml_string);
  845. if (!value.is_object())
  846. return false;
  847. return load_from_json(value.as_object(), unregistered_child_handler);
  848. }
  849. bool Widget::load_from_json(const JsonObject& json, RefPtr<Widget> (*unregistered_child_handler)(const String&))
  850. {
  851. json.for_each_member([&](auto& key, auto& value) {
  852. set_property(key, value);
  853. });
  854. auto layout_value = json.get("layout");
  855. if (!layout_value.is_null() && !layout_value.is_object()) {
  856. dbgln("layout is not an object");
  857. return false;
  858. }
  859. if (layout_value.is_object()) {
  860. auto& layout = layout_value.as_object();
  861. auto class_name = layout.get("class");
  862. if (class_name.is_null()) {
  863. dbgln("Invalid layout class name");
  864. return false;
  865. }
  866. if (class_name.to_string() == "GUI::VerticalBoxLayout") {
  867. set_layout<GUI::VerticalBoxLayout>();
  868. } else if (class_name.to_string() == "GUI::HorizontalBoxLayout") {
  869. set_layout<GUI::HorizontalBoxLayout>();
  870. } else {
  871. dbgln("Unknown layout class: '{}'", class_name.to_string());
  872. return false;
  873. }
  874. layout.for_each_member([&](auto& key, auto& value) {
  875. this->layout()->set_property(key, value);
  876. });
  877. }
  878. auto children = json.get("children");
  879. if (children.is_array()) {
  880. for (auto& child_json_value : children.as_array().values()) {
  881. if (!child_json_value.is_object())
  882. return false;
  883. auto& child_json = child_json_value.as_object();
  884. auto class_name = child_json.get("class");
  885. if (!class_name.is_string()) {
  886. dbgln("No class name in entry");
  887. return false;
  888. }
  889. RefPtr<Widget> child_widget;
  890. if (auto* registration = WidgetClassRegistration::find(class_name.as_string())) {
  891. child_widget = registration->construct();
  892. } else {
  893. child_widget = unregistered_child_handler(class_name.as_string());
  894. if (!child_widget)
  895. return false;
  896. }
  897. add_child(*child_widget);
  898. child_widget->load_from_json(child_json, unregistered_child_handler);
  899. }
  900. }
  901. return true;
  902. }
  903. bool Widget::has_focus_within() const
  904. {
  905. auto* window = this->window();
  906. if (!window)
  907. return false;
  908. if (!window->focused_widget())
  909. return false;
  910. auto& effective_focus_widget = focus_proxy() ? *focus_proxy() : *this;
  911. return window->focused_widget() == &effective_focus_widget || is_ancestor_of(*window->focused_widget());
  912. }
  913. void Widget::set_shrink_to_fit(bool b)
  914. {
  915. if (m_shrink_to_fit == b)
  916. return;
  917. m_shrink_to_fit = b;
  918. invalidate_layout();
  919. }
  920. bool Widget::has_pending_drop() const
  921. {
  922. return Application::the()->pending_drop_widget() == this;
  923. }
  924. }