xref: /haiku/src/kits/interface/GroupLayout.cpp (revision b55a57da7173b9af0432bd3e148d03f06161d036)
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 <GroupLayout.h>
7 
8 #include <LayoutItem.h>
9 
10 
11 // ItemLayoutData
12 struct BGroupLayout::ItemLayoutData {
13 	float	weight;
14 
15 	ItemLayoutData()
16 		: weight(1)
17 	{
18 	}
19 };
20 
21 // constructor
22 BGroupLayout::BGroupLayout(enum orientation orientation, float spacing)
23 	: BTwoDimensionalLayout(),
24 	  fOrientation(orientation)
25 {
26 	SetSpacing(spacing);
27 }
28 
29 // destructor
30 BGroupLayout::~BGroupLayout()
31 {
32 }
33 
34 // Spacing
35 float
36 BGroupLayout::Spacing() const
37 {
38 	return fHSpacing;
39 }
40 
41 // SetSpacing
42 void
43 BGroupLayout::SetSpacing(float spacing)
44 {
45 	if (spacing != fHSpacing) {
46 		fHSpacing = spacing;
47 		fVSpacing = spacing;
48 		InvalidateLayout();
49 	}
50 }
51 
52 // Orientation
53 orientation
54 BGroupLayout::Orientation() const
55 {
56 	return fOrientation;
57 }
58 
59 // SetOrientation
60 void
61 BGroupLayout::SetOrientation(enum orientation orientation)
62 {
63 	if (orientation != fOrientation) {
64 		fOrientation = orientation;
65 
66 		InvalidateLayout();
67 	}
68 }
69 
70 // ItemWeight
71 float
72 BGroupLayout::ItemWeight(int32 index) const
73 {
74 	if (index < 0 || index >= CountItems())
75 		return 0;
76 
77 	ItemLayoutData* data = _LayoutDataForItem(ItemAt(index));
78 	return (data ? data->weight : 0);
79 }
80 
81 // SetItemWeight
82 void
83 BGroupLayout::SetItemWeight(int32 index, float weight)
84 {
85 	if (index < 0 || index >= CountItems())
86 		return;
87 
88 	if (ItemLayoutData* data = _LayoutDataForItem(ItemAt(index)))
89 		data->weight = weight;
90 
91 	InvalidateLayout();
92 }
93 
94 // AddView
95 BLayoutItem*
96 BGroupLayout::AddView(BView* child)
97 {
98 	return BTwoDimensionalLayout::AddView(child);
99 }
100 
101 // AddView
102 BLayoutItem*
103 BGroupLayout::AddView(int32 index, BView* child)
104 {
105 	return BTwoDimensionalLayout::AddView(index, child);
106 }
107 
108 // AddView
109 BLayoutItem*
110 BGroupLayout::AddView(BView* child, float weight)
111 {
112 	return AddView(-1, child, weight);
113 }
114 
115 // AddView
116 BLayoutItem*
117 BGroupLayout::AddView(int32 index, BView* child, float weight)
118 {
119 	BLayoutItem* item = AddView(index, child);
120 	if (ItemLayoutData* data = _LayoutDataForItem(item))
121 		data->weight = weight;
122 
123 	return item;
124 }
125 
126 // AddItem
127 bool
128 BGroupLayout::AddItem(BLayoutItem* item)
129 {
130 	return BTwoDimensionalLayout::AddItem(item);
131 }
132 
133 // AddItem
134 bool
135 BGroupLayout::AddItem(int32 index, BLayoutItem* item)
136 {
137 	return BTwoDimensionalLayout::AddItem(index, item);
138 }
139 
140 // AddItem
141 bool
142 BGroupLayout::AddItem(BLayoutItem* item, float weight)
143 {
144 	return AddItem(-1, item, weight);
145 }
146 
147 // AddItem
148 bool
149 BGroupLayout::AddItem(int32 index, BLayoutItem* item, float weight)
150 {
151 	bool success = AddItem(index, item);
152 	if (success) {
153 		if (ItemLayoutData* data = _LayoutDataForItem(item))
154 			data->weight = weight;
155 	}
156 
157 	return success;
158 }
159 
160 // ItemAdded
161 void
162 BGroupLayout::ItemAdded(BLayoutItem* item)
163 {
164 	item->SetLayoutData(new ItemLayoutData);
165 }
166 
167 // ItemRemoved
168 void
169 BGroupLayout::ItemRemoved(BLayoutItem* item)
170 {
171 	if (ItemLayoutData* data = _LayoutDataForItem(item)) {
172 		item->SetLayoutData(NULL);
173 		delete data;
174 	}
175 }
176 
177 // PrepareItems
178 void
179 BGroupLayout::PrepareItems(enum orientation orientation)
180 {
181 	// filter the visible items
182 	fVisibleItems.MakeEmpty();
183 	int32 itemCount = CountItems();
184 	for (int i = 0; i < itemCount; i++) {
185 		BLayoutItem* item = ItemAt(i);
186 		if (item->IsVisible())
187 			fVisibleItems.AddItem(item);
188 	}
189 }
190 
191 // InternalCountColumns
192 int32
193 BGroupLayout::InternalCountColumns()
194 {
195 	return (fOrientation == B_HORIZONTAL ? fVisibleItems.CountItems() : 1);
196 }
197 
198 // InternalCountRows
199 int32
200 BGroupLayout::InternalCountRows()
201 {
202 	return (fOrientation == B_VERTICAL ? fVisibleItems.CountItems() : 1);
203 }
204 
205 // GetColumnRowConstraints
206 void
207 BGroupLayout::GetColumnRowConstraints(enum orientation orientation, int32 index,
208 	ColumnRowConstraints* constraints)
209 {
210 	if (index >= 0 && index < fVisibleItems.CountItems()) {
211 		BLayoutItem* item = (BLayoutItem*)fVisibleItems.ItemAt(index);
212 		constraints->min = -1;
213 		constraints->max = B_SIZE_UNLIMITED;
214 		if (ItemLayoutData* data = _LayoutDataForItem(item))
215 			constraints->weight = data->weight;
216 		else
217 			constraints->weight = 1;
218 	}
219 }
220 
221 // ItemDimensions
222 void
223 BGroupLayout::GetItemDimensions(BLayoutItem* item, Dimensions* dimensions)
224 {
225 	int32 index = fVisibleItems.IndexOf(item);
226 	if (index < 0)
227 		return;
228 
229 	if (fOrientation == B_HORIZONTAL) {
230 		dimensions->x = index;
231 		dimensions->y = 0;
232 		dimensions->width = 1;
233 		dimensions->height = 1;
234 	} else {
235 		dimensions->x = 0;
236 		dimensions->y = index;
237 		dimensions->width = 1;
238 		dimensions->height = 1;
239 	}
240 }
241 
242 // _LayoutDataForItem
243 BGroupLayout::ItemLayoutData*
244 BGroupLayout::_LayoutDataForItem(BLayoutItem* item) const
245 {
246 	if (!item)
247 		return NULL;
248 	return (ItemLayoutData*)item->LayoutData();
249 }
250