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