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