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#include <AK/Badge.h>
28#include <LibGUI/Layout.h>
29#include <LibGUI/Widget.h>
30
31namespace GUI {
32
33Layout::Layout()
34{
35}
36
37Layout::~Layout()
38{
39}
40
41void Layout::notify_adopted(Badge<Widget>, Widget& widget)
42{
43 if (m_owner == &widget)
44 return;
45 m_owner = widget.make_weak_ptr();
46}
47
48void Layout::notify_disowned(Badge<Widget>, Widget& widget)
49{
50 ASSERT(m_owner == &widget);
51 m_owner.clear();
52}
53
54void Layout::add_entry(Entry&& entry)
55{
56 m_entries.append(move(entry));
57 if (m_owner)
58 m_owner->notify_layout_changed({});
59}
60
61void Layout::add_spacer()
62{
63 Entry entry;
64 entry.type = Entry::Type::Spacer;
65 add_entry(move(entry));
66}
67
68void Layout::add_layout(OwnPtr<Layout>&& layout)
69{
70 Entry entry;
71 entry.type = Entry::Type::Layout;
72 entry.layout = move(layout);
73 add_entry(move(entry));
74}
75
76void Layout::add_widget(Widget& widget)
77{
78 Entry entry;
79 entry.type = Entry::Type::Widget;
80 entry.widget = widget.make_weak_ptr();
81 add_entry(move(entry));
82}
83
84void Layout::insert_widget_before(Widget& widget, Widget& before_widget)
85{
86 Entry entry;
87 entry.type = Entry::Type::Widget;
88 entry.widget = widget.make_weak_ptr();
89 m_entries.insert_before_matching(move(entry), [&](auto& existing_entry) {
90 return existing_entry.type == Entry::Type::Widget && existing_entry.widget.ptr() == &before_widget;
91 });
92 if (m_owner)
93 m_owner->notify_layout_changed({});
94}
95
96void Layout::remove_widget(Widget& widget)
97{
98 m_entries.remove_first_matching([&](auto& entry) {
99 return entry.widget == &widget;
100 });
101 if (m_owner)
102 m_owner->notify_layout_changed({});
103}
104
105void Layout::set_spacing(int spacing)
106{
107 if (m_spacing == spacing)
108 return;
109 m_spacing = spacing;
110 if (m_owner)
111 m_owner->notify_layout_changed({});
112}
113
114void Layout::set_margins(const Margins& margins)
115{
116 if (m_margins == margins)
117 return;
118 m_margins = margins;
119 if (m_owner)
120 m_owner->notify_layout_changed({});
121}
122
123}