GWidget.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #pragma once
  2. #include "GEvent.h"
  3. #include "GObject.h"
  4. #include <SharedGraphics/Rect.h>
  5. #include <SharedGraphics/Color.h>
  6. #include <SharedGraphics/Font.h>
  7. #include <AK/Badge.h>
  8. #include <AK/AKString.h>
  9. class GraphicsBitmap;
  10. class GLayout;
  11. class GWindow;
  12. enum class SizePolicy { Fixed, Fill };
  13. enum class Orientation { Horizontal, Vertical };
  14. class GWidget : public GObject {
  15. public:
  16. explicit GWidget(GWidget* parent = nullptr);
  17. virtual ~GWidget() override;
  18. GLayout* layout() { return m_layout.ptr(); }
  19. void set_layout(OwnPtr<GLayout>&&);
  20. SizePolicy horizontal_size_policy() const { return m_horizontal_size_policy; }
  21. SizePolicy vertical_size_policy() const { return m_vertical_size_policy; }
  22. SizePolicy size_policy(Orientation orientation) { return orientation == Orientation::Horizontal ? m_horizontal_size_policy : m_vertical_size_policy; }
  23. void set_size_policy(SizePolicy horizontal_policy, SizePolicy vertical_policy);
  24. Size preferred_size() const { return m_preferred_size; }
  25. void set_preferred_size(const Size&);
  26. virtual void event(GEvent&) override;
  27. virtual void paint_event(GPaintEvent&);
  28. virtual void resize_event(GResizeEvent&);
  29. virtual void show_event(GShowEvent&);
  30. virtual void hide_event(GHideEvent&);
  31. virtual void keydown_event(GKeyEvent&);
  32. virtual void keyup_event(GKeyEvent&);
  33. virtual void mousemove_event(GMouseEvent&);
  34. virtual void mousedown_event(GMouseEvent&);
  35. virtual void mouseup_event(GMouseEvent&);
  36. virtual void focusin_event(GEvent&);
  37. virtual void focusout_event(GEvent&);
  38. virtual void enter_event(GEvent&);
  39. virtual void leave_event(GEvent&);
  40. Rect relative_rect() const { return m_relative_rect; }
  41. Point relative_position() const { return m_relative_rect.location(); }
  42. Rect window_relative_rect() const;
  43. int x() const { return m_relative_rect.x(); }
  44. int y() const { return m_relative_rect.y(); }
  45. int width() const { return m_relative_rect.width(); }
  46. int height() const { return m_relative_rect.height(); }
  47. Rect rect() const { return { 0, 0, width(), height() }; }
  48. Size size() const { return m_relative_rect.size(); }
  49. void update();
  50. void update(const Rect&);
  51. virtual bool accepts_focus() const { return false; }
  52. bool is_focused() const;
  53. void set_focus(bool);
  54. struct HitTestResult {
  55. GWidget* widget { nullptr };
  56. int localX { 0 };
  57. int localY { 0 };
  58. };
  59. HitTestResult hit_test(int x, int y);
  60. virtual const char* class_name() const override { return "GWidget"; }
  61. void set_relative_rect(const Rect&);
  62. void set_relative_rect(int x, int y, int width, int height) { set_relative_rect({ x, y, width, height }); }
  63. void move_to(const Point& point) { set_relative_rect({ point, relative_rect().size() }); }
  64. void move_to(int x, int y) { move_to({ x, y }); }
  65. void resize(const Size& size) { set_relative_rect({ relative_rect().location(), size }); }
  66. void resize(int width, int height) { resize({ width, height }); }
  67. Color background_color() const { return m_background_color; }
  68. Color foreground_color() const { return m_foreground_color; }
  69. void set_background_color(Color color) { m_background_color = color; }
  70. void set_foreground_color(Color color) { m_foreground_color = color; }
  71. GWindow* window()
  72. {
  73. if (auto* pw = parent_widget())
  74. return pw->window();
  75. return m_window;
  76. }
  77. const GWindow* window() const
  78. {
  79. if (auto* pw = parent_widget())
  80. return pw->window();
  81. return m_window;
  82. }
  83. void set_window(GWindow*);
  84. GWidget* parent_widget() { return static_cast<GWidget*>(parent()); }
  85. const GWidget* parent_widget() const { return static_cast<const GWidget*>(parent()); }
  86. void set_fill_with_background_color(bool b) { m_fill_with_background_color = b; }
  87. bool fill_with_background_color() const { return m_fill_with_background_color; }
  88. const Font& font() const { return *m_font; }
  89. void set_font(RetainPtr<Font>&&);
  90. void set_global_cursor_tracking(bool);
  91. bool global_cursor_tracking() const;
  92. void notify_layout_changed(Badge<GLayout>);
  93. private:
  94. void handle_paint_event(GPaintEvent&);
  95. void handle_resize_event(GResizeEvent&);
  96. void do_layout();
  97. void invalidate_layout();
  98. GWindow* m_window { nullptr };
  99. OwnPtr<GLayout> m_layout;
  100. Rect m_relative_rect;
  101. Color m_background_color;
  102. Color m_foreground_color;
  103. RetainPtr<Font> m_font;
  104. SizePolicy m_horizontal_size_policy { SizePolicy::Fill };
  105. SizePolicy m_vertical_size_policy { SizePolicy::Fill };
  106. Size m_preferred_size;
  107. bool m_fill_with_background_color { true };
  108. };