Widget.cpp 31 KB

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