Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include <AK/Function.h>
30#include <AK/String.h>
31#include <AK/WeakPtr.h>
32#include <LibCore/Object.h>
33#include <LibGUI/Forward.h>
34#include <LibGUI/WindowType.h>
35#include <LibGfx/Color.h>
36#include <LibGfx/Forward.h>
37#include <LibGfx/Rect.h>
38
39namespace GUI {
40
41enum class StandardCursor {
42 None = 0,
43 Arrow,
44 IBeam,
45 ResizeHorizontal,
46 ResizeVertical,
47 ResizeDiagonalTLBR,
48 ResizeDiagonalBLTR,
49 Hand,
50};
51
52class Window : public Core::Object {
53 C_OBJECT(Window)
54public:
55 virtual ~Window() override;
56
57 static Window* from_window_id(int);
58
59 bool is_modal() const { return m_modal; }
60 void set_modal(bool);
61
62 bool is_fullscreen() const { return m_fullscreen; }
63 void set_fullscreen(bool);
64
65 bool is_resizable() const { return m_resizable; }
66 void set_resizable(bool resizable) { m_resizable = resizable; }
67
68 bool is_minimizable() const { return m_minimizable; }
69 void set_minimizable(bool minimizable) { m_minimizable = minimizable; }
70
71 void set_double_buffering_enabled(bool);
72 void set_has_alpha_channel(bool);
73 void set_opacity(float);
74 void set_window_type(WindowType);
75
76 int window_id() const { return m_window_id; }
77
78 String title() const;
79 void set_title(const StringView&);
80
81 bool show_titlebar() const { return m_show_titlebar; };
82 void set_show_titlebar(bool show) { m_show_titlebar = show; };
83
84 Color background_color() const { return m_background_color; }
85 void set_background_color(Color color) { m_background_color = color; }
86
87 enum class CloseRequestDecision {
88 StayOpen,
89 Close,
90 };
91
92 Function<CloseRequestDecision()> on_close_request;
93
94 int x() const { return rect().x(); }
95 int y() const { return rect().y(); }
96 int width() const { return rect().width(); }
97 int height() const { return rect().height(); }
98
99 Gfx::Rect rect() const;
100 Gfx::Size size() const { return rect().size(); }
101 void set_rect(const Gfx::Rect&);
102 void set_rect(int x, int y, int width, int height) { set_rect({ x, y, width, height }); }
103
104 Gfx::Point position() const { return rect().location(); }
105
106 void move_to(int x, int y) { move_to({ x, y }); }
107 void move_to(const Gfx::Point& point) { set_rect({ point, size() }); }
108
109 void resize(int width, int height) { resize({ width, height }); }
110 void resize(const Gfx::Size& size) { set_rect({ position(), size }); }
111
112 virtual void event(Core::Event&) override;
113
114 bool is_visible() const;
115 bool is_active() const { return m_is_active; }
116
117 void show();
118 void hide();
119 virtual void close();
120 void move_to_front();
121
122 void start_wm_resize();
123
124 Widget* main_widget() { return m_main_widget; }
125 const Widget* main_widget() const { return m_main_widget; }
126 void set_main_widget(Widget*);
127
128 template<class T, class... Args>
129 inline T& set_main_widget(Args&&... args)
130 {
131 auto widget = T::construct(forward<Args>(args)...);
132 set_main_widget(widget.ptr());
133 return *widget;
134 }
135
136 Widget* focused_widget() { return m_focused_widget; }
137 const Widget* focused_widget() const { return m_focused_widget; }
138 void set_focused_widget(Widget*);
139
140 void update();
141 void update(const Gfx::Rect&);
142
143 void set_global_cursor_tracking_widget(Widget*);
144 Widget* global_cursor_tracking_widget() { return m_global_cursor_tracking_widget.ptr(); }
145 const Widget* global_cursor_tracking_widget() const { return m_global_cursor_tracking_widget.ptr(); }
146
147 void set_automatic_cursor_tracking_widget(Widget*);
148 Widget* automatic_cursor_tracking_widget() { return m_automatic_cursor_tracking_widget.ptr(); }
149 const Widget* automatic_cursor_tracking_widget() const { return m_automatic_cursor_tracking_widget.ptr(); }
150
151 Widget* hovered_widget() { return m_hovered_widget.ptr(); }
152 const Widget* hovered_widget() const { return m_hovered_widget.ptr(); }
153 void set_hovered_widget(Widget*);
154
155 Gfx::Bitmap* front_bitmap() { return m_front_bitmap.ptr(); }
156 Gfx::Bitmap* back_bitmap() { return m_back_bitmap.ptr(); }
157
158 Gfx::Size size_increment() const { return m_size_increment; }
159 void set_size_increment(const Gfx::Size&);
160 Gfx::Size base_size() const { return m_base_size; }
161 void set_base_size(const Gfx::Size&);
162
163 void set_override_cursor(StandardCursor);
164
165 void set_icon(const Gfx::Bitmap*);
166 void apply_icon();
167 const Gfx::Bitmap* icon() const { return m_icon.ptr(); }
168
169 Vector<Widget*> focusable_widgets() const;
170
171 virtual void save_to(AK::JsonObject&) override;
172
173 void schedule_relayout();
174
175 static void for_each_window(Badge<WindowServerConnection>, Function<void(Window&)>);
176 static void update_all_windows(Badge<WindowServerConnection>);
177 void notify_state_changed(Badge<WindowServerConnection>, bool minimized, bool occluded);
178
179 virtual bool is_visible_for_timer_purposes() const override { return m_visible_for_timer_purposes; }
180
181 Action* action_for_key_event(const KeyEvent&);
182
183 void did_remove_widget(Badge<Widget>, const Widget&);
184
185protected:
186 Window(Core::Object* parent = nullptr);
187 virtual void wm_event(WMEvent&);
188
189private:
190 virtual bool is_window() const override final { return true; }
191
192 NonnullRefPtr<Gfx::Bitmap> create_backing_bitmap(const Gfx::Size&);
193 NonnullRefPtr<Gfx::Bitmap> create_shared_bitmap(Gfx::BitmapFormat, const Gfx::Size&);
194 void set_current_backing_bitmap(Gfx::Bitmap&, bool flush_immediately = false);
195 void flip(const Vector<Gfx::Rect, 32>& dirty_rects);
196 void force_update();
197
198 RefPtr<Gfx::Bitmap> m_front_bitmap;
199 RefPtr<Gfx::Bitmap> m_back_bitmap;
200 RefPtr<Gfx::Bitmap> m_icon;
201 int m_window_id { 0 };
202 float m_opacity_when_windowless { 1.0f };
203 RefPtr<Widget> m_main_widget;
204 WeakPtr<Widget> m_focused_widget;
205 WeakPtr<Widget> m_global_cursor_tracking_widget;
206 WeakPtr<Widget> m_automatic_cursor_tracking_widget;
207 WeakPtr<Widget> m_hovered_widget;
208 Gfx::Rect m_rect_when_windowless;
209 String m_title_when_windowless;
210 Vector<Gfx::Rect, 32> m_pending_paint_event_rects;
211 Gfx::Size m_size_increment;
212 Gfx::Size m_base_size;
213 Color m_background_color { Color::WarmGray };
214 WindowType m_window_type { WindowType::Normal };
215 StandardCursor m_override_cursor { StandardCursor::None };
216 bool m_is_active { false };
217 bool m_has_alpha_channel { false };
218 bool m_double_buffering_enabled { true };
219 bool m_modal { false };
220 bool m_resizable { true };
221 bool m_minimizable { true };
222 bool m_fullscreen { false };
223 bool m_show_titlebar { true };
224 bool m_layout_pending { false };
225 bool m_visible_for_timer_purposes { true };
226 bool m_visible { false };
227};
228
229}
230
231template<>
232inline bool Core::is<GUI::Window>(const Core::Object& object)
233{
234 return object.is_window();
235}