Widget.cpp 33 KB

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