xref: /haiku/src/kits/interface/SpaceLayoutItem.cpp (revision 68ea01249e1e2088933cb12f9c28d4e5c5d1c9ef)
1 /*
2  * Copyright 2010, Haiku, Inc.
3  * Copyright 2006, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
4  * All rights reserved. Distributed under the terms of the MIT License.
5  */
6 
7 
8 #include <SpaceLayoutItem.h>
9 
10 #include <new>
11 
12 #include <ControlLook.h>
13 #include <Message.h>
14 
15 
16 namespace {
17 	const char* const kSizesField = "BSpaceLayoutItem:sizes";
18 		// kSizesField = {min, max, preferred}
19 	const char* const kAlignmentField = "BSpaceLayoutItem:alignment";
20 	const char* const kFrameField = "BSpaceLayoutItem:frame";
21 	const char* const kVisibleField = "BSpaceLayoutItem:visible";
22 
23 	BSize& ComposeSpacingInPlace(BSize& size)
24 	{
25 		size.width = BControlLook::ComposeSpacing(size.width);
26 		size.height = BControlLook::ComposeSpacing(size.height);
27 		return size;
28 	}
29 }
30 
31 
32 BSpaceLayoutItem::BSpaceLayoutItem(BSize minSize, BSize maxSize,
33 	BSize preferredSize, BAlignment alignment)
34 	:
35 	fFrame(),
36 	fMinSize(ComposeSpacingInPlace(minSize)),
37 	fMaxSize(ComposeSpacingInPlace(maxSize)),
38 	fPreferredSize(ComposeSpacingInPlace(preferredSize)),
39 	fAlignment(alignment),
40 	fVisible(true)
41 {
42 }
43 
44 
45 BSpaceLayoutItem::BSpaceLayoutItem(BMessage* archive)
46 	:
47 	BLayoutItem(archive)
48 {
49 	archive->FindSize(kSizesField, 0, &fMinSize);
50 	archive->FindSize(kSizesField, 1, &fMaxSize);
51 	archive->FindSize(kSizesField, 2, &fPreferredSize);
52 
53 	archive->FindAlignment(kAlignmentField, &fAlignment);
54 
55 	archive->FindRect(kFrameField, &fFrame);
56 	archive->FindBool(kVisibleField, &fVisible);
57 }
58 
59 
60 BSpaceLayoutItem::~BSpaceLayoutItem()
61 {
62 }
63 
64 
65 BSpaceLayoutItem*
66 BSpaceLayoutItem::CreateGlue()
67 {
68 	return new BSpaceLayoutItem(
69 		BSize(-1, -1),
70 		BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED),
71 		BSize(-1, -1),
72 		BAlignment(B_ALIGN_HORIZONTAL_CENTER, B_ALIGN_VERTICAL_CENTER));
73 }
74 
75 
76 BSpaceLayoutItem*
77 BSpaceLayoutItem::CreateHorizontalStrut(float width)
78 {
79 	return new BSpaceLayoutItem(
80 		BSize(width, -1),
81 		BSize(width, B_SIZE_UNLIMITED),
82 		BSize(width, -1),
83 		BAlignment(B_ALIGN_HORIZONTAL_CENTER, B_ALIGN_VERTICAL_CENTER));
84 }
85 
86 
87 BSpaceLayoutItem*
88 BSpaceLayoutItem::CreateVerticalStrut(float height)
89 {
90 	return new BSpaceLayoutItem(
91 		BSize(-1, height),
92 		BSize(B_SIZE_UNLIMITED, height),
93 		BSize(-1, height),
94 		BAlignment(B_ALIGN_HORIZONTAL_CENTER, B_ALIGN_VERTICAL_CENTER));
95 }
96 
97 
98 BSize
99 BSpaceLayoutItem::MinSize()
100 {
101 	return fMinSize;
102 }
103 
104 
105 BSize
106 BSpaceLayoutItem::MaxSize()
107 {
108 	return fMaxSize;
109 }
110 
111 
112 BSize
113 BSpaceLayoutItem::PreferredSize()
114 {
115 	return fPreferredSize;
116 }
117 
118 
119 BAlignment
120 BSpaceLayoutItem::Alignment()
121 {
122 	return fAlignment;
123 }
124 
125 
126 void
127 BSpaceLayoutItem::SetExplicitMinSize(BSize size)
128 {
129 	if (size.IsWidthSet())
130 		fMinSize.width = size.width;
131 	if (size.IsHeightSet())
132 		fMinSize.height = size.height;
133 
134 	InvalidateLayout();
135 }
136 
137 
138 void
139 BSpaceLayoutItem::SetExplicitMaxSize(BSize size)
140 {
141 	if (size.IsWidthSet())
142 		fMaxSize.width = size.width;
143 	if (size.IsHeightSet())
144 		fMaxSize.height = size.height;
145 
146 	InvalidateLayout();
147 }
148 
149 
150 void
151 BSpaceLayoutItem::SetExplicitPreferredSize(BSize size)
152 {
153 	if (size.IsWidthSet())
154 		fPreferredSize.width = size.width;
155 	if (size.IsHeightSet())
156 		fPreferredSize.height = size.height;
157 
158 	InvalidateLayout();
159 }
160 
161 
162 void
163 BSpaceLayoutItem::SetExplicitAlignment(BAlignment alignment)
164 {
165 	if (alignment.IsHorizontalSet())
166 		fAlignment.horizontal = alignment.horizontal;
167 	if (alignment.IsVerticalSet())
168 		fAlignment.vertical = alignment.vertical;
169 
170 	InvalidateLayout();
171 }
172 
173 
174 bool
175 BSpaceLayoutItem::IsVisible()
176 {
177 	return fVisible;
178 }
179 
180 
181 void
182 BSpaceLayoutItem::SetVisible(bool visible)
183 {
184 	fVisible = visible;
185 }
186 
187 
188 BRect
189 BSpaceLayoutItem::Frame()
190 {
191 	return fFrame;
192 }
193 
194 
195 void
196 BSpaceLayoutItem::SetFrame(BRect frame)
197 {
198 	fFrame = frame;
199 }
200 
201 
202 status_t
203 BSpaceLayoutItem::Archive(BMessage* into, bool deep) const
204 {
205 	status_t err = BLayoutItem::Archive(into, deep);
206 
207 	if (err == B_OK)
208 		err = into->AddRect(kFrameField, fFrame);
209 
210 	if (err == B_OK)
211 		err = into->AddSize(kSizesField, fMinSize);
212 
213 	if (err == B_OK)
214 		err = into->AddSize(kSizesField, fMaxSize);
215 
216 	if (err == B_OK)
217 		err = into->AddSize(kSizesField, fPreferredSize);
218 
219 	if (err == B_OK)
220 		err = into->AddAlignment(kAlignmentField, fAlignment);
221 
222 	if (err == B_OK)
223 		err = into->AddBool(kVisibleField, fVisible);
224 
225 	return err;
226 }
227 
228 
229 BArchivable*
230 BSpaceLayoutItem::Instantiate(BMessage* from)
231 {
232 	if (validate_instantiation(from, "BSpaceLayoutItem"))
233 		return new(std::nothrow) BSpaceLayoutItem(from);
234 	return NULL;
235 }
236 
237 
238 void BSpaceLayoutItem::_ReservedSpaceLayoutItem1() {}
239 void BSpaceLayoutItem::_ReservedSpaceLayoutItem2() {}
240 void BSpaceLayoutItem::_ReservedSpaceLayoutItem3() {}
241 void BSpaceLayoutItem::_ReservedSpaceLayoutItem4() {}
242 void BSpaceLayoutItem::_ReservedSpaceLayoutItem5() {}
243 void BSpaceLayoutItem::_ReservedSpaceLayoutItem6() {}
244 void BSpaceLayoutItem::_ReservedSpaceLayoutItem7() {}
245 void BSpaceLayoutItem::_ReservedSpaceLayoutItem8() {}
246 void BSpaceLayoutItem::_ReservedSpaceLayoutItem9() {}
247 void BSpaceLayoutItem::_ReservedSpaceLayoutItem10() {}
248 
249