xref: /haiku/src/kits/interface/GroupLayoutBuilder.cpp (revision 1acbe440b8dd798953bec31d18ee589aa3f71b73)
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 // GetTopView
64 BGroupLayoutBuilder&
65 BGroupLayoutBuilder::GetTopView(BView** _view)
66 {
67 	if (BGroupLayout* layout = TopLayout())
68 		*_view = layout->View();
69 	else
70 		*_view = NULL;
71 
72 	return *this;
73 }
74 
75 // Add
76 BGroupLayoutBuilder&
77 BGroupLayoutBuilder::Add(BView* view)
78 {
79 	if (BGroupLayout* layout = TopLayout())
80 		layout->AddView(view);
81 	return *this;
82 }
83 
84 // Add
85 BGroupLayoutBuilder&
86 BGroupLayoutBuilder::Add(BView* view, float weight)
87 {
88 	if (BGroupLayout* layout = TopLayout())
89 		layout->AddView(view, weight);
90 	return *this;
91 }
92 
93 // Add
94 BGroupLayoutBuilder&
95 BGroupLayoutBuilder::Add(BLayoutItem* item)
96 {
97 	if (BGroupLayout* layout = TopLayout())
98 		layout->AddItem(item);
99 	return *this;
100 }
101 
102 // Add
103 BGroupLayoutBuilder&
104 BGroupLayoutBuilder::Add(BLayoutItem* item, float weight)
105 {
106 	if (BGroupLayout* layout = TopLayout())
107 		layout->AddItem(item, weight);
108 	return *this;
109 }
110 
111 // AddGroup
112 BGroupLayoutBuilder&
113 BGroupLayoutBuilder::AddGroup(enum orientation orientation,
114 	float spacing, float weight)
115 {
116 	if (BGroupLayout* layout = TopLayout()) {
117 		BGroupView* group = new(nothrow) BGroupView(orientation, spacing);
118 		if (group) {
119 			if (layout->AddView(group, weight))
120 				_PushLayout(group->GroupLayout());
121 			else
122 				delete group;
123 		}
124 	}
125 
126 	return *this;
127 }
128 
129 // End
130 BGroupLayoutBuilder&
131 BGroupLayoutBuilder::End()
132 {
133 	_PopLayout();
134 	return *this;
135 }
136 
137 // AddGlue
138 BGroupLayoutBuilder&
139 BGroupLayoutBuilder::AddGlue(float weight)
140 {
141 	if (BGroupLayout* layout = TopLayout())
142 		layout->AddItem(BSpaceLayoutItem::CreateGlue(), weight);
143 
144 	return *this;
145 }
146 
147 // AddStrut
148 BGroupLayoutBuilder&
149 BGroupLayoutBuilder::AddStrut(float size)
150 {
151 	if (BGroupLayout* layout = TopLayout()) {
152 		if (layout->Orientation() == B_HORIZONTAL)
153 			layout->AddItem(BSpaceLayoutItem::CreateHorizontalStrut(size));
154 		else
155 			layout->AddItem(BSpaceLayoutItem::CreateVerticalStrut(size));
156 	}
157 
158 	return *this;
159 }
160 
161 // SetInsets
162 BGroupLayoutBuilder&
163 BGroupLayoutBuilder::SetInsets(float left, float top, float right, float bottom)
164 {
165 	if (BGroupLayout* layout = TopLayout())
166 		layout->SetInsets(left, top, right, bottom);
167 
168 	return *this;
169 }
170 
171 // cast operator BGroupLayout*
172 BGroupLayoutBuilder::operator BGroupLayout*()
173 {
174 	return fRootLayout;
175 }
176 
177 // cast operator BView*
178 BGroupLayoutBuilder::operator BView*()
179 {
180 	return fRootLayout->View();
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