Widget.h 14 KB

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