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/String.h>
30#include <LibCore/Object.h>
31#include <LibGUI/Event.h>
32#include <LibGUI/Forward.h>
33#include <LibGfx/Color.h>
34#include <LibGfx/Forward.h>
35#include <LibGfx/Orientation.h>
36#include <LibGfx/Rect.h>
37
38#define REGISTER_GWIDGET(class_name) \
39 extern WidgetClassRegistration registration_##class_name; \
40 WidgetClassRegistration registration_##class_name(#class_name, []() { return class_name::construct(); });
41
42template<>
43inline bool Core::is<GUI::Widget>(const Core::Object& object)
44{
45 return object.is_widget();
46}
47
48namespace GUI {
49
50enum class SizePolicy {
51 Fixed,
52 Fill
53};
54inline const char* to_string(SizePolicy policy)
55{
56 switch (policy) {
57 case SizePolicy::Fixed:
58 return "SizePolicy::Fixed";
59 case SizePolicy::Fill:
60 return "SizePolicy::Fill";
61 }
62 return "SizePolicy::(Invalid)";
63}
64
65enum class HorizontalDirection {
66 Left,
67 Right
68};
69enum class VerticalDirection {
70 Up,
71 Down
72};
73
74class WidgetClassRegistration {
75 AK_MAKE_NONCOPYABLE(WidgetClassRegistration)
76 AK_MAKE_NONMOVABLE(WidgetClassRegistration)
77public:
78 WidgetClassRegistration(const String& class_name, Function<NonnullRefPtr<Widget>()> factory);
79 ~WidgetClassRegistration();
80
81 String class_name() const { return m_class_name; }
82 NonnullRefPtr<Widget> construct() const { return m_factory(); }
83
84 static void for_each(Function<void(const WidgetClassRegistration&)>);
85 static const WidgetClassRegistration* find(const String& class_name);
86
87private:
88 String m_class_name;
89 Function<NonnullRefPtr<Widget>()> m_factory;
90};
91
92class Widget : public Core::Object {
93 C_OBJECT(Widget)
94public:
95 virtual ~Widget() override;
96
97 Layout* layout() { return m_layout.ptr(); }
98 const Layout* layout() const { return m_layout.ptr(); }
99 void set_layout(OwnPtr<Layout>&&);
100
101 SizePolicy horizontal_size_policy() const { return m_horizontal_size_policy; }
102 SizePolicy vertical_size_policy() const { return m_vertical_size_policy; }
103 SizePolicy size_policy(Orientation orientation) { return orientation == Orientation::Horizontal ? m_horizontal_size_policy : m_vertical_size_policy; }
104 void set_size_policy(SizePolicy horizontal_policy, SizePolicy vertical_policy);
105 void set_size_policy(Orientation, SizePolicy);
106
107 Gfx::Size preferred_size() const { return m_preferred_size; }
108 void set_preferred_size(const Gfx::Size&);
109 void set_preferred_size(int width, int height) { set_preferred_size({ width, height }); }
110
111 bool has_tooltip() const { return !m_tooltip.is_empty(); }
112 String tooltip() const { return m_tooltip; }
113 void set_tooltip(const StringView& tooltip) { m_tooltip = tooltip; }
114
115 bool is_enabled() const { return m_enabled; }
116 void set_enabled(bool);
117
118 bool updates_enabled() const { return m_updates_enabled; }
119 void set_updates_enabled(bool);
120
121 virtual void event(Core::Event&) override;
122
123 // This is called after children have been painted.
124 virtual void second_paint_event(PaintEvent&);
125
126 Gfx::Rect relative_rect() const { return m_relative_rect; }
127 Gfx::Point relative_position() const { return m_relative_rect.location(); }
128
129 Gfx::Rect window_relative_rect() const;
130 Gfx::Rect screen_relative_rect() const;
131
132 int x() const { return m_relative_rect.x(); }
133 int y() const { return m_relative_rect.y(); }
134 int width() const { return m_relative_rect.width(); }
135 int height() const { return m_relative_rect.height(); }
136 int length(Orientation orientation) const { return orientation == Orientation::Vertical ? height() : width(); }
137
138 Gfx::Rect rect() const { return { 0, 0, width(), height() }; }
139 Gfx::Size size() const { return m_relative_rect.size(); }
140
141 void update();
142 void update(const Gfx::Rect&);
143
144 virtual bool accepts_focus() const { return false; }
145
146 bool is_focused() const;
147 void set_focus(bool);
148
149 enum class ShouldRespectGreediness { No = 0,
150 Yes };
151 struct HitTestResult {
152 Widget* widget { nullptr };
153 Gfx::Point local_position;
154 };
155 HitTestResult hit_test(const Gfx::Point&, ShouldRespectGreediness = ShouldRespectGreediness::Yes);
156 Widget* child_at(const Gfx::Point&) const;
157
158 void set_relative_rect(const Gfx::Rect&);
159 void set_relative_rect(int x, int y, int width, int height) { set_relative_rect({ x, y, width, height }); }
160
161 void set_x(int x) { set_relative_rect(x, y(), width(), height()); }
162 void set_y(int y) { set_relative_rect(x(), y, width(), height()); }
163 void set_width(int width) { set_relative_rect(x(), y(), width, height()); }
164 void set_height(int height) { set_relative_rect(x(), y(), width(), height); }
165
166 void move_to(const Gfx::Point& point) { set_relative_rect({ point, relative_rect().size() }); }
167 void move_to(int x, int y) { move_to({ x, y }); }
168 void resize(const Gfx::Size& size) { set_relative_rect({ relative_rect().location(), size }); }
169 void resize(int width, int height) { resize({ width, height }); }
170
171 void move_by(int x, int y) { move_by({ x, y }); }
172 void move_by(const Gfx::Point& delta) { set_relative_rect({ relative_position().translated(delta), size() }); }
173
174 Gfx::ColorRole background_role() const { return m_background_role; }
175 void set_background_role(Gfx::ColorRole);
176
177 Gfx::ColorRole foreground_role() const { return m_foreground_role; }
178 void set_foreground_role(Gfx::ColorRole);
179
180 Color background_color() const { return m_background_color; }
181 Color foreground_color() const { return m_foreground_color; }
182
183 void set_background_color(Color color) { m_background_color = color; }
184 void set_foreground_color(Color color) { m_foreground_color = color; }
185
186 void set_backcolor(const StringView&);
187 void set_forecolor(const StringView&);
188
189 void set_autofill(bool b) { set_fill_with_background_color(b); }
190
191 Window* window()
192 {
193 if (auto* pw = parent_widget())
194 return pw->window();
195 return m_window;
196 }
197
198 const Window* window() const
199 {
200 if (auto* pw = parent_widget())
201 return pw->window();
202 return m_window;
203 }
204
205 void set_window(Window*);
206
207 Widget* parent_widget();
208 const Widget* parent_widget() const;
209
210 void set_fill_with_background_color(bool b) { m_fill_with_background_color = b; }
211 bool fill_with_background_color() const { return m_fill_with_background_color; }
212
213 const Gfx::Font& font() const { return *m_font; }
214 void set_font(const Gfx::Font*);
215 void set_font(const Gfx::Font& font) { set_font(&font); }
216
217 void set_global_cursor_tracking(bool);
218 bool global_cursor_tracking() const;
219
220 void notify_layout_changed(Badge<Layout>);
221 void invalidate_layout();
222
223 bool is_visible() const { return m_visible; }
224 void set_visible(bool);
225
226 bool spans_entire_window_horizontally() const;
227
228 bool is_greedy_for_hits() const { return m_greedy_for_hits; }
229 void set_greedy_for_hits(bool b) { m_greedy_for_hits = b; }
230
231 void move_to_front();
232 void move_to_back();
233
234 bool is_frontmost() const;
235 bool is_backmost() const;
236
237 Action* action_for_key_event(const KeyEvent&);
238
239 template<typename Callback>
240 void for_each_child_widget(Callback callback)
241 {
242 for_each_child([&](auto& child) {
243 if (Core::is<Widget>(child))
244 return callback(Core::to<Widget>(child));
245 return IterationDecision::Continue;
246 });
247 }
248
249 Vector<Widget*> child_widgets() const;
250
251 virtual bool is_radio_button() const { return false; }
252 virtual bool is_abstract_button() const { return false; }
253
254 virtual void save_to(AK::JsonObject&) override;
255
256 void do_layout();
257
258 Gfx::Palette palette() const;
259 void set_palette(const Gfx::Palette&);
260
261protected:
262 Widget();
263
264 virtual void custom_layout() {}
265 virtual void did_change_font() {}
266 virtual void did_layout() {}
267 virtual void paint_event(PaintEvent&);
268 virtual void resize_event(ResizeEvent&);
269 virtual void show_event(ShowEvent&);
270 virtual void hide_event(HideEvent&);
271 virtual void keydown_event(KeyEvent&);
272 virtual void keyup_event(KeyEvent&);
273 virtual void mousemove_event(MouseEvent&);
274 virtual void mousedown_event(MouseEvent&);
275 virtual void mouseup_event(MouseEvent&);
276 virtual void mousewheel_event(MouseEvent&);
277 virtual void click_event(MouseEvent&);
278 virtual void doubleclick_event(MouseEvent&);
279 virtual void context_menu_event(ContextMenuEvent&);
280 virtual void focusin_event(Core::Event&);
281 virtual void focusout_event(Core::Event&);
282 virtual void enter_event(Core::Event&);
283 virtual void leave_event(Core::Event&);
284 virtual void child_event(Core::ChildEvent&) override;
285 virtual void change_event(Event&);
286 virtual void drag_move_event(DragEvent&);
287 virtual void drop_event(DropEvent&);
288
289private:
290 void handle_paint_event(PaintEvent&);
291 void handle_resize_event(ResizeEvent&);
292 void handle_mousedown_event(MouseEvent&);
293 void handle_mousedoubleclick_event(MouseEvent&);
294 void handle_mouseup_event(MouseEvent&);
295 void handle_enter_event(Core::Event&);
296 void handle_leave_event(Core::Event&);
297 void focus_previous_widget();
298 void focus_next_widget();
299
300 Window* m_window { nullptr };
301 OwnPtr<Layout> m_layout;
302
303 Gfx::Rect m_relative_rect;
304 Gfx::ColorRole m_background_role;
305 Gfx::ColorRole m_foreground_role;
306 Color m_background_color;
307 Color m_foreground_color;
308 NonnullRefPtr<Gfx::Font> m_font;
309 String m_tooltip;
310
311 SizePolicy m_horizontal_size_policy { SizePolicy::Fill };
312 SizePolicy m_vertical_size_policy { SizePolicy::Fill };
313 Gfx::Size m_preferred_size;
314
315 bool m_fill_with_background_color { false };
316 bool m_visible { true };
317 bool m_greedy_for_hits { false };
318 bool m_enabled { true };
319 bool m_updates_enabled { true };
320
321 NonnullRefPtr<Gfx::PaletteImpl> m_palette;
322};
323
324inline Widget* Widget::parent_widget()
325{
326 if (parent() && Core::is<Widget>(*parent()))
327 return &Core::to<Widget>(*parent());
328 return nullptr;
329}
330inline const Widget* Widget::parent_widget() const
331{
332 if (parent() && Core::is<Widget>(*parent()))
333 return &Core::to<const Widget>(*parent());
334 return nullptr;
335}
336}