Widget.h 17 KB

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