Widget.h 13 KB

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