1 /*
2 * Copyright 2012, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7 #include "ALMLayoutBuilder.h"
8
9
10 #include <Window.h>
11
12 #include "Area.h"
13
14
15 namespace BALM {
16
17
BALMLayoutBuilder(BView * view,float hSpacing,float vSpacing,BALMLayout * friendLayout)18 BALMLayoutBuilder::BALMLayoutBuilder(BView* view, float hSpacing,
19 float vSpacing, BALMLayout* friendLayout)
20 {
21 fLayout = new BALMLayout(hSpacing, vSpacing, friendLayout);
22 view->SetLayout(fLayout);
23 }
24
25
BALMLayoutBuilder(BView * view,BALMLayout * layout)26 BALMLayoutBuilder::BALMLayoutBuilder(BView* view, BALMLayout* layout)
27 {
28 fLayout = layout;
29 view->SetLayout(layout);
30 }
31
32
BALMLayoutBuilder(BWindow * window,float hSpacing,float vSpacing,BALMLayout * friendLayout)33 BALMLayoutBuilder::BALMLayoutBuilder(BWindow* window, float hSpacing,
34 float vSpacing, BALMLayout* friendLayout)
35 {
36 fLayout = new BALMLayout(hSpacing, vSpacing, friendLayout);
37 window->SetLayout(fLayout);
38
39 fLayout->Owner()->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
40 // TODO: we get a white background if we don't do this
41 }
42
43
BALMLayoutBuilder(BWindow * window,BALMLayout * layout)44 BALMLayoutBuilder::BALMLayoutBuilder(BWindow* window, BALMLayout* layout)
45 {
46 fLayout = layout;
47 window->SetLayout(fLayout);
48
49 fLayout->Owner()->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
50 // TODO: we get a white background if we don't do this
51 }
52
53
BALMLayoutBuilder(BALMLayout * layout)54 BALMLayoutBuilder::BALMLayoutBuilder(BALMLayout* layout)
55 {
56 fLayout = layout;
57 }
58
59
60 BALMLayoutBuilder&
Add(BView * view,XTab * left,YTab * top,XTab * right,YTab * bottom)61 BALMLayoutBuilder::Add(BView* view, XTab* left, YTab* top,
62 XTab* right, YTab* bottom)
63 {
64 Area* a = (fLayout->AddView(view, left, top, right, bottom));
65 _SetCurrentArea(a);
66 return *this;
67 }
68
69
70 BALMLayoutBuilder&
Add(BView * view,Row * row,Column * column)71 BALMLayoutBuilder::Add(BView* view, Row* row, Column* column)
72 {
73 _SetCurrentArea(fLayout->AddView(view, row, column));
74 return *this;
75 }
76
77
78 BALMLayoutBuilder&
Add(BLayoutItem * item,XTab * left,YTab * top,XTab * right,YTab * bottom)79 BALMLayoutBuilder::Add(BLayoutItem* item, XTab* left, YTab* top,
80 XTab* right, YTab* bottom)
81 {
82 _SetCurrentArea(fLayout->AddItem(item, left, top, right, bottom));
83 return *this;
84 }
85
86
87 BALMLayoutBuilder&
Add(BLayoutItem * item,Row * row,Column * column)88 BALMLayoutBuilder::Add(BLayoutItem* item, Row* row, Column* column)
89 {
90 fLayout->AddItem(item, row, column);
91 return *this;
92 }
93
94
95
96 BALMLayoutBuilder&
SetInsets(float insets)97 BALMLayoutBuilder::SetInsets(float insets)
98 {
99 fLayout->SetInsets(insets);
100 return *this;
101 }
102
103
104 BALMLayoutBuilder&
SetInsets(float horizontal,float vertical)105 BALMLayoutBuilder::SetInsets(float horizontal, float vertical)
106 {
107 fLayout->SetInsets(horizontal, vertical);
108 return *this;
109 }
110
111
112 BALMLayoutBuilder&
SetInsets(float left,float top,float right,float bottom)113 BALMLayoutBuilder::SetInsets(float left, float top, float right,
114 float bottom)
115 {
116 fLayout->SetInsets(left, top, right, bottom);
117 return *this;
118 }
119
120
121 BALMLayoutBuilder&
SetSpacing(float horizontal,float vertical)122 BALMLayoutBuilder::SetSpacing(float horizontal, float vertical)
123 {
124 fLayout->SetSpacing(horizontal, vertical);
125 return *this;
126 }
127
128
129 BALMLayoutBuilder&
StartingAt(BView * view)130 BALMLayoutBuilder::StartingAt(BView* view)
131 {
132 fAreaStack.MakeEmpty();
133 fAreaStack.AddItem(fLayout->AreaFor(view));
134 return *this;
135 }
136
137
138 BALMLayoutBuilder&
StartingAt(BLayoutItem * item)139 BALMLayoutBuilder::StartingAt(BLayoutItem* item)
140 {
141 fAreaStack.MakeEmpty();
142 fAreaStack.AddItem(fLayout->AreaFor(item));
143 return *this;
144 }
145
146
147 BALMLayoutBuilder&
AddToLeft(BView * view,XTab * _left,YTab * top,YTab * bottom)148 BALMLayoutBuilder::AddToLeft(BView* view, XTab* _left, YTab* top, YTab* bottom)
149 {
150 Area* currentArea = _CurrentArea();
151 BReference<XTab> left = _left;
152 if (_left == NULL)
153 left = fLayout->AddXTab();
154 XTab* right = currentArea->Left();
155 if (!top)
156 top = currentArea->Top();
157 if (!bottom)
158 bottom = currentArea->Bottom();
159
160 return Add(view, left, top, right, bottom);
161 }
162
163
164 BALMLayoutBuilder&
AddToRight(BView * view,XTab * _right,YTab * top,YTab * bottom)165 BALMLayoutBuilder::AddToRight(BView* view, XTab* _right, YTab* top,
166 YTab* bottom)
167 {
168 Area* currentArea = _CurrentArea();
169 XTab* left = currentArea->Right();
170 BReference<XTab> right = _right;
171 if (_right == NULL)
172 right = fLayout->AddXTab();
173 if (!top)
174 top = currentArea->Top();
175 if (!bottom)
176 bottom = currentArea->Bottom();
177
178 return Add(view, left, top, right, bottom);
179 }
180
181
182 BALMLayoutBuilder&
AddAbove(BView * view,YTab * _top,XTab * left,XTab * right)183 BALMLayoutBuilder::AddAbove(BView* view, YTab* _top, XTab* left,
184 XTab* right)
185 {
186 Area* currentArea = _CurrentArea();
187 if (!left)
188 left = currentArea->Left();
189 if (!right)
190 right = currentArea->Right();
191 BReference<YTab> top = _top;
192 if (_top == NULL)
193 top = fLayout->AddYTab();
194 YTab* bottom = currentArea->Top();
195
196 return Add(view, left, top, right, bottom);
197 }
198
199
200 BALMLayoutBuilder&
AddBelow(BView * view,YTab * _bottom,XTab * left,XTab * right)201 BALMLayoutBuilder::AddBelow(BView* view, YTab* _bottom, XTab* left,
202 XTab* right)
203 {
204 Area* currentArea = _CurrentArea();
205 if (!left)
206 left = currentArea->Left();
207 if (!right)
208 right = currentArea->Right();
209 YTab* top = currentArea->Bottom();
210 BReference<YTab> bottom = _bottom;
211 if (_bottom == NULL)
212 bottom = fLayout->AddYTab();
213
214 return Add(view, left, top, right, bottom);
215 }
216
217
218 BALMLayoutBuilder&
AddToLeft(BLayoutItem * item,XTab * _left,YTab * top,YTab * bottom)219 BALMLayoutBuilder::AddToLeft(BLayoutItem* item, XTab* _left, YTab* top,
220 YTab* bottom)
221 {
222 Area* currentArea = _CurrentArea();
223 BReference<XTab> left = _left;
224 if (_left == NULL)
225 left = fLayout->AddXTab();
226 XTab* right = currentArea->Left();
227 if (!top)
228 top = currentArea->Top();
229 if (!bottom)
230 bottom = currentArea->Bottom();
231
232 return Add(item, left, top, right, bottom);
233 }
234
235
236 BALMLayoutBuilder&
AddToRight(BLayoutItem * item,XTab * _right,YTab * top,YTab * bottom)237 BALMLayoutBuilder::AddToRight(BLayoutItem* item, XTab* _right, YTab* top,
238 YTab* bottom)
239 {
240 Area* currentArea = _CurrentArea();
241 XTab* left = currentArea->Right();
242 BReference<XTab> right = _right;
243 if (_right == NULL)
244 right = fLayout->AddXTab();
245 if (!top)
246 top = currentArea->Top();
247 if (!bottom)
248 bottom = currentArea->Bottom();
249
250 return Add(item, left, top, right, bottom);
251 }
252
253
254 BALMLayoutBuilder&
AddAbove(BLayoutItem * item,YTab * _top,XTab * left,XTab * right)255 BALMLayoutBuilder::AddAbove(BLayoutItem* item, YTab* _top, XTab* left,
256 XTab* right)
257 {
258 Area* currentArea = _CurrentArea();
259 if (!left)
260 left = currentArea->Left();
261 if (!right)
262 right = currentArea->Right();
263 BReference<YTab> top = _top;
264 if (_top == NULL)
265 top = fLayout->AddYTab();
266 YTab* bottom = currentArea->Top();
267
268 return Add(item, left, top, right, bottom);
269 }
270
271
272 BALMLayoutBuilder&
AddBelow(BLayoutItem * item,YTab * _bottom,XTab * left,XTab * right)273 BALMLayoutBuilder::AddBelow(BLayoutItem* item, YTab* _bottom, XTab* left,
274 XTab* right)
275 {
276 Area* currentArea = _CurrentArea();
277 if (!left)
278 left = currentArea->Left();
279 if (!right)
280 right = currentArea->Right();
281 YTab* top = currentArea->Bottom();
282 BReference<YTab> bottom = _bottom;
283 if (_bottom == NULL)
284 bottom = fLayout->AddYTab();
285
286 return Add(item, left, top, right, bottom);
287 }
288
289
290 BALMLayoutBuilder&
Push()291 BALMLayoutBuilder::Push()
292 {
293 fAreaStack.AddItem(_CurrentArea());
294 return *this;
295 }
296
297
298 BALMLayoutBuilder&
Pop()299 BALMLayoutBuilder::Pop()
300 {
301 if (fAreaStack.CountItems() > 0)
302 fAreaStack.RemoveItemAt(fAreaStack.CountItems() - 1);
303 return *this;
304 }
305
306
307 void
_SetCurrentArea(Area * area)308 BALMLayoutBuilder::_SetCurrentArea(Area* area)
309 {
310 if (fAreaStack.CountItems() > 0)
311 fAreaStack.ReplaceItem(fAreaStack.CountItems() - 1, area);
312 else
313 fAreaStack.AddItem(area);
314 }
315
316
317 Area*
_CurrentArea() const318 BALMLayoutBuilder::_CurrentArea() const
319 {
320 return fAreaStack.ItemAt(fAreaStack.CountItems() - 1);
321 }
322
323
324 };
325