btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / kits / interface / CardLayout.cpp
blob28e79b51091998527bbe9e5498e86c76c0251be4
1 /*
2 * Copyright 2006-2009, Ingo Weinhold <ingo_weinhold@gmx.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
6 #include <CardLayout.h>
8 #include <LayoutItem.h>
9 #include <Message.h>
10 #include <View.h>
13 namespace {
14 const char* kVisibleItemField = "BCardLayout:visibleItem";
18 BCardLayout::BCardLayout()
20 BAbstractLayout(),
21 fMin(0, 0),
22 fMax(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED),
23 fPreferred(0, 0),
24 fVisibleItem(NULL),
25 fMinMaxValid(false)
30 BCardLayout::BCardLayout(BMessage* from)
32 BAbstractLayout(BUnarchiver::PrepareArchive(from)),
33 fMin(0, 0),
34 fMax(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED),
35 fPreferred(0, 0),
36 fVisibleItem(NULL),
37 fMinMaxValid(false)
39 BUnarchiver(from).Finish();
43 BCardLayout::~BCardLayout()
48 BLayoutItem*
49 BCardLayout::VisibleItem() const
51 return fVisibleItem;
55 int32
56 BCardLayout::VisibleIndex() const
58 return IndexOfItem(fVisibleItem);
62 void
63 BCardLayout::SetVisibleItem(int32 index)
65 SetVisibleItem(ItemAt(index));
69 void
70 BCardLayout::SetVisibleItem(BLayoutItem* item)
72 if (item == fVisibleItem)
73 return;
75 if (item != NULL && IndexOfItem(item) < 0)
76 return;
78 if (fVisibleItem != NULL)
79 fVisibleItem->SetVisible(false);
81 fVisibleItem = item;
83 if (fVisibleItem != NULL) {
84 fVisibleItem->SetVisible(true);
86 Relayout();
91 BSize
92 BCardLayout::BaseMinSize()
94 _ValidateMinMax();
95 return fMin;
99 BSize
100 BCardLayout::BaseMaxSize()
102 _ValidateMinMax();
103 return fMax;
107 BSize
108 BCardLayout::BasePreferredSize()
110 _ValidateMinMax();
111 return fPreferred;
115 BAlignment
116 BCardLayout::BaseAlignment()
118 return BAlignment(B_ALIGN_USE_FULL_WIDTH, B_ALIGN_USE_FULL_HEIGHT);
122 bool
123 BCardLayout::HasHeightForWidth()
125 int32 count = CountItems();
126 for (int32 i = 0; i < count; i++) {
127 if (ItemAt(i)->HasHeightForWidth())
128 return true;
131 return false;
135 void
136 BCardLayout::GetHeightForWidth(float width, float* min, float* max,
137 float* preferred)
139 _ValidateMinMax();
141 // init with useful values
142 float minHeight = fMin.height;
143 float maxHeight = fMax.height;
144 float preferredHeight = fPreferred.height;
146 // apply the items' constraints
147 int32 count = CountItems();
148 for (int32 i = 0; i < count; i++) {
149 BLayoutItem* item = ItemAt(i);
150 if (item->HasHeightForWidth()) {
151 float itemMinHeight;
152 float itemMaxHeight;
153 float itemPreferredHeight;
154 item->GetHeightForWidth(width, &itemMinHeight, &itemMaxHeight,
155 &itemPreferredHeight);
156 minHeight = max_c(minHeight, itemMinHeight);
157 maxHeight = min_c(maxHeight, itemMaxHeight);
158 preferredHeight = min_c(preferredHeight, itemPreferredHeight);
162 // adjust max and preferred, if necessary
163 maxHeight = max_c(maxHeight, minHeight);
164 preferredHeight = max_c(preferredHeight, minHeight);
165 preferredHeight = min_c(preferredHeight, maxHeight);
167 if (min)
168 *min = minHeight;
169 if (max)
170 *max = maxHeight;
171 if (preferred)
172 *preferred = preferredHeight;
176 void
177 BCardLayout::LayoutInvalidated(bool children)
179 fMinMaxValid = false;
183 void
184 BCardLayout::DoLayout()
186 _ValidateMinMax();
188 BSize size(LayoutArea().Size());
190 // this cannot be done when we are viewless, as our children
191 // would not get cut off in the right place.
192 if (Owner()) {
193 size.width = max_c(size.width, fMin.width);
194 size.height = max_c(size.height, fMin.height);
197 if (fVisibleItem != NULL)
198 fVisibleItem->AlignInFrame(BRect(LayoutArea().LeftTop(), size));
202 status_t
203 BCardLayout::Archive(BMessage* into, bool deep) const
205 BArchiver archiver(into);
206 status_t err = BAbstractLayout::Archive(into, deep);
208 if (err == B_OK && deep)
209 err = into->AddInt32(kVisibleItemField, IndexOfItem(fVisibleItem));
211 return archiver.Finish(err);
215 status_t
216 BCardLayout::AllArchived(BMessage* archive) const
218 return BAbstractLayout::AllArchived(archive);
222 status_t
223 BCardLayout::AllUnarchived(const BMessage* from)
225 status_t err = BLayout::AllUnarchived(from);
226 if (err != B_OK)
227 return err;
229 int32 visibleIndex;
230 err = from->FindInt32(kVisibleItemField, &visibleIndex);
231 if (err == B_OK)
232 SetVisibleItem(visibleIndex);
234 return err;
238 status_t
239 BCardLayout::ItemArchived(BMessage* into, BLayoutItem* item, int32 index) const
241 return BAbstractLayout::ItemArchived(into, item, index);
245 status_t
246 BCardLayout::ItemUnarchived(const BMessage* from, BLayoutItem* item,
247 int32 index)
249 return BAbstractLayout::ItemUnarchived(from, item, index);
254 BArchivable*
255 BCardLayout::Instantiate(BMessage* from)
257 if (validate_instantiation(from, "BCardLayout"))
258 return new BCardLayout(from);
259 return NULL;
263 bool
264 BCardLayout::ItemAdded(BLayoutItem* item, int32 atIndex)
266 item->SetVisible(false);
267 return true;
271 void
272 BCardLayout::ItemRemoved(BLayoutItem* item, int32 fromIndex)
274 if (fVisibleItem == item) {
275 BLayoutItem* newVisibleItem = NULL;
276 SetVisibleItem(newVisibleItem);
281 void
282 BCardLayout::_ValidateMinMax()
284 if (fMinMaxValid)
285 return;
287 fMin.width = 0;
288 fMin.height = 0;
289 fMax.width = B_SIZE_UNLIMITED;
290 fMax.height = B_SIZE_UNLIMITED;
291 fPreferred.width = 0;
292 fPreferred.height = 0;
294 int32 itemCount = CountItems();
295 for (int32 i = 0; i < itemCount; i++) {
296 BLayoutItem* item = ItemAt(i);
298 BSize min = item->MinSize();
299 BSize max = item->MaxSize();
300 BSize preferred = item->PreferredSize();
302 fMin.width = max_c(fMin.width, min.width);
303 fMin.height = max_c(fMin.height, min.height);
305 fMax.width = min_c(fMax.width, max.width);
306 fMax.height = min_c(fMax.height, max.height);
308 fPreferred.width = max_c(fPreferred.width, preferred.width);
309 fPreferred.height = max_c(fPreferred.height, preferred.height);
312 fMax.width = max_c(fMax.width, fMin.width);
313 fMax.height = max_c(fMax.height, fMin.height);
315 fPreferred.width = max_c(fPreferred.width, fMin.width);
316 fPreferred.height = max_c(fPreferred.height, fMin.height);
317 fPreferred.width = min_c(fPreferred.width, fMax.width);
318 fPreferred.height = min_c(fPreferred.height, fMax.height);
320 fMinMaxValid = true;
321 ResetLayoutInvalidation();
325 status_t
326 BCardLayout::Perform(perform_code d, void* arg)
328 return BAbstractLayout::Perform(d, arg);
332 void BCardLayout::_ReservedCardLayout1() {}
333 void BCardLayout::_ReservedCardLayout2() {}
334 void BCardLayout::_ReservedCardLayout3() {}
335 void BCardLayout::_ReservedCardLayout4() {}
336 void BCardLayout::_ReservedCardLayout5() {}
337 void BCardLayout::_ReservedCardLayout6() {}
338 void BCardLayout::_ReservedCardLayout7() {}
339 void BCardLayout::_ReservedCardLayout8() {}
340 void BCardLayout::_ReservedCardLayout9() {}
341 void BCardLayout::_ReservedCardLayout10() {}