Widget.cpp 31 KB

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