Mercurial > projects > dwt-addons
annotate dwtx/ui/forms/widgets/ColumnLayout.d @ 192:c3583c6ec027
Added missing default cases for switch statements
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 03 Nov 2008 22:52:26 +0100 |
parents | 5d489b9f966c |
children |
rev | line source |
---|---|
75 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2007 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 * dinko.ivanov@sap.com - patch #70790 | |
11 * Port to the D programming language: | |
12 * Frank Benoit <benoit@tionex.de> | |
13 *******************************************************************************/ | |
14 module dwtx.ui.forms.widgets.ColumnLayout; | |
15 | |
16 import dwtx.ui.forms.widgets.ILayoutExtension; | |
17 import dwtx.ui.forms.widgets.ColumnLayoutData; | |
18 | |
19 import dwt.DWT; | |
20 import dwt.graphics.Point; | |
21 import dwt.graphics.Rectangle; | |
22 import dwt.widgets.Composite; | |
23 import dwt.widgets.Control; | |
24 import dwt.widgets.Layout; | |
25 | |
26 import dwt.dwthelper.utils; | |
27 | |
28 /** | |
29 * This layout manager arranges children of the composite parent in vertical | |
30 * columns. All the columns are identical size and children are stretched | |
31 * horizontally to fill the column width. The goal is to give layout some | |
32 * reasonable range of column numbers to allow it to handle various parent | |
33 * widths. That way, column number will drop to the lowest number in the range | |
34 * when width decreases, and grow up to the highest number in the range when | |
35 * allowed by the parent width. | |
36 * <p> | |
37 * In addition, the layout attempts to 'fill the space' equally i.e. to avoid | |
38 * large gaps at the and of the last column. | |
39 * <p> | |
40 * Child controls are layed out according to their 'natural' (preferred) size. | |
41 * For 'stretchy' controls that do not have natural preferred size, it is | |
42 * possible to set width and/or height hints using ColumnLayoutData objects. | |
43 * | |
44 * @see ColumnLayoutData | |
45 * @since 3.0 | |
46 */ | |
47 public final class ColumnLayout : Layout, ILayoutExtension { | |
48 /** | |
49 * Minimum number of columns (default is 1). | |
50 */ | |
51 public int minNumColumns = 1; | |
52 /** | |
53 * Maximum number of columns (default is 3). | |
54 */ | |
55 public int maxNumColumns = 3; | |
56 /** | |
57 * Horizontal spacing between columns (default is 5). | |
58 */ | |
59 public int horizontalSpacing = 5; | |
60 /** | |
61 * Vertical spacing between controls (default is 5). | |
62 */ | |
63 public int verticalSpacing = 5; | |
64 /** | |
65 * Top margin (default is 5). | |
66 */ | |
67 public int topMargin = 5; | |
68 /** | |
69 * Left margin (default is 5). | |
70 */ | |
71 public int leftMargin = 5; | |
72 /** | |
73 * Bottom margin (default is 5). | |
74 */ | |
75 public int bottomMargin = 5; | |
76 /** | |
77 * Right margin (default is 5). | |
78 */ | |
79 public int rightMargin = 5; | |
80 | |
81 /** | |
82 * Creates a new instance of the column layout. | |
83 */ | |
84 public this() { | |
85 } | |
86 | |
87 /+protected+/ Point computeSize(Composite composite, int wHint, int hHint, bool flushCache) { | |
88 if (wHint is 0) | |
89 return computeSize(composite, wHint, hHint, minNumColumns); | |
90 else if (wHint is DWT.DEFAULT) | |
91 return computeSize(composite, wHint, hHint, maxNumColumns); | |
92 else | |
93 return computeSize(composite, wHint, hHint, -1); | |
94 } | |
95 | |
96 private Point computeSize(Composite parent, int wHint, int hHint, int ncolumns) { | |
97 Control[] children = parent.getChildren(); | |
98 int cwidth = 0; | |
99 int cheight = 0; | |
100 Point[] sizes = new Point[children.length]; | |
101 | |
102 int cwHint = DWT.DEFAULT; | |
103 if (ncolumns !is -1) { | |
104 cwHint = wHint - leftMargin - rightMargin - (ncolumns - 1) * horizontalSpacing; | |
105 if (cwHint <= 0) | |
106 cwHint = 0; | |
107 else | |
108 cwHint /= ncolumns; | |
109 } | |
110 | |
111 for (int i = 0; i < children.length; i++) { | |
112 sizes[i] = computeControlSize(children[i], cwHint); | |
113 cwidth = Math.max(cwidth, sizes[i].x); | |
114 cheight += sizes[i].y; | |
115 } | |
116 if (ncolumns is -1) { | |
117 // must compute | |
118 ncolumns = (wHint - leftMargin - rightMargin - horizontalSpacing) / (cwidth + horizontalSpacing); | |
119 ncolumns = Math.min(ncolumns, children.length); | |
120 ncolumns = Math.max(ncolumns, minNumColumns); | |
121 ncolumns = Math.min(ncolumns, maxNumColumns); | |
122 } | |
123 int perColHeight = cheight / ncolumns; | |
124 if (cheight % ncolumns !is 0) | |
125 perColHeight++; | |
126 int colHeight = 0; | |
127 int[] heights = new int[ncolumns]; | |
128 int ncol = 0; | |
129 | |
130 bool fillIn = false; | |
131 | |
132 for (int i = 0; i < sizes.length; i++) { | |
133 int childHeight = sizes[i].y; | |
134 if (i>0 && colHeight + childHeight > perColHeight) { | |
135 heights[ncol] = colHeight; | |
136 ncol++; | |
137 if (ncol is ncolumns || fillIn) { | |
138 // overflow - start filling in | |
139 fillIn = true; | |
140 ncol = findShortestColumn(heights); | |
141 } | |
142 colHeight = heights[ncol]; | |
143 } | |
144 if (colHeight > 0) | |
145 colHeight += verticalSpacing; | |
146 colHeight += childHeight; | |
147 } | |
148 heights[ncol] = Math.max(heights[ncol],colHeight); | |
149 | |
150 Point size = new Point(0, 0); | |
151 for (int i = 0; i < ncolumns; i++) { | |
152 size.y = Math.max(size.y, heights[i]); | |
153 } | |
154 size.x = cwidth * ncolumns + (ncolumns - 1) * horizontalSpacing; | |
155 size.x += leftMargin + rightMargin; | |
156 //System.out.println("ColumnLayout: whint="+wHint+", size.x="+size.x); | |
157 size.y += topMargin + bottomMargin; | |
158 return size; | |
159 } | |
160 | |
161 private Point computeControlSize(Control c, int wHint) { | |
162 ColumnLayoutData cd = cast(ColumnLayoutData) c.getLayoutData(); | |
163 int widthHint = cd !is null ? cd.widthHint : wHint; | |
164 int heightHint = cd !is null ? cd.heightHint : DWT.DEFAULT; | |
165 return c.computeSize(widthHint, heightHint); | |
166 } | |
167 | |
168 private int findShortestColumn(int[] heights) { | |
169 int result = 0; | |
170 int height = Integer.MAX_VALUE; | |
171 for (int i = 0; i < heights.length; i++) { | |
172 if (height > heights[i]) { | |
173 height = heights[i]; | |
174 result = i; | |
175 } | |
176 } | |
177 return result; | |
178 } | |
179 | |
180 /* | |
181 * (non-Javadoc) | |
182 * | |
183 * @see dwt.widgets.Layout#layout(dwt.widgets.Composite, | |
184 * bool) | |
185 */ | |
186 protected void layout(Composite parent, bool flushCache) { | |
187 Control[] children = parent.getChildren(); | |
188 Rectangle carea = parent.getClientArea(); | |
189 int cwidth = 0; | |
190 int cheight = 0; | |
191 Point[] sizes = new Point[children.length]; | |
192 for (int i = 0; i < children.length; i++) { | |
193 sizes[i] = computeControlSize(children[i], DWT.DEFAULT); | |
194 cwidth = Math.max(cwidth, sizes[i].x); | |
195 cheight += sizes[i].y; | |
196 } | |
197 int ncolumns = (carea.width - leftMargin - rightMargin - horizontalSpacing) / (cwidth + horizontalSpacing); | |
198 ncolumns = Math.min(ncolumns, children.length); | |
199 ncolumns = Math.max(ncolumns, minNumColumns); | |
200 ncolumns = Math.min(ncolumns, maxNumColumns); | |
201 int realWidth = (carea.width - leftMargin - rightMargin + horizontalSpacing) / ncolumns - horizontalSpacing; | |
202 // int childrenPerColumn = children.length / ncolumns; | |
203 // if (children.length % ncolumns !is 0) | |
204 // childrenPerColumn++; | |
205 // int colWidth = 0; | |
206 | |
207 int fillWidth = Math.max(cwidth, realWidth); | |
208 | |
209 int perColHeight = cheight / ncolumns; | |
210 if (cheight % ncolumns !is 0) | |
211 perColHeight++; | |
212 | |
213 int colHeight = 0; | |
214 int[] heights = new int[ncolumns]; | |
215 int ncol = 0; | |
216 int x = leftMargin; | |
217 bool fillIn = false; | |
218 | |
219 for (int i = 0; i < sizes.length; i++) { | |
220 Control child = children[i]; | |
221 Point csize = sizes[i]; | |
222 ColumnLayoutData cd = cast(ColumnLayoutData) child.getLayoutData(); | |
223 int align_ = cd !is null ? cd.horizontalAlignment : ColumnLayoutData.FILL; | |
224 int childWidth = align_ is ColumnLayoutData.FILL ? fillWidth : csize.x; | |
225 | |
226 if (i>0 && colHeight + csize.y > perColHeight) { | |
227 heights[ncol] = colHeight; | |
228 if (fillIn || ncol is ncolumns-1) { | |
229 // overflow - start filling in | |
230 fillIn = true; | |
231 ncol = findShortestColumn(heights); | |
232 | |
233 x = leftMargin + ncol * (fillWidth + horizontalSpacing); | |
234 | |
235 } | |
236 else { | |
237 ncol++; | |
238 x += fillWidth + horizontalSpacing; | |
239 } | |
240 colHeight = heights[ncol]; | |
241 } | |
242 if (colHeight > 0) | |
243 colHeight += verticalSpacing; | |
244 | |
245 | |
246 switch (align_) { | |
247 case ColumnLayoutData.LEFT : | |
248 case ColumnLayoutData.FILL : | |
249 child.setBounds(x, topMargin+colHeight, childWidth, csize.y); | |
250 break; | |
251 case ColumnLayoutData.RIGHT : | |
252 child.setBounds(x + fillWidth - childWidth, topMargin+colHeight, childWidth, csize.y); | |
253 break; | |
254 case ColumnLayoutData.CENTER : | |
255 child.setBounds(x + fillWidth / 2 - childWidth / 2, topMargin+colHeight, childWidth, csize.y); | |
256 break; | |
192
c3583c6ec027
Added missing default cases for switch statements
Frank Benoit <benoit@tionex.de>
parents:
75
diff
changeset
|
257 default: |
75 | 258 } |
259 | |
260 colHeight += csize.y; | |
261 } | |
262 } | |
263 | |
264 /* | |
265 * (non-Javadoc) | |
266 * | |
267 * @see dwtx.ui.forms.widgets.ILayoutExtension#computeMaximumWidth(dwt.widgets.Composite, | |
268 * bool) | |
269 */ | |
270 public int computeMaximumWidth(Composite parent, bool changed) { | |
271 return computeSize(parent, DWT.DEFAULT, DWT.DEFAULT, changed).x; | |
272 } | |
273 | |
274 /* | |
275 * (non-Javadoc) | |
276 * | |
277 * @see dwtx.ui.forms.widgets.ILayoutExtension#computeMinimumWidth(dwt.widgets.Composite, | |
278 * bool) | |
279 */ | |
280 public int computeMinimumWidth(Composite parent, bool changed) { | |
281 return computeSize(parent, 0, DWT.DEFAULT, changed).x; | |
282 } | |
283 } |