Widget.cpp 37 KB

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