GWidget.h 8.7 KB

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