xref: /haiku/src/kits/interface/GroupLayoutBuilder.cpp (revision 1d6c7b6cb6f46c2672074ff137a18833d4dd3041)
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 // constructor
32 BGroupLayoutBuilder::BGroupLayoutBuilder(BGroupView* view)
33 	: fRootLayout(view->GroupLayout())
34 {
35 	_PushLayout(fRootLayout);
36 }
37 
38 // RootLayout
39 BGroupLayout*
40 BGroupLayoutBuilder::RootLayout() const
41 {
42 	return fRootLayout;
43 }
44 
45 // TopLayout
46 BGroupLayout*
47 BGroupLayoutBuilder::TopLayout() const
48 {
49 	int32 count = fLayoutStack.CountItems();
50 	return (count > 0
51 		? (BGroupLayout*)fLayoutStack.ItemAt(count - 1) : NULL);
52 }
53 
54 // GetTopLayout
55 BGroupLayoutBuilder&
56 BGroupLayoutBuilder::GetTopLayout(BGroupLayout** _layout)
57 {
58 	*_layout = TopLayout();
59 	return *this;
60 }
61 
62 // TopView
63 BView*
64 BGroupLayoutBuilder::TopView() const
65 {
66 	return TopLayout()->Owner();
67 }
68 
69 // GetTopView
70 BGroupLayoutBuilder&
71 BGroupLayoutBuilder::GetTopView(BView** _view)
72 {
73 	if (BGroupLayout* layout = TopLayout())
74 		*_view = layout->Owner();
75 	else
76 		*_view = NULL;
77 
78 	return *this;
79 }
80 
81 // Add
82 BGroupLayoutBuilder&
83 BGroupLayoutBuilder::Add(BView* view)
84 {
85 	if (BGroupLayout* layout = TopLayout())
86 		layout->AddView(view);
87 	return *this;
88 }
89 
90 // Add
91 BGroupLayoutBuilder&
92 BGroupLayoutBuilder::Add(BView* view, float weight)
93 {
94 	if (BGroupLayout* layout = TopLayout())
95 		layout->AddView(view, weight);
96 	return *this;
97 }
98 
99 // Add
100 BGroupLayoutBuilder&
101 BGroupLayoutBuilder::Add(BLayoutItem* item)
102 {
103 	if (BGroupLayout* layout = TopLayout())
104 		layout->AddItem(item);
105 	return *this;
106 }
107 
108 // Add
109 BGroupLayoutBuilder&
110 BGroupLayoutBuilder::Add(BLayoutItem* item, float weight)
111 {
112 	if (BGroupLayout* layout = TopLayout())
113 		layout->AddItem(item, weight);
114 	return *this;
115 }
116 
117 // AddGroup
118 BGroupLayoutBuilder&
119 BGroupLayoutBuilder::AddGroup(enum orientation orientation,
120 	float spacing, float weight)
121 {
122 	if (BGroupLayout* layout = TopLayout()) {
123 		BGroupView* group = new(nothrow) BGroupView(orientation, spacing);
124 		if (group) {
125 			if (layout->AddView(group, weight))
126 				_PushLayout(group->GroupLayout());
127 			else
128 				delete group;
129 		}
130 	}
131 
132 	return *this;
133 }
134 
135 // End
136 BGroupLayoutBuilder&
137 BGroupLayoutBuilder::End()
138 {
139 	_PopLayout();
140 	return *this;
141 }
142 
143 // AddGlue
144 BGroupLayoutBuilder&
145 BGroupLayoutBuilder::AddGlue(float weight)
146 {
147 	if (BGroupLayout* layout = TopLayout())
148 		layout->AddItem(BSpaceLayoutItem::CreateGlue(), weight);
149 
150 	return *this;
151 }
152 
153 // AddStrut
154 BGroupLayoutBuilder&
155 BGroupLayoutBuilder::AddStrut(float size)
156 {
157 	if (BGroupLayout* layout = TopLayout()) {
158 		if (layout->Orientation() == B_HORIZONTAL)
159 			layout->AddItem(BSpaceLayoutItem::CreateHorizontalStrut(size));
160 		else
161 			layout->AddItem(BSpaceLayoutItem::CreateVerticalStrut(size));
162 	}
163 
164 	return *this;
165 }
166 
167 // SetInsets
168 BGroupLayoutBuilder&
169 BGroupLayoutBuilder::SetInsets(float left, float top, float right, float bottom)
170 {
171 	if (BGroupLayout* layout = TopLayout())
172 		layout->SetInsets(left, top, right, bottom);
173 
174 	return *this;
175 }
176 
177 // cast operator BGroupLayout*
178 BGroupLayoutBuilder::operator BGroupLayout*()
179 {
180 	return fRootLayout;
181 }
182 
183 // _PushLayout
184 bool
185 BGroupLayoutBuilder::_PushLayout(BGroupLayout* layout)
186 {
187 	return fLayoutStack.AddItem(layout);
188 }
189 
190 // _PopLayout
191 void
192 BGroupLayoutBuilder::_PopLayout()
193 {
194 	int32 count = fLayoutStack.CountItems();
195 	if (count > 0)
196 		fLayoutStack.RemoveItem(count - 1);
197 }
198