Widget.h 12 KB

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