GWidget.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #pragma once
  2. #include <AK/String.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. virtual ~GWidget() override;
  44. GLayout* layout() { return m_layout.ptr(); }
  45. const GLayout* layout() const { 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. // This is called after children have been painted.
  64. virtual void second_paint_event(GPaintEvent&);
  65. Rect relative_rect() const { return m_relative_rect; }
  66. Point relative_position() const { return m_relative_rect.location(); }
  67. Rect window_relative_rect() const;
  68. Rect screen_relative_rect() const;
  69. int x() const { return m_relative_rect.x(); }
  70. int y() const { return m_relative_rect.y(); }
  71. int width() const { return m_relative_rect.width(); }
  72. int height() const { return m_relative_rect.height(); }
  73. int length(Orientation orientation) const { return orientation == Orientation::Vertical ? height() : width(); }
  74. Rect rect() const { return { 0, 0, width(), height() }; }
  75. Size size() const { return m_relative_rect.size(); }
  76. void update();
  77. void update(const Rect&);
  78. virtual bool accepts_focus() const { return false; }
  79. virtual bool supports_keyboard_activation() const { return false; }
  80. bool is_focused() const;
  81. void set_focus(bool);
  82. enum class ShouldRespectGreediness { No = 0, Yes };
  83. struct HitTestResult {
  84. GWidget* widget { nullptr };
  85. Point local_position;
  86. };
  87. HitTestResult hit_test(const Point&, ShouldRespectGreediness = ShouldRespectGreediness::Yes);
  88. GWidget* child_at(const Point&) const;
  89. void set_relative_rect(const Rect&);
  90. void set_relative_rect(int x, int y, int width, int height) { set_relative_rect({ x, y, width, height }); }
  91. void set_x(int x) { set_relative_rect(x, y(), width(), height()); }
  92. void set_y(int y) { set_relative_rect(x(), y, width(), height()); }
  93. void set_width(int width) { set_relative_rect(x(), y(), width, height()); }
  94. void set_height(int height) { set_relative_rect(x(), y(), width(), height); }
  95. void move_to(const Point& point) { set_relative_rect({ point, relative_rect().size() }); }
  96. void move_to(int x, int y) { move_to({ x, y }); }
  97. void resize(const Size& size) { set_relative_rect({ relative_rect().location(), size }); }
  98. void resize(int width, int height) { resize({ width, height }); }
  99. void move_by(int x, int y) { move_by({ x, y }); }
  100. void move_by(const Point& delta) { set_relative_rect({ relative_position().translated(delta), size() }); }
  101. Color background_color() const { return m_background_color; }
  102. Color foreground_color() const { return m_foreground_color; }
  103. void set_background_color(Color color) { m_background_color = color; }
  104. void set_foreground_color(Color color) { m_foreground_color = color; }
  105. void set_backcolor(const StringView&);
  106. void set_forecolor(const StringView&);
  107. void set_autofill(bool b) { set_fill_with_background_color(b); }
  108. GWindow* window()
  109. {
  110. if (auto* pw = parent_widget())
  111. return pw->window();
  112. return m_window;
  113. }
  114. const GWindow* window() const
  115. {
  116. if (auto* pw = parent_widget())
  117. return pw->window();
  118. return m_window;
  119. }
  120. void set_window(GWindow*);
  121. GWidget* parent_widget();
  122. const GWidget* parent_widget() const;
  123. void set_fill_with_background_color(bool b) { m_fill_with_background_color = b; }
  124. bool fill_with_background_color() const { return m_fill_with_background_color; }
  125. const Font& font() const { return *m_font; }
  126. void set_font(const Font*);
  127. void set_font(const Font& font) { set_font(&font); }
  128. void set_global_cursor_tracking(bool);
  129. bool global_cursor_tracking() const;
  130. void notify_layout_changed(Badge<GLayout>);
  131. void invalidate_layout();
  132. bool is_visible() const { return m_visible; }
  133. void set_visible(bool);
  134. bool spans_entire_window_horizontally() const;
  135. bool is_greedy_for_hits() const { return m_greedy_for_hits; }
  136. void set_greedy_for_hits(bool b) { m_greedy_for_hits = b; }
  137. void move_to_front();
  138. void move_to_back();
  139. bool is_frontmost() const;
  140. bool is_backmost() const;
  141. GAction* action_for_key_event(const GKeyEvent&);
  142. void register_local_shortcut_action(Badge<GAction>, GAction&);
  143. void unregister_local_shortcut_action(Badge<GAction>, GAction&);
  144. template<typename Callback>
  145. void for_each_child_widget(Callback callback)
  146. {
  147. for_each_child([&](auto& child) {
  148. if (is<GWidget>(child))
  149. return callback(to<GWidget>(child));
  150. return IterationDecision::Continue;
  151. });
  152. }
  153. virtual bool is_radio_button() const { return false; }
  154. virtual bool is_abstract_button() const { return false; }
  155. virtual void save_to(AK::JsonObject&) override;
  156. protected:
  157. explicit GWidget(GWidget* parent = nullptr);
  158. virtual void custom_layout() {}
  159. virtual void did_change_font() {}
  160. virtual void paint_event(GPaintEvent&);
  161. virtual void resize_event(GResizeEvent&);
  162. virtual void show_event(GShowEvent&);
  163. virtual void hide_event(GHideEvent&);
  164. virtual void keydown_event(GKeyEvent&);
  165. virtual void keyup_event(GKeyEvent&);
  166. virtual void mousemove_event(GMouseEvent&);
  167. virtual void mousedown_event(GMouseEvent&);
  168. virtual void mouseup_event(GMouseEvent&);
  169. virtual void mousewheel_event(GMouseEvent&);
  170. virtual void click_event(GMouseEvent&);
  171. virtual void doubleclick_event(GMouseEvent&);
  172. virtual void context_menu_event(GContextMenuEvent&);
  173. virtual void focusin_event(CEvent&);
  174. virtual void focusout_event(CEvent&);
  175. virtual void enter_event(CEvent&);
  176. virtual void leave_event(CEvent&);
  177. virtual void child_event(CChildEvent&) override;
  178. virtual void change_event(GEvent&);
  179. private:
  180. void handle_paint_event(GPaintEvent&);
  181. void handle_resize_event(GResizeEvent&);
  182. void handle_mousedown_event(GMouseEvent&);
  183. void handle_mousedoubleclick_event(GMouseEvent&);
  184. void handle_mouseup_event(GMouseEvent&);
  185. void handle_enter_event(CEvent&);
  186. void handle_leave_event(CEvent&);
  187. void do_layout();
  188. void focus_previous_widget();
  189. void focus_next_widget();
  190. GWindow* m_window { nullptr };
  191. OwnPtr<GLayout> m_layout;
  192. Rect m_relative_rect;
  193. Color m_background_color;
  194. Color m_foreground_color;
  195. NonnullRefPtr<Font> m_font;
  196. String m_tooltip;
  197. SizePolicy m_horizontal_size_policy { SizePolicy::Fill };
  198. SizePolicy m_vertical_size_policy { SizePolicy::Fill };
  199. Size m_preferred_size;
  200. bool m_fill_with_background_color { false };
  201. bool m_visible { true };
  202. bool m_greedy_for_hits { false };
  203. bool m_enabled { true };
  204. bool m_layout_dirty { false };
  205. bool m_updates_enabled { true };
  206. HashMap<GShortcut, GAction*> m_local_shortcut_actions;
  207. };
  208. template<>
  209. inline bool is<GWidget>(const CObject& object)
  210. {
  211. return object.is_widget();
  212. }
  213. inline GWidget* GWidget::parent_widget()
  214. {
  215. if (parent() && is<GWidget>(*parent()))
  216. return &to<GWidget>(*parent());
  217. return nullptr;
  218. }
  219. inline const GWidget* GWidget::parent_widget() const
  220. {
  221. if (parent() && is<GWidget>(*parent()))
  222. return &to<const GWidget>(*parent());
  223. return nullptr;
  224. }