Window.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Function.h>
  8. #include <AK/OwnPtr.h>
  9. #include <AK/String.h>
  10. #include <AK/WeakPtr.h>
  11. #include <LibCore/Object.h>
  12. #include <LibGUI/FocusSource.h>
  13. #include <LibGUI/Forward.h>
  14. #include <LibGUI/WindowType.h>
  15. #include <LibGfx/Color.h>
  16. #include <LibGfx/Forward.h>
  17. #include <LibGfx/Rect.h>
  18. #include <LibGfx/StandardCursor.h>
  19. namespace GUI {
  20. class WindowBackingStore;
  21. class Window : public Core::Object {
  22. C_OBJECT(Window)
  23. public:
  24. virtual ~Window() override;
  25. static Window* from_window_id(int);
  26. bool is_modified() const;
  27. void set_modified(bool);
  28. bool is_modal() const { return m_modal; }
  29. void set_modal(bool);
  30. bool is_fullscreen() const { return m_fullscreen; }
  31. void set_fullscreen(bool);
  32. bool is_maximized() const;
  33. void set_maximized(bool);
  34. bool is_frameless() const { return m_frameless; }
  35. void set_frameless(bool);
  36. void set_forced_shadow(bool);
  37. bool is_resizable() const { return m_resizable; }
  38. void set_resizable(bool resizable) { m_resizable = resizable; }
  39. bool is_minimizable() const { return m_minimizable; }
  40. void set_minimizable(bool minimizable) { m_minimizable = minimizable; }
  41. void set_double_buffering_enabled(bool);
  42. void set_has_alpha_channel(bool);
  43. bool has_alpha_channel() const { return m_has_alpha_channel; }
  44. void set_opacity(float);
  45. float opacity() const { return m_opacity_when_windowless; }
  46. void set_alpha_hit_threshold(float);
  47. float alpha_hit_threshold() const { return m_alpha_hit_threshold; }
  48. WindowType window_type() const { return m_window_type; }
  49. void set_window_type(WindowType);
  50. int window_id() const { return m_window_id; }
  51. void make_window_manager(unsigned event_mask);
  52. String title() const;
  53. void set_title(String);
  54. Color background_color() const { return m_background_color; }
  55. void set_background_color(Color color) { m_background_color = color; }
  56. enum class CloseRequestDecision {
  57. StayOpen,
  58. Close,
  59. };
  60. Function<void()> on_close;
  61. Function<CloseRequestDecision()> on_close_request;
  62. Function<void(bool is_active_input)> on_active_input_change;
  63. int x() const { return rect().x(); }
  64. int y() const { return rect().y(); }
  65. int width() const { return rect().width(); }
  66. int height() const { return rect().height(); }
  67. Gfx::IntRect rect() const;
  68. Gfx::IntRect applet_rect_on_screen() const;
  69. Gfx::IntSize size() const { return rect().size(); }
  70. void set_rect(const Gfx::IntRect&);
  71. void set_rect(int x, int y, int width, int height) { set_rect({ x, y, width, height }); }
  72. Gfx::IntPoint position() const { return rect().location(); }
  73. Gfx::IntSize minimum_size() const;
  74. void set_minimum_size(const Gfx::IntSize&);
  75. void set_minimum_size(int width, int height) { set_minimum_size({ width, height }); }
  76. void move_to(int x, int y) { move_to({ x, y }); }
  77. void move_to(const Gfx::IntPoint& point) { set_rect({ point, size() }); }
  78. void resize(int width, int height) { resize({ width, height }); }
  79. void resize(const Gfx::IntSize& size) { set_rect({ position(), size }); }
  80. void center_on_screen();
  81. void center_within(const Window&);
  82. virtual void event(Core::Event&) override;
  83. bool is_visible() const;
  84. bool is_active() const;
  85. bool is_active_input() const { return m_is_active_input; }
  86. bool is_accessory() const { return m_accessory; }
  87. void set_accessory(bool accessory) { m_accessory = accessory; }
  88. void show();
  89. void hide();
  90. virtual void close();
  91. void move_to_front();
  92. void start_interactive_resize();
  93. Widget* main_widget() { return m_main_widget; }
  94. const Widget* main_widget() const { return m_main_widget; }
  95. void set_main_widget(Widget*);
  96. template<class T, class... Args>
  97. inline T& set_main_widget(Args&&... args)
  98. {
  99. auto widget = T::construct(forward<Args>(args)...);
  100. set_main_widget(widget.ptr());
  101. return *widget;
  102. }
  103. Widget* focused_widget() { return m_focused_widget; }
  104. const Widget* focused_widget() const { return m_focused_widget; }
  105. void set_focused_widget(Widget*, FocusSource = FocusSource::Programmatic);
  106. void update();
  107. void update(const Gfx::IntRect&);
  108. void set_global_cursor_tracking_widget(Widget*);
  109. Widget* global_cursor_tracking_widget() { return m_global_cursor_tracking_widget.ptr(); }
  110. const Widget* global_cursor_tracking_widget() const { return m_global_cursor_tracking_widget.ptr(); }
  111. void set_automatic_cursor_tracking_widget(Widget*);
  112. Widget* automatic_cursor_tracking_widget() { return m_automatic_cursor_tracking_widget.ptr(); }
  113. const Widget* automatic_cursor_tracking_widget() const { return m_automatic_cursor_tracking_widget.ptr(); }
  114. Widget* hovered_widget() { return m_hovered_widget.ptr(); }
  115. const Widget* hovered_widget() const { return m_hovered_widget.ptr(); }
  116. void set_hovered_widget(Widget*);
  117. Gfx::Bitmap* back_bitmap();
  118. Gfx::IntSize size_increment() const { return m_size_increment; }
  119. void set_size_increment(const Gfx::IntSize&);
  120. Gfx::IntSize base_size() const { return m_base_size; }
  121. void set_base_size(const Gfx::IntSize&);
  122. const Optional<Gfx::IntSize>& resize_aspect_ratio() const { return m_resize_aspect_ratio; }
  123. void set_resize_aspect_ratio(int width, int height) { set_resize_aspect_ratio(Gfx::IntSize(width, height)); }
  124. void set_no_resize_aspect_ratio() { set_resize_aspect_ratio({}); }
  125. void set_resize_aspect_ratio(const Optional<Gfx::IntSize>& ratio);
  126. void set_cursor(Gfx::StandardCursor);
  127. void set_cursor(const Gfx::Bitmap&);
  128. void set_icon(const Gfx::Bitmap*);
  129. void apply_icon();
  130. const Gfx::Bitmap* icon() const { return m_icon.ptr(); }
  131. Vector<Widget&> focusable_widgets(FocusSource) const;
  132. void schedule_relayout();
  133. void refresh_system_theme();
  134. static void for_each_window(Badge<WindowServerConnection>, Function<void(Window&)>);
  135. static void update_all_windows(Badge<WindowServerConnection>);
  136. void notify_state_changed(Badge<WindowServerConnection>, bool minimized, bool occluded);
  137. virtual bool is_visible_for_timer_purposes() const override { return m_visible_for_timer_purposes; }
  138. Action* action_for_key_event(const KeyEvent&);
  139. void did_add_widget(Badge<Widget>, Widget&);
  140. void did_remove_widget(Badge<Widget>, Widget&);
  141. Window* find_parent_window();
  142. void set_progress(Optional<int>);
  143. void update_cursor(Badge<Widget>) { update_cursor(); }
  144. void did_disable_focused_widget(Badge<Widget>);
  145. void set_menubar(RefPtr<Menubar>);
  146. protected:
  147. Window(Core::Object* parent = nullptr);
  148. virtual void wm_event(WMEvent&);
  149. virtual void screen_rects_change_event(ScreenRectsChangeEvent&);
  150. private:
  151. void update_cursor();
  152. void focus_a_widget_if_possible(FocusSource);
  153. void handle_drop_event(DropEvent&);
  154. void handle_mouse_event(MouseEvent&);
  155. void handle_multi_paint_event(MultiPaintEvent&);
  156. void handle_key_event(KeyEvent&);
  157. void handle_resize_event(ResizeEvent&);
  158. void handle_input_entered_or_left_event(Core::Event&);
  159. void handle_became_active_or_inactive_event(Core::Event&);
  160. void handle_close_request();
  161. void handle_theme_change_event(ThemeChangeEvent&);
  162. void handle_screen_rects_change_event(ScreenRectsChangeEvent&);
  163. void handle_drag_move_event(DragEvent&);
  164. void handle_left_event();
  165. void server_did_destroy();
  166. OwnPtr<WindowBackingStore> create_backing_store(const Gfx::IntSize&);
  167. void set_current_backing_store(WindowBackingStore&, bool flush_immediately = false);
  168. void flip(const Vector<Gfx::IntRect, 32>& dirty_rects);
  169. void force_update();
  170. WeakPtr<Widget> m_previously_focused_widget;
  171. OwnPtr<WindowBackingStore> m_front_store;
  172. OwnPtr<WindowBackingStore> m_back_store;
  173. RefPtr<Menubar> m_menubar;
  174. RefPtr<Gfx::Bitmap> m_icon;
  175. RefPtr<Gfx::Bitmap> m_custom_cursor;
  176. int m_window_id { 0 };
  177. float m_opacity_when_windowless { 1.0f };
  178. float m_alpha_hit_threshold { 0.0f };
  179. RefPtr<Widget> m_main_widget;
  180. WeakPtr<Widget> m_focused_widget;
  181. WeakPtr<Widget> m_global_cursor_tracking_widget;
  182. WeakPtr<Widget> m_automatic_cursor_tracking_widget;
  183. WeakPtr<Widget> m_hovered_widget;
  184. Gfx::IntRect m_rect_when_windowless;
  185. Gfx::IntSize m_minimum_size_when_windowless { 50, 50 };
  186. bool m_minimum_size_modified { false };
  187. String m_title_when_windowless;
  188. Vector<Gfx::IntRect, 32> m_pending_paint_event_rects;
  189. Gfx::IntSize m_size_increment;
  190. Gfx::IntSize m_base_size;
  191. Color m_background_color { Color::WarmGray };
  192. WindowType m_window_type { WindowType::Normal };
  193. Gfx::StandardCursor m_cursor { Gfx::StandardCursor::None };
  194. Gfx::StandardCursor m_effective_cursor { Gfx::StandardCursor::None };
  195. bool m_is_active_input { false };
  196. bool m_has_alpha_channel { false };
  197. bool m_double_buffering_enabled { true };
  198. bool m_modal { false };
  199. bool m_resizable { true };
  200. Optional<Gfx::IntSize> m_resize_aspect_ratio {};
  201. bool m_minimizable { true };
  202. bool m_fullscreen { false };
  203. bool m_frameless { false };
  204. bool m_forced_shadow { false };
  205. bool m_layout_pending { false };
  206. bool m_visible_for_timer_purposes { true };
  207. bool m_visible { false };
  208. bool m_accessory { false };
  209. bool m_moved_by_client { false };
  210. };
  211. }
  212. template<>
  213. struct AK::Formatter<GUI::Window> : Formatter<Core::Object> {
  214. };