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