Serenity Operating System
1/*
2 * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/DeprecatedString.h>
10#include <AK/Error.h>
11#include <AK/Function.h>
12#include <AK/NonnullOwnPtr.h>
13#include <AK/OwnPtr.h>
14#include <AK/Variant.h>
15#include <AK/WeakPtr.h>
16#include <LibCore/Object.h>
17#include <LibGUI/FocusSource.h>
18#include <LibGUI/Forward.h>
19#include <LibGUI/ResizeDirection.h>
20#include <LibGUI/WindowMode.h>
21#include <LibGUI/WindowType.h>
22#include <LibGfx/Forward.h>
23#include <LibGfx/Rect.h>
24#include <LibGfx/StandardCursor.h>
25
26namespace GUI {
27
28class WindowBackingStore;
29
30class Window : public Core::Object {
31 C_OBJECT(Window)
32public:
33 virtual ~Window() override;
34
35 static Window* from_window_id(int);
36
37 bool is_modified() const;
38 void set_modified(bool);
39
40 bool is_modal() const { return m_window_mode != WindowMode::Modeless; }
41 bool is_blocking() const { return m_window_mode == WindowMode::Blocking; }
42
43 bool is_popup() const { return m_window_type == WindowType::Popup; }
44 bool is_autocomplete() const { return m_window_type == WindowType::Autocomplete; }
45
46 bool is_fullscreen() const { return m_fullscreen; }
47 void set_fullscreen(bool);
48
49 bool is_maximized() const { return m_maximized; }
50 void set_maximized(bool);
51
52 bool is_minimized() const { return m_minimized; }
53 void set_minimized(bool);
54
55 bool is_frameless() const { return m_frameless; }
56 void set_frameless(bool);
57
58 void set_forced_shadow(bool);
59
60 bool is_resizable() const { return m_resizable; }
61 void set_resizable(bool resizable) { m_resizable = resizable; }
62
63 bool is_obeying_widget_min_size() { return m_obey_widget_min_size; }
64 void set_obey_widget_min_size(bool);
65
66 bool is_minimizable() const { return m_minimizable; }
67 void set_minimizable(bool minimizable) { m_minimizable = minimizable; }
68
69 bool is_closeable() const { return m_closeable; }
70 void set_closeable(bool closeable) { m_closeable = closeable; }
71
72 void set_double_buffering_enabled(bool);
73 void set_has_alpha_channel(bool);
74 bool has_alpha_channel() const { return m_has_alpha_channel; }
75 void set_opacity(float);
76 float opacity() const { return m_opacity_when_windowless; }
77
78 void set_alpha_hit_threshold(float);
79 float alpha_hit_threshold() const { return m_alpha_hit_threshold; }
80
81 WindowType window_type() const { return m_window_type; }
82 void set_window_type(WindowType);
83
84 WindowMode window_mode() const { return m_window_mode; }
85 void set_window_mode(WindowMode);
86
87 int window_id() const { return m_window_id; }
88
89 void make_window_manager(unsigned event_mask);
90
91 DeprecatedString title() const;
92 void set_title(DeprecatedString);
93
94 enum class CloseRequestDecision {
95 StayOpen,
96 Close,
97 };
98
99 Function<void()> on_close;
100 Function<CloseRequestDecision()> on_close_request;
101 Function<void(bool is_preempted)> on_input_preemption_change;
102 Function<void(bool is_active_window)> on_active_window_change;
103
104 int x() const { return rect().x(); }
105 int y() const { return rect().y(); }
106 int width() const { return rect().width(); }
107 int height() const { return rect().height(); }
108
109 Gfx::IntRect rect() const;
110 Gfx::IntRect applet_rect_on_screen() const;
111 Gfx::IntSize size() const { return rect().size(); }
112 void set_rect(Gfx::IntRect const&);
113 void set_rect(int x, int y, int width, int height) { set_rect({ x, y, width, height }); }
114
115 Gfx::IntPoint position() const { return rect().location(); }
116
117 Gfx::IntSize minimum_size() const;
118 void set_minimum_size(Gfx::IntSize);
119 void set_minimum_size(int width, int height) { set_minimum_size({ width, height }); }
120
121 void move_to(int x, int y) { move_to({ x, y }); }
122 void move_to(Gfx::IntPoint point) { set_rect({ point, size() }); }
123
124 void resize(int width, int height) { resize({ width, height }); }
125 void resize(Gfx::IntSize size) { set_rect({ position(), size }); }
126
127 void center_on_screen();
128 void center_within(Window const&);
129
130 virtual void event(Core::Event&) override;
131
132 bool is_visible() const;
133 bool is_active() const;
134 bool is_focusable() const { return is_active() || is_popup() || is_autocomplete(); }
135
136 void show();
137 void hide();
138 virtual void close();
139 void move_to_front();
140
141 void start_interactive_resize(ResizeDirection resize_direction);
142
143 Widget* main_widget() { return m_main_widget; }
144 Widget const* main_widget() const { return m_main_widget; }
145 void set_main_widget(Widget*);
146
147 template<class T, class... Args>
148 inline ErrorOr<NonnullRefPtr<T>> set_main_widget(Args&&... args)
149 {
150 auto widget = TRY(T::try_create(forward<Args>(args)...));
151 set_main_widget(widget.ptr());
152 return widget;
153 }
154
155 Widget* default_return_key_widget() { return m_default_return_key_widget; }
156 Widget const* default_return_key_widget() const { return m_default_return_key_widget; }
157 void set_default_return_key_widget(Widget*);
158
159 Widget* focused_widget() { return m_focused_widget; }
160 Widget const* focused_widget() const { return m_focused_widget; }
161 void set_focused_widget(Widget*, FocusSource = FocusSource::Programmatic);
162
163 void update();
164 void update(Gfx::IntRect const&);
165
166 void set_automatic_cursor_tracking_widget(Widget*);
167 Widget* automatic_cursor_tracking_widget() { return m_automatic_cursor_tracking_widget.ptr(); }
168 Widget const* automatic_cursor_tracking_widget() const { return m_automatic_cursor_tracking_widget.ptr(); }
169
170 Widget* hovered_widget() { return m_hovered_widget.ptr(); }
171 Widget const* hovered_widget() const { return m_hovered_widget.ptr(); }
172 void set_hovered_widget(Widget*);
173
174 Gfx::Bitmap* back_bitmap();
175
176 Gfx::IntSize size_increment() const { return m_size_increment; }
177 void set_size_increment(Gfx::IntSize);
178 Gfx::IntSize base_size() const { return m_base_size; }
179 void set_base_size(Gfx::IntSize);
180 Optional<Gfx::IntSize> const& resize_aspect_ratio() const { return m_resize_aspect_ratio; }
181 void set_resize_aspect_ratio(int width, int height) { set_resize_aspect_ratio(Gfx::IntSize(width, height)); }
182 void set_no_resize_aspect_ratio() { set_resize_aspect_ratio({}); }
183 void set_resize_aspect_ratio(Optional<Gfx::IntSize> const& ratio);
184
185 void set_cursor(Gfx::StandardCursor);
186 void set_cursor(NonnullRefPtr<Gfx::Bitmap const>);
187
188 void set_icon(Gfx::Bitmap const*);
189 void apply_icon();
190 Gfx::Bitmap const* icon() const { return m_icon.ptr(); }
191
192 Vector<Widget&> focusable_widgets(FocusSource) const;
193
194 void schedule_relayout();
195
196 void refresh_system_theme();
197
198 static void for_each_window(Badge<ConnectionToWindowServer>, Function<void(Window&)>);
199 static void update_all_windows(Badge<ConnectionToWindowServer>);
200 void notify_state_changed(Badge<ConnectionToWindowServer>, bool minimized, bool maximized, bool occluded);
201
202 virtual bool is_visible_for_timer_purposes() const override { return m_visible_for_timer_purposes; }
203
204 Action* action_for_shortcut(Shortcut const&);
205
206 void did_add_widget(Badge<Widget>, Widget&);
207 void did_remove_widget(Badge<Widget>, Widget&);
208
209 Window* find_parent_window();
210
211 void set_progress(Optional<int>);
212
213 void update_cursor(Badge<Widget>) { update_cursor(); }
214
215 void did_disable_focused_widget(Badge<Widget>);
216
217 Menu& add_menu(DeprecatedString name);
218 ErrorOr<NonnullRefPtr<Menu>> try_add_menu(DeprecatedString name);
219 ErrorOr<void> try_add_menu(NonnullRefPtr<Menu> menu);
220 void flash_menubar_menu_for(MenuItem const&);
221
222 void flush_pending_paints_immediately();
223
224 Menubar& menubar() { return *m_menubar; }
225 Menubar const& menubar() const { return *m_menubar; }
226
227 void set_blocks_emoji_input(bool b) { m_blocks_emoji_input = b; }
228 bool blocks_emoji_input() const { return m_blocks_emoji_input; }
229
230 void set_always_on_top(bool always_on_top = true);
231
232 void propagate_shortcuts_up_to_application(KeyEvent& event, Widget* widget);
233
234protected:
235 Window(Core::Object* parent = nullptr);
236 virtual void wm_event(WMEvent&);
237 virtual void screen_rects_change_event(ScreenRectsChangeEvent&);
238 virtual void applet_area_rect_change_event(AppletAreaRectChangeEvent&);
239
240 virtual void enter_event(Core::Event&);
241 virtual void leave_event(Core::Event&);
242
243private:
244 void update_min_size();
245
246 void update_cursor();
247 void focus_a_widget_if_possible(FocusSource);
248
249 void handle_drop_event(DropEvent&);
250 void handle_mouse_event(MouseEvent&);
251 void handle_multi_paint_event(MultiPaintEvent&);
252 void handle_key_event(KeyEvent&);
253 void handle_resize_event(ResizeEvent&);
254 void handle_input_preemption_event(Core::Event&);
255 void handle_became_active_or_inactive_event(Core::Event&);
256 void handle_close_request();
257 void handle_theme_change_event(ThemeChangeEvent&);
258 void handle_fonts_change_event(FontsChangeEvent&);
259 void handle_screen_rects_change_event(ScreenRectsChangeEvent&);
260 void handle_applet_area_rect_change_event(AppletAreaRectChangeEvent&);
261 void handle_drag_move_event(DragEvent&);
262 void handle_entered_event(Core::Event&);
263 void handle_left_event(Core::Event&);
264
265 void server_did_destroy();
266
267 ErrorOr<NonnullOwnPtr<WindowBackingStore>> create_backing_store(Gfx::IntSize);
268 Gfx::IntSize backing_store_size(Gfx::IntSize) const;
269 void set_current_backing_store(WindowBackingStore&, bool flush_immediately = false) const;
270 void flip(Vector<Gfx::IntRect, 32> const& dirty_rects);
271 void force_update();
272
273 bool are_cursors_the_same(AK::Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap const>> const&, AK::Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap const>> const&) const;
274
275 WeakPtr<Widget> m_previously_focused_widget;
276
277 OwnPtr<WindowBackingStore> m_front_store;
278 OwnPtr<WindowBackingStore> m_back_store;
279
280 NonnullRefPtr<Menubar> m_menubar;
281
282 RefPtr<Gfx::Bitmap const> m_icon;
283 int m_window_id { 0 };
284 float m_opacity_when_windowless { 1.0f };
285 float m_alpha_hit_threshold { 0.0f };
286 RefPtr<Widget> m_main_widget;
287 WeakPtr<Widget> m_default_return_key_widget;
288 WeakPtr<Widget> m_focused_widget;
289 WeakPtr<Widget> m_automatic_cursor_tracking_widget;
290 WeakPtr<Widget> m_hovered_widget;
291 Gfx::IntRect m_rect_when_windowless;
292 Gfx::IntSize m_minimum_size_when_windowless { 0, 0 };
293 DeprecatedString m_title_when_windowless;
294 Vector<Gfx::IntRect, 32> m_pending_paint_event_rects;
295 Gfx::IntSize m_size_increment;
296 Gfx::IntSize m_base_size;
297 WindowType m_window_type { WindowType::Normal };
298 WindowMode m_window_mode { WindowMode::Modeless };
299 AK::Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap const>> m_cursor { Gfx::StandardCursor::None };
300 AK::Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap const>> m_effective_cursor { Gfx::StandardCursor::None };
301 bool m_has_alpha_channel { false };
302 bool m_double_buffering_enabled { true };
303 bool m_resizable { true };
304 bool m_obey_widget_min_size { true };
305 Optional<Gfx::IntSize> m_resize_aspect_ratio {};
306 bool m_minimizable { true };
307 bool m_closeable { true };
308 bool m_maximized { false };
309 bool m_minimized { false };
310 bool m_fullscreen { false };
311 bool m_frameless { false };
312 bool m_forced_shadow { false };
313 bool m_layout_pending { false };
314 bool m_visible_for_timer_purposes { true };
315 bool m_visible { false };
316 bool m_moved_by_client { false };
317 bool m_blocks_emoji_input { false };
318 bool m_resizing { false };
319};
320
321}
322
323template<>
324struct AK::Formatter<GUI::Window> : Formatter<Core::Object> {
325};