Widget.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. bool has_tooltip() const { return !m_tooltip.is_empty(); }
  92. String tooltip() const { return m_tooltip; }
  93. void set_tooltip(String);
  94. bool is_enabled() const { return m_enabled; }
  95. void set_enabled(bool);
  96. bool updates_enabled() const { return m_updates_enabled; }
  97. void set_updates_enabled(bool);
  98. Gfx::IntRect relative_rect() const { return m_relative_rect; }
  99. Gfx::IntPoint relative_position() const { return m_relative_rect.location(); }
  100. Gfx::IntRect window_relative_rect() const;
  101. Gfx::IntRect screen_relative_rect() const;
  102. int x() const { return m_relative_rect.x(); }
  103. int y() const { return m_relative_rect.y(); }
  104. int width() const { return m_relative_rect.width(); }
  105. int height() const { return m_relative_rect.height(); }
  106. int length(Orientation orientation) const { return orientation == Orientation::Vertical ? height() : width(); }
  107. Gfx::IntRect rect() const { return { 0, 0, width(), height() }; }
  108. Gfx::IntSize size() const { return m_relative_rect.size(); }
  109. void update();
  110. void update(const Gfx::IntRect&);
  111. bool is_focused() const;
  112. void set_focus(bool, FocusSource = FocusSource::Programmatic);
  113. Function<void(const bool, const FocusSource)> on_focus_change;
  114. // Returns true if this widget or one of its descendants is focused.
  115. bool has_focus_within() const;
  116. Widget* focus_proxy() { return m_focus_proxy; }
  117. const Widget* focus_proxy() const { return m_focus_proxy; }
  118. void set_focus_proxy(Widget*);
  119. void set_focus_policy(FocusPolicy policy);
  120. FocusPolicy focus_policy() const;
  121. enum class ShouldRespectGreediness {
  122. No = 0,
  123. Yes
  124. };
  125. struct HitTestResult {
  126. WeakPtr<Widget> widget;
  127. Gfx::IntPoint local_position;
  128. };
  129. HitTestResult hit_test(const Gfx::IntPoint&, ShouldRespectGreediness = ShouldRespectGreediness::Yes);
  130. Widget* child_at(const Gfx::IntPoint&) const;
  131. void set_relative_rect(const Gfx::IntRect&);
  132. void set_relative_rect(int x, int y, int width, int height) { set_relative_rect({ x, y, width, height }); }
  133. void set_x(int x) { set_relative_rect(x, y(), width(), height()); }
  134. void set_y(int y) { set_relative_rect(x(), y, width(), height()); }
  135. void set_width(int width) { set_relative_rect(x(), y(), width, height()); }
  136. void set_height(int height) { set_relative_rect(x(), y(), width(), height); }
  137. void move_to(const Gfx::IntPoint& point) { set_relative_rect({ point, relative_rect().size() }); }
  138. void move_to(int x, int y) { move_to({ x, y }); }
  139. void resize(const Gfx::IntSize& size) { set_relative_rect({ relative_rect().location(), size }); }
  140. void resize(int width, int height) { resize({ width, height }); }
  141. void move_by(int x, int y) { move_by({ x, y }); }
  142. void move_by(const Gfx::IntPoint& delta) { set_relative_rect({ relative_position().translated(delta), size() }); }
  143. Gfx::ColorRole background_role() const { return m_background_role; }
  144. void set_background_role(Gfx::ColorRole);
  145. Gfx::ColorRole foreground_role() const { return m_foreground_role; }
  146. void set_foreground_role(Gfx::ColorRole);
  147. void set_autofill(bool b) { set_fill_with_background_color(b); }
  148. Window* window()
  149. {
  150. if (auto* pw = parent_widget())
  151. return pw->window();
  152. return m_window;
  153. }
  154. const Window* window() const
  155. {
  156. if (auto* pw = parent_widget())
  157. return pw->window();
  158. return m_window;
  159. }
  160. void set_window(Window*);
  161. Widget* parent_widget();
  162. const Widget* parent_widget() const;
  163. void set_fill_with_background_color(bool b) { m_fill_with_background_color = b; }
  164. bool fill_with_background_color() const { return m_fill_with_background_color; }
  165. const Gfx::Font& font() const { return *m_font; }
  166. void set_font(const Gfx::Font*);
  167. void set_font(const Gfx::Font& font) { set_font(&font); }
  168. void set_font_family(const String&);
  169. void set_font_size(unsigned);
  170. void set_font_weight(unsigned);
  171. void set_font_fixed_width(bool);
  172. void set_global_cursor_tracking(bool);
  173. bool global_cursor_tracking() const;
  174. void notify_layout_changed(Badge<Layout>);
  175. void invalidate_layout();
  176. bool is_visible() const { return m_visible; }
  177. void set_visible(bool);
  178. bool spans_entire_window_horizontally() const;
  179. bool is_greedy_for_hits() const { return m_greedy_for_hits; }
  180. void set_greedy_for_hits(bool b) { m_greedy_for_hits = b; }
  181. void move_to_front();
  182. void move_to_back();
  183. bool is_frontmost() const;
  184. bool is_backmost() const;
  185. Action* action_for_key_event(const KeyEvent&);
  186. template<typename Callback>
  187. void for_each_child_widget(Callback callback)
  188. {
  189. for_each_child([&](auto& child) {
  190. if (is<Widget>(child))
  191. return callback(downcast<Widget>(child));
  192. return IterationDecision::Continue;
  193. });
  194. }
  195. Vector<Widget&> child_widgets() const;
  196. void do_layout();
  197. Gfx::Palette palette() const;
  198. void set_palette(const Gfx::Palette&);
  199. const Margins& content_margins() const { return m_content_margins; }
  200. void set_content_margins(const Margins&);
  201. Gfx::IntRect content_rect() const;
  202. void set_accepts_emoji_input(bool b) { m_accepts_emoji_input = b; }
  203. bool accepts_emoji_input() const { return m_accepts_emoji_input; }
  204. virtual Gfx::IntRect children_clip_rect() const;
  205. Gfx::StandardCursor override_cursor() const { return m_override_cursor; }
  206. void set_override_cursor(Gfx::StandardCursor);
  207. bool load_from_gml(const StringView&);
  208. bool load_from_gml(const StringView&, RefPtr<Core::Object> (*unregistered_child_handler)(const String&));
  209. void set_shrink_to_fit(bool);
  210. bool is_shrink_to_fit() const { return m_shrink_to_fit; }
  211. bool has_pending_drop() const;
  212. protected:
  213. Widget();
  214. virtual void event(Core::Event&) override;
  215. // This is called after children have been painted.
  216. virtual void second_paint_event(PaintEvent&);
  217. virtual void custom_layout() { }
  218. virtual void did_change_font() { }
  219. virtual void did_layout() { }
  220. virtual void paint_event(PaintEvent&);
  221. virtual void resize_event(ResizeEvent&);
  222. virtual void show_event(ShowEvent&);
  223. virtual void hide_event(HideEvent&);
  224. virtual void keydown_event(KeyEvent&);
  225. virtual void keyup_event(KeyEvent&);
  226. virtual void mousemove_event(MouseEvent&);
  227. virtual void mousedown_event(MouseEvent&);
  228. virtual void mouseup_event(MouseEvent&);
  229. virtual void mousewheel_event(MouseEvent&);
  230. virtual void doubleclick_event(MouseEvent&);
  231. virtual void context_menu_event(ContextMenuEvent&);
  232. virtual void focusin_event(FocusEvent&);
  233. virtual void focusout_event(FocusEvent&);
  234. virtual void enter_event(Core::Event&);
  235. virtual void leave_event(Core::Event&);
  236. virtual void child_event(Core::ChildEvent&) override;
  237. virtual void change_event(Event&);
  238. virtual void drag_enter_event(DragEvent&);
  239. virtual void drag_move_event(DragEvent&);
  240. virtual void drag_leave_event(Event&);
  241. virtual void drop_event(DropEvent&);
  242. virtual void theme_change_event(ThemeChangeEvent&);
  243. virtual void screen_rects_change_event(ScreenRectsChangeEvent&);
  244. virtual void did_begin_inspection() override;
  245. virtual void did_end_inspection() override;
  246. void show_or_hide_tooltip();
  247. private:
  248. void handle_paint_event(PaintEvent&);
  249. void handle_resize_event(ResizeEvent&);
  250. void handle_mousedown_event(MouseEvent&);
  251. void handle_mousedoubleclick_event(MouseEvent&);
  252. void handle_mouseup_event(MouseEvent&);
  253. void handle_keydown_event(KeyEvent&);
  254. void handle_enter_event(Core::Event&);
  255. void handle_leave_event(Core::Event&);
  256. void focus_previous_widget(FocusSource, bool siblings_only);
  257. void focus_next_widget(FocusSource, bool siblings_only);
  258. virtual bool load_from_json(const JsonObject&, RefPtr<Core::Object> (*unregistered_child_handler)(const String&)) override;
  259. // HACK: These are used as property getters for the fixed_* size property aliases.
  260. int dummy_fixed_width() { return 0; }
  261. int dummy_fixed_height() { return 0; }
  262. Gfx::IntSize dummy_fixed_size() { return {}; }
  263. Window* m_window { nullptr };
  264. RefPtr<Layout> m_layout;
  265. Gfx::IntRect m_relative_rect;
  266. Gfx::ColorRole m_background_role;
  267. Gfx::ColorRole m_foreground_role;
  268. NonnullRefPtr<Gfx::Font> m_font;
  269. String m_tooltip;
  270. Gfx::IntSize m_min_size { -1, -1 };
  271. Gfx::IntSize m_max_size { -1, -1 };
  272. Margins m_content_margins;
  273. bool m_fill_with_background_color { false };
  274. bool m_visible { true };
  275. bool m_greedy_for_hits { false };
  276. bool m_enabled { true };
  277. bool m_updates_enabled { true };
  278. bool m_accepts_emoji_input { false };
  279. bool m_shrink_to_fit { false };
  280. NonnullRefPtr<Gfx::PaletteImpl> m_palette;
  281. WeakPtr<Widget> m_focus_proxy;
  282. FocusPolicy m_focus_policy { FocusPolicy::NoFocus };
  283. Gfx::StandardCursor m_override_cursor { Gfx::StandardCursor::None };
  284. };
  285. inline Widget* Widget::parent_widget()
  286. {
  287. if (parent() && is<Widget>(*parent()))
  288. return &downcast<Widget>(*parent());
  289. return nullptr;
  290. }
  291. inline const Widget* Widget::parent_widget() const
  292. {
  293. if (parent() && is<Widget>(*parent()))
  294. return &downcast<const Widget>(*parent());
  295. return nullptr;
  296. }
  297. }
  298. template<>
  299. struct AK::Formatter<GUI::Widget> : AK::Formatter<Core::Object> {
  300. };