2 * Copyright 2010, Haiku, Inc.
3 * All rights reserved. Distributed under the terms of the MIT License.
7 #include <AbstractLayout.h>
8 #include <LayoutUtils.h>
11 #include <ViewPrivate.h>
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
)
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;
63 struct BAbstractLayout::DataProxy
: Proxy
{
67 Proxy(DATA_PROXY_TYPE
),
82 void SetMinSize(const BSize
& min
)
92 void SetMaxSize(const BSize
& max
)
97 BSize
PreferredSize() const
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
;
122 void SetFrame(const BRect
& frame
)
124 if (frame
== this->frame
)
129 bool IsVisible(bool) const
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
);
143 err
= archive
->AddSize(kSizesField
, maxSize
);
145 err
= archive
->AddSize(kSizesField
, preferredSize
);
147 err
= archive
->AddAlignment(kAlignmentField
, alignment
);
149 err
= archive
->AddRect(kFrameField
, frame
);
151 err
= archive
->AddBool(kVisibleField
, visible
);
156 status_t
RestoreDataFromArchive(const BMessage
* archive
)
158 status_t err
= archive
->FindSize(kSizesField
, 0, &minSize
);
160 err
= archive
->FindSize(kSizesField
, 1, &maxSize
);
162 err
= archive
->FindSize(kSizesField
, 2, &preferredSize
);
164 err
= archive
->FindAlignment(kAlignmentField
, &alignment
);
166 err
= archive
->FindRect(kFrameField
, &frame
);
168 err
= archive
->FindBool(kVisibleField
, &visible
);
176 BAlignment alignment
;
182 struct BAbstractLayout::ViewProxy
: Proxy
{
183 ViewProxy(BView
* target
)
185 Proxy(VIEW_PROXY_TYPE
),
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
);
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
257 status_t
AddDataToArchive(BMessage
* archive
, bool ancestorHidden
)
262 status_t
RestoreDataFromArchive(const BMessage
* archive
)
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
;
294 BAbstractLayout::MinSize()
296 return BLayoutUtils::ComposeSize(fExplicitData
->MinSize(), BaseMinSize());
301 BAbstractLayout::MaxSize()
303 return BLayoutUtils::ComposeSize(fExplicitData
->MaxSize(), BaseMaxSize());
308 BAbstractLayout::PreferredSize()
310 return BLayoutUtils::ComposeSize(fExplicitData
->PreferredSize(),
311 BasePreferredSize());
316 BAbstractLayout::Alignment()
318 return BLayoutUtils::ComposeAlignment(fExplicitData
->Alignment(),
324 BAbstractLayout::SetExplicitMinSize(BSize size
)
326 fExplicitData
->SetMinSize(size
);
331 BAbstractLayout::SetExplicitMaxSize(BSize size
)
333 fExplicitData
->SetMaxSize(size
);
338 BAbstractLayout::SetExplicitPreferredSize(BSize size
)
340 fExplicitData
->SetPreferredSize(size
);
345 BAbstractLayout::SetExplicitAlignment(BAlignment alignment
)
347 fExplicitData
->SetAlignment(alignment
);
352 BAbstractLayout::BaseMinSize()
359 BAbstractLayout::BaseMaxSize()
361 return BSize(B_SIZE_UNLIMITED
, B_SIZE_UNLIMITED
);
366 BAbstractLayout::BasePreferredSize()
373 BAbstractLayout::BaseAlignment()
375 return BAlignment(B_ALIGN_USE_FULL_WIDTH
, B_ALIGN_USE_FULL_HEIGHT
);
380 BAbstractLayout::Frame()
382 return fExplicitData
->Frame();
387 BAbstractLayout::SetFrame(BRect frame
)
389 if (frame
!= fExplicitData
->Frame()) {
390 fExplicitData
->SetFrame(frame
);
398 BAbstractLayout::IsVisible()
400 return fExplicitData
->IsVisible(AncestorsVisible());
405 BAbstractLayout::SetVisible(bool visible
)
407 if (visible
!= fExplicitData
->IsVisible(AncestorsVisible())) {
408 fExplicitData
->SetVisible(visible
);
410 Layout()->InvalidateLayout(false);
411 VisibilityChanged(visible
);
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
);
427 BAbstractLayout::AllArchived(BMessage
* archive
) const
429 return BLayout::AllArchived(archive
);
434 BAbstractLayout::AllUnarchived(const BMessage
* from
)
436 status_t err
= fExplicitData
->RestoreDataFromArchive(from
);
440 return BLayout::AllUnarchived(from
);
445 BAbstractLayout::ItemArchived(BMessage
* into
, BLayoutItem
* item
,
448 return BLayout::ItemArchived(into
, item
, index
);
453 BAbstractLayout::ItemUnarchived(const BMessage
* from
, BLayoutItem
* item
,
456 return BLayout::ItemUnarchived(from
, item
, index
);
461 BAbstractLayout::ItemAdded(BLayoutItem
* item
, int32 atIndex
)
463 return BLayout::ItemAdded(item
, atIndex
);
468 BAbstractLayout::ItemRemoved(BLayoutItem
* item
, int32 fromIndex
)
470 BLayout::ItemRemoved(item
, fromIndex
);
475 BAbstractLayout::LayoutInvalidated(bool children
)
477 BLayout::LayoutInvalidated(children
);
482 BAbstractLayout::OwnerChanged(BView
* was
)
485 static_cast<ViewProxy
*>(fExplicitData
)->view
= Owner();
489 delete fExplicitData
;
490 fExplicitData
= new ViewProxy(Owner());
495 BAbstractLayout::AttachedToLayout()
497 BLayout::AttachedToLayout();
502 BAbstractLayout::DetachedFromLayout(BLayout
* layout
)
504 BLayout::DetachedFromLayout(layout
);
509 BAbstractLayout::AncestorVisibilityChanged(bool shown
)
511 if (AncestorsVisible() == shown
)
514 if (BView
* owner
= Owner()) {
520 BLayout::AncestorVisibilityChanged(shown
);
524 // Binary compatibility stuff
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() {}