btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / kits / interface / AbstractLayout.cpp
blobac9c29f2b9056f4cf36c7e13a16b95714b50800d
1 /*
2 * Copyright 2010, Haiku, Inc.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
7 #include <AbstractLayout.h>
8 #include <LayoutUtils.h>
9 #include <Message.h>
10 #include <View.h>
11 #include <ViewPrivate.h>
14 namespace {
15 const char* const kSizesField = "BAbstractLayout:sizes";
16 // kSizesField == {min, max, preferred}
17 const char* const kAlignmentField = "BAbstractLayout:alignment";
18 const char* const kFrameField = "BAbstractLayout:frame";
19 const char* const kVisibleField = "BAbstractLayout:visible";
21 enum proxy_type { VIEW_PROXY_TYPE, DATA_PROXY_TYPE };
25 struct BAbstractLayout::Proxy {
27 Proxy(proxy_type type)
29 type(type)
33 virtual ~Proxy()
37 virtual BSize MinSize() const = 0;
38 virtual void SetMinSize(const BSize&) = 0;
40 virtual BSize MaxSize() const = 0;
41 virtual void SetMaxSize(const BSize&) = 0;
43 virtual BSize PreferredSize() const = 0;
44 virtual void SetPreferredSize(const BSize&) = 0;
46 virtual BAlignment Alignment() const = 0;
47 virtual void SetAlignment(const BAlignment&) = 0;
49 virtual BRect Frame() const = 0;
50 virtual void SetFrame(const BRect& frame) = 0;
52 virtual bool IsVisible(bool ancestorHidden) const = 0;
53 virtual void SetVisible(bool visible) = 0;
55 virtual status_t AddDataToArchive(BMessage* archive,
56 bool ancestorHidden) = 0;
57 virtual status_t RestoreDataFromArchive(const BMessage* archive) = 0;
59 proxy_type type;
63 struct BAbstractLayout::DataProxy : Proxy {
65 DataProxy()
67 Proxy(DATA_PROXY_TYPE),
68 minSize(),
69 maxSize(),
70 preferredSize(),
71 alignment(),
72 frame(-1, -1, 0, 0),
73 visible(true)
77 BSize MinSize() const
79 return minSize;
82 void SetMinSize(const BSize& min)
84 minSize = min;
87 BSize MaxSize() const
89 return maxSize;
92 void SetMaxSize(const BSize& max)
94 maxSize = max;
97 BSize PreferredSize() const
99 return preferredSize;
102 void SetPreferredSize(const BSize& preferred)
104 preferredSize = preferred;
107 BAlignment Alignment() const
109 return this->alignment;
112 void SetAlignment(const BAlignment& align)
114 this->alignment = align;
117 BRect Frame() const
119 return frame;
122 void SetFrame(const BRect& frame)
124 if (frame == this->frame)
125 return;
126 this->frame = frame;
129 bool IsVisible(bool) const
131 return visible;
134 void SetVisible(bool visible)
136 this->visible = visible;
139 status_t AddDataToArchive(BMessage* archive, bool ancestorHidden)
141 status_t err = archive->AddSize(kSizesField, minSize);
142 if (err == B_OK)
143 err = archive->AddSize(kSizesField, maxSize);
144 if (err == B_OK)
145 err = archive->AddSize(kSizesField, preferredSize);
146 if (err == B_OK)
147 err = archive->AddAlignment(kAlignmentField, alignment);
148 if (err == B_OK)
149 err = archive->AddRect(kFrameField, frame);
150 if (err == B_OK)
151 err = archive->AddBool(kVisibleField, visible);
153 return err;
156 status_t RestoreDataFromArchive(const BMessage* archive)
158 status_t err = archive->FindSize(kSizesField, 0, &minSize);
159 if (err == B_OK)
160 err = archive->FindSize(kSizesField, 1, &maxSize);
161 if (err == B_OK)
162 err = archive->FindSize(kSizesField, 2, &preferredSize);
163 if (err == B_OK)
164 err = archive->FindAlignment(kAlignmentField, &alignment);
165 if (err == B_OK)
166 err = archive->FindRect(kFrameField, &frame);
167 if (err == B_OK)
168 err = archive->FindBool(kVisibleField, &visible);
170 return err;
173 BSize minSize;
174 BSize maxSize;
175 BSize preferredSize;
176 BAlignment alignment;
177 BRect frame;
178 bool visible;
182 struct BAbstractLayout::ViewProxy : Proxy {
183 ViewProxy(BView* target)
185 Proxy(VIEW_PROXY_TYPE),
186 view(target)
190 BSize MinSize() const
192 return view->ExplicitMinSize();
195 void SetMinSize(const BSize& min)
197 view->SetExplicitMinSize(min);
200 BSize MaxSize() const
202 return view->ExplicitMaxSize();
205 void SetMaxSize(const BSize& min)
207 view->SetExplicitMaxSize(min);
210 BSize PreferredSize() const
212 return view->ExplicitPreferredSize();
215 void SetPreferredSize(const BSize& preferred)
217 view->SetExplicitPreferredSize(preferred);
220 BAlignment Alignment() const
222 return view->ExplicitAlignment();
225 void SetAlignment(const BAlignment& alignment)
227 view->SetExplicitAlignment(alignment);
230 BRect Frame() const
232 return view->Frame();
235 void SetFrame(const BRect& frame)
237 view->MoveTo(frame.LeftTop());
238 view->ResizeTo(frame.Width(), frame.Height());
241 bool IsVisible(bool ancestorsVisible) const
243 int16 showLevel = BView::Private(view).ShowLevel();
244 return (showLevel - (ancestorsVisible) ? 0 : 1) <= 0;
247 void SetVisible(bool visible)
249 // No need to check that we are not re-hiding, that is done
250 // for us.
251 if (visible)
252 view->Show();
253 else
254 view->Hide();
257 status_t AddDataToArchive(BMessage* archive, bool ancestorHidden)
259 return B_OK;
262 status_t RestoreDataFromArchive(const BMessage* archive)
264 return B_OK;
267 BView* view;
271 BAbstractLayout::BAbstractLayout()
273 fExplicitData(new BAbstractLayout::DataProxy())
278 BAbstractLayout::BAbstractLayout(BMessage* from)
280 BLayout(BUnarchiver::PrepareArchive(from)),
281 fExplicitData(new DataProxy())
283 BUnarchiver(from).Finish();
287 BAbstractLayout::~BAbstractLayout()
289 delete fExplicitData;
293 BSize
294 BAbstractLayout::MinSize()
296 return BLayoutUtils::ComposeSize(fExplicitData->MinSize(), BaseMinSize());
300 BSize
301 BAbstractLayout::MaxSize()
303 return BLayoutUtils::ComposeSize(fExplicitData->MaxSize(), BaseMaxSize());
307 BSize
308 BAbstractLayout::PreferredSize()
310 return BLayoutUtils::ComposeSize(fExplicitData->PreferredSize(),
311 BasePreferredSize());
315 BAlignment
316 BAbstractLayout::Alignment()
318 return BLayoutUtils::ComposeAlignment(fExplicitData->Alignment(),
319 BaseAlignment());
323 void
324 BAbstractLayout::SetExplicitMinSize(BSize size)
326 fExplicitData->SetMinSize(size);
330 void
331 BAbstractLayout::SetExplicitMaxSize(BSize size)
333 fExplicitData->SetMaxSize(size);
337 void
338 BAbstractLayout::SetExplicitPreferredSize(BSize size)
340 fExplicitData->SetPreferredSize(size);
344 void
345 BAbstractLayout::SetExplicitAlignment(BAlignment alignment)
347 fExplicitData->SetAlignment(alignment);
351 BSize
352 BAbstractLayout::BaseMinSize()
354 return BSize(0, 0);
358 BSize
359 BAbstractLayout::BaseMaxSize()
361 return BSize(B_SIZE_UNLIMITED, B_SIZE_UNLIMITED);
365 BSize
366 BAbstractLayout::BasePreferredSize()
368 return BSize(0, 0);
372 BAlignment
373 BAbstractLayout::BaseAlignment()
375 return BAlignment(B_ALIGN_USE_FULL_WIDTH, B_ALIGN_USE_FULL_HEIGHT);
379 BRect
380 BAbstractLayout::Frame()
382 return fExplicitData->Frame();
386 void
387 BAbstractLayout::SetFrame(BRect frame)
389 if (frame != fExplicitData->Frame()) {
390 fExplicitData->SetFrame(frame);
391 if (!Owner())
392 Relayout();
397 bool
398 BAbstractLayout::IsVisible()
400 return fExplicitData->IsVisible(AncestorsVisible());
404 void
405 BAbstractLayout::SetVisible(bool visible)
407 if (visible != fExplicitData->IsVisible(AncestorsVisible())) {
408 fExplicitData->SetVisible(visible);
409 if (Layout())
410 Layout()->InvalidateLayout(false);
411 VisibilityChanged(visible);
416 status_t
417 BAbstractLayout::Archive(BMessage* into, bool deep) const
419 BArchiver archiver(into);
420 status_t err = BLayout::Archive(into, deep);
422 return archiver.Finish(err);
426 status_t
427 BAbstractLayout::AllArchived(BMessage* archive) const
429 return BLayout::AllArchived(archive);
433 status_t
434 BAbstractLayout::AllUnarchived(const BMessage* from)
436 status_t err = fExplicitData->RestoreDataFromArchive(from);
437 if (err != B_OK)
438 return err;
440 return BLayout::AllUnarchived(from);
444 status_t
445 BAbstractLayout::ItemArchived(BMessage* into, BLayoutItem* item,
446 int32 index) const
448 return BLayout::ItemArchived(into, item, index);
452 status_t
453 BAbstractLayout::ItemUnarchived(const BMessage* from, BLayoutItem* item,
454 int32 index)
456 return BLayout::ItemUnarchived(from, item, index);
460 bool
461 BAbstractLayout::ItemAdded(BLayoutItem* item, int32 atIndex)
463 return BLayout::ItemAdded(item, atIndex);
467 void
468 BAbstractLayout::ItemRemoved(BLayoutItem* item, int32 fromIndex)
470 BLayout::ItemRemoved(item, fromIndex);
474 void
475 BAbstractLayout::LayoutInvalidated(bool children)
477 BLayout::LayoutInvalidated(children);
481 void
482 BAbstractLayout::OwnerChanged(BView* was)
484 if (was) {
485 static_cast<ViewProxy*>(fExplicitData)->view = Owner();
486 return;
489 delete fExplicitData;
490 fExplicitData = new ViewProxy(Owner());
494 void
495 BAbstractLayout::AttachedToLayout()
497 BLayout::AttachedToLayout();
501 void
502 BAbstractLayout::DetachedFromLayout(BLayout* layout)
504 BLayout::DetachedFromLayout(layout);
508 void
509 BAbstractLayout::AncestorVisibilityChanged(bool shown)
511 if (AncestorsVisible() == shown)
512 return;
514 if (BView* owner = Owner()) {
515 if (shown)
516 owner->Show();
517 else
518 owner->Hide();
520 BLayout::AncestorVisibilityChanged(shown);
524 // Binary compatibility stuff
527 status_t
528 BAbstractLayout::Perform(perform_code code, void* _data)
530 return BLayout::Perform(code, _data);
534 void BAbstractLayout::_ReservedAbstractLayout1() {}
535 void BAbstractLayout::_ReservedAbstractLayout2() {}
536 void BAbstractLayout::_ReservedAbstractLayout3() {}
537 void BAbstractLayout::_ReservedAbstractLayout4() {}
538 void BAbstractLayout::_ReservedAbstractLayout5() {}
539 void BAbstractLayout::_ReservedAbstractLayout6() {}
540 void BAbstractLayout::_ReservedAbstractLayout7() {}
541 void BAbstractLayout::_ReservedAbstractLayout8() {}
542 void BAbstractLayout::_ReservedAbstractLayout9() {}
543 void BAbstractLayout::_ReservedAbstractLayout10() {}