Widget.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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/String.h>
  28. #include <LibCore/Object.h>
  29. #include <LibGUI/Event.h>
  30. #include <LibGUI/Forward.h>
  31. #include <LibGUI/Margins.h>
  32. #include <LibGfx/Color.h>
  33. #include <LibGfx/Forward.h>
  34. #include <LibGfx/Orientation.h>
  35. #include <LibGfx/Rect.h>
  36. #define REGISTER_WIDGET(class_name) \
  37. extern WidgetClassRegistration registration_##class_name; \
  38. WidgetClassRegistration registration_##class_name(#class_name, []() { return class_name::construct(); });
  39. namespace GUI {
  40. enum class SizePolicy {
  41. Fixed,
  42. Fill
  43. };
  44. inline const char* to_string(SizePolicy policy)
  45. {
  46. switch (policy) {
  47. case SizePolicy::Fixed:
  48. return "SizePolicy::Fixed";
  49. case SizePolicy::Fill:
  50. return "SizePolicy::Fill";
  51. }
  52. return "SizePolicy::(Invalid)";
  53. }
  54. enum class HorizontalDirection {
  55. Left,
  56. Right
  57. };
  58. enum class VerticalDirection {
  59. Up,
  60. Down
  61. };
  62. class WidgetClassRegistration {
  63. AK_MAKE_NONCOPYABLE(WidgetClassRegistration)
  64. AK_MAKE_NONMOVABLE(WidgetClassRegistration)
  65. public:
  66. WidgetClassRegistration(const String& class_name, Function<NonnullRefPtr<Widget>()> factory);
  67. ~WidgetClassRegistration();
  68. String class_name() const { return m_class_name; }
  69. NonnullRefPtr<Widget> construct() const { return m_factory(); }
  70. static void for_each(Function<void(const WidgetClassRegistration&)>);
  71. static const WidgetClassRegistration* find(const String& class_name);
  72. private:
  73. String m_class_name;
  74. Function<NonnullRefPtr<Widget>()> m_factory;
  75. };
  76. class Widget : public Core::Object {
  77. C_OBJECT(Widget)
  78. public:
  79. virtual ~Widget() override;
  80. Layout* layout() { return m_layout.ptr(); }
  81. const Layout* layout() const { return m_layout.ptr(); }
  82. void set_layout(NonnullRefPtr<Layout>);
  83. template<class T, class... Args>
  84. inline T& set_layout(Args&&... args)
  85. {
  86. auto layout = T::construct(forward<Args>(args)...);
  87. set_layout(*layout);
  88. return layout;
  89. }
  90. SizePolicy horizontal_size_policy() const { return m_horizontal_size_policy; }
  91. SizePolicy vertical_size_policy() const { return m_vertical_size_policy; }
  92. SizePolicy size_policy(Orientation orientation) { return orientation == Orientation::Horizontal ? m_horizontal_size_policy : m_vertical_size_policy; }
  93. void set_size_policy(SizePolicy horizontal_policy, SizePolicy vertical_policy);
  94. void set_size_policy(Orientation, SizePolicy);
  95. Gfx::IntSize preferred_size() const { return m_preferred_size; }
  96. void set_preferred_size(const Gfx::IntSize&);
  97. void set_preferred_size(int width, int height) { set_preferred_size({ width, height }); }
  98. bool has_tooltip() const { return !m_tooltip.is_empty(); }
  99. String tooltip() const { return m_tooltip; }
  100. void set_tooltip(const StringView& tooltip) { m_tooltip = tooltip; }
  101. bool is_enabled() const { return m_enabled; }
  102. void set_enabled(bool);
  103. bool updates_enabled() const { return m_updates_enabled; }
  104. void set_updates_enabled(bool);
  105. virtual void event(Core::Event&) override;
  106. // This is called after children have been painted.
  107. virtual void second_paint_event(PaintEvent&);
  108. Gfx::IntRect relative_rect() const { return m_relative_rect; }
  109. Gfx::IntPoint relative_position() const { return m_relative_rect.location(); }
  110. Gfx::IntRect window_relative_rect() const;
  111. Gfx::IntRect screen_relative_rect() const;
  112. int x() const { return m_relative_rect.x(); }
  113. int y() const { return m_relative_rect.y(); }
  114. int width() const { return m_relative_rect.width(); }
  115. int height() const { return m_relative_rect.height(); }
  116. int length(Orientation orientation) const { return orientation == Orientation::Vertical ? height() : width(); }
  117. Gfx::IntRect rect() const { return { 0, 0, width(), height() }; }
  118. Gfx::IntSize size() const { return m_relative_rect.size(); }
  119. void update();
  120. void update(const Gfx::IntRect&);
  121. virtual bool accepts_focus() const { return false; }
  122. bool is_focused() const;
  123. void set_focus(bool, FocusSource = FocusSource::Programmatic);
  124. enum class ShouldRespectGreediness { No = 0,
  125. Yes };
  126. struct HitTestResult {
  127. Widget* widget { nullptr };
  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. Color background_color() const { return m_background_color; }
  149. Color foreground_color() const { return m_foreground_color; }
  150. void set_background_color(Color color) { m_background_color = color; }
  151. void set_foreground_color(Color color) { m_foreground_color = color; }
  152. void set_backcolor(const StringView&);
  153. void set_forecolor(const StringView&);
  154. void set_autofill(bool b) { set_fill_with_background_color(b); }
  155. Window* window()
  156. {
  157. if (auto* pw = parent_widget())
  158. return pw->window();
  159. return m_window;
  160. }
  161. const Window* window() const
  162. {
  163. if (auto* pw = parent_widget())
  164. return pw->window();
  165. return m_window;
  166. }
  167. void set_window(Window*);
  168. Widget* parent_widget();
  169. const Widget* parent_widget() const;
  170. void set_fill_with_background_color(bool b) { m_fill_with_background_color = b; }
  171. bool fill_with_background_color() const { return m_fill_with_background_color; }
  172. const Gfx::Font& font() const { return *m_font; }
  173. void set_font(const Gfx::Font*);
  174. void set_font(const Gfx::Font& font) { set_font(&font); }
  175. void set_global_cursor_tracking(bool);
  176. bool global_cursor_tracking() const;
  177. void notify_layout_changed(Badge<Layout>);
  178. void invalidate_layout();
  179. bool is_visible() const { return m_visible; }
  180. void set_visible(bool);
  181. bool spans_entire_window_horizontally() const;
  182. bool is_greedy_for_hits() const { return m_greedy_for_hits; }
  183. void set_greedy_for_hits(bool b) { m_greedy_for_hits = b; }
  184. void move_to_front();
  185. void move_to_back();
  186. bool is_frontmost() const;
  187. bool is_backmost() const;
  188. Action* action_for_key_event(const KeyEvent&);
  189. template<typename Callback>
  190. void for_each_child_widget(Callback callback)
  191. {
  192. for_each_child([&](auto& child) {
  193. if (is<Widget>(child))
  194. return callback(downcast<Widget>(child));
  195. return IterationDecision::Continue;
  196. });
  197. }
  198. Vector<Widget*> child_widgets() const;
  199. virtual bool is_radio_button() const { return false; }
  200. virtual bool is_abstract_button() const { return false; }
  201. virtual void save_to(AK::JsonObject&) override;
  202. void do_layout();
  203. Gfx::Palette palette() const;
  204. void set_palette(const Gfx::Palette&);
  205. const Margins& content_margins() const { return m_content_margins; }
  206. void set_content_margins(const Margins&);
  207. Gfx::IntRect content_rect() const;
  208. void set_accepts_emoji_input(bool b) { m_accepts_emoji_input = b; }
  209. bool accepts_emoji_input() const { return m_accepts_emoji_input; }
  210. protected:
  211. Widget();
  212. virtual void custom_layout() { }
  213. virtual void did_change_font() { }
  214. virtual void did_layout() { }
  215. virtual void paint_event(PaintEvent&);
  216. virtual void resize_event(ResizeEvent&);
  217. virtual void show_event(ShowEvent&);
  218. virtual void hide_event(HideEvent&);
  219. virtual void keydown_event(KeyEvent&);
  220. virtual void keyup_event(KeyEvent&);
  221. virtual void mousemove_event(MouseEvent&);
  222. virtual void mousedown_event(MouseEvent&);
  223. virtual void mouseup_event(MouseEvent&);
  224. virtual void mousewheel_event(MouseEvent&);
  225. virtual void doubleclick_event(MouseEvent&);
  226. virtual void context_menu_event(ContextMenuEvent&);
  227. virtual void focusin_event(FocusEvent&);
  228. virtual void focusout_event(FocusEvent&);
  229. virtual void enter_event(Core::Event&);
  230. virtual void leave_event(Core::Event&);
  231. virtual void child_event(Core::ChildEvent&) override;
  232. virtual void change_event(Event&);
  233. virtual void drag_move_event(DragEvent&);
  234. virtual void drop_event(DropEvent&);
  235. virtual void theme_change_event(ThemeChangeEvent&);
  236. virtual void did_begin_inspection() override;
  237. virtual void did_end_inspection() override;
  238. virtual bool set_property(const StringView& name, const JsonValue& value) override;
  239. private:
  240. void handle_paint_event(PaintEvent&);
  241. void handle_resize_event(ResizeEvent&);
  242. void handle_mousedown_event(MouseEvent&);
  243. void handle_mousedoubleclick_event(MouseEvent&);
  244. void handle_mouseup_event(MouseEvent&);
  245. void handle_enter_event(Core::Event&);
  246. void handle_leave_event(Core::Event&);
  247. void focus_previous_widget(FocusSource);
  248. void focus_next_widget(FocusSource);
  249. Window* m_window { nullptr };
  250. RefPtr<Layout> m_layout;
  251. Gfx::IntRect m_relative_rect;
  252. Gfx::ColorRole m_background_role;
  253. Gfx::ColorRole m_foreground_role;
  254. Color m_background_color;
  255. Color m_foreground_color;
  256. NonnullRefPtr<Gfx::Font> m_font;
  257. String m_tooltip;
  258. SizePolicy m_horizontal_size_policy { SizePolicy::Fill };
  259. SizePolicy m_vertical_size_policy { SizePolicy::Fill };
  260. Gfx::IntSize m_preferred_size;
  261. Margins m_content_margins;
  262. bool m_fill_with_background_color { false };
  263. bool m_visible { true };
  264. bool m_greedy_for_hits { false };
  265. bool m_enabled { true };
  266. bool m_updates_enabled { true };
  267. bool m_accepts_emoji_input { false };
  268. NonnullRefPtr<Gfx::PaletteImpl> m_palette;
  269. };
  270. inline Widget* Widget::parent_widget()
  271. {
  272. if (parent() && is<Widget>(*parent()))
  273. return &downcast<Widget>(*parent());
  274. return nullptr;
  275. }
  276. inline const Widget* Widget::parent_widget() const
  277. {
  278. if (parent() && is<Widget>(*parent()))
  279. return &downcast<const Widget>(*parent());
  280. return nullptr;
  281. }
  282. }
  283. AK_BEGIN_TYPE_TRAITS(GUI::Widget)
  284. static bool is_type(const Core::Object& object) { return object.is_widget(); }
  285. AK_END_TYPE_TRAITS()