GWindow.h 5.9 KB

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