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