Widget.cpp 36 KB

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