Widget.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/JsonObject.h>
  28. #include <AK/String.h>
  29. #include <LibCore/Object.h>
  30. #include <LibGUI/Event.h>
  31. #include <LibGUI/Forward.h>
  32. #include <LibGUI/Margins.h>
  33. #include <LibGfx/Color.h>
  34. #include <LibGfx/Forward.h>
  35. #include <LibGfx/Orientation.h>
  36. #include <LibGfx/Rect.h>
  37. #include <LibGfx/StandardCursor.h>
  38. #define REGISTER_WIDGET(namespace_, class_name) \
  39. namespace { \
  40. GUI::WidgetClassRegistration registration_##class_name(#namespace_ "::" #class_name, []() { return namespace_::class_name::construct(); }); \
  41. }
  42. namespace GUI {
  43. enum class HorizontalDirection {
  44. Left,
  45. Right
  46. };
  47. enum class VerticalDirection {
  48. Up,
  49. Down
  50. };
  51. class WidgetClassRegistration {
  52. AK_MAKE_NONCOPYABLE(WidgetClassRegistration);
  53. AK_MAKE_NONMOVABLE(WidgetClassRegistration);
  54. public:
  55. WidgetClassRegistration(const String& class_name, Function<NonnullRefPtr<Widget>()> factory);
  56. ~WidgetClassRegistration();
  57. String class_name() const { return m_class_name; }
  58. NonnullRefPtr<Widget> construct() const { return m_factory(); }
  59. static void for_each(Function<void(const WidgetClassRegistration&)>);
  60. static const WidgetClassRegistration* find(const String& class_name);
  61. private:
  62. String m_class_name;
  63. Function<NonnullRefPtr<Widget>()> m_factory;
  64. };
  65. enum class FocusPolicy {
  66. NoFocus = 0,
  67. TabFocus = 0x1,
  68. ClickFocus = 0x2,
  69. StrongFocus = TabFocus | ClickFocus,
  70. };
  71. class Widget : public Core::Object {
  72. C_OBJECT(Widget)
  73. public:
  74. virtual ~Widget() override;
  75. Layout* layout() { return m_layout.ptr(); }
  76. const Layout* layout() const { return m_layout.ptr(); }
  77. void set_layout(NonnullRefPtr<Layout>);
  78. template<class T, class... Args>
  79. inline T& set_layout(Args&&... args)
  80. {
  81. auto layout = T::construct(forward<Args>(args)...);
  82. set_layout(*layout);
  83. return layout;
  84. }
  85. Gfx::IntSize min_size() const { return m_min_size; }
  86. void set_min_size(const Gfx::IntSize&);
  87. void set_min_size(int width, int height) { set_min_size({ width, height }); }
  88. int min_width() const { return m_min_size.width(); }
  89. int min_height() const { return m_min_size.height(); }
  90. void set_min_width(int width) { set_min_size(width, min_height()); }
  91. void set_min_height(int height) { set_min_size(min_width(), height); }
  92. Gfx::IntSize max_size() const { return m_max_size; }
  93. void set_max_size(const Gfx::IntSize&);
  94. void set_max_size(int width, int height) { set_max_size({ width, height }); }
  95. int max_width() const { return m_max_size.width(); }
  96. int max_height() const { return m_max_size.height(); }
  97. void set_max_width(int width) { set_max_size(width, max_height()); }
  98. void set_max_height(int height) { set_max_size(max_width(), height); }
  99. void set_fixed_size(const Gfx::IntSize& size)
  100. {
  101. set_min_size(size);
  102. set_max_size(size);
  103. }
  104. void set_fixed_size(int width, int height) { set_fixed_size({ width, height }); }
  105. void set_fixed_width(int width)
  106. {
  107. set_min_width(width);
  108. set_max_width(width);
  109. }
  110. void set_fixed_height(int height)
  111. {
  112. set_min_height(height);
  113. set_max_height(height);
  114. }
  115. bool has_tooltip() const { return !m_tooltip.is_empty(); }
  116. String tooltip() const { return m_tooltip; }
  117. void set_tooltip(const StringView&);
  118. bool is_enabled() const { return m_enabled; }
  119. void set_enabled(bool);
  120. bool updates_enabled() const { return m_updates_enabled; }
  121. void set_updates_enabled(bool);
  122. virtual void event(Core::Event&) override;
  123. // This is called after children have been painted.
  124. virtual void second_paint_event(PaintEvent&);
  125. Gfx::IntRect relative_rect() const { return m_relative_rect; }
  126. Gfx::IntPoint relative_position() const { return m_relative_rect.location(); }
  127. Gfx::IntRect window_relative_rect() const;
  128. Gfx::IntRect screen_relative_rect() const;
  129. int x() const { return m_relative_rect.x(); }
  130. int y() const { return m_relative_rect.y(); }
  131. int width() const { return m_relative_rect.width(); }
  132. int height() const { return m_relative_rect.height(); }
  133. int length(Orientation orientation) const { return orientation == Orientation::Vertical ? height() : width(); }
  134. Gfx::IntRect rect() const { return { 0, 0, width(), height() }; }
  135. Gfx::IntSize size() const { return m_relative_rect.size(); }
  136. void update();
  137. void update(const Gfx::IntRect&);
  138. bool is_focused() const;
  139. void set_focus(bool, FocusSource = FocusSource::Programmatic);
  140. Function<void(const bool, const FocusSource)> on_focus_change;
  141. // Returns true if this widget or one of its descendants is focused.
  142. bool has_focus_within() const;
  143. Widget* focus_proxy() { return m_focus_proxy; }
  144. const Widget* focus_proxy() const { return m_focus_proxy; }
  145. void set_focus_proxy(Widget*);
  146. void set_focus_policy(FocusPolicy policy);
  147. FocusPolicy focus_policy() const;
  148. enum class ShouldRespectGreediness {
  149. No = 0,
  150. Yes
  151. };
  152. struct HitTestResult {
  153. WeakPtr<Widget> widget;
  154. Gfx::IntPoint local_position;
  155. };
  156. HitTestResult hit_test(const Gfx::IntPoint&, ShouldRespectGreediness = ShouldRespectGreediness::Yes);
  157. Widget* child_at(const Gfx::IntPoint&) const;
  158. void set_relative_rect(const Gfx::IntRect&);
  159. void set_relative_rect(int x, int y, int width, int height) { set_relative_rect({ x, y, width, height }); }
  160. void set_x(int x) { set_relative_rect(x, y(), width(), height()); }
  161. void set_y(int y) { set_relative_rect(x(), y, width(), height()); }
  162. void set_width(int width) { set_relative_rect(x(), y(), width, height()); }
  163. void set_height(int height) { set_relative_rect(x(), y(), width(), height); }
  164. void move_to(const Gfx::IntPoint& point) { set_relative_rect({ point, relative_rect().size() }); }
  165. void move_to(int x, int y) { move_to({ x, y }); }
  166. void resize(const Gfx::IntSize& size) { set_relative_rect({ relative_rect().location(), size }); }
  167. void resize(int width, int height) { resize({ width, height }); }
  168. void move_by(int x, int y) { move_by({ x, y }); }
  169. void move_by(const Gfx::IntPoint& delta) { set_relative_rect({ relative_position().translated(delta), size() }); }
  170. Gfx::ColorRole background_role() const { return m_background_role; }
  171. void set_background_role(Gfx::ColorRole);
  172. Gfx::ColorRole foreground_role() const { return m_foreground_role; }
  173. void set_foreground_role(Gfx::ColorRole);
  174. void set_autofill(bool b) { set_fill_with_background_color(b); }
  175. Window* window()
  176. {
  177. if (auto* pw = parent_widget())
  178. return pw->window();
  179. return m_window;
  180. }
  181. const Window* window() const
  182. {
  183. if (auto* pw = parent_widget())
  184. return pw->window();
  185. return m_window;
  186. }
  187. void set_window(Window*);
  188. Widget* parent_widget();
  189. const Widget* parent_widget() const;
  190. void set_fill_with_background_color(bool b) { m_fill_with_background_color = b; }
  191. bool fill_with_background_color() const { return m_fill_with_background_color; }
  192. const Gfx::Font& font() const { return *m_font; }
  193. void set_font(const Gfx::Font*);
  194. void set_font(const Gfx::Font& font) { set_font(&font); }
  195. void set_global_cursor_tracking(bool);
  196. bool global_cursor_tracking() const;
  197. void notify_layout_changed(Badge<Layout>);
  198. void invalidate_layout();
  199. bool is_visible() const { return m_visible; }
  200. void set_visible(bool);
  201. bool spans_entire_window_horizontally() const;
  202. bool is_greedy_for_hits() const { return m_greedy_for_hits; }
  203. void set_greedy_for_hits(bool b) { m_greedy_for_hits = b; }
  204. void move_to_front();
  205. void move_to_back();
  206. bool is_frontmost() const;
  207. bool is_backmost() const;
  208. Action* action_for_key_event(const KeyEvent&);
  209. template<typename Callback>
  210. void for_each_child_widget(Callback callback)
  211. {
  212. for_each_child([&](auto& child) {
  213. if (is<Widget>(child))
  214. return callback(downcast<Widget>(child));
  215. return IterationDecision::Continue;
  216. });
  217. }
  218. Vector<Widget*> child_widgets() const;
  219. void do_layout();
  220. Gfx::Palette palette() const;
  221. void set_palette(const Gfx::Palette&);
  222. const Margins& content_margins() const { return m_content_margins; }
  223. void set_content_margins(const Margins&);
  224. Gfx::IntRect content_rect() const;
  225. void set_accepts_emoji_input(bool b) { m_accepts_emoji_input = b; }
  226. bool accepts_emoji_input() const { return m_accepts_emoji_input; }
  227. virtual Gfx::IntRect children_clip_rect() const;
  228. Gfx::StandardCursor override_cursor() const { return m_override_cursor; }
  229. void set_override_cursor(Gfx::StandardCursor);
  230. bool load_from_gml(const StringView&);
  231. bool load_from_gml(const StringView&, RefPtr<Widget> (*unregistered_child_handler)(const String&));
  232. void set_shrink_to_fit(bool);
  233. bool is_shrink_to_fit() const { return m_shrink_to_fit; }
  234. bool has_pending_drop() const;
  235. protected:
  236. Widget();
  237. virtual void custom_layout() { }
  238. virtual void did_change_font() { }
  239. virtual void did_layout() { }
  240. virtual void paint_event(PaintEvent&);
  241. virtual void resize_event(ResizeEvent&);
  242. virtual void show_event(ShowEvent&);
  243. virtual void hide_event(HideEvent&);
  244. virtual void keydown_event(KeyEvent&);
  245. virtual void keyup_event(KeyEvent&);
  246. virtual void mousemove_event(MouseEvent&);
  247. virtual void mousedown_event(MouseEvent&);
  248. virtual void mouseup_event(MouseEvent&);
  249. virtual void mousewheel_event(MouseEvent&);
  250. virtual void doubleclick_event(MouseEvent&);
  251. virtual void context_menu_event(ContextMenuEvent&);
  252. virtual void focusin_event(FocusEvent&);
  253. virtual void focusout_event(FocusEvent&);
  254. virtual void enter_event(Core::Event&);
  255. virtual void leave_event(Core::Event&);
  256. virtual void child_event(Core::ChildEvent&) override;
  257. virtual void change_event(Event&);
  258. virtual void drag_enter_event(DragEvent&);
  259. virtual void drag_move_event(DragEvent&);
  260. virtual void drag_leave_event(Event&);
  261. virtual void drop_event(DropEvent&);
  262. virtual void theme_change_event(ThemeChangeEvent&);
  263. virtual void did_begin_inspection() override;
  264. virtual void did_end_inspection() override;
  265. void show_or_hide_tooltip();
  266. private:
  267. void handle_paint_event(PaintEvent&);
  268. void handle_resize_event(ResizeEvent&);
  269. void handle_mousedown_event(MouseEvent&);
  270. void handle_mousedoubleclick_event(MouseEvent&);
  271. void handle_mouseup_event(MouseEvent&);
  272. void handle_enter_event(Core::Event&);
  273. void handle_leave_event(Core::Event&);
  274. void focus_previous_widget(FocusSource, bool siblings_only);
  275. void focus_next_widget(FocusSource, bool siblings_only);
  276. bool load_from_json(const JsonObject&, RefPtr<Widget> (*unregistered_child_handler)(const String&));
  277. // HACK: These are used as property getters for the fixed_* size property aliases.
  278. int dummy_fixed_width() { return 0; }
  279. int dummy_fixed_height() { return 0; }
  280. Gfx::IntSize dummy_fixed_size() { return {}; }
  281. Window* m_window { nullptr };
  282. RefPtr<Layout> m_layout;
  283. Gfx::IntRect m_relative_rect;
  284. Gfx::ColorRole m_background_role;
  285. Gfx::ColorRole m_foreground_role;
  286. NonnullRefPtr<Gfx::Font> m_font;
  287. String m_tooltip;
  288. Gfx::IntSize m_min_size { -1, -1 };
  289. Gfx::IntSize m_max_size { -1, -1 };
  290. Margins m_content_margins;
  291. bool m_fill_with_background_color { false };
  292. bool m_visible { true };
  293. bool m_greedy_for_hits { false };
  294. bool m_enabled { true };
  295. bool m_updates_enabled { true };
  296. bool m_accepts_emoji_input { false };
  297. bool m_shrink_to_fit { false };
  298. NonnullRefPtr<Gfx::PaletteImpl> m_palette;
  299. WeakPtr<Widget> m_focus_proxy;
  300. FocusPolicy m_focus_policy { FocusPolicy::NoFocus };
  301. Gfx::StandardCursor m_override_cursor { Gfx::StandardCursor::None };
  302. };
  303. inline Widget* Widget::parent_widget()
  304. {
  305. if (parent() && is<Widget>(*parent()))
  306. return &downcast<Widget>(*parent());
  307. return nullptr;
  308. }
  309. inline const Widget* Widget::parent_widget() const
  310. {
  311. if (parent() && is<Widget>(*parent()))
  312. return &downcast<const Widget>(*parent());
  313. return nullptr;
  314. }
  315. }
  316. template<>
  317. struct AK::Formatter<GUI::Widget> : AK::Formatter<Core::Object> {
  318. };