GWindow.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #pragma once
  2. #include <AK/String.h>
  3. #include <AK/HashMap.h>
  4. #include <AK/WeakPtr.h>
  5. #include <LibCore/CObject.h>
  6. #include <LibDraw/GraphicsBitmap.h>
  7. #include <LibDraw/Rect.h>
  8. #include <LibGUI/GWindowType.h>
  9. class GWidget;
  10. class GWMEvent;
  11. enum class GStandardCursor {
  12. None = 0,
  13. Arrow,
  14. IBeam,
  15. ResizeHorizontal,
  16. ResizeVertical,
  17. ResizeDiagonalTLBR,
  18. ResizeDiagonalBLTR,
  19. Hand,
  20. };
  21. class GWindow : public CObject {
  22. C_OBJECT(GWindow)
  23. public:
  24. virtual ~GWindow() override;
  25. static GWindow* from_window_id(int);
  26. bool is_modal() const { return m_modal; }
  27. void set_modal(bool);
  28. bool is_fullscreen() const { return m_fullscreen; }
  29. void set_fullscreen(bool);
  30. bool is_resizable() const { return m_resizable; }
  31. void set_resizable(bool resizable) { m_resizable = resizable; }
  32. void set_double_buffering_enabled(bool);
  33. void set_has_alpha_channel(bool);
  34. void set_opacity(float);
  35. void set_window_type(GWindowType);
  36. int window_id() const { return m_window_id; }
  37. String title() const;
  38. void set_title(const StringView&);
  39. bool show_titlebar() const { return m_show_titlebar; };
  40. void set_show_titlebar(bool show) { m_show_titlebar = show; };
  41. Color background_color() const { return m_background_color; }
  42. void set_background_color(Color color) { m_background_color = color; }
  43. enum class CloseRequestDecision {
  44. StayOpen,
  45. Close,
  46. };
  47. Function<CloseRequestDecision()> on_close_request;
  48. int x() const { return rect().x(); }
  49. int y() const { return rect().y(); }
  50. int width() const { return rect().width(); }
  51. int height() const { return rect().height(); }
  52. Rect rect() const;
  53. Size size() const { return rect().size(); }
  54. void set_rect(const Rect&);
  55. void set_rect(int x, int y, int width, int height) { set_rect({ x, y, width, height }); }
  56. Point position() const { return rect().location(); }
  57. void move_to(int x, int y) { move_to({ x, y }); }
  58. void move_to(const Point& point) { set_rect({ point, size() }); }
  59. void resize(int width, int height) { resize({ width, height }); }
  60. void resize(const Size& size) { set_rect({ position(), size }); }
  61. virtual void event(CEvent&) override;
  62. bool is_visible() const;
  63. bool is_active() const { return m_is_active; }
  64. void show();
  65. void hide();
  66. virtual void close();
  67. void move_to_front();
  68. void start_wm_resize();
  69. GWidget* main_widget() { return m_main_widget; }
  70. const GWidget* main_widget() const { return m_main_widget; }
  71. void set_main_widget(GWidget*);
  72. GWidget* focused_widget() { return m_focused_widget; }
  73. const GWidget* focused_widget() const { return m_focused_widget; }
  74. void set_focused_widget(GWidget*);
  75. void update(const Rect& = Rect());
  76. void set_global_cursor_tracking_widget(GWidget*);
  77. GWidget* global_cursor_tracking_widget() { return m_global_cursor_tracking_widget.ptr(); }
  78. const GWidget* global_cursor_tracking_widget() const { return m_global_cursor_tracking_widget.ptr(); }
  79. void set_automatic_cursor_tracking_widget(GWidget*);
  80. GWidget* automatic_cursor_tracking_widget() { return m_automatic_cursor_tracking_widget.ptr(); }
  81. const GWidget* automatic_cursor_tracking_widget() const { return m_automatic_cursor_tracking_widget.ptr(); }
  82. GWidget* hovered_widget() { return m_hovered_widget.ptr(); }
  83. const GWidget* hovered_widget() const { return m_hovered_widget.ptr(); }
  84. void set_hovered_widget(GWidget*);
  85. GraphicsBitmap* front_bitmap() { return m_front_bitmap.ptr(); }
  86. GraphicsBitmap* back_bitmap() { return m_back_bitmap.ptr(); }
  87. Size size_increment() const { return m_size_increment; }
  88. void set_size_increment(const Size& increment) { m_size_increment = increment; }
  89. Size base_size() const { return m_base_size; }
  90. void set_base_size(const Size& size) { m_base_size = size; }
  91. void set_override_cursor(GStandardCursor);
  92. void set_icon(const GraphicsBitmap*);
  93. void apply_icon();
  94. const GraphicsBitmap* icon() const { return m_icon.ptr(); }
  95. Vector<GWidget*> focusable_widgets() const;
  96. virtual void save_to(AK::JsonObject&) override;
  97. void schedule_relayout();
  98. protected:
  99. GWindow(CObject* parent = nullptr);
  100. virtual void wm_event(GWMEvent&);
  101. private:
  102. virtual bool is_window() const override final { return true; }
  103. void paint_keybinds();
  104. void collect_keyboard_activation_targets();
  105. NonnullRefPtr<GraphicsBitmap> create_backing_bitmap(const Size&);
  106. NonnullRefPtr<GraphicsBitmap> create_shared_bitmap(GraphicsBitmap::Format, const Size&);
  107. void set_current_backing_bitmap(GraphicsBitmap&, bool flush_immediately = false);
  108. void flip(const Vector<Rect, 32>& dirty_rects);
  109. RefPtr<GraphicsBitmap> m_front_bitmap;
  110. RefPtr<GraphicsBitmap> m_back_bitmap;
  111. RefPtr<GraphicsBitmap> m_icon;
  112. int m_window_id { 0 };
  113. float m_opacity_when_windowless { 1.0f };
  114. RefPtr<GWidget> m_main_widget;
  115. WeakPtr<GWidget> m_focused_widget;
  116. WeakPtr<GWidget> m_global_cursor_tracking_widget;
  117. WeakPtr<GWidget> m_automatic_cursor_tracking_widget;
  118. WeakPtr<GWidget> m_hovered_widget;
  119. Rect m_rect_when_windowless;
  120. String m_title_when_windowless;
  121. Vector<Rect, 32> m_pending_paint_event_rects;
  122. Size m_size_increment;
  123. Size m_base_size;
  124. Color m_background_color { Color::WarmGray };
  125. GWindowType m_window_type { GWindowType::Normal };
  126. bool m_is_active { false };
  127. bool m_has_alpha_channel { false };
  128. bool m_double_buffering_enabled { true };
  129. bool m_modal { false };
  130. bool m_resizable { true };
  131. bool m_fullscreen { false };
  132. bool m_show_titlebar { true };
  133. bool m_keybind_mode { false };
  134. String m_entered_keybind;
  135. int m_max_keybind_length { 0 };
  136. HashMap<String, WeakPtr<GWidget>> m_keyboard_activation_targets;
  137. bool m_layout_pending { false };
  138. };