Widget.h 14 KB

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