Widget.h 13 KB

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