xref: /haiku/src/kits/interface/GroupLayoutBuilder.cpp (revision e70fa080b2b7f29e420fa0ac7598f48ff2b15882)
1 /*
2  * Copyright 2006, Ingo Weinhold <bonefish@cs.tu-berlin.de>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 
6 #include <GroupLayoutBuilder.h>
7 
8 #include <new>
9 
10 #include <SpaceLayoutItem.h>
11 
12 
13 using std::nothrow;
14 
15 
16 // constructor
17 BGroupLayoutBuilder::BGroupLayoutBuilder(enum orientation orientation,
18 	float spacing)
19 	: fRootLayout((new BGroupView(orientation, spacing))->GroupLayout())
20 {
21 	_PushLayout(fRootLayout);
22 }
23 
24 // constructor
25 BGroupLayoutBuilder::BGroupLayoutBuilder(BGroupLayout* layout)
26 	: fRootLayout(layout)
27 {
28 	_PushLayout(fRootLayout);
29 }
30 
31 
32 // constructor
33 BGroupLayoutBuilder::BGroupLayoutBuilder(BGroupView* view)
34 	: fRootLayout(view->GroupLayout())
35 {
36 	_PushLayout(fRootLayout);
37 }
38 
39 // RootLayout
40 BGroupLayout*
41 BGroupLayoutBuilder::RootLayout() const
42 {
43 	return fRootLayout;
44 }
45 
46 // TopLayout
47 BGroupLayout*
48 BGroupLayoutBuilder::TopLayout() const
49 {
50 	int32 count = fLayoutStack.CountItems();
51 	return (count > 0
52 		? (BGroupLayout*)fLayoutStack.ItemAt(count - 1) : NULL);
53 }
54 
55 // GetTopLayout
56 BGroupLayoutBuilder&
57 BGroupLayoutBuilder::GetTopLayout(BGroupLayout** _layout)
58 {
59 	*_layout = TopLayout();
60 	return *this;
61 }
62 
63 // TopView
64 BView*
65 BGroupLayoutBuilder::TopView() const
66 {
67 	return TopLayout()->View();
68 }
69 
70 // GetTopView
71 BGroupLayoutBuilder&
72 BGroupLayoutBuilder::GetTopView(BView** _view)
73 {
74 	if (BGroupLayout* layout = TopLayout())
75 		*_view = layout->View();
76 	else
77 		*_view = NULL;
78 
79 	return *this;
80 }
81 
82 // Add
83 BGroupLayoutBuilder&
84 BGroupLayoutBuilder::Add(BView* view)
85 {
86 	if (BGroupLayout* layout = TopLayout())
87 		layout->AddView(view);
88 	return *this;
89 }
90 
91 // Add
92 BGroupLayoutBuilder&
93 BGroupLayoutBuilder::Add(BView* view, float weight)
94 {
95 	if (BGroupLayout* layout = TopLayout())
96 		layout->AddView(view, weight);
97 	return *this;
98 }
99 
100 // Add
101 BGroupLayoutBuilder&
102 BGroupLayoutBuilder::Add(BLayoutItem* item)
103 {
104 	if (BGroupLayout* layout = TopLayout())
105 		layout->AddItem(item);
106 	return *this;
107 }
108 
109 // Add
110 BGroupLayoutBuilder&
111 BGroupLayoutBuilder::Add(BLayoutItem* item, float weight)
112 {
113 	if (BGroupLayout* layout = TopLayout())
114 		layout->AddItem(item, weight);
115 	return *this;
116 }
117 
118 // AddGroup
119 BGroupLayoutBuilder&
120 BGroupLayoutBuilder::AddGroup(enum orientation orientation,
121 	float spacing, float weight)
122 {
123 	if (BGroupLayout* layout = TopLayout()) {
124 		BGroupView* group = new(nothrow) BGroupView(orientation, spacing);
125 		if (group) {
126 			if (layout->AddView(group, weight))
127 				_PushLayout(group->GroupLayout());
128 			else
129 				delete group;
130 		}
131 	}
132 
133 	return *this;
134 }
135 
136 // End
137 BGroupLayoutBuilder&
138 BGroupLayoutBuilder::End()
139 {
140 	_PopLayout();
141 	return *this;
142 }
143 
144 // AddGlue
145 BGroupLayoutBuilder&
146 BGroupLayoutBuilder::AddGlue(float weight)
147 {
148 	if (BGroupLayout* layout = TopLayout())
149 		layout->AddItem(BSpaceLayoutItem::CreateGlue(), weight);
150 
151 	return *this;
152 }
153 
154 // AddStrut
155 BGroupLayoutBuilder&
156 BGroupLayoutBuilder::AddStrut(float size)
157 {
158 	if (BGroupLayout* layout = TopLayout()) {
159 		if (layout->Orientation() == B_HORIZONTAL)
160 			layout->AddItem(BSpaceLayoutItem::CreateHorizontalStrut(size));
161 		else
162 			layout->AddItem(BSpaceLayoutItem::CreateVerticalStrut(size));
163 	}
164 
165 	return *this;
166 }
167 
168 // SetInsets
169 BGroupLayoutBuilder&
170 BGroupLayoutBuilder::SetInsets(float left, float top, float right, float bottom)
171 {
172 	if (BGroupLayout* layout = TopLayout())
173 		layout->SetInsets(left, top, right, bottom);
174 
175 	return *this;
176 }
177 
178 // cast operator BGroupLayout*
179 BGroupLayoutBuilder::operator BGroupLayout*()
180 {
181 	return fRootLayout;
182 }
183 
184 // cast operator BView*
185 BGroupLayoutBuilder::operator BView*()
186 {
187 	return fRootLayout->View();
188 }
189 
190 // _PushLayout
191 bool
192 BGroupLayoutBuilder::_PushLayout(BGroupLayout* layout)
193 {
194 	return fLayoutStack.AddItem(layout);
195 }
196 
197 // _PopLayout
198 void
199 BGroupLayoutBuilder::_PopLayout()
200 {
201 	int32 count = fLayoutStack.CountItems();
202 	if (count > 0)
203 		fLayoutStack.RemoveItem(count - 1);
204 }
205