Widget.cpp 32 KB

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