GWindow.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #pragma once
  2. #include <LibCore/CObject.h>
  3. #include <LibGUI/GWindowType.h>
  4. #include <SharedGraphics/Rect.h>
  5. #include <SharedGraphics/GraphicsBitmap.h>
  6. #include <AK/AKString.h>
  7. #include <AK/WeakPtr.h>
  8. class GWidget;
  9. class GWMEvent;
  10. enum class GStandardCursor {
  11. None = 0,
  12. Arrow,
  13. IBeam,
  14. ResizeHorizontal,
  15. ResizeVertical,
  16. ResizeDiagonalTLBR,
  17. ResizeDiagonalBLTR,
  18. };
  19. class GWindow : public CObject {
  20. public:
  21. GWindow(CObject* parent = nullptr);
  22. virtual ~GWindow() override;
  23. static GWindow* from_window_id(int);
  24. bool is_modal() const { return m_modal; }
  25. void set_modal(bool);
  26. bool is_fullscreen() const { return m_fullscreen; }
  27. void set_fullscreen(bool fullscreen) { m_fullscreen = fullscreen; }
  28. bool is_resizable() const { return m_resizable; }
  29. void set_resizable(bool resizable) { m_resizable = resizable; }
  30. void set_double_buffering_enabled(bool);
  31. void set_has_alpha_channel(bool);
  32. void set_opacity(float);
  33. void set_window_type(GWindowType);
  34. int window_id() const { return m_window_id; }
  35. String title() const;
  36. void set_title(const String&);
  37. Color background_color() const { return m_background_color; }
  38. void set_background_color(Color color) { m_background_color = color; }
  39. int x() const { return rect().x(); }
  40. int y() const { return rect().y(); }
  41. int width() const { return rect().width(); }
  42. int height() const { return rect().height(); }
  43. Rect rect() const;
  44. Size size() const { return rect().size(); }
  45. void set_rect(const Rect&);
  46. void set_rect(int x, int y, int width, int height) { set_rect({ x, y, width, height }); }
  47. Point position() const { return rect().location(); }
  48. void move_to(int x, int y) { move_to({ x, y }); }
  49. void move_to(const Point& point) { set_rect({ point, size() }); }
  50. void resize(int width, int height) { resize({ width, height }); }
  51. void resize(const Size& size) { set_rect({ position(), size }); }
  52. virtual void event(CEvent&) override;
  53. bool is_visible() const;
  54. bool is_active() const { return m_is_active; }
  55. void show();
  56. void hide();
  57. void close();
  58. void start_wm_resize();
  59. GWidget* main_widget() { return m_main_widget; }
  60. const GWidget* main_widget() const { return m_main_widget; }
  61. void set_main_widget(GWidget*);
  62. GWidget* focused_widget() { return m_focused_widget; }
  63. const GWidget* focused_widget() const { return m_focused_widget; }
  64. void set_focused_widget(GWidget*);
  65. void update(const Rect& = Rect());
  66. void set_global_cursor_tracking_widget(GWidget*);
  67. GWidget* global_cursor_tracking_widget() { return m_global_cursor_tracking_widget.ptr(); }
  68. const GWidget* global_cursor_tracking_widget() const { return m_global_cursor_tracking_widget.ptr(); }
  69. void set_automatic_cursor_tracking_widget(GWidget*);
  70. GWidget* automatic_cursor_tracking_widget() { return m_automatic_cursor_tracking_widget.ptr(); }
  71. const GWidget* automatic_cursor_tracking_widget() const { return m_automatic_cursor_tracking_widget.ptr(); }
  72. bool should_exit_event_loop_on_close() const { return m_should_exit_app_on_close; }
  73. void set_should_exit_event_loop_on_close(bool b) { m_should_exit_app_on_close = b; }
  74. GWidget* hovered_widget() { return m_hovered_widget.ptr(); }
  75. const GWidget* hovered_widget() const { return m_hovered_widget.ptr(); }
  76. void set_hovered_widget(GWidget*);
  77. GraphicsBitmap* front_bitmap() { return m_front_bitmap.ptr(); }
  78. GraphicsBitmap* back_bitmap() { return m_back_bitmap.ptr(); }
  79. Size size_increment() const { return m_size_increment; }
  80. void set_size_increment(const Size& increment) { m_size_increment = increment; }
  81. Size base_size() const { return m_base_size; }
  82. void set_base_size(const Size& size) { m_base_size = size; }
  83. void set_override_cursor(GStandardCursor);
  84. String icon_path() const { return m_icon_path; }
  85. void set_icon_path(const String&);
  86. Vector<GWidget*> focusable_widgets() const;
  87. virtual const char* class_name() const override { return "GWindow"; }
  88. protected:
  89. virtual void wm_event(GWMEvent&);
  90. private:
  91. virtual bool is_window() const override final { return true; }
  92. Retained<GraphicsBitmap> create_backing_bitmap(const Size&);
  93. void set_current_backing_bitmap(GraphicsBitmap&, bool flush_immediately = false);
  94. void flip(const Rect& dirty_rect);
  95. RetainPtr<GraphicsBitmap> m_front_bitmap;
  96. RetainPtr<GraphicsBitmap> m_back_bitmap;
  97. int m_window_id { 0 };
  98. float m_opacity_when_windowless { 1.0f };
  99. GWidget* m_main_widget { nullptr };
  100. WeakPtr<GWidget> m_focused_widget;
  101. WeakPtr<GWidget> m_global_cursor_tracking_widget;
  102. WeakPtr<GWidget> m_automatic_cursor_tracking_widget;
  103. WeakPtr<GWidget> m_hovered_widget;
  104. Rect m_rect_when_windowless;
  105. String m_title_when_windowless;
  106. String m_icon_path;
  107. Vector<Rect, 32> m_pending_paint_event_rects;
  108. Size m_size_increment;
  109. Size m_base_size;
  110. Color m_background_color { Color::LightGray };
  111. GWindowType m_window_type { GWindowType::Normal };
  112. bool m_is_active { false };
  113. bool m_should_exit_app_on_close { false };
  114. bool m_has_alpha_channel { false };
  115. bool m_double_buffering_enabled { true };
  116. bool m_modal { false };
  117. bool m_resizable { true };
  118. bool m_fullscreen { false };
  119. };