Widget.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/ByteString.h>
  8. #include <AK/EnumBits.h>
  9. #include <AK/JsonObject.h>
  10. #include <AK/NonnullRefPtr.h>
  11. #include <AK/Optional.h>
  12. #include <AK/String.h>
  13. #include <AK/Variant.h>
  14. #include <LibCore/Timer.h>
  15. #include <LibGUI/Event.h>
  16. #include <LibGUI/FocusPolicy.h>
  17. #include <LibGUI/Forward.h>
  18. #include <LibGUI/GML/AST.h>
  19. #include <LibGUI/Margins.h>
  20. #include <LibGUI/Object.h>
  21. #include <LibGUI/Property.h>
  22. #include <LibGUI/UIDimensions.h>
  23. #include <LibGfx/Color.h>
  24. #include <LibGfx/Forward.h>
  25. #include <LibGfx/Orientation.h>
  26. #include <LibGfx/Rect.h>
  27. #include <LibGfx/StandardCursor.h>
  28. namespace GUI::Registration {
  29. extern GUI::ObjectClassRegistration registration_Widget;
  30. }
  31. #define REGISTER_WIDGET(namespace_, class_name) \
  32. namespace GUI::Registration { \
  33. GUI::ObjectClassRegistration registration_##class_name( \
  34. #namespace_ "::" #class_name##sv, []() -> ErrorOr<NonnullRefPtr<GUI::Object>> { return static_ptr_cast<GUI::Object>(TRY(namespace_::class_name::try_create())); }, &registration_Widget); \
  35. }
  36. namespace GUI {
  37. enum class HorizontalDirection {
  38. Left,
  39. Right
  40. };
  41. enum class VerticalDirection {
  42. Up,
  43. Down
  44. };
  45. constexpr VerticalDirection operator!(VerticalDirection const& other)
  46. {
  47. if (other == VerticalDirection::Up)
  48. return VerticalDirection::Down;
  49. return VerticalDirection::Up;
  50. }
  51. constexpr VerticalDirection key_code_to_vertical_direction(KeyCode const& key)
  52. {
  53. if (key == Key_Up)
  54. return VerticalDirection::Up;
  55. if (key == Key_Down)
  56. return VerticalDirection::Down;
  57. VERIFY_NOT_REACHED();
  58. }
  59. enum class AllowCallback {
  60. No,
  61. Yes
  62. };
  63. class Widget : public GUI::Object {
  64. C_OBJECT(Widget)
  65. public:
  66. virtual ~Widget() override;
  67. Layout* layout() { return m_layout.ptr(); }
  68. Layout const* layout() const { return m_layout.ptr(); }
  69. void set_layout(NonnullRefPtr<Layout>);
  70. template<class T, class... Args>
  71. inline void set_layout(Args&&... args)
  72. {
  73. auto layout = T::construct(forward<Args>(args)...);
  74. set_layout(*layout);
  75. }
  76. UISize min_size() const { return m_min_size; }
  77. void set_min_size(UISize const&);
  78. void set_min_size(UIDimension width, UIDimension height) { set_min_size({ width, height }); }
  79. UIDimension min_width() const { return m_min_size.width(); }
  80. UIDimension min_height() const { return m_min_size.height(); }
  81. void set_min_width(UIDimension width) { set_min_size(width, min_height()); }
  82. void set_min_height(UIDimension height) { set_min_size(min_width(), height); }
  83. UISize max_size() const { return m_max_size; }
  84. void set_max_size(UISize const&);
  85. void set_max_size(UIDimension width, UIDimension height) { set_max_size({ width, height }); }
  86. UIDimension max_width() const { return m_max_size.width(); }
  87. UIDimension max_height() const { return m_max_size.height(); }
  88. void set_max_width(UIDimension width) { set_max_size(width, max_height()); }
  89. void set_max_height(UIDimension height) { set_max_size(max_width(), height); }
  90. UISize preferred_size() const { return m_preferred_size; }
  91. void set_preferred_size(UISize const&);
  92. void set_preferred_size(UIDimension width, UIDimension height) { set_preferred_size({ width, height }); }
  93. UIDimension preferred_width() const { return m_preferred_size.width(); }
  94. UIDimension preferred_height() const { return m_preferred_size.height(); }
  95. void set_preferred_width(UIDimension width) { set_preferred_size(width, preferred_height()); }
  96. void set_preferred_height(UIDimension height) { set_preferred_size(preferred_width(), height); }
  97. virtual Optional<UISize> calculated_preferred_size() const;
  98. virtual Optional<UISize> calculated_min_size() const;
  99. UISize effective_preferred_size() const
  100. {
  101. auto effective_preferred_size = preferred_size();
  102. if (effective_preferred_size.either_is(SpecialDimension::Shrink))
  103. effective_preferred_size.replace_component_if_matching_with(SpecialDimension::Shrink, effective_min_size());
  104. if (effective_preferred_size.either_is(SpecialDimension::Fit) && calculated_preferred_size().has_value())
  105. effective_preferred_size.replace_component_if_matching_with(SpecialDimension::Fit, calculated_preferred_size().value());
  106. return effective_preferred_size;
  107. }
  108. UISize effective_min_size() const
  109. {
  110. auto effective_min_size = min_size();
  111. if (effective_min_size.either_is(SpecialDimension::Shrink) && calculated_min_size().has_value())
  112. effective_min_size.replace_component_if_matching_with(SpecialDimension::Shrink, calculated_min_size().value());
  113. return effective_min_size;
  114. }
  115. void set_fixed_size(UISize const& size)
  116. {
  117. VERIFY(size.has_only_int_values());
  118. set_min_size(size);
  119. set_max_size(size);
  120. }
  121. void set_fixed_size(UIDimension width, UIDimension height) { set_fixed_size({ width, height }); }
  122. void set_fixed_width(UIDimension width)
  123. {
  124. VERIFY(width.is_int());
  125. set_min_width(width);
  126. set_max_width(width);
  127. }
  128. void set_fixed_height(UIDimension height)
  129. {
  130. VERIFY(height.is_int());
  131. set_min_height(height);
  132. set_max_height(height);
  133. }
  134. virtual bool is_visible_for_timer_purposes() const override;
  135. bool has_tooltip() const { return !m_tooltip.is_empty(); }
  136. String tooltip() const { return m_tooltip; }
  137. void set_tooltip(String tooltip);
  138. bool is_auto_focusable() const { return m_auto_focusable; }
  139. void set_auto_focusable(bool auto_focusable) { m_auto_focusable = auto_focusable; }
  140. bool is_enabled() const { return m_enabled; }
  141. void set_enabled(bool);
  142. bool updates_enabled() const { return m_updates_enabled; }
  143. void set_updates_enabled(bool);
  144. Gfx::IntRect relative_rect() const { return m_relative_rect; }
  145. Gfx::IntPoint relative_position() const { return m_relative_rect.location(); }
  146. Gfx::IntRect window_relative_rect() const;
  147. Gfx::IntRect screen_relative_rect() const;
  148. int x() const { return m_relative_rect.x(); }
  149. int y() const { return m_relative_rect.y(); }
  150. int width() const { return m_relative_rect.width(); }
  151. int height() const { return m_relative_rect.height(); }
  152. int length(Orientation orientation) const { return orientation == Orientation::Vertical ? height() : width(); }
  153. virtual Margins content_margins() const { return { 0 }; }
  154. Gfx::IntRect rect() const { return { 0, 0, width(), height() }; }
  155. Gfx::IntSize size() const { return m_relative_rect.size(); }
  156. Gfx::IntRect content_rect() const { return this->content_margins().applied_to(rect()); }
  157. Gfx::IntSize content_size() const { return this->content_rect().size(); }
  158. // Invalidate the widget (or an area thereof), causing a repaint to happen soon.
  159. void update();
  160. void update(Gfx::IntRect const&);
  161. // Repaint the widget (or an area thereof) immediately.
  162. void repaint();
  163. void repaint(Gfx::IntRect const&);
  164. bool is_focused() const;
  165. void set_focus(bool, FocusSource = FocusSource::Programmatic);
  166. bool focus_preempted() const { return m_focus_preempted; }
  167. void set_focus_preempted(bool b) { m_focus_preempted = b; }
  168. Function<void(bool const, const FocusSource)> on_focus_change;
  169. // Returns true if this widget or one of its descendants is focused.
  170. bool has_focus_within() const;
  171. Widget* focus_proxy() { return m_focus_proxy; }
  172. Widget const* focus_proxy() const { return m_focus_proxy; }
  173. void set_focus_proxy(Widget*);
  174. Vector<WeakPtr<Widget>>& focus_delegators() { return m_focus_delegators; }
  175. Vector<WeakPtr<Widget>> const& focus_delegators() const { return m_focus_delegators; }
  176. void set_focus_policy(FocusPolicy policy);
  177. FocusPolicy focus_policy() const;
  178. enum class ShouldRespectGreediness {
  179. No = 0,
  180. Yes
  181. };
  182. struct HitTestResult {
  183. WeakPtr<Widget> widget;
  184. Gfx::IntPoint local_position;
  185. };
  186. HitTestResult hit_test(Gfx::IntPoint, ShouldRespectGreediness = ShouldRespectGreediness::Yes);
  187. Widget* child_at(Gfx::IntPoint) const;
  188. void set_relative_rect(Gfx::IntRect const&);
  189. void set_relative_rect(int x, int y, int width, int height) { set_relative_rect({ x, y, width, height }); }
  190. void set_x(int x) { set_relative_rect(x, y(), width(), height()); }
  191. void set_y(int y) { set_relative_rect(x(), y, width(), height()); }
  192. void set_width(int width) { set_relative_rect(x(), y(), width, height()); }
  193. void set_height(int height) { set_relative_rect(x(), y(), width(), height); }
  194. void move_to(Gfx::IntPoint point) { set_relative_rect({ point, relative_rect().size() }); }
  195. void move_to(int x, int y) { move_to({ x, y }); }
  196. void resize(Gfx::IntSize size) { set_relative_rect({ relative_rect().location(), size }); }
  197. void resize(int width, int height) { resize({ width, height }); }
  198. void move_by(int x, int y) { move_by({ x, y }); }
  199. void move_by(Gfx::IntPoint delta) { set_relative_rect({ relative_position().translated(delta), size() }); }
  200. Gfx::ColorRole background_role() const { return m_background_role; }
  201. void set_background_role(Gfx::ColorRole);
  202. Gfx::ColorRole foreground_role() const { return m_foreground_role; }
  203. void set_foreground_role(Gfx::ColorRole);
  204. void set_background_color(Gfx::Color);
  205. void set_autofill(bool b) { set_fill_with_background_color(b); }
  206. Window* window()
  207. {
  208. if (auto* pw = parent_widget())
  209. return pw->window();
  210. return m_window;
  211. }
  212. Window const* window() const
  213. {
  214. if (auto* pw = parent_widget())
  215. return pw->window();
  216. return m_window;
  217. }
  218. void set_window(Window*);
  219. Widget* parent_widget();
  220. Widget const* parent_widget() const;
  221. void set_fill_with_background_color(bool b) { m_fill_with_background_color = b; }
  222. bool fill_with_background_color() const { return m_fill_with_background_color; }
  223. Gfx::Font const& font() const { return *m_font; }
  224. void set_font(Gfx::Font const*);
  225. void set_font(Gfx::Font const& font) { set_font(&font); }
  226. void set_font_family(String const&);
  227. void set_font_size(unsigned);
  228. void set_font_weight(unsigned);
  229. void set_font_fixed_width(bool);
  230. bool is_font_fixed_width();
  231. void notify_layout_changed(Badge<Layout>);
  232. void invalidate_layout();
  233. bool is_visible() const { return m_visible; }
  234. void set_visible(bool);
  235. bool spans_entire_window_horizontally() const;
  236. bool is_greedy_for_hits() const { return m_greedy_for_hits; }
  237. void set_greedy_for_hits(bool b) { m_greedy_for_hits = b; }
  238. void move_to_front();
  239. void move_to_back();
  240. bool is_frontmost() const;
  241. bool is_backmost() const;
  242. Action* action_for_shortcut(Shortcut const&);
  243. template<typename Callback>
  244. void for_each_child_widget(Callback callback)
  245. {
  246. for_each_child([&](auto& child) {
  247. if (is<Widget>(child))
  248. return callback(verify_cast<Widget>(child));
  249. return IterationDecision::Continue;
  250. });
  251. }
  252. Vector<Widget&> child_widgets() const;
  253. void do_layout();
  254. Gfx::Palette palette() const;
  255. void set_palette(Gfx::Palette&);
  256. String title() const;
  257. void set_title(String);
  258. Margins const& grabbable_margins() const { return m_grabbable_margins; }
  259. void set_grabbable_margins(Margins const&);
  260. Gfx::IntRect relative_non_grabbable_rect() const;
  261. Function<void(StringView)> on_emoji_input;
  262. void set_accepts_command_palette(bool b) { m_accepts_command_palette = b; }
  263. bool accepts_command_palette() const { return m_accepts_command_palette; }
  264. virtual Gfx::IntRect children_clip_rect() const;
  265. AK::Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap const>> const& override_cursor() const { return m_override_cursor; }
  266. void set_override_cursor(AK::Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap const>>);
  267. using UnregisteredChildHandler = ErrorOr<NonnullRefPtr<Core::EventReceiver>>(StringView);
  268. ErrorOr<void> load_from_gml(StringView);
  269. ErrorOr<void> load_from_gml(StringView, UnregisteredChildHandler);
  270. // FIXME: remove this when all uses of shrink_to_fit are eliminated
  271. void set_shrink_to_fit(bool);
  272. bool is_shrink_to_fit() const { return preferred_width().is_shrink() || preferred_height().is_shrink(); }
  273. bool has_pending_drop() const;
  274. // In order for others to be able to call this, it needs to be public.
  275. virtual ErrorOr<void> load_from_gml_ast(NonnullRefPtr<GUI::GML::Node const> ast, UnregisteredChildHandler);
  276. void add_spacer();
  277. protected:
  278. Widget();
  279. virtual void event(Core::Event&) override;
  280. // This is called after children have been painted.
  281. virtual void second_paint_event(PaintEvent&);
  282. virtual void layout_relevant_change_occurred();
  283. virtual void custom_layout() { }
  284. virtual void did_change_font() { }
  285. virtual void did_layout() { }
  286. virtual void paint_event(PaintEvent&);
  287. virtual void resize_event(ResizeEvent&);
  288. virtual void show_event(ShowEvent&);
  289. virtual void hide_event(HideEvent&);
  290. virtual void keydown_event(KeyEvent&);
  291. virtual void keyup_event(KeyEvent&);
  292. virtual void mousemove_event(MouseEvent&);
  293. virtual void mousedown_event(MouseEvent&);
  294. virtual void mouseup_event(MouseEvent&);
  295. virtual void mousewheel_event(MouseEvent&);
  296. virtual void doubleclick_event(MouseEvent&);
  297. virtual void context_menu_event(ContextMenuEvent&);
  298. virtual void focusin_event(FocusEvent&);
  299. virtual void focusout_event(FocusEvent&);
  300. virtual void enter_event(Core::Event&);
  301. virtual void leave_event(Core::Event&);
  302. virtual void child_event(Core::ChildEvent&) override;
  303. virtual void change_event(Event&);
  304. virtual void drag_enter_event(DragEvent&);
  305. virtual void drag_move_event(DragEvent&);
  306. virtual void drag_leave_event(Event&);
  307. virtual void drop_event(DropEvent&);
  308. virtual void theme_change_event(ThemeChangeEvent&);
  309. virtual void fonts_change_event(FontsChangeEvent&);
  310. virtual void screen_rects_change_event(ScreenRectsChangeEvent&);
  311. virtual void applet_area_rect_change_event(AppletAreaRectChangeEvent&);
  312. void show_or_hide_tooltip();
  313. void add_focus_delegator(Widget*);
  314. void remove_focus_delegator(Widget*);
  315. private:
  316. virtual bool is_widget() const final { return true; }
  317. void handle_paint_event(PaintEvent&);
  318. void handle_resize_event(ResizeEvent&);
  319. void handle_mousedown_event(MouseEvent&);
  320. void handle_mousedoubleclick_event(MouseEvent&);
  321. void handle_mouseup_event(MouseEvent&);
  322. void handle_keydown_event(KeyEvent&);
  323. void handle_enter_event(Core::Event&);
  324. void handle_leave_event(Core::Event&);
  325. void focus_previous_widget(FocusSource, bool siblings_only);
  326. void focus_next_widget(FocusSource, bool siblings_only);
  327. // HACK: These are used as property getters for the fixed_* size property aliases.
  328. int dummy_fixed_width() { return 0; }
  329. int dummy_fixed_height() { return 0; }
  330. Gfx::IntSize dummy_fixed_size() { return {}; }
  331. // HACK: Used as property getter for the font_family property, can be removed when Font is migrated from ByteString.
  332. String font_family() const;
  333. Window* m_window { nullptr };
  334. RefPtr<Layout> m_layout;
  335. Gfx::IntRect m_relative_rect;
  336. Gfx::ColorRole m_background_role;
  337. Gfx::ColorRole m_foreground_role;
  338. NonnullRefPtr<Gfx::Font const> m_font;
  339. String m_tooltip;
  340. UISize m_min_size { SpecialDimension::Shrink };
  341. UISize m_max_size { SpecialDimension::Grow };
  342. UISize m_preferred_size { SpecialDimension::Grow };
  343. Margins m_grabbable_margins;
  344. bool m_fill_with_background_color { false };
  345. bool m_visible { true };
  346. bool m_greedy_for_hits { false };
  347. bool m_auto_focusable { true };
  348. bool m_focus_preempted { false };
  349. bool m_enabled { true };
  350. bool m_updates_enabled { true };
  351. bool m_accepts_command_palette { true };
  352. bool m_default_font { true };
  353. NonnullRefPtr<Gfx::PaletteImpl> m_palette;
  354. String m_title;
  355. WeakPtr<Widget> m_focus_proxy;
  356. Vector<WeakPtr<Widget>> m_focus_delegators;
  357. FocusPolicy m_focus_policy { FocusPolicy::NoFocus };
  358. AK::Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap const>> m_override_cursor { Gfx::StandardCursor::None };
  359. };
  360. inline Widget* Widget::parent_widget()
  361. {
  362. if (parent() && is<Widget>(*parent()))
  363. return &verify_cast<Widget>(*parent());
  364. return nullptr;
  365. }
  366. inline Widget const* Widget::parent_widget() const
  367. {
  368. if (parent() && is<Widget>(*parent()))
  369. return &verify_cast<Widget const>(*parent());
  370. return nullptr;
  371. }
  372. }
  373. template<>
  374. inline bool Core::EventReceiver::fast_is<GUI::Widget>() const { return is_widget(); }
  375. template<>
  376. struct AK::Formatter<GUI::Widget> : AK::Formatter<Core::EventReceiver> {
  377. };