GWidget.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #pragma once
  2. #include <AK/Badge.h>
  3. #include <AK/HashMap.h>
  4. #include <AK/String.h>
  5. #include <LibCore/CElapsedTimer.h>
  6. #include <LibCore/CObject.h>
  7. #include <LibDraw/Color.h>
  8. #include <LibDraw/Font.h>
  9. #include <LibDraw/Orientation.h>
  10. #include <LibDraw/Rect.h>
  11. #include <LibDraw/SystemTheme.h>
  12. #include <LibGUI/GEvent.h>
  13. #include <LibGUI/GShortcut.h>
  14. #define REGISTER_GWIDGET(class_name) \
  15. extern GWidgetClassRegistration registration_##class_name; \
  16. GWidgetClassRegistration registration_##class_name(#class_name, [](GWidget* parent) { return class_name::construct(parent); });
  17. class GAction;
  18. class GLayout;
  19. class GMenu;
  20. class GWindow;
  21. class GraphicsBitmap;
  22. class Palette;
  23. enum class SizePolicy {
  24. Fixed,
  25. Fill
  26. };
  27. inline const char* to_string(SizePolicy policy)
  28. {
  29. switch (policy) {
  30. case SizePolicy::Fixed:
  31. return "SizePolicy::Fixed";
  32. case SizePolicy::Fill:
  33. return "SizePolicy::Fill";
  34. }
  35. return "SizePolicy::(Invalid)";
  36. }
  37. enum class HorizontalDirection {
  38. Left,
  39. Right
  40. };
  41. enum class VerticalDirection {
  42. Up,
  43. Down
  44. };
  45. class GWidget;
  46. class GWidgetClassRegistration {
  47. AK_MAKE_NONCOPYABLE(GWidgetClassRegistration)
  48. AK_MAKE_NONMOVABLE(GWidgetClassRegistration)
  49. public:
  50. GWidgetClassRegistration(const String& class_name, Function<NonnullRefPtr<GWidget>(GWidget*)> factory);
  51. ~GWidgetClassRegistration();
  52. String class_name() const { return m_class_name; }
  53. NonnullRefPtr<GWidget> construct(GWidget* parent) const { return m_factory(parent); }
  54. static void for_each(Function<void(const GWidgetClassRegistration&)>);
  55. static const GWidgetClassRegistration* find(const String& class_name);
  56. private:
  57. String m_class_name;
  58. Function<NonnullRefPtr<GWidget>(GWidget*)> m_factory;
  59. };
  60. class GWidget : public CObject {
  61. C_OBJECT(GWidget)
  62. public:
  63. virtual ~GWidget() override;
  64. GLayout* layout() { return m_layout.ptr(); }
  65. const GLayout* layout() const { return m_layout.ptr(); }
  66. void set_layout(OwnPtr<GLayout>&&);
  67. SizePolicy horizontal_size_policy() const { return m_horizontal_size_policy; }
  68. SizePolicy vertical_size_policy() const { return m_vertical_size_policy; }
  69. SizePolicy size_policy(Orientation orientation) { return orientation == Orientation::Horizontal ? m_horizontal_size_policy : m_vertical_size_policy; }
  70. void set_size_policy(SizePolicy horizontal_policy, SizePolicy vertical_policy);
  71. void set_size_policy(Orientation, SizePolicy);
  72. Size preferred_size() const { return m_preferred_size; }
  73. void set_preferred_size(const Size&);
  74. void set_preferred_size(int width, int height) { set_preferred_size({ width, height }); }
  75. bool has_tooltip() const { return !m_tooltip.is_empty(); }
  76. String tooltip() const { return m_tooltip; }
  77. void set_tooltip(const StringView& tooltip) { m_tooltip = tooltip; }
  78. bool is_enabled() const { return m_enabled; }
  79. void set_enabled(bool);
  80. bool updates_enabled() const { return m_updates_enabled; }
  81. void set_updates_enabled(bool);
  82. virtual void event(CEvent&) override;
  83. // This is called after children have been painted.
  84. virtual void second_paint_event(GPaintEvent&);
  85. Rect relative_rect() const { return m_relative_rect; }
  86. Point relative_position() const { return m_relative_rect.location(); }
  87. Rect window_relative_rect() const;
  88. Rect screen_relative_rect() const;
  89. int x() const { return m_relative_rect.x(); }
  90. int y() const { return m_relative_rect.y(); }
  91. int width() const { return m_relative_rect.width(); }
  92. int height() const { return m_relative_rect.height(); }
  93. int length(Orientation orientation) const { return orientation == Orientation::Vertical ? height() : width(); }
  94. Rect rect() const { return { 0, 0, width(), height() }; }
  95. Size size() const { return m_relative_rect.size(); }
  96. void update();
  97. void update(const Rect&);
  98. virtual bool accepts_focus() const { return false; }
  99. virtual bool supports_keyboard_activation() const { return false; }
  100. bool is_focused() const;
  101. void set_focus(bool);
  102. enum class ShouldRespectGreediness { No = 0,
  103. Yes };
  104. struct HitTestResult {
  105. GWidget* widget { nullptr };
  106. Point local_position;
  107. };
  108. HitTestResult hit_test(const Point&, ShouldRespectGreediness = ShouldRespectGreediness::Yes);
  109. GWidget* child_at(const Point&) const;
  110. void set_relative_rect(const Rect&);
  111. void set_relative_rect(int x, int y, int width, int height) { set_relative_rect({ x, y, width, height }); }
  112. void set_x(int x) { set_relative_rect(x, y(), width(), height()); }
  113. void set_y(int y) { set_relative_rect(x(), y, width(), height()); }
  114. void set_width(int width) { set_relative_rect(x(), y(), width, height()); }
  115. void set_height(int height) { set_relative_rect(x(), y(), width(), height); }
  116. void move_to(const Point& point) { set_relative_rect({ point, relative_rect().size() }); }
  117. void move_to(int x, int y) { move_to({ x, y }); }
  118. void resize(const Size& size) { set_relative_rect({ relative_rect().location(), size }); }
  119. void resize(int width, int height) { resize({ width, height }); }
  120. void move_by(int x, int y) { move_by({ x, y }); }
  121. void move_by(const Point& delta) { set_relative_rect({ relative_position().translated(delta), size() }); }
  122. ColorRole background_role() const { return m_background_role; }
  123. void set_background_role(ColorRole role) { m_background_role = role; }
  124. ColorRole foreground_role() const { return m_foreground_role; }
  125. void set_foreground_role(ColorRole role) { m_foreground_role = role; }
  126. Color background_color() const { return m_background_color; }
  127. Color foreground_color() const { return m_foreground_color; }
  128. void set_background_color(Color color) { m_background_color = color; }
  129. void set_foreground_color(Color color) { m_foreground_color = color; }
  130. void set_backcolor(const StringView&);
  131. void set_forecolor(const StringView&);
  132. void set_autofill(bool b) { set_fill_with_background_color(b); }
  133. GWindow* window()
  134. {
  135. if (auto* pw = parent_widget())
  136. return pw->window();
  137. return m_window;
  138. }
  139. const GWindow* window() const
  140. {
  141. if (auto* pw = parent_widget())
  142. return pw->window();
  143. return m_window;
  144. }
  145. void set_window(GWindow*);
  146. GWidget* parent_widget();
  147. const GWidget* parent_widget() const;
  148. void set_fill_with_background_color(bool b) { m_fill_with_background_color = b; }
  149. bool fill_with_background_color() const { return m_fill_with_background_color; }
  150. const Font& font() const { return *m_font; }
  151. void set_font(const Font*);
  152. void set_font(const Font& font) { set_font(&font); }
  153. void set_global_cursor_tracking(bool);
  154. bool global_cursor_tracking() const;
  155. void notify_layout_changed(Badge<GLayout>);
  156. void invalidate_layout();
  157. bool is_visible() const { return m_visible; }
  158. void set_visible(bool);
  159. bool spans_entire_window_horizontally() const;
  160. bool is_greedy_for_hits() const { return m_greedy_for_hits; }
  161. void set_greedy_for_hits(bool b) { m_greedy_for_hits = b; }
  162. void move_to_front();
  163. void move_to_back();
  164. bool is_frontmost() const;
  165. bool is_backmost() const;
  166. GAction* action_for_key_event(const GKeyEvent&);
  167. void register_local_shortcut_action(Badge<GAction>, GAction&);
  168. void unregister_local_shortcut_action(Badge<GAction>, GAction&);
  169. template<typename Callback>
  170. void for_each_child_widget(Callback callback)
  171. {
  172. for_each_child([&](auto& child) {
  173. if (is<GWidget>(child))
  174. return callback(to<GWidget>(child));
  175. return IterationDecision::Continue;
  176. });
  177. }
  178. Vector<GWidget*> child_widgets() const;
  179. virtual bool is_radio_button() const { return false; }
  180. virtual bool is_abstract_button() const { return false; }
  181. virtual void save_to(AK::JsonObject&) override;
  182. void do_layout();
  183. const Palette& palette() const { return *m_palette; }
  184. void set_palette(const Palette&);
  185. protected:
  186. explicit GWidget(GWidget* parent = nullptr);
  187. virtual void custom_layout() {}
  188. virtual void did_change_font() {}
  189. virtual void paint_event(GPaintEvent&);
  190. virtual void resize_event(GResizeEvent&);
  191. virtual void show_event(GShowEvent&);
  192. virtual void hide_event(GHideEvent&);
  193. virtual void keydown_event(GKeyEvent&);
  194. virtual void keyup_event(GKeyEvent&);
  195. virtual void mousemove_event(GMouseEvent&);
  196. virtual void mousedown_event(GMouseEvent&);
  197. virtual void mouseup_event(GMouseEvent&);
  198. virtual void mousewheel_event(GMouseEvent&);
  199. virtual void click_event(GMouseEvent&);
  200. virtual void doubleclick_event(GMouseEvent&);
  201. virtual void context_menu_event(GContextMenuEvent&);
  202. virtual void focusin_event(CEvent&);
  203. virtual void focusout_event(CEvent&);
  204. virtual void enter_event(CEvent&);
  205. virtual void leave_event(CEvent&);
  206. virtual void child_event(CChildEvent&) override;
  207. virtual void change_event(GEvent&);
  208. virtual void drop_event(GDropEvent&);
  209. private:
  210. void handle_paint_event(GPaintEvent&);
  211. void handle_resize_event(GResizeEvent&);
  212. void handle_mousedown_event(GMouseEvent&);
  213. void handle_mousedoubleclick_event(GMouseEvent&);
  214. void handle_mouseup_event(GMouseEvent&);
  215. void handle_enter_event(CEvent&);
  216. void handle_leave_event(CEvent&);
  217. void focus_previous_widget();
  218. void focus_next_widget();
  219. GWindow* m_window { nullptr };
  220. OwnPtr<GLayout> m_layout;
  221. Rect m_relative_rect;
  222. ColorRole m_background_role { ColorRole::Window };
  223. ColorRole m_foreground_role { ColorRole::WindowText };
  224. Color m_background_color;
  225. Color m_foreground_color;
  226. NonnullRefPtr<Font> m_font;
  227. String m_tooltip;
  228. SizePolicy m_horizontal_size_policy { SizePolicy::Fill };
  229. SizePolicy m_vertical_size_policy { SizePolicy::Fill };
  230. Size m_preferred_size;
  231. bool m_fill_with_background_color { false };
  232. bool m_visible { true };
  233. bool m_greedy_for_hits { false };
  234. bool m_enabled { true };
  235. bool m_layout_dirty { false };
  236. bool m_updates_enabled { true };
  237. HashMap<GShortcut, GAction*> m_local_shortcut_actions;
  238. NonnullRefPtr<Palette> m_palette;
  239. };
  240. template<>
  241. inline bool is<GWidget>(const CObject& object)
  242. {
  243. return object.is_widget();
  244. }
  245. inline GWidget* GWidget::parent_widget()
  246. {
  247. if (parent() && is<GWidget>(*parent()))
  248. return &to<GWidget>(*parent());
  249. return nullptr;
  250. }
  251. inline const GWidget* GWidget::parent_widget() const
  252. {
  253. if (parent() && is<GWidget>(*parent()))
  254. return &to<const GWidget>(*parent());
  255. return nullptr;
  256. }