Widget.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Assertions.h>
  8. #include <AK/Debug.h>
  9. #include <AK/IterationDecision.h>
  10. #include <AK/JsonObject.h>
  11. #include <AK/NonnullRefPtr.h>
  12. #include <AK/RefPtr.h>
  13. #include <LibGUI/Action.h>
  14. #include <LibGUI/Application.h>
  15. #include <LibGUI/BoxLayout.h>
  16. #include <LibGUI/ConnectionToWindowServer.h>
  17. #include <LibGUI/Event.h>
  18. #include <LibGUI/GML/AST.h>
  19. #include <LibGUI/GML/Parser.h>
  20. #include <LibGUI/Layout.h>
  21. #include <LibGUI/Menu.h>
  22. #include <LibGUI/Painter.h>
  23. #include <LibGUI/TabWidget.h>
  24. #include <LibGUI/Widget.h>
  25. #include <LibGUI/Window.h>
  26. #include <LibGfx/Bitmap.h>
  27. #include <LibGfx/Font/Font.h>
  28. #include <LibGfx/Font/FontDatabase.h>
  29. #include <LibGfx/Palette.h>
  30. #include <LibGfx/SystemTheme.h>
  31. #include <unistd.h>
  32. REGISTER_GUI_OBJECT(GUI, Widget)
  33. namespace GUI {
  34. Widget::Widget()
  35. : m_background_role(Gfx::ColorRole::Window)
  36. , m_foreground_role(Gfx::ColorRole::WindowText)
  37. , m_font(Gfx::FontDatabase::default_font())
  38. , m_palette(Application::the()->palette().impl())
  39. {
  40. REGISTER_READONLY_STRING_PROPERTY("class_name", class_name);
  41. REGISTER_DEPRECATED_STRING_PROPERTY("name", name, set_name);
  42. register_property(
  43. "address", [this] { return FlatPtr(this); },
  44. [](auto&) { return false; });
  45. register_property(
  46. "parent", [this] { return FlatPtr(this->parent()); },
  47. [](auto&) { return false; });
  48. REGISTER_RECT_PROPERTY("relative_rect", relative_rect, set_relative_rect);
  49. REGISTER_BOOL_PROPERTY("fill_with_background_color", fill_with_background_color, set_fill_with_background_color);
  50. REGISTER_BOOL_PROPERTY("visible", is_visible, set_visible);
  51. REGISTER_BOOL_PROPERTY("focused", is_focused, set_focus);
  52. REGISTER_BOOL_PROPERTY("enabled", is_enabled, set_enabled);
  53. REGISTER_STRING_PROPERTY("tooltip", tooltip, set_tooltip);
  54. REGISTER_UI_SIZE_PROPERTY("min_size", min_size, set_min_size);
  55. REGISTER_READONLY_UI_SIZE_PROPERTY("effective_min_size", effective_min_size);
  56. REGISTER_UI_SIZE_PROPERTY("max_size", max_size, set_max_size);
  57. REGISTER_UI_SIZE_PROPERTY("preferred_size", preferred_size, set_preferred_size);
  58. REGISTER_READONLY_UI_SIZE_PROPERTY("effective_preferred_size", effective_preferred_size);
  59. REGISTER_INT_PROPERTY("width", width, set_width);
  60. REGISTER_UI_DIMENSION_PROPERTY("min_width", min_width, set_min_width);
  61. REGISTER_UI_DIMENSION_PROPERTY("max_width", max_width, set_max_width);
  62. REGISTER_UI_DIMENSION_PROPERTY("preferred_width", preferred_width, set_preferred_width);
  63. REGISTER_INT_PROPERTY("height", height, set_height);
  64. REGISTER_UI_DIMENSION_PROPERTY("min_height", min_height, set_min_height);
  65. REGISTER_UI_DIMENSION_PROPERTY("max_height", max_height, set_max_height);
  66. REGISTER_UI_DIMENSION_PROPERTY("preferred_height", preferred_height, set_preferred_height);
  67. REGISTER_INT_PROPERTY("fixed_width", dummy_fixed_width, set_fixed_width);
  68. REGISTER_INT_PROPERTY("fixed_height", dummy_fixed_height, set_fixed_height);
  69. REGISTER_SIZE_PROPERTY("fixed_size", dummy_fixed_size, set_fixed_size);
  70. REGISTER_BOOL_PROPERTY("shrink_to_fit", is_shrink_to_fit, set_shrink_to_fit);
  71. REGISTER_INT_PROPERTY("x", x, set_x);
  72. REGISTER_INT_PROPERTY("y", y, set_y);
  73. REGISTER_STRING_PROPERTY("font", font_family, set_font_family);
  74. REGISTER_INT_PROPERTY("font_size", m_font->presentation_size, set_font_size);
  75. REGISTER_FONT_WEIGHT_PROPERTY("font_weight", m_font->weight, set_font_weight);
  76. REGISTER_STRING_PROPERTY("title", title, set_title);
  77. REGISTER_BOOL_PROPERTY("font_fixed_width", is_font_fixed_width, set_font_fixed_width)
  78. register_property(
  79. "font_type", [this] { return m_font->is_fixed_width() ? "FixedWidth" : "Normal"; },
  80. [this](auto& value) {
  81. if (value.to_deprecated_string() == "FixedWidth") {
  82. set_font_fixed_width(true);
  83. return true;
  84. }
  85. if (value.to_deprecated_string() == "Normal") {
  86. set_font_fixed_width(false);
  87. return true;
  88. }
  89. return false;
  90. });
  91. register_property(
  92. "focus_policy", [this]() -> JsonValue {
  93. auto policy = focus_policy();
  94. if (policy == GUI::FocusPolicy::ClickFocus)
  95. return "ClickFocus";
  96. if (policy == GUI::FocusPolicy::NoFocus)
  97. return "NoFocus";
  98. if (policy == GUI::FocusPolicy::TabFocus)
  99. return "TabFocus";
  100. if (policy == GUI::FocusPolicy::StrongFocus)
  101. return "StrongFocus";
  102. return JsonValue(); },
  103. [this](auto& value) {
  104. if (!value.is_string())
  105. return false;
  106. if (value.as_string() == "ClickFocus") {
  107. set_focus_policy(GUI::FocusPolicy::ClickFocus);
  108. return true;
  109. }
  110. if (value.as_string() == "NoFocus") {
  111. set_focus_policy(GUI::FocusPolicy::NoFocus);
  112. return true;
  113. }
  114. if (value.as_string() == "TabFocus") {
  115. set_focus_policy(GUI::FocusPolicy::TabFocus);
  116. return true;
  117. }
  118. if (value.as_string() == "StrongFocus") {
  119. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  120. return true;
  121. }
  122. return false;
  123. });
  124. register_property(
  125. "foreground_color", [this]() -> JsonValue { return palette().color(foreground_role()).to_deprecated_string(); },
  126. [this](auto& value) {
  127. auto c = Color::from_string(value.to_deprecated_string());
  128. if (c.has_value()) {
  129. auto _palette = palette();
  130. _palette.set_color(foreground_role(), c.value());
  131. set_palette(_palette);
  132. return true;
  133. }
  134. return false;
  135. });
  136. register_property(
  137. "background_color", [this]() -> JsonValue { return palette().color(background_role()).to_deprecated_string(); },
  138. [this](auto& value) {
  139. auto c = Color::from_string(value.to_deprecated_string());
  140. if (c.has_value()) {
  141. auto _palette = palette();
  142. _palette.set_color(background_role(), c.value());
  143. set_palette(_palette);
  144. return true;
  145. }
  146. return false;
  147. });
  148. register_property(
  149. "foreground_role", [this]() -> JsonValue { return Gfx::to_string(foreground_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_foreground_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_foreground_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. register_property(
  170. "background_role", [this]() -> JsonValue { return Gfx::to_string(background_role()); },
  171. [this](auto& value) {
  172. if (!value.is_string())
  173. return false;
  174. auto str = value.as_string();
  175. if (str == "NoRole") {
  176. set_background_role(Gfx::ColorRole::NoRole);
  177. return true;
  178. }
  179. #undef __ENUMERATE_COLOR_ROLE
  180. #define __ENUMERATE_COLOR_ROLE(role) \
  181. else if (str == #role) \
  182. { \
  183. set_background_role(Gfx::ColorRole::role); \
  184. return true; \
  185. }
  186. ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
  187. #undef __ENUMERATE_COLOR_ROLE
  188. return false;
  189. });
  190. }
  191. Widget::~Widget() = default;
  192. void Widget::layout_relevant_change_occurred()
  193. {
  194. if (auto* parent = parent_widget())
  195. parent->layout_relevant_change_occurred();
  196. else if (window())
  197. window()->schedule_relayout();
  198. }
  199. void Widget::child_event(Core::ChildEvent& event)
  200. {
  201. if (event.type() == Event::ChildAdded) {
  202. if (event.child() && is<Widget>(*event.child()) && layout()) {
  203. if (event.insertion_before_child() && is<Widget>(event.insertion_before_child()))
  204. layout()->insert_widget_before(verify_cast<Widget>(*event.child()), verify_cast<Widget>(*event.insertion_before_child()));
  205. else
  206. layout()->add_widget(verify_cast<Widget>(*event.child()));
  207. layout_relevant_change_occurred();
  208. }
  209. if (window() && event.child() && is<Widget>(*event.child()))
  210. window()->did_add_widget({}, verify_cast<Widget>(*event.child()));
  211. if (event.child() && is<Widget>(*event.child()) && static_cast<Widget const&>(*event.child()).is_visible()) {
  212. ShowEvent show_event;
  213. event.child()->dispatch_event(show_event);
  214. }
  215. }
  216. if (event.type() == Event::ChildRemoved) {
  217. if (layout()) {
  218. if (event.child() && is<Widget>(*event.child()))
  219. layout()->remove_widget(verify_cast<Widget>(*event.child()));
  220. layout_relevant_change_occurred();
  221. }
  222. if (window() && event.child() && is<Widget>(*event.child()))
  223. window()->did_remove_widget({}, verify_cast<Widget>(*event.child()));
  224. if (event.child() && is<Widget>(*event.child())) {
  225. HideEvent hide_event;
  226. event.child()->dispatch_event(hide_event);
  227. }
  228. update();
  229. }
  230. return Core::EventReceiver::child_event(event);
  231. }
  232. void Widget::set_relative_rect(Gfx::IntRect const& a_rect)
  233. {
  234. // Get rid of negative width/height values.
  235. Gfx::IntRect rect = {
  236. a_rect.x(),
  237. a_rect.y(),
  238. max(a_rect.width(), 0),
  239. max(a_rect.height(), 0)
  240. };
  241. if (rect == m_relative_rect)
  242. return;
  243. auto old_rect = m_relative_rect;
  244. bool size_changed = m_relative_rect.size() != rect.size();
  245. m_relative_rect = rect;
  246. if (size_changed) {
  247. ResizeEvent resize_event(rect.size());
  248. event(resize_event);
  249. }
  250. if (auto* parent = parent_widget())
  251. parent->update(old_rect);
  252. update();
  253. }
  254. void Widget::event(Core::Event& event)
  255. {
  256. if (!is_enabled()) {
  257. switch (event.type()) {
  258. case Event::MouseUp:
  259. case Event::MouseDown:
  260. case Event::MouseMove:
  261. case Event::MouseWheel:
  262. case Event::MouseDoubleClick:
  263. case Event::KeyUp:
  264. case Event::KeyDown:
  265. return;
  266. default:
  267. break;
  268. }
  269. }
  270. switch (event.type()) {
  271. case Event::Paint:
  272. return handle_paint_event(static_cast<PaintEvent&>(event));
  273. case Event::Resize:
  274. return handle_resize_event(static_cast<ResizeEvent&>(event));
  275. case Event::FocusIn:
  276. return focusin_event(static_cast<FocusEvent&>(event));
  277. case Event::FocusOut:
  278. return focusout_event(static_cast<FocusEvent&>(event));
  279. case Event::Show:
  280. return show_event(static_cast<ShowEvent&>(event));
  281. case Event::Hide:
  282. return hide_event(static_cast<HideEvent&>(event));
  283. case Event::KeyDown:
  284. return handle_keydown_event(static_cast<KeyEvent&>(event));
  285. case Event::KeyUp:
  286. return keyup_event(static_cast<KeyEvent&>(event));
  287. case Event::MouseMove:
  288. return mousemove_event(static_cast<MouseEvent&>(event));
  289. case Event::MouseDown:
  290. return handle_mousedown_event(static_cast<MouseEvent&>(event));
  291. case Event::MouseDoubleClick:
  292. return handle_mousedoubleclick_event(static_cast<MouseEvent&>(event));
  293. case Event::MouseUp:
  294. return handle_mouseup_event(static_cast<MouseEvent&>(event));
  295. case Event::MouseWheel:
  296. return mousewheel_event(static_cast<MouseEvent&>(event));
  297. case Event::DragEnter:
  298. return drag_enter_event(static_cast<DragEvent&>(event));
  299. case Event::DragMove:
  300. return drag_move_event(static_cast<DragEvent&>(event));
  301. case Event::DragLeave:
  302. return drag_leave_event(static_cast<Event&>(event));
  303. case Event::Drop:
  304. return drop_event(static_cast<DropEvent&>(event));
  305. case Event::ThemeChange:
  306. return theme_change_event(static_cast<ThemeChangeEvent&>(event));
  307. case Event::FontsChange:
  308. return fonts_change_event(static_cast<FontsChangeEvent&>(event));
  309. case Event::Enter:
  310. return handle_enter_event(event);
  311. case Event::Leave:
  312. return handle_leave_event(event);
  313. case Event::EnabledChange:
  314. return change_event(static_cast<Event&>(event));
  315. case Event::ContextMenu:
  316. return context_menu_event(static_cast<ContextMenuEvent&>(event));
  317. case Event::AppletAreaRectChange:
  318. return applet_area_rect_change_event(static_cast<AppletAreaRectChangeEvent&>(event));
  319. default:
  320. return Core::EventReceiver::event(event);
  321. }
  322. }
  323. void Widget::handle_keydown_event(KeyEvent& event)
  324. {
  325. keydown_event(event);
  326. if (event.is_accepted())
  327. return;
  328. if (auto action = Action::find_action_for_shortcut(*this, Shortcut(event.modifiers(), event.key()))) {
  329. action->process_event(*window(), event);
  330. if (event.is_accepted())
  331. return;
  332. }
  333. if (event.key() == KeyCode::Key_Menu) {
  334. ContextMenuEvent c_event(window_relative_rect().bottom_right().translated(-1), screen_relative_rect().bottom_right().translated(-1));
  335. dispatch_event(c_event);
  336. return;
  337. }
  338. event.ignore();
  339. }
  340. void Widget::handle_paint_event(PaintEvent& event)
  341. {
  342. VERIFY(is_visible());
  343. if (!rect().intersects(event.rect())) {
  344. // This widget is not inside the paint event rect.
  345. // Since widgets fully contain their children, we don't need to recurse further.
  346. return;
  347. }
  348. if (fill_with_background_color()) {
  349. Painter painter(*this);
  350. painter.fill_rect(event.rect(), palette().color(background_role()));
  351. }
  352. paint_event(event);
  353. auto children_clip_rect = this->children_clip_rect();
  354. for_each_child_widget([&](auto& child) {
  355. if (!child.is_visible())
  356. return IterationDecision::Continue;
  357. if (child.relative_rect().intersects(event.rect())) {
  358. PaintEvent local_event(event.rect().intersected(children_clip_rect).intersected(child.relative_rect()).translated(-child.relative_position()));
  359. child.dispatch_event(local_event, this);
  360. }
  361. return IterationDecision::Continue;
  362. });
  363. second_paint_event(event);
  364. auto* app = Application::the();
  365. if (app && app->dnd_debugging_enabled() && has_pending_drop()) {
  366. Painter painter(*this);
  367. painter.draw_rect(rect(), Color::Blue);
  368. }
  369. if (app && app->focus_debugging_enabled() && is_focused()) {
  370. Painter painter(*this);
  371. painter.draw_rect(rect(), Color::Cyan);
  372. }
  373. if (app && app->hover_debugging_enabled() && this == window()->hovered_widget()) {
  374. Painter painter(*this);
  375. painter.draw_rect(rect(), Color::Red);
  376. }
  377. }
  378. void Widget::set_layout(NonnullRefPtr<Layout> layout)
  379. {
  380. if (m_layout) {
  381. m_layout->notify_disowned({}, *this);
  382. m_layout->remove_from_parent();
  383. }
  384. m_layout = move(layout);
  385. if (m_layout) {
  386. add_child(*m_layout);
  387. m_layout->notify_adopted({}, *this);
  388. do_layout();
  389. } else {
  390. update();
  391. }
  392. layout_relevant_change_occurred();
  393. }
  394. void Widget::do_layout()
  395. {
  396. for_each_child_widget([&](auto& child) {
  397. child.do_layout();
  398. return IterationDecision::Continue;
  399. });
  400. custom_layout();
  401. if (!m_layout)
  402. return;
  403. m_layout->run(*this);
  404. did_layout();
  405. update();
  406. }
  407. void Widget::notify_layout_changed(Badge<Layout>)
  408. {
  409. invalidate_layout();
  410. }
  411. void Widget::handle_resize_event(ResizeEvent& event)
  412. {
  413. resize_event(event);
  414. do_layout();
  415. }
  416. void Widget::handle_mouseup_event(MouseEvent& event)
  417. {
  418. mouseup_event(event);
  419. }
  420. void Widget::handle_mousedown_event(MouseEvent& event)
  421. {
  422. if (has_flag(focus_policy(), FocusPolicy::ClickFocus))
  423. set_focus(true, FocusSource::Mouse);
  424. mousedown_event(event);
  425. if (event.button() == MouseButton::Secondary) {
  426. ContextMenuEvent c_event(event.position(), screen_relative_rect().location().translated(event.position()));
  427. dispatch_event(c_event);
  428. }
  429. }
  430. void Widget::handle_mousedoubleclick_event(MouseEvent& event)
  431. {
  432. doubleclick_event(event);
  433. }
  434. void Widget::handle_enter_event(Core::Event& event)
  435. {
  436. if (auto* window = this->window())
  437. window->update_cursor({});
  438. show_or_hide_tooltip();
  439. enter_event(event);
  440. }
  441. void Widget::handle_leave_event(Core::Event& event)
  442. {
  443. if (auto* window = this->window())
  444. window->update_cursor({});
  445. if (Application::the()->tooltip_source_widget() == this)
  446. Application::the()->hide_tooltip();
  447. leave_event(event);
  448. }
  449. void Widget::doubleclick_event(MouseEvent&)
  450. {
  451. }
  452. void Widget::resize_event(ResizeEvent&)
  453. {
  454. }
  455. void Widget::paint_event(PaintEvent&)
  456. {
  457. }
  458. void Widget::second_paint_event(PaintEvent&)
  459. {
  460. }
  461. void Widget::show_event(ShowEvent&)
  462. {
  463. }
  464. void Widget::hide_event(HideEvent&)
  465. {
  466. }
  467. void Widget::keydown_event(KeyEvent& event)
  468. {
  469. if (!event.alt() && !event.ctrl() && !event.super()) {
  470. if (event.key() == KeyCode::Key_Tab) {
  471. if (event.shift())
  472. focus_previous_widget(FocusSource::Keyboard, false);
  473. else
  474. focus_next_widget(FocusSource::Keyboard, false);
  475. event.accept();
  476. return;
  477. }
  478. if (!event.shift() && (event.key() == KeyCode::Key_Left || event.key() == KeyCode::Key_Up)) {
  479. focus_previous_widget(FocusSource::Keyboard, true);
  480. event.accept();
  481. return;
  482. }
  483. if (!event.shift() && (event.key() == KeyCode::Key_Right || event.key() == KeyCode::Key_Down)) {
  484. focus_next_widget(FocusSource::Keyboard, true);
  485. event.accept();
  486. return;
  487. }
  488. }
  489. event.ignore();
  490. }
  491. void Widget::keyup_event(KeyEvent& event)
  492. {
  493. event.ignore();
  494. }
  495. void Widget::mousedown_event(MouseEvent&)
  496. {
  497. }
  498. void Widget::mouseup_event(MouseEvent&)
  499. {
  500. }
  501. void Widget::mousemove_event(MouseEvent&)
  502. {
  503. }
  504. void Widget::mousewheel_event(MouseEvent& event)
  505. {
  506. event.ignore();
  507. }
  508. void Widget::context_menu_event(ContextMenuEvent& event)
  509. {
  510. event.ignore();
  511. }
  512. void Widget::focusin_event(FocusEvent&)
  513. {
  514. }
  515. void Widget::focusout_event(FocusEvent&)
  516. {
  517. }
  518. void Widget::enter_event(Core::Event&)
  519. {
  520. }
  521. void Widget::leave_event(Core::Event&)
  522. {
  523. }
  524. void Widget::change_event(Event&)
  525. {
  526. }
  527. void Widget::drag_move_event(DragEvent&)
  528. {
  529. }
  530. void Widget::drag_enter_event(DragEvent& event)
  531. {
  532. StringBuilder builder;
  533. builder.join(',', event.mime_types());
  534. dbgln_if(DRAG_DEBUG, "{} {:p} DRAG ENTER @ {}, {}", class_name(), this, event.position(), builder.string_view());
  535. }
  536. void Widget::drag_leave_event(Event&)
  537. {
  538. dbgln_if(DRAG_DEBUG, "{} {:p} DRAG LEAVE", class_name(), this);
  539. }
  540. void Widget::drop_event(DropEvent& event)
  541. {
  542. dbgln_if(DRAG_DEBUG, "{} {:p} DROP @ {}, '{}'", class_name(), this, event.position(), event.text());
  543. event.ignore();
  544. }
  545. void Widget::theme_change_event(ThemeChangeEvent&)
  546. {
  547. }
  548. void Widget::fonts_change_event(FontsChangeEvent&)
  549. {
  550. if (m_default_font)
  551. set_font(nullptr);
  552. }
  553. void Widget::screen_rects_change_event(ScreenRectsChangeEvent&)
  554. {
  555. }
  556. void Widget::applet_area_rect_change_event(AppletAreaRectChangeEvent&)
  557. {
  558. }
  559. void Widget::update()
  560. {
  561. if (rect().is_empty())
  562. return;
  563. update(rect());
  564. for (auto& it : m_focus_delegators) {
  565. if (!it.is_null() && !it->rect().is_empty())
  566. it->update(it->rect());
  567. }
  568. }
  569. void Widget::update(Gfx::IntRect const& rect)
  570. {
  571. if (!is_visible())
  572. return;
  573. if (!updates_enabled())
  574. return;
  575. auto bound_by_widget = rect.intersected(this->rect());
  576. if (bound_by_widget.is_empty())
  577. return;
  578. Window* window = m_window;
  579. Widget* parent = parent_widget();
  580. while (parent) {
  581. if (!parent->updates_enabled())
  582. return;
  583. window = parent->m_window;
  584. parent = parent->parent_widget();
  585. }
  586. if (window)
  587. window->update(bound_by_widget.translated(window_relative_rect().location()));
  588. }
  589. void Widget::repaint()
  590. {
  591. if (rect().is_empty())
  592. return;
  593. repaint(rect());
  594. }
  595. void Widget::repaint(Gfx::IntRect const& rect)
  596. {
  597. auto* window = this->window();
  598. if (!window)
  599. return;
  600. update(rect);
  601. window->flush_pending_paints_immediately();
  602. }
  603. Gfx::IntRect Widget::window_relative_rect() const
  604. {
  605. auto rect = relative_rect();
  606. for (auto* parent = parent_widget(); parent; parent = parent->parent_widget()) {
  607. rect.translate_by(parent->relative_position());
  608. }
  609. return rect;
  610. }
  611. Gfx::IntRect Widget::screen_relative_rect() const
  612. {
  613. auto window_position = window()->window_type() == WindowType::Applet
  614. ? window()->applet_rect_on_screen().location()
  615. : window()->rect().location();
  616. return window_relative_rect().translated(window_position);
  617. }
  618. Widget* Widget::child_at(Gfx::IntPoint point) const
  619. {
  620. for (int i = children().size() - 1; i >= 0; --i) {
  621. if (!is<Widget>(children()[i]))
  622. continue;
  623. auto& child = verify_cast<Widget>(*children()[i]);
  624. if (!child.is_visible())
  625. continue;
  626. if (child.relative_non_grabbable_rect().contains(point))
  627. return const_cast<Widget*>(&child);
  628. }
  629. return nullptr;
  630. }
  631. Widget::HitTestResult Widget::hit_test(Gfx::IntPoint position, ShouldRespectGreediness should_respect_greediness)
  632. {
  633. if (should_respect_greediness == ShouldRespectGreediness::Yes && is_greedy_for_hits())
  634. return { this, position };
  635. if (auto* child = child_at(position))
  636. return child->hit_test(position - child->relative_position());
  637. return { this, position };
  638. }
  639. void Widget::set_window(Window* window)
  640. {
  641. if (m_window == window)
  642. return;
  643. m_window = window;
  644. }
  645. void Widget::set_focus_proxy(Widget* proxy)
  646. {
  647. if (m_focus_proxy == proxy)
  648. return;
  649. if (proxy)
  650. proxy->add_focus_delegator(this);
  651. else if (m_focus_proxy)
  652. m_focus_proxy->remove_focus_delegator(this);
  653. m_focus_proxy = proxy;
  654. }
  655. void Widget::add_focus_delegator(Widget* delegator)
  656. {
  657. m_focus_delegators.remove_all_matching([&](auto& entry) {
  658. return entry.is_null() || entry == delegator;
  659. });
  660. m_focus_delegators.append(delegator);
  661. }
  662. void Widget::remove_focus_delegator(Widget* delegator)
  663. {
  664. m_focus_delegators.remove_first_matching([&](auto& entry) {
  665. return entry == delegator;
  666. });
  667. }
  668. FocusPolicy Widget::focus_policy() const
  669. {
  670. if (m_focus_proxy)
  671. return m_focus_proxy->focus_policy();
  672. return m_focus_policy;
  673. }
  674. void Widget::set_focus_policy(FocusPolicy policy)
  675. {
  676. if (m_focus_proxy)
  677. return m_focus_proxy->set_focus_policy(policy);
  678. m_focus_policy = policy;
  679. }
  680. bool Widget::is_focused() const
  681. {
  682. if (m_focus_proxy)
  683. return m_focus_proxy->is_focused();
  684. auto* win = window();
  685. if (!win)
  686. return false;
  687. if (win->is_focusable())
  688. return win->focused_widget() == this;
  689. return false;
  690. }
  691. void Widget::set_focus(bool focus, FocusSource source)
  692. {
  693. if (m_focus_proxy)
  694. return m_focus_proxy->set_focus(focus, source);
  695. auto* win = window();
  696. if (!win)
  697. return;
  698. if (focus) {
  699. win->set_focused_widget(this, source);
  700. } else {
  701. if (win->focused_widget() == this)
  702. win->set_focused_widget(nullptr, source);
  703. }
  704. }
  705. void Widget::set_font(Gfx::Font const* font)
  706. {
  707. if (m_font.ptr() == font)
  708. return;
  709. if (!font) {
  710. m_font = Gfx::FontDatabase::default_font();
  711. m_default_font = true;
  712. } else {
  713. m_font = *font;
  714. m_default_font = false;
  715. }
  716. did_change_font();
  717. update();
  718. }
  719. void Widget::set_font_family(String const& family)
  720. {
  721. set_font(Gfx::FontDatabase::the().get(family, m_font->presentation_size(), m_font->weight(), m_font->width(), m_font->slope()));
  722. }
  723. void Widget::set_font_size(unsigned size)
  724. {
  725. set_font(Gfx::FontDatabase::the().get(m_font->family(), size, m_font->weight(), m_font->width(), m_font->slope()));
  726. }
  727. void Widget::set_font_weight(unsigned weight)
  728. {
  729. set_font(Gfx::FontDatabase::the().get(m_font->family(), m_font->presentation_size(), weight, m_font->width(), m_font->slope()));
  730. }
  731. void Widget::set_font_fixed_width(bool fixed_width)
  732. {
  733. if (fixed_width)
  734. set_font(Gfx::FontDatabase::the().get(Gfx::FontDatabase::the().default_fixed_width_font().family(), m_font->presentation_size(), m_font->weight(), m_font->width(), m_font->slope()));
  735. else
  736. set_font(Gfx::FontDatabase::the().get(Gfx::FontDatabase::the().default_font().family(), m_font->presentation_size(), m_font->weight(), m_font->width(), m_font->slope()));
  737. }
  738. bool Widget::is_font_fixed_width()
  739. {
  740. return font().is_fixed_width();
  741. }
  742. void Widget::set_min_size(UISize const& size)
  743. {
  744. VERIFY(size.width().is_one_of(SpecialDimension::Regular, SpecialDimension::Shrink));
  745. if (m_min_size == size)
  746. return;
  747. m_min_size = size;
  748. layout_relevant_change_occurred();
  749. }
  750. void Widget::set_max_size(UISize const& size)
  751. {
  752. VERIFY(size.width().is_one_of(SpecialDimension::Regular, SpecialDimension::Grow));
  753. if (m_max_size == size)
  754. return;
  755. m_max_size = size;
  756. layout_relevant_change_occurred();
  757. }
  758. void Widget::set_preferred_size(UISize const& size)
  759. {
  760. if (m_preferred_size == size)
  761. return;
  762. m_preferred_size = size;
  763. layout_relevant_change_occurred();
  764. }
  765. Optional<UISize> Widget::calculated_preferred_size() const
  766. {
  767. if (layout())
  768. return { layout()->preferred_size() };
  769. return {};
  770. }
  771. Optional<UISize> Widget::calculated_min_size() const
  772. {
  773. if (layout())
  774. return { layout()->min_size() };
  775. // Fall back to at least displaying the margins, so the Widget is not 0 size.
  776. auto m = content_margins();
  777. if (!m.is_null())
  778. return UISize { m.left() + m.right(), m.top() + m.bottom() };
  779. return {};
  780. }
  781. void Widget::invalidate_layout()
  782. {
  783. if (window())
  784. window()->schedule_relayout();
  785. }
  786. void Widget::set_visible(bool visible)
  787. {
  788. if (visible == m_visible)
  789. return;
  790. m_visible = visible;
  791. layout_relevant_change_occurred();
  792. if (m_visible)
  793. update();
  794. if (!m_visible && is_focused())
  795. set_focus(false);
  796. if (m_visible) {
  797. ShowEvent e;
  798. event(e);
  799. } else {
  800. HideEvent e;
  801. event(e);
  802. }
  803. }
  804. bool Widget::spans_entire_window_horizontally() const
  805. {
  806. auto* w = window();
  807. if (!w)
  808. return false;
  809. auto* main_widget = w->main_widget();
  810. if (!main_widget)
  811. return false;
  812. if (main_widget == this)
  813. return true;
  814. auto wrr = window_relative_rect();
  815. return wrr.left() == main_widget->rect().left() && wrr.right() == main_widget->rect().right();
  816. }
  817. void Widget::set_enabled(bool enabled)
  818. {
  819. if (m_enabled == enabled)
  820. return;
  821. m_enabled = enabled;
  822. for_each_child_widget([enabled](auto& child) {
  823. child.set_enabled(enabled);
  824. return IterationDecision::Continue;
  825. });
  826. if (!m_enabled && window() && window()->focused_widget() == this) {
  827. window()->did_disable_focused_widget({});
  828. }
  829. if (!m_enabled)
  830. set_override_cursor(Gfx::StandardCursor::None);
  831. Event e(Event::EnabledChange);
  832. event(e);
  833. update();
  834. }
  835. void Widget::move_to_front()
  836. {
  837. auto* parent = parent_widget();
  838. if (!parent)
  839. return;
  840. if (parent->children().size() == 1)
  841. return;
  842. parent->children().remove_first_matching([this](auto& entry) {
  843. return entry == this;
  844. });
  845. parent->children().append(*this);
  846. parent->update();
  847. }
  848. void Widget::move_to_back()
  849. {
  850. auto* parent = parent_widget();
  851. if (!parent)
  852. return;
  853. if (parent->children().size() == 1)
  854. return;
  855. parent->children().remove_first_matching([this](auto& entry) {
  856. return entry == this;
  857. });
  858. parent->children().prepend(*this);
  859. parent->update();
  860. }
  861. bool Widget::is_frontmost() const
  862. {
  863. auto* parent = parent_widget();
  864. if (!parent)
  865. return true;
  866. return parent->children().last() == this;
  867. }
  868. bool Widget::is_backmost() const
  869. {
  870. auto* parent = parent_widget();
  871. if (!parent)
  872. return true;
  873. return parent->children().first() == this;
  874. }
  875. Action* Widget::action_for_shortcut(Shortcut const& shortcut)
  876. {
  877. return Action::find_action_for_shortcut(*this, shortcut);
  878. }
  879. void Widget::set_updates_enabled(bool enabled)
  880. {
  881. if (m_updates_enabled == enabled)
  882. return;
  883. m_updates_enabled = enabled;
  884. if (enabled)
  885. update();
  886. }
  887. void Widget::focus_previous_widget(FocusSource source, bool siblings_only)
  888. {
  889. auto focusable_widgets = window()->focusable_widgets(source);
  890. if (siblings_only)
  891. focusable_widgets.remove_all_matching([this](auto& entry) { return entry.parent() != parent(); });
  892. for (int i = focusable_widgets.size() - 1; i >= 0; --i) {
  893. if (&focusable_widgets[i] != this)
  894. continue;
  895. if (i > 0)
  896. focusable_widgets[i - 1].set_focus(true, source);
  897. else
  898. focusable_widgets.last().set_focus(true, source);
  899. }
  900. }
  901. void Widget::focus_next_widget(FocusSource source, bool siblings_only)
  902. {
  903. auto focusable_widgets = window()->focusable_widgets(source);
  904. if (siblings_only)
  905. focusable_widgets.remove_all_matching([this](auto& entry) { return entry.parent() != parent(); });
  906. for (size_t i = 0; i < focusable_widgets.size(); ++i) {
  907. if (&focusable_widgets[i] != this)
  908. continue;
  909. if (i < focusable_widgets.size() - 1)
  910. focusable_widgets[i + 1].set_focus(true, source);
  911. else
  912. focusable_widgets.first().set_focus(true, source);
  913. }
  914. }
  915. Vector<Widget&> Widget::child_widgets() const
  916. {
  917. Vector<Widget&> widgets;
  918. widgets.ensure_capacity(children().size());
  919. for (auto& child : const_cast<Widget*>(this)->children()) {
  920. if (is<Widget>(*child))
  921. widgets.append(static_cast<Widget&>(*child));
  922. }
  923. return widgets;
  924. }
  925. void Widget::set_palette(Palette& palette)
  926. {
  927. m_palette = palette.impl();
  928. update();
  929. }
  930. void Widget::set_title(String title)
  931. {
  932. m_title = move(title);
  933. layout_relevant_change_occurred();
  934. // For tab widget children, our change in title also affects the parent.
  935. if (parent_widget())
  936. parent_widget()->update();
  937. }
  938. String Widget::title() const
  939. {
  940. return m_title;
  941. }
  942. void Widget::set_background_role(ColorRole role)
  943. {
  944. m_background_role = role;
  945. update();
  946. }
  947. void Widget::set_foreground_role(ColorRole role)
  948. {
  949. m_foreground_role = role;
  950. update();
  951. }
  952. Gfx::Palette Widget::palette() const
  953. {
  954. return Gfx::Palette(*m_palette);
  955. }
  956. void Widget::set_grabbable_margins(Margins const& margins)
  957. {
  958. if (m_grabbable_margins == margins)
  959. return;
  960. m_grabbable_margins = margins;
  961. layout_relevant_change_occurred();
  962. }
  963. Gfx::IntRect Widget::relative_non_grabbable_rect() const
  964. {
  965. auto rect = relative_rect();
  966. rect.translate_by(m_grabbable_margins.left(), m_grabbable_margins.top());
  967. rect.set_width(rect.width() - (m_grabbable_margins.left() + m_grabbable_margins.right()));
  968. rect.set_height(rect.height() - (m_grabbable_margins.top() + m_grabbable_margins.bottom()));
  969. return rect;
  970. }
  971. void Widget::set_tooltip_deprecated(DeprecatedString tooltip)
  972. {
  973. set_tooltip(String::from_deprecated_string(move(tooltip)).release_value_but_fixme_should_propagate_errors());
  974. }
  975. void Widget::set_tooltip(String tooltip)
  976. {
  977. m_tooltip = move(tooltip);
  978. if (Application::the()->tooltip_source_widget() == this)
  979. show_or_hide_tooltip();
  980. }
  981. void Widget::show_or_hide_tooltip()
  982. {
  983. if (has_tooltip())
  984. Application::the()->show_tooltip(m_tooltip, this);
  985. else
  986. Application::the()->hide_tooltip();
  987. }
  988. Gfx::IntRect Widget::children_clip_rect() const
  989. {
  990. return rect();
  991. }
  992. void Widget::set_override_cursor(AK::Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap const>> cursor)
  993. {
  994. if (m_override_cursor == cursor)
  995. return;
  996. m_override_cursor = move(cursor);
  997. if (auto* window = this->window()) {
  998. window->update_cursor({});
  999. }
  1000. }
  1001. ErrorOr<void> Widget::load_from_gml(StringView gml_string)
  1002. {
  1003. return load_from_gml(gml_string, [](StringView class_name) -> ErrorOr<NonnullRefPtr<Core::EventReceiver>> {
  1004. dbgln("Class '{}' not registered", class_name);
  1005. return Error::from_string_literal("Class not registered");
  1006. });
  1007. }
  1008. ErrorOr<void> Widget::load_from_gml(StringView gml_string, UnregisteredChildHandler unregistered_child_handler)
  1009. {
  1010. auto value = TRY(GML::parse_gml(gml_string));
  1011. return load_from_gml_ast(value, unregistered_child_handler);
  1012. }
  1013. ErrorOr<void> Widget::load_from_gml_ast(NonnullRefPtr<GUI::GML::Node const> ast, UnregisteredChildHandler unregistered_child_handler)
  1014. {
  1015. if (is<GUI::GML::GMLFile>(ast.ptr()))
  1016. return load_from_gml_ast(static_cast<GUI::GML::GMLFile const&>(*ast).main_class(), unregistered_child_handler);
  1017. VERIFY(is<GUI::GML::Object>(ast.ptr()));
  1018. auto const& object = static_cast<GUI::GML::Object const&>(*ast);
  1019. object.for_each_property([&](auto key, auto value) {
  1020. set_property(key, value);
  1021. });
  1022. auto layout = object.layout_object();
  1023. if (!layout.is_null()) {
  1024. auto class_name = layout->name();
  1025. if (class_name.is_null()) {
  1026. return Error::from_string_literal("Invalid layout class name");
  1027. }
  1028. auto& layout_class = *GUI::ObjectClassRegistration::find("GUI::Layout"sv);
  1029. if (auto* registration = GUI::ObjectClassRegistration::find(class_name)) {
  1030. auto layout = TRY(registration->construct());
  1031. if (!registration->is_derived_from(layout_class)) {
  1032. dbgln("Invalid layout class: '{}'", class_name.to_deprecated_string());
  1033. return Error::from_string_literal("Invalid layout class");
  1034. }
  1035. set_layout(static_ptr_cast<Layout>(layout));
  1036. } else {
  1037. dbgln("Unknown layout class: '{}'", class_name.to_deprecated_string());
  1038. return Error::from_string_literal("Unknown layout class");
  1039. }
  1040. layout->for_each_property([&](auto key, auto value) {
  1041. this->layout()->set_property(key, value);
  1042. });
  1043. }
  1044. auto& widget_class = *GUI::ObjectClassRegistration::find("GUI::Widget"sv);
  1045. bool is_tab_widget = is<TabWidget>(*this);
  1046. TRY(object.try_for_each_child_object([&](auto const& child_data) -> ErrorOr<void> {
  1047. auto class_name = child_data.name();
  1048. // It is very questionable if this pseudo object should exist, but it works fine like this for now.
  1049. if (class_name == "GUI::Layout::Spacer") {
  1050. if (!this->layout()) {
  1051. return Error::from_string_literal("Specified GUI::Layout::Spacer in GML, but the parent has no Layout.");
  1052. }
  1053. add_spacer();
  1054. } else {
  1055. RefPtr<Core::EventReceiver> child;
  1056. if (auto* registration = GUI::ObjectClassRegistration::find(class_name)) {
  1057. child = TRY(registration->construct());
  1058. if (!registration->is_derived_from(widget_class)) {
  1059. dbgln("Invalid widget class: '{}'", class_name);
  1060. return Error::from_string_literal("Invalid widget class");
  1061. }
  1062. } else {
  1063. child = TRY(unregistered_child_handler(class_name));
  1064. }
  1065. if (!child)
  1066. return Error::from_string_literal("Unable to construct a Widget class for child");
  1067. add_child(*child);
  1068. // This is possible as we ensure that Widget is a base class above.
  1069. TRY(static_ptr_cast<Widget>(child)->load_from_gml_ast(child_data, unregistered_child_handler));
  1070. if (is_tab_widget) {
  1071. // FIXME: We need to have the child added before loading it so that it can access us. But the TabWidget logic requires the child to not be present yet.
  1072. remove_child(*child);
  1073. reinterpret_cast<TabWidget*>(this)->add_widget(*static_ptr_cast<Widget>(child));
  1074. }
  1075. }
  1076. return {};
  1077. }));
  1078. return {};
  1079. }
  1080. bool Widget::has_focus_within() const
  1081. {
  1082. auto* window = this->window();
  1083. if (!window)
  1084. return false;
  1085. if (!window->focused_widget())
  1086. return false;
  1087. auto& effective_focus_widget = focus_proxy() ? *focus_proxy() : *this;
  1088. return window->focused_widget() == &effective_focus_widget || is_ancestor_of(*window->focused_widget());
  1089. }
  1090. void Widget::set_shrink_to_fit(bool shrink_to_fit)
  1091. {
  1092. // This function is deprecated, and soon to be removed, it is only still here to ease the transition to UIDimensions
  1093. if (shrink_to_fit)
  1094. set_preferred_size(SpecialDimension::Fit);
  1095. }
  1096. bool Widget::has_pending_drop() const
  1097. {
  1098. return Application::the()->pending_drop_widget() == this;
  1099. }
  1100. bool Widget::is_visible_for_timer_purposes() const
  1101. {
  1102. return is_visible() && Object::is_visible_for_timer_purposes();
  1103. }
  1104. void Widget::add_spacer()
  1105. {
  1106. VERIFY(layout());
  1107. return layout()->add_spacer();
  1108. }
  1109. String Widget::font_family() const
  1110. {
  1111. return m_font->family();
  1112. }
  1113. }