Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/OwnPtr.h>
10#include <AK/Vector.h>
11#include <AK/WeakPtr.h>
12#include <LibCore/Object.h>
13#include <LibGUI/Forward.h>
14#include <LibGUI/Margins.h>
15#include <LibGUI/UIDimensions.h>
16#include <LibGfx/Forward.h>
17
18namespace Core {
19namespace Registration {
20extern Core::ObjectClassRegistration registration_Layout;
21}
22}
23
24#define REGISTER_LAYOUT(namespace_, class_name) \
25 namespace Core { \
26 namespace Registration { \
27 Core::ObjectClassRegistration registration_##class_name( \
28 #namespace_ "::" #class_name##sv, []() { return static_ptr_cast<Core::Object>(namespace_::class_name::construct()); }, ®istration_Layout); \
29 } \
30 }
31
32namespace GUI {
33
34class Layout : public Core::Object {
35 C_OBJECT_ABSTRACT(Layout);
36
37public:
38 virtual ~Layout();
39
40 void add_widget(Widget&);
41 void insert_widget_before(Widget& widget, Widget& before_widget);
42 void add_layout(OwnPtr<Layout>&&);
43 void add_spacer();
44
45 ErrorOr<void> try_add_widget(Widget&);
46 ErrorOr<void> try_insert_widget_before(Widget& widget, Widget& before_widget);
47 ErrorOr<void> try_add_spacer();
48
49 void remove_widget(Widget&);
50
51 virtual void run(Widget&) = 0;
52 virtual UISize preferred_size() const = 0;
53 virtual UISize min_size() const = 0;
54
55 void notify_adopted(Badge<Widget>, Widget&);
56 void notify_disowned(Badge<Widget>, Widget&);
57
58 Margins const& margins() const { return m_margins; }
59 void set_margins(Margins const&);
60
61 static constexpr int default_spacing = 3;
62 int spacing() const { return m_spacing; }
63 void set_spacing(int);
64
65protected:
66 Layout(Margins, int spacing);
67
68 struct Entry {
69 enum class Type {
70 Invalid = 0,
71 Widget,
72 Layout,
73 Spacer,
74 };
75
76 Type type { Type::Invalid };
77 WeakPtr<Widget> widget {};
78 OwnPtr<Layout> layout {};
79 };
80 void add_entry(Entry&&);
81 ErrorOr<void> try_add_entry(Entry&&);
82
83 WeakPtr<Widget> m_owner;
84 Vector<Entry> m_entries;
85
86 Margins m_margins;
87 int m_spacing { default_spacing };
88};
89
90}