19ecf9d1cSIngo Weinhold/*
2e5ea208dSIngo Weinhold * Copyright 2010, Haiku, Inc.
39ecf9d1cSIngo Weinhold * Copyright 2006, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
49ecf9d1cSIngo Weinhold * All rights reserved. Distributed under the terms of the MIT License.
59ecf9d1cSIngo Weinhold */
69ecf9d1cSIngo Weinhold
7e5ea208dSIngo Weinhold
89ecf9d1cSIngo Weinhold#include "ViewLayoutItem.h"
99ecf9d1cSIngo Weinhold
10e5ea208dSIngo Weinhold#include <new>
11e5ea208dSIngo Weinhold
12e5ea208dSIngo Weinhold#include <Layout.h>
139ecf9d1cSIngo Weinhold#include <View.h>
141d6c7b6cSAlex Wilson#include <ViewPrivate.h>
159ecf9d1cSIngo Weinhold
169ecf9d1cSIngo Weinhold
17e5ea208dSIngo Weinholdnamespace {
184a254e4dSAlex Wilson	const char* const kViewField = "BViewLayoutItem:view";
19e5ea208dSIngo Weinhold}
20e5ea208dSIngo Weinhold
21e5ea208dSIngo Weinhold
229ecf9d1cSIngo WeinholdBViewLayoutItem::BViewLayoutItem(BView* view)
23e5ea208dSIngo Weinhold	:
241d6c7b6cSAlex Wilson	fView(view),
251d6c7b6cSAlex Wilson	fAncestorsVisible(true)
26e5ea208dSIngo Weinhold{
27e5ea208dSIngo Weinhold}
28e5ea208dSIngo Weinhold
29e5ea208dSIngo Weinhold
30e5ea208dSIngo WeinholdBViewLayoutItem::BViewLayoutItem(BMessage* from)
31e5ea208dSIngo Weinhold	:
32e5ea208dSIngo Weinhold	BLayoutItem(BUnarchiver::PrepareArchive(from)),
331d6c7b6cSAlex Wilson	fView(NULL),
341d6c7b6cSAlex Wilson	fAncestorsVisible(true)
359ecf9d1cSIngo Weinhold{
36e5ea208dSIngo Weinhold	BUnarchiver unarchiver(from);
37e5ea208dSIngo Weinhold	unarchiver.Finish(unarchiver.FindObject<BView>(kViewField, 0,
38e5ea208dSIngo Weinhold		BUnarchiver::B_DONT_ASSUME_OWNERSHIP, fView));
399ecf9d1cSIngo Weinhold}
409ecf9d1cSIngo Weinhold
41e5ea208dSIngo Weinhold
429ecf9d1cSIngo WeinholdBViewLayoutItem::~BViewLayoutItem()
439ecf9d1cSIngo Weinhold{
449ecf9d1cSIngo Weinhold}
459ecf9d1cSIngo Weinhold
46e5ea208dSIngo Weinhold
479ecf9d1cSIngo WeinholdBSize
489ecf9d1cSIngo WeinholdBViewLayoutItem::MinSize()
499ecf9d1cSIngo Weinhold{
509ecf9d1cSIngo Weinhold	return fView->MinSize();
519ecf9d1cSIngo Weinhold}
529ecf9d1cSIngo Weinhold
53e5ea208dSIngo Weinhold
549ecf9d1cSIngo WeinholdBSize
559ecf9d1cSIngo WeinholdBViewLayoutItem::MaxSize()
569ecf9d1cSIngo Weinhold{
579ecf9d1cSIngo Weinhold	return fView->MaxSize();
589ecf9d1cSIngo Weinhold}
599ecf9d1cSIngo Weinhold
60e5ea208dSIngo Weinhold
619ecf9d1cSIngo WeinholdBSize
629ecf9d1cSIngo WeinholdBViewLayoutItem::PreferredSize()
639ecf9d1cSIngo Weinhold{
649ecf9d1cSIngo Weinhold	return fView->PreferredSize();
659ecf9d1cSIngo Weinhold}
669ecf9d1cSIngo Weinhold
67e5ea208dSIngo Weinhold
689ecf9d1cSIngo WeinholdBAlignment
699ecf9d1cSIngo WeinholdBViewLayoutItem::Alignment()
709ecf9d1cSIngo Weinhold{
7152e06f98SStephan Aßmus	return fView->LayoutAlignment();
729ecf9d1cSIngo Weinhold}
739ecf9d1cSIngo Weinhold
74e5ea208dSIngo Weinhold
759ecf9d1cSIngo Weinholdvoid
769ecf9d1cSIngo WeinholdBViewLayoutItem::SetExplicitMinSize(BSize size)
779ecf9d1cSIngo Weinhold{
789ecf9d1cSIngo Weinhold	fView->SetExplicitMinSize(size);
799ecf9d1cSIngo Weinhold}
809ecf9d1cSIngo Weinhold
81e5ea208dSIngo Weinhold
829ecf9d1cSIngo Weinholdvoid
839ecf9d1cSIngo WeinholdBViewLayoutItem::SetExplicitMaxSize(BSize size)
849ecf9d1cSIngo Weinhold{
859ecf9d1cSIngo Weinhold	fView->SetExplicitMaxSize(size);
869ecf9d1cSIngo Weinhold}
879ecf9d1cSIngo Weinhold
88e5ea208dSIngo Weinhold
899ecf9d1cSIngo Weinholdvoid
909ecf9d1cSIngo WeinholdBViewLayoutItem::SetExplicitPreferredSize(BSize size)
919ecf9d1cSIngo Weinhold{
929ecf9d1cSIngo Weinhold	fView->SetExplicitPreferredSize(size);
939ecf9d1cSIngo Weinhold}
949ecf9d1cSIngo Weinhold
95e5ea208dSIngo Weinhold
969ecf9d1cSIngo Weinholdvoid
979ecf9d1cSIngo WeinholdBViewLayoutItem::SetExplicitAlignment(BAlignment alignment)
989ecf9d1cSIngo Weinhold{
999ecf9d1cSIngo Weinhold	fView->SetExplicitAlignment(alignment);
1009ecf9d1cSIngo Weinhold}
1019ecf9d1cSIngo Weinhold
102e5ea208dSIngo Weinhold
1039ecf9d1cSIngo Weinholdbool
1049ecf9d1cSIngo WeinholdBViewLayoutItem::IsVisible()
1059ecf9d1cSIngo Weinhold{
1061d6c7b6cSAlex Wilson	int16 showLevel = BView::Private(fView).ShowLevel();
1071d6c7b6cSAlex Wilson	return showLevel - (fAncestorsVisible ? 0 : 1) <= 0;
1089ecf9d1cSIngo Weinhold}
1099ecf9d1cSIngo Weinhold
110e5ea208dSIngo Weinhold
1119ecf9d1cSIngo Weinholdvoid
1129ecf9d1cSIngo WeinholdBViewLayoutItem::SetVisible(bool visible)
1139ecf9d1cSIngo Weinhold{
1149ecf9d1cSIngo Weinhold	if (visible != IsVisible()) {
1159ecf9d1cSIngo Weinhold		if (visible)
1169ecf9d1cSIngo Weinhold			fView->Show();
1179ecf9d1cSIngo Weinhold		else
1189ecf9d1cSIngo Weinhold			fView->Hide();
1199ecf9d1cSIngo Weinhold	}
1209ecf9d1cSIngo Weinhold}
1219ecf9d1cSIngo Weinhold
122e5ea208dSIngo Weinhold
1239ecf9d1cSIngo WeinholdBRect
1249ecf9d1cSIngo WeinholdBViewLayoutItem::Frame()
1259ecf9d1cSIngo Weinhold{
1269ecf9d1cSIngo Weinhold	return fView->Frame();
1279ecf9d1cSIngo Weinhold}
1289ecf9d1cSIngo Weinhold
129e5ea208dSIngo Weinhold
1309ecf9d1cSIngo Weinholdvoid
1319ecf9d1cSIngo WeinholdBViewLayoutItem::SetFrame(BRect frame)
1329ecf9d1cSIngo Weinhold{
1339ecf9d1cSIngo Weinhold	fView->MoveTo(frame.LeftTop());
1349ecf9d1cSIngo Weinhold	fView->ResizeTo(frame.Width(), frame.Height());
1359ecf9d1cSIngo Weinhold}
1369ecf9d1cSIngo Weinhold
137e5ea208dSIngo Weinhold
1389ecf9d1cSIngo Weinholdbool
1399ecf9d1cSIngo WeinholdBViewLayoutItem::HasHeightForWidth()
1409ecf9d1cSIngo Weinhold{
1419ecf9d1cSIngo Weinhold	return fView->HasHeightForWidth();
1429ecf9d1cSIngo Weinhold}
1439ecf9d1cSIngo Weinhold
144e5ea208dSIngo Weinhold
1459ecf9d1cSIngo Weinholdvoid
1469ecf9d1cSIngo WeinholdBViewLayoutItem::GetHeightForWidth(float width, float* min, float* max,
1479ecf9d1cSIngo Weinhold	float* preferred)
1489ecf9d1cSIngo Weinhold{
1499ecf9d1cSIngo Weinhold	fView->GetHeightForWidth(width, min, max, preferred);
1509ecf9d1cSIngo Weinhold}
1519ecf9d1cSIngo Weinhold
152e5ea208dSIngo Weinhold
1539ecf9d1cSIngo WeinholdBView*
1549ecf9d1cSIngo WeinholdBViewLayoutItem::View()
1559ecf9d1cSIngo Weinhold{
1569ecf9d1cSIngo Weinhold	return fView;
1579ecf9d1cSIngo Weinhold}
1589ecf9d1cSIngo Weinhold
159e5ea208dSIngo Weinhold
1601d6c7b6cSAlex Wilsonvoid
1611d6c7b6cSAlex WilsonBViewLayoutItem::Relayout(bool immediate)
1621d6c7b6cSAlex Wilson{
1631d6c7b6cSAlex Wilson	if (immediate)
1641d6c7b6cSAlex Wilson		fView->Layout(false);
1651d6c7b6cSAlex Wilson	else
1661d6c7b6cSAlex Wilson		fView->Relayout();
1679ecf9d1cSIngo Weinhold}
168e5ea208dSIngo Weinhold
169e5ea208dSIngo Weinhold
170e5ea208dSIngo Weinholdstatus_t
171e5ea208dSIngo WeinholdBViewLayoutItem::Archive(BMessage* into, bool deep) const
172e5ea208dSIngo Weinhold{
173e5ea208dSIngo Weinhold	BArchiver archiver(into);
174e5ea208dSIngo Weinhold	status_t err = BLayoutItem::Archive(into, deep);
175e5ea208dSIngo Weinhold
176e5ea208dSIngo Weinhold	return archiver.Finish(err);
177e5ea208dSIngo Weinhold}
178e5ea208dSIngo Weinhold
179e5ea208dSIngo Weinhold
180e5ea208dSIngo Weinholdstatus_t
181e5ea208dSIngo WeinholdBViewLayoutItem::AllArchived(BMessage* into) const
182e5ea208dSIngo Weinhold{
183e5ea208dSIngo Weinhold	BArchiver archiver(into);
184e5ea208dSIngo Weinhold	status_t err = BLayoutItem::AllArchived(into);
185e5ea208dSIngo Weinhold
186e5ea208dSIngo Weinhold	if (err == B_OK) {
187e5ea208dSIngo Weinhold		if (archiver.IsArchived(fView))
188e5ea208dSIngo Weinhold			err = archiver.AddArchivable(kViewField, fView);
189e5ea208dSIngo Weinhold		else
190e5ea208dSIngo Weinhold			err = B_NAME_NOT_FOUND;
191e5ea208dSIngo Weinhold	}
192e5ea208dSIngo Weinhold
193e5ea208dSIngo Weinhold	return err;
194e5ea208dSIngo Weinhold}
195e5ea208dSIngo Weinhold
196e5ea208dSIngo Weinhold
197e5ea208dSIngo Weinholdstatus_t
198e5ea208dSIngo WeinholdBViewLayoutItem::AllUnarchived(const BMessage* from)
199e5ea208dSIngo Weinhold{
200e5ea208dSIngo Weinhold	if (!fView)
201e5ea208dSIngo Weinhold		return B_ERROR;
202e5ea208dSIngo Weinhold
203e5ea208dSIngo Weinhold	return BLayoutItem::AllUnarchived(from);
204e5ea208dSIngo Weinhold}
205e5ea208dSIngo Weinhold
206e5ea208dSIngo Weinhold
207e5ea208dSIngo WeinholdBArchivable*
208e5ea208dSIngo WeinholdBViewLayoutItem::Instantiate(BMessage* from)
209e5ea208dSIngo Weinhold{
210e5ea208dSIngo Weinhold	if (validate_instantiation(from, "BViewLayoutItem"))
211e5ea208dSIngo Weinhold		return new(std::nothrow) BViewLayoutItem(from);
212e5ea208dSIngo Weinhold	return NULL;
213e5ea208dSIngo Weinhold}
2141d6c7b6cSAlex Wilson
2151d6c7b6cSAlex Wilson
216c74faed4SAlex Wilsonvoid
217c74faed4SAlex WilsonBViewLayoutItem::LayoutInvalidated(bool children)
218c74faed4SAlex Wilson{
219c74faed4SAlex Wilson	fView->InvalidateLayout(children);
220c74faed4SAlex Wilson}
221c74faed4SAlex Wilson
222c74faed4SAlex Wilson
2231d6c7b6cSAlex Wilsonvoid
2241d6c7b6cSAlex WilsonBViewLayoutItem::AncestorVisibilityChanged(bool shown)
2251d6c7b6cSAlex Wilson{
2261d6c7b6cSAlex Wilson	if (fAncestorsVisible == shown)
2271d6c7b6cSAlex Wilson		return;
2281d6c7b6cSAlex Wilson
2291d6c7b6cSAlex Wilson	fAncestorsVisible = shown;
2301d6c7b6cSAlex Wilson	if (shown)
2311d6c7b6cSAlex Wilson		fView->Show();
2321d6c7b6cSAlex Wilson	if (!shown)
2331d6c7b6cSAlex Wilson		fView->Hide();
2341d6c7b6cSAlex Wilson}
2351d6c7b6cSAlex Wilson
236