GWidget.h 8.4 KB

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