xref: /haiku/src/kits/interface/ViewLayoutItem.cpp (revision 7a74a5df454197933bc6e80a542102362ee98703)
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 "ViewLayoutItem.h"
9 
10 #include <new>
11 
12 #include <Layout.h>
13 #include <View.h>
14 #include <ViewPrivate.h>
15 
16 
17 namespace {
18 	const char* const kViewField = "BViewLayoutItem:view";
19 }
20 
21 
22 BViewLayoutItem::BViewLayoutItem(BView* view)
23 	:
24 	fView(view),
25 	fAncestorsVisible(true)
26 {
27 }
28 
29 
30 BViewLayoutItem::BViewLayoutItem(BMessage* from)
31 	:
32 	BLayoutItem(BUnarchiver::PrepareArchive(from)),
33 	fView(NULL),
34 	fAncestorsVisible(true)
35 {
36 	BUnarchiver unarchiver(from);
37 	unarchiver.Finish(unarchiver.FindObject<BView>(kViewField, 0,
38 		BUnarchiver::B_DONT_ASSUME_OWNERSHIP, fView));
39 }
40 
41 
42 BViewLayoutItem::~BViewLayoutItem()
43 {
44 }
45 
46 
47 BSize
48 BViewLayoutItem::MinSize()
49 {
50 	return fView->MinSize();
51 }
52 
53 
54 BSize
55 BViewLayoutItem::MaxSize()
56 {
57 	return fView->MaxSize();
58 }
59 
60 
61 BSize
62 BViewLayoutItem::PreferredSize()
63 {
64 	return fView->PreferredSize();
65 }
66 
67 
68 BAlignment
69 BViewLayoutItem::Alignment()
70 {
71 	return fView->LayoutAlignment();
72 }
73 
74 
75 void
76 BViewLayoutItem::SetExplicitMinSize(BSize size)
77 {
78 	fView->SetExplicitMinSize(size);
79 }
80 
81 
82 void
83 BViewLayoutItem::SetExplicitMaxSize(BSize size)
84 {
85 	fView->SetExplicitMaxSize(size);
86 }
87 
88 
89 void
90 BViewLayoutItem::SetExplicitPreferredSize(BSize size)
91 {
92 	fView->SetExplicitPreferredSize(size);
93 }
94 
95 
96 void
97 BViewLayoutItem::SetExplicitAlignment(BAlignment alignment)
98 {
99 	fView->SetExplicitAlignment(alignment);
100 }
101 
102 
103 bool
104 BViewLayoutItem::IsVisible()
105 {
106 	int16 showLevel = BView::Private(fView).ShowLevel();
107 	return showLevel - (fAncestorsVisible ? 0 : 1) <= 0;
108 }
109 
110 
111 void
112 BViewLayoutItem::SetVisible(bool visible)
113 {
114 	if (visible != IsVisible()) {
115 		if (visible)
116 			fView->Show();
117 		else
118 			fView->Hide();
119 	}
120 }
121 
122 
123 BRect
124 BViewLayoutItem::Frame()
125 {
126 	return fView->Frame();
127 }
128 
129 
130 void
131 BViewLayoutItem::SetFrame(BRect frame)
132 {
133 	fView->MoveTo(frame.LeftTop());
134 	fView->ResizeTo(frame.Width(), frame.Height());
135 }
136 
137 
138 bool
139 BViewLayoutItem::HasHeightForWidth()
140 {
141 	return fView->HasHeightForWidth();
142 }
143 
144 
145 void
146 BViewLayoutItem::GetHeightForWidth(float width, float* min, float* max,
147 	float* preferred)
148 {
149 	fView->GetHeightForWidth(width, min, max, preferred);
150 }
151 
152 
153 BView*
154 BViewLayoutItem::View()
155 {
156 	return fView;
157 }
158 
159 
160 void
161 BViewLayoutItem::Relayout(bool immediate)
162 {
163 	if (immediate)
164 		fView->Layout(false);
165 	else
166 		fView->Relayout();
167 }
168 
169 
170 status_t
171 BViewLayoutItem::Archive(BMessage* into, bool deep) const
172 {
173 	BArchiver archiver(into);
174 	status_t err = BLayoutItem::Archive(into, deep);
175 
176 	return archiver.Finish(err);
177 }
178 
179 
180 status_t
181 BViewLayoutItem::AllArchived(BMessage* into) const
182 {
183 	BArchiver archiver(into);
184 	status_t err = BLayoutItem::AllArchived(into);
185 
186 	if (err == B_OK) {
187 		if (archiver.IsArchived(fView))
188 			err = archiver.AddArchivable(kViewField, fView);
189 		else
190 			err = B_NAME_NOT_FOUND;
191 	}
192 
193 	return err;
194 }
195 
196 
197 status_t
198 BViewLayoutItem::AllUnarchived(const BMessage* from)
199 {
200 	if (!fView)
201 		return B_ERROR;
202 
203 	return BLayoutItem::AllUnarchived(from);
204 }
205 
206 
207 BArchivable*
208 BViewLayoutItem::Instantiate(BMessage* from)
209 {
210 	if (validate_instantiation(from, "BViewLayoutItem"))
211 		return new(std::nothrow) BViewLayoutItem(from);
212 	return NULL;
213 }
214 
215 
216 void
217 BViewLayoutItem::LayoutInvalidated(bool children)
218 {
219 	fView->InvalidateLayout(children);
220 }
221 
222 
223 void
224 BViewLayoutItem::AncestorVisibilityChanged(bool shown)
225 {
226 	if (fAncestorsVisible == shown)
227 		return;
228 
229 	fAncestorsVisible = shown;
230 	if (shown)
231 		fView->Show();
232 	if (!shown)
233 		fView->Hide();
234 }
235 
236