Mercurial > projects > dwt-addons
comparison dwtx/draw2d/BorderLayout.d @ 98:95307ad235d9
Added Draw2d code, still work in progress
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sun, 03 Aug 2008 00:52:14 +0200 |
parents | |
children | 2d6540440fe6 |
comparison
equal
deleted
inserted
replaced
96:b492ba44e44d | 98:95307ad235d9 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2005 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.draw2d.BorderLayout; | |
14 | |
15 import dwt.dwthelper.utils; | |
16 | |
17 import dwtx.draw2d.geometry.Dimension; | |
18 import dwtx.draw2d.geometry.Insets; | |
19 import dwtx.draw2d.geometry.Rectangle; | |
20 import dwtx.draw2d.AbstractHintLayout; | |
21 import dwtx.draw2d.IFigure; | |
22 import dwtx.draw2d.PositionConstants; | |
23 | |
24 /** | |
25 * @author Pratik Shah | |
26 */ | |
27 public class BorderLayout | |
28 : AbstractHintLayout | |
29 { | |
30 | |
31 /** | |
32 * Constant to be used as a constraint for child figures | |
33 */ | |
34 public static const Integer CENTER; | |
35 /** | |
36 * Constant to be used as a constraint for child figures | |
37 */ | |
38 public static const Integer TOP; | |
39 /** | |
40 * Constant to be used as a constraint for child figures | |
41 */ | |
42 public static const Integer BOTTOM; | |
43 /** | |
44 * Constant to be used as a constraint for child figures | |
45 */ | |
46 public static const Integer LEFT; | |
47 /** | |
48 * Constant to be used as a constraint for child figures | |
49 */ | |
50 public static const Integer RIGHT; | |
51 | |
52 static this(){ | |
53 CENTER = new Integer(PositionConstants.CENTER); | |
54 TOP = new Integer(PositionConstants.TOP); | |
55 BOTTOM = new Integer(PositionConstants.BOTTOM); | |
56 LEFT = new Integer(PositionConstants.LEFT); | |
57 RIGHT = new Integer(PositionConstants.RIGHT); | |
58 } | |
59 | |
60 private IFigure center, left, top, bottom, right; | |
61 private int vGap = 0, hGap = 0; | |
62 | |
63 /** | |
64 * @see dwtx.draw2d.AbstractHintLayout#calculateMinimumSize(IFigure, int, int) | |
65 */ | |
66 protected Dimension calculateMinimumSize(IFigure container, int wHint, int hHint) { | |
67 int minWHint = 0, minHHint = 0; | |
68 if (wHint < 0) { | |
69 minWHint = -1; | |
70 } | |
71 if (hHint < 0) { | |
72 minHHint = -1; | |
73 } | |
74 Insets border = container.getInsets(); | |
75 wHint = Math.max(minWHint, wHint - border.getWidth()); | |
76 hHint = Math.max(minHHint, hHint - border.getHeight()); | |
77 Dimension minSize = new Dimension(); | |
78 int middleRowWidth = 0, middleRowHeight = 0; | |
79 int rows = 0, columns = 0; | |
80 | |
81 if (top !is null && top.isVisible()) { | |
82 Dimension childSize = top.getMinimumSize(wHint, hHint); | |
83 hHint = Math.max(minHHint, hHint - (childSize.height + vGap)); | |
84 minSize.setSize(childSize); | |
85 rows += 1; | |
86 } | |
87 if (bottom !is null && bottom.isVisible()) { | |
88 Dimension childSize = bottom.getMinimumSize(wHint, hHint); | |
89 hHint = Math.max(minHHint, hHint - (childSize.height + vGap)); | |
90 minSize.width = Math.max(minSize.width, childSize.width); | |
91 minSize.height += childSize.height; | |
92 rows += 1; | |
93 } | |
94 if (left !is null && left.isVisible()) { | |
95 Dimension childSize = left.getMinimumSize(wHint, hHint); | |
96 middleRowWidth = childSize.width; | |
97 middleRowHeight = childSize.height; | |
98 wHint = Math.max(minWHint, wHint - (childSize.width + hGap)); | |
99 columns += 1; | |
100 } | |
101 if (right !is null && right.isVisible()) { | |
102 Dimension childSize = right.getMinimumSize(wHint, hHint); | |
103 middleRowWidth += childSize.width; | |
104 middleRowHeight = Math.max(childSize.height, middleRowHeight); | |
105 wHint = Math.max(minWHint, wHint - (childSize.width + hGap)); | |
106 columns += 1; | |
107 } | |
108 if (center !is null && center.isVisible()) { | |
109 Dimension childSize = center.getMinimumSize(wHint, hHint); | |
110 middleRowWidth += childSize.width; | |
111 middleRowHeight = Math.max(childSize.height, middleRowHeight); | |
112 columns += 1; | |
113 } | |
114 | |
115 rows += columns > 0 ? 1 : 0; | |
116 // Add spacing, insets, and the size of the middle row | |
117 minSize.height += middleRowHeight + border.getHeight() + ((rows - 1) * vGap); | |
118 minSize.width = Math.max(minSize.width, middleRowWidth) + border.getWidth() | |
119 + ((columns - 1) * hGap); | |
120 | |
121 return minSize; | |
122 } | |
123 | |
124 /** | |
125 * @see AbstractLayout#calculatePreferredSize(IFigure, int, int) | |
126 */ | |
127 protected Dimension calculatePreferredSize(IFigure container, int wHint, int hHint) { | |
128 int minWHint = 0, minHHint = 0; | |
129 if (wHint < 0) | |
130 minWHint = -1; | |
131 | |
132 if (hHint < 0) | |
133 minHHint = -1; | |
134 | |
135 Insets border = container.getInsets(); | |
136 wHint = Math.max(minWHint, wHint - border.getWidth()); | |
137 hHint = Math.max(minHHint, hHint - border.getHeight()); | |
138 Dimension prefSize = new Dimension(); | |
139 int middleRowWidth = 0, middleRowHeight = 0; | |
140 int rows = 0, columns = 0; | |
141 | |
142 if (top !is null && top.isVisible()) { | |
143 Dimension childSize = top.getPreferredSize(wHint, hHint); | |
144 hHint = Math.max(minHHint, hHint - (childSize.height + vGap)); | |
145 prefSize.setSize(childSize); | |
146 rows += 1; | |
147 } | |
148 if (bottom !is null && bottom.isVisible()) { | |
149 Dimension childSize = bottom.getPreferredSize(wHint, hHint); | |
150 hHint = Math.max(minHHint, hHint - (childSize.height + vGap)); | |
151 prefSize.width = Math.max(prefSize.width, childSize.width); | |
152 prefSize.height += childSize.height; | |
153 rows += 1; | |
154 } | |
155 if (left !is null && left.isVisible()) { | |
156 Dimension childSize = left.getPreferredSize(wHint, hHint); | |
157 middleRowWidth = childSize.width; | |
158 middleRowHeight = childSize.height; | |
159 wHint = Math.max(minWHint, wHint - (childSize.width + hGap)); | |
160 columns += 1; | |
161 } | |
162 if (right !is null && right.isVisible()) { | |
163 Dimension childSize = right.getPreferredSize(wHint, hHint); | |
164 middleRowWidth += childSize.width; | |
165 middleRowHeight = Math.max(childSize.height, middleRowHeight); | |
166 wHint = Math.max(minWHint, wHint - (childSize.width + hGap)); | |
167 columns += 1; | |
168 } | |
169 if (center !is null && center.isVisible()) { | |
170 Dimension childSize = center.getPreferredSize(wHint, hHint); | |
171 middleRowWidth += childSize.width; | |
172 middleRowHeight = Math.max(childSize.height, middleRowHeight); | |
173 columns += 1; | |
174 } | |
175 | |
176 rows += columns > 0 ? 1 : 0; | |
177 // Add spacing, insets, and the size of the middle row | |
178 prefSize.height += middleRowHeight + border.getHeight() + ((rows - 1) * vGap); | |
179 prefSize.width = Math.max(prefSize.width, middleRowWidth) + border.getWidth() | |
180 + ((columns - 1) * hGap); | |
181 | |
182 return prefSize; | |
183 } | |
184 | |
185 /** | |
186 * @see dwtx.draw2d.LayoutManager#layout(IFigure) | |
187 */ | |
188 public void layout(IFigure container) { | |
189 Rectangle area = container.getClientArea(); | |
190 Rectangle rect = new Rectangle(); | |
191 | |
192 Dimension childSize; | |
193 | |
194 if (top !is null && top.isVisible()) { | |
195 childSize = top.getPreferredSize(area.width, -1); | |
196 rect.setLocation(area.x, area.y); | |
197 rect.setSize(childSize); | |
198 rect.width = area.width; | |
199 top.setBounds(rect); | |
200 area.y += rect.height + vGap; | |
201 area.height -= rect.height + vGap; | |
202 } | |
203 if (bottom !is null && bottom.isVisible()) { | |
204 childSize = bottom.getPreferredSize(Math.max(area.width, 0), -1); | |
205 rect.setSize(childSize); | |
206 rect.width = area.width; | |
207 rect.setLocation(area.x, area.y + area.height - rect.height); | |
208 bottom.setBounds(rect); | |
209 area.height -= rect.height + vGap; | |
210 } | |
211 if (left !is null && left.isVisible()) { | |
212 childSize = left.getPreferredSize(-1, Math.max(0, area.height)); | |
213 rect.setLocation(area.x, area.y); | |
214 rect.width = childSize.width; | |
215 rect.height = Math.max(0, area.height); | |
216 left.setBounds(rect); | |
217 area.x += rect.width + hGap; | |
218 area.width -= rect.width + hGap; | |
219 } | |
220 if (right !is null && right.isVisible()) { | |
221 childSize = right.getPreferredSize(-1, Math.max(0, area.height)); | |
222 rect.width = childSize.width; | |
223 rect.height = Math.max(0, area.height); | |
224 rect.setLocation(area.x + area.width - rect.width, area.y); | |
225 right.setBounds(rect); | |
226 area.width -= rect.width + hGap; | |
227 } | |
228 if (center !is null && center.isVisible()) { | |
229 if (area.width < 0) | |
230 area.width = 0; | |
231 if (area.height < 0) | |
232 area.height = 0; | |
233 center.setBounds(area); | |
234 } | |
235 } | |
236 | |
237 /** | |
238 * @see dwtx.draw2d.AbstractLayout#remove(IFigure) | |
239 */ | |
240 public void remove(IFigure child) { | |
241 if (center is child) { | |
242 center = null; | |
243 } else if (top is child) { | |
244 top = null; | |
245 } else if (bottom is child) { | |
246 bottom = null; | |
247 } else if (right is child) { | |
248 right = null; | |
249 } else if (left is child) { | |
250 left = null; | |
251 } | |
252 } | |
253 | |
254 /** | |
255 * Sets the location of hte given child in this layout. Valid constraints: | |
256 * <UL> | |
257 * <LI>{@link #CENTER}</LI> | |
258 * <LI>{@link #TOP}</LI> | |
259 * <LI>{@link #BOTTOM}</LI> | |
260 * <LI>{@link #LEFT}</LI> | |
261 * <LI>{@link #RIGHT}</LI> | |
262 * <LI><code>null</code> (to remove a child's constraint)</LI> | |
263 * </UL> | |
264 * | |
265 * <p> | |
266 * Ensure that the given Figure is indeed a child of the Figure on which this layout has | |
267 * been set. Proper behaviour cannot be guaranteed if that is not the case. Also ensure | |
268 * that every child has a valid constraint. | |
269 * </p> | |
270 * <p> | |
271 * Passing a <code>null</code> constraint will invoke {@link #remove(IFigure)}. | |
272 * </p> | |
273 * <p> | |
274 * If the given child was assigned another constraint earlier, it will be re-assigned to | |
275 * the new constraint. If there is another child with the given constraint, it will be | |
276 * over-ridden so that the given child now has that constraint. | |
277 * </p> | |
278 * | |
279 * @see dwtx.draw2d.AbstractLayout#setConstraint(IFigure, Object) | |
280 */ | |
281 public void setConstraint(IFigure child, Object constraint) { | |
282 remove(child); | |
283 super.setConstraint(child, constraint); | |
284 if (constraint is null) { | |
285 return; | |
286 } | |
287 | |
288 switch ((cast(Integer) constraint).intValue()) { | |
289 case PositionConstants.CENTER : | |
290 center = child; | |
291 break; | |
292 case PositionConstants.TOP : | |
293 top = child; | |
294 break; | |
295 case PositionConstants.BOTTOM : | |
296 bottom = child; | |
297 break; | |
298 case PositionConstants.RIGHT : | |
299 right = child; | |
300 break; | |
301 case PositionConstants.LEFT : | |
302 left = child; | |
303 break; | |
304 default : | |
305 break; | |
306 } | |
307 } | |
308 | |
309 /** | |
310 * Sets the horizontal spacing to be used between the children. Default is 0. | |
311 * | |
312 * @param gap The horizonal spacing | |
313 */ | |
314 public void setHorizontalSpacing(int gap) { | |
315 hGap = gap; | |
316 } | |
317 | |
318 /** | |
319 * Sets the vertical spacing ot be used between the children. Default is 0. | |
320 * | |
321 * @param gap The vertical spacing | |
322 */ | |
323 public void setVerticalSpacing(int gap) { | |
324 vGap = gap; | |
325 } | |
326 | |
327 } |