25
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 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 org.eclipse.swt.layout.GridLayout;
|
|
14
|
|
15 import org.eclipse.swt.layout.GridData;
|
|
16 import org.eclipse.swt.SWT;
|
|
17 import org.eclipse.swt.graphics.Point;
|
|
18 import org.eclipse.swt.graphics.Rectangle;
|
|
19 import org.eclipse.swt.widgets.Control;
|
|
20 import org.eclipse.swt.widgets.Composite;
|
|
21 import org.eclipse.swt.widgets.Layout;
|
|
22 import org.eclipse.swt.widgets.Scrollable;
|
|
23
|
|
24
|
48
|
25 import java.lang.all;
|
|
26 version(Tango){
|
51
|
27 import tango.util.Convert;
|
48
|
28 } else { // Phobos
|
51
|
29 import std.conv;
|
48
|
30 }
|
25
|
31
|
|
32
|
|
33 /**
|
|
34 * Instances of this class lay out the control children of a
|
|
35 * <code>Composite</code> in a grid.
|
|
36 * <p>
|
|
37 * <code>GridLayout</code> has a number of configuration fields, and the
|
|
38 * controls it lays out can have an associated layout data object, called
|
|
39 * <code>GridData</code>. The power of <code>GridLayout</code> lies in the
|
|
40 * ability to configure <code>GridData</code> for each control in the layout.
|
|
41 * </p>
|
|
42 * <p>
|
|
43 * The following code creates a shell managed by a <code>GridLayout</code>
|
|
44 * with 3 columns:
|
|
45 * <pre>
|
|
46 * Display display = new Display();
|
|
47 * Shell shell = new Shell(display);
|
|
48 * GridLayout gridLayout = new GridLayout();
|
|
49 * gridLayout.numColumns = 3;
|
|
50 * shell.setLayout(gridLayout);
|
|
51 * </pre>
|
|
52 * The <code>numColumns</code> field is the most important field in a
|
|
53 * <code>GridLayout</code>. Widgets are laid out in columns from left
|
|
54 * to right, and a new row is created when <code>numColumns</code> + 1
|
|
55 * controls are added to the <code>Composite<code>.
|
|
56 * </p>
|
|
57 *
|
|
58 * @see GridData
|
|
59 * @see <a href="http://www.eclipse.org/swt/snippets/#gridlayout">GridLayout snippets</a>
|
|
60 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: LayoutExample</a>
|
|
61 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
|
|
62 */
|
|
63 public final class GridLayout : Layout {
|
|
64
|
|
65 /**
|
|
66 * numColumns specifies the number of cell columns in the layout.
|
|
67 * If numColumns has a value less than 1, the layout will not
|
|
68 * set the size and position of any controls.
|
|
69 *
|
|
70 * The default value is 1.
|
|
71 */
|
|
72 public int numColumns = 1;
|
|
73
|
|
74 /**
|
|
75 * makeColumnsEqualWidth specifies whether all columns in the layout
|
|
76 * will be forced to have the same width.
|
|
77 *
|
|
78 * The default value is false.
|
|
79 */
|
|
80 public bool makeColumnsEqualWidth = false;
|
|
81
|
|
82 /**
|
|
83 * marginWidth specifies the number of pixels of horizontal margin
|
|
84 * that will be placed along the left and right edges of the layout.
|
|
85 *
|
|
86 * The default value is 5.
|
|
87 */
|
|
88 public int marginWidth = 5;
|
|
89
|
|
90 /**
|
|
91 * marginHeight specifies the number of pixels of vertical margin
|
|
92 * that will be placed along the top and bottom edges of the layout.
|
|
93 *
|
|
94 * The default value is 5.
|
|
95 */
|
|
96 public int marginHeight = 5;
|
|
97
|
|
98 /**
|
|
99 * marginLeft specifies the number of pixels of horizontal margin
|
|
100 * that will be placed along the left edge of the layout.
|
|
101 *
|
|
102 * The default value is 0.
|
|
103 *
|
|
104 * @since 3.1
|
|
105 */
|
|
106 public int marginLeft = 0;
|
|
107
|
|
108 /**
|
|
109 * marginTop specifies the number of pixels of vertical margin
|
|
110 * that will be placed along the top edge of the layout.
|
|
111 *
|
|
112 * The default value is 0.
|
|
113 *
|
|
114 * @since 3.1
|
|
115 */
|
|
116 public int marginTop = 0;
|
|
117
|
|
118 /**
|
|
119 * marginRight specifies the number of pixels of horizontal margin
|
|
120 * that will be placed along the right edge of the layout.
|
|
121 *
|
|
122 * The default value is 0.
|
|
123 *
|
|
124 * @since 3.1
|
|
125 */
|
|
126 public int marginRight = 0;
|
|
127
|
|
128 /**
|
|
129 * marginBottom specifies the number of pixels of vertical margin
|
|
130 * that will be placed along the bottom edge of the layout.
|
|
131 *
|
|
132 * The default value is 0.
|
|
133 *
|
|
134 * @since 3.1
|
|
135 */
|
|
136 public int marginBottom = 0;
|
|
137
|
|
138 /**
|
|
139 * horizontalSpacing specifies the number of pixels between the right
|
|
140 * edge of one cell and the left edge of its neighbouring cell to
|
|
141 * the right.
|
|
142 *
|
|
143 * The default value is 5.
|
|
144 */
|
|
145 public int horizontalSpacing = 5;
|
|
146
|
|
147 /**
|
|
148 * verticalSpacing specifies the number of pixels between the bottom
|
|
149 * edge of one cell and the top edge of its neighbouring cell underneath.
|
|
150 *
|
|
151 * The default value is 5.
|
|
152 */
|
|
153 public int verticalSpacing = 5;
|
|
154
|
|
155 /**
|
|
156 * Constructs a new instance of this class.
|
|
157 */
|
|
158 public this () {}
|
|
159
|
|
160 /**
|
|
161 * Constructs a new instance of this class given the
|
|
162 * number of columns, and whether or not the columns
|
|
163 * should be forced to have the same width.
|
|
164 * If numColumns has a value less than 1, the layout will not
|
|
165 * set the size and position of any controls.
|
|
166 *
|
|
167 * @param numColumns the number of columns in the grid
|
|
168 * @param makeColumnsEqualWidth whether or not the columns will have equal width
|
|
169 *
|
|
170 * @since 2.0
|
|
171 */
|
|
172 public this (int numColumns, bool makeColumnsEqualWidth) {
|
|
173 this.numColumns = numColumns;
|
|
174 this.makeColumnsEqualWidth = makeColumnsEqualWidth;
|
|
175 }
|
|
176
|
|
177 override protected Point computeSize (Composite composite, int wHint, int hHint, bool flushCache_) {
|
|
178 Point size = layout (composite, false, 0, 0, wHint, hHint, flushCache_);
|
|
179 if (wHint !is SWT.DEFAULT) size.x = wHint;
|
|
180 if (hHint !is SWT.DEFAULT) size.y = hHint;
|
|
181 return size;
|
|
182 }
|
|
183
|
|
184 override protected bool flushCache (Control control) {
|
|
185 Object data = control.getLayoutData ();
|
|
186 if (data !is null) (cast(GridData) data).flushCache ();
|
|
187 return true;
|
|
188 }
|
|
189
|
|
190 GridData getData (Control [][] grid, int row, int column, int rowCount, int columnCount, bool first) {
|
|
191 Control control = grid [row] [column];
|
|
192 if (control !is null) {
|
|
193 GridData data = cast(GridData) control.getLayoutData ();
|
|
194 int hSpan = Math.max (1, Math.min (data.horizontalSpan, columnCount));
|
|
195 int vSpan = Math.max (1, data.verticalSpan);
|
|
196 int i = first ? row + vSpan - 1 : row - vSpan + 1;
|
|
197 int j = first ? column + hSpan - 1 : column - hSpan + 1;
|
|
198 if (0 <= i && i < rowCount) {
|
|
199 if (0 <= j && j < columnCount) {
|
|
200 if (control is grid [i][j]) return data;
|
|
201 }
|
|
202 }
|
|
203 }
|
|
204 return null;
|
|
205 }
|
|
206
|
|
207 override protected void layout (Composite composite, bool flushCache_) {
|
|
208 Rectangle rect = composite.getClientArea ();
|
|
209 layout (composite, true, rect.x, rect.y, rect.width, rect.height, flushCache_);
|
|
210 }
|
|
211
|
|
212 Point layout (Composite composite, bool move, int x, int y, int width, int height, bool flushCache_) {
|
|
213 if (numColumns < 1) {
|
|
214 return new Point (marginLeft + marginWidth * 2 + marginRight, marginTop + marginHeight * 2 + marginBottom);
|
|
215 }
|
|
216 Control [] children = composite.getChildren ();
|
|
217 int count = 0;
|
|
218 for (int i=0; i<children.length; i++) {
|
|
219 Control control = children [i];
|
|
220 GridData data = cast(GridData) control.getLayoutData ();
|
|
221 if (data is null || !data.exclude) {
|
|
222 children [count++] = children [i];
|
|
223 }
|
|
224 }
|
|
225 if (count is 0) {
|
|
226 return new Point (marginLeft + marginWidth * 2 + marginRight, marginTop + marginHeight * 2 + marginBottom);
|
|
227 }
|
|
228 for (int i=0; i<count; i++) {
|
|
229 Control child = children [i];
|
|
230 GridData data = cast(GridData) child.getLayoutData ();
|
|
231 if (data is null) child.setLayoutData (data = new GridData ());
|
|
232 if (flushCache_) data.flushCache ();
|
|
233 data.computeSize (child, data.widthHint, data.heightHint, flushCache_);
|
|
234 if (data.grabExcessHorizontalSpace && data.minimumWidth > 0) {
|
|
235 if (data.cacheWidth < data.minimumWidth) {
|
|
236 int trim = 0;
|
|
237 //TEMPORARY CODE
|
|
238 if ( auto sa = cast(Scrollable)child ) {
|
|
239 Rectangle rect = sa.computeTrim (0, 0, 0, 0);
|
|
240 trim = rect.width;
|
|
241 } else {
|
|
242 trim = child.getBorderWidth () * 2;
|
|
243 }
|
|
244 data.cacheWidth = data.cacheHeight = SWT.DEFAULT;
|
|
245 data.computeSize (child, Math.max (0, data.minimumWidth - trim), data.heightHint, false);
|
|
246 }
|
|
247 }
|
|
248 if (data.grabExcessVerticalSpace && data.minimumHeight > 0) {
|
|
249 data.cacheHeight = Math.max (data.cacheHeight, data.minimumHeight);
|
|
250 }
|
|
251 }
|
|
252
|
|
253 /* Build the grid */
|
|
254 int row = 0, column = 0, rowCount = 0, columnCount = numColumns;
|
|
255 Control [][] grid = new Control [][]( 4, columnCount );
|
|
256 for (int i=0; i<count; i++) {
|
|
257 Control child = children [i];
|
|
258 GridData data = cast(GridData) child.getLayoutData ();
|
|
259 int hSpan = Math.max (1, Math.min (data.horizontalSpan, columnCount));
|
|
260 int vSpan = Math.max (1, data.verticalSpan);
|
|
261 while (true) {
|
|
262 int lastRow = row + vSpan;
|
|
263 if (lastRow >= grid.length) {
|
|
264 Control [][] newGrid = new Control[][]( lastRow + 4, columnCount );
|
|
265 SimpleType!(Control[]).arraycopy (grid, 0, newGrid, 0, grid.length);
|
|
266 grid = newGrid;
|
|
267 }
|
|
268 if (grid [row] is null) {
|
|
269 grid [row] = new Control [columnCount];
|
|
270 }
|
|
271 while (column < columnCount && grid [row] [column] !is null) {
|
|
272 column++;
|
|
273 }
|
|
274 int endCount = column + hSpan;
|
|
275 if (endCount <= columnCount) {
|
|
276 int index = column;
|
|
277 while (index < endCount && grid [row] [index] is null) {
|
|
278 index++;
|
|
279 }
|
|
280 if (index is endCount) break;
|
|
281 column = index;
|
|
282 }
|
|
283 if (column + hSpan >= columnCount) {
|
|
284 column = 0;
|
|
285 row++;
|
|
286 }
|
|
287 }
|
|
288 for (int j=0; j<vSpan; j++) {
|
|
289 if (grid [row + j] is null) {
|
|
290 grid [row + j] = new Control [columnCount];
|
|
291 }
|
|
292 for (int k=0; k<hSpan; k++) {
|
|
293 grid [row + j] [column + k] = child;
|
|
294 }
|
|
295 }
|
|
296 rowCount = Math.max (rowCount, row + vSpan);
|
|
297 column += hSpan;
|
|
298 }
|
|
299
|
|
300 /* Column widths */
|
|
301 int availableWidth = width - horizontalSpacing * (columnCount - 1) - (marginLeft + marginWidth * 2 + marginRight);
|
|
302 int expandCount = 0;
|
|
303 int [] widths = new int [columnCount];
|
|
304 int [] minWidths = new int [columnCount];
|
|
305 bool [] expandColumn = new bool [columnCount];
|
|
306 for (int j=0; j<columnCount; j++) {
|
|
307 for (int i=0; i<rowCount; i++) {
|
|
308 GridData data = getData (grid, i, j, rowCount, columnCount, true);
|
|
309 if (data !is null) {
|
|
310 int hSpan = Math.max (1, Math.min (data.horizontalSpan, columnCount));
|
|
311 if (hSpan is 1) {
|
|
312 int w = data.cacheWidth + data.horizontalIndent;
|
|
313 widths [j] = Math.max (widths [j], w);
|
|
314 if (data.grabExcessHorizontalSpace) {
|
|
315 if (!expandColumn [j]) expandCount++;
|
|
316 expandColumn [j] = true;
|
|
317 }
|
|
318 if (!data.grabExcessHorizontalSpace || data.minimumWidth !is 0) {
|
|
319 w = !data.grabExcessHorizontalSpace || data.minimumWidth is SWT.DEFAULT ? data.cacheWidth : data.minimumWidth;
|
|
320 w += data.horizontalIndent;
|
|
321 minWidths [j] = Math.max (minWidths [j], w);
|
|
322 }
|
|
323 }
|
|
324 }
|
|
325 }
|
|
326 for (int i=0; i<rowCount; i++) {
|
|
327 GridData data = getData (grid, i, j, rowCount, columnCount, false);
|
|
328 if (data !is null) {
|
|
329 int hSpan = Math.max (1, Math.min (data.horizontalSpan, columnCount));
|
|
330 if (hSpan > 1) {
|
|
331 int spanWidth = 0, spanMinWidth = 0, spanExpandCount = 0;
|
|
332 for (int k=0; k<hSpan; k++) {
|
|
333 spanWidth += widths [j-k];
|
|
334 spanMinWidth += minWidths [j-k];
|
|
335 if (expandColumn [j-k]) spanExpandCount++;
|
|
336 }
|
|
337 if (data.grabExcessHorizontalSpace && spanExpandCount is 0) {
|
|
338 expandCount++;
|
|
339 expandColumn [j] = true;
|
|
340 }
|
|
341 int w = data.cacheWidth + data.horizontalIndent - spanWidth - (hSpan - 1) * horizontalSpacing;
|
|
342 if (w > 0) {
|
|
343 if (makeColumnsEqualWidth) {
|
|
344 int equalWidth = (w + spanWidth) / hSpan;
|
|
345 int remainder = (w + spanWidth) % hSpan, last = -1;
|
|
346 for (int k = 0; k < hSpan; k++) {
|
|
347 widths [last=j-k] = Math.max (equalWidth, widths [j-k]);
|
|
348 }
|
|
349 if (last > -1) widths [last] += remainder;
|
|
350 } else {
|
|
351 if (spanExpandCount is 0) {
|
|
352 widths [j] += w;
|
|
353 } else {
|
|
354 int delta = w / spanExpandCount;
|
|
355 int remainder = w % spanExpandCount, last = -1;
|
|
356 for (int k = 0; k < hSpan; k++) {
|
|
357 if (expandColumn [j-k]) {
|
|
358 widths [last=j-k] += delta;
|
|
359 }
|
|
360 }
|
|
361 if (last > -1) widths [last] += remainder;
|
|
362 }
|
|
363 }
|
|
364 }
|
|
365 if (!data.grabExcessHorizontalSpace || data.minimumWidth !is 0) {
|
|
366 w = !data.grabExcessHorizontalSpace || data.minimumWidth is SWT.DEFAULT ? data.cacheWidth : data.minimumWidth;
|
|
367 w += data.horizontalIndent - spanMinWidth - (hSpan - 1) * horizontalSpacing;
|
|
368 if (w > 0) {
|
|
369 if (spanExpandCount is 0) {
|
|
370 minWidths [j] += w;
|
|
371 } else {
|
|
372 int delta = w / spanExpandCount;
|
|
373 int remainder = w % spanExpandCount, last = -1;
|
|
374 for (int k = 0; k < hSpan; k++) {
|
|
375 if (expandColumn [j-k]) {
|
|
376 minWidths [last=j-k] += delta;
|
|
377 }
|
|
378 }
|
|
379 if (last > -1) minWidths [last] += remainder;
|
|
380 }
|
|
381 }
|
|
382 }
|
|
383 }
|
|
384 }
|
|
385 }
|
|
386 }
|
|
387 if (makeColumnsEqualWidth) {
|
|
388 int minColumnWidth = 0;
|
|
389 int columnWidth = 0;
|
|
390 for (int i=0; i<columnCount; i++) {
|
|
391 minColumnWidth = Math.max (minColumnWidth, minWidths [i]);
|
|
392 columnWidth = Math.max (columnWidth, widths [i]);
|
|
393 }
|
|
394 columnWidth = width is SWT.DEFAULT || expandCount is 0 ? columnWidth : Math.max (minColumnWidth, availableWidth / columnCount);
|
|
395 for (int i=0; i<columnCount; i++) {
|
|
396 expandColumn [i] = expandCount > 0;
|
|
397 widths [i] = columnWidth;
|
|
398 }
|
|
399 } else {
|
|
400 if (width !is SWT.DEFAULT && expandCount > 0) {
|
|
401 int totalWidth = 0;
|
|
402 for (int i=0; i<columnCount; i++) {
|
|
403 totalWidth += widths [i];
|
|
404 }
|
|
405 int c = expandCount;
|
|
406 int delta = (availableWidth - totalWidth) / c;
|
|
407 int remainder = (availableWidth - totalWidth) % c;
|
|
408 int last = -1;
|
|
409 while (totalWidth !is availableWidth) {
|
|
410 for (int j=0; j<columnCount; j++) {
|
|
411 if (expandColumn [j]) {
|
|
412 if (widths [j] + delta > minWidths [j]) {
|
|
413 widths [last = j] = widths [j] + delta;
|
|
414 } else {
|
|
415 widths [j] = minWidths [j];
|
|
416 expandColumn [j] = false;
|
|
417 c--;
|
|
418 }
|
|
419 }
|
|
420 }
|
|
421 if (last > -1) widths [last] += remainder;
|
|
422
|
|
423 for (int j=0; j<columnCount; j++) {
|
|
424 for (int i=0; i<rowCount; i++) {
|
|
425 GridData data = getData (grid, i, j, rowCount, columnCount, false);
|
|
426 if (data !is null) {
|
|
427 int hSpan = Math.max (1, Math.min (data.horizontalSpan, columnCount));
|
|
428 if (hSpan > 1) {
|
|
429 if (!data.grabExcessHorizontalSpace || data.minimumWidth !is 0) {
|
|
430 int spanWidth = 0, spanExpandCount = 0;
|
|
431 for (int k=0; k<hSpan; k++) {
|
|
432 spanWidth += widths [j-k];
|
|
433 if (expandColumn [j-k]) spanExpandCount++;
|
|
434 }
|
|
435 int w = !data.grabExcessHorizontalSpace || data.minimumWidth is SWT.DEFAULT ? data.cacheWidth : data.minimumWidth;
|
|
436 w += data.horizontalIndent - spanWidth - (hSpan - 1) * horizontalSpacing;
|
|
437 if (w > 0) {
|
|
438 if (spanExpandCount is 0) {
|
|
439 widths [j] += w;
|
|
440 } else {
|
|
441 int delta2 = w / spanExpandCount;
|
|
442 int remainder2 = w % spanExpandCount, last2 = -1;
|
|
443 for (int k = 0; k < hSpan; k++) {
|
|
444 if (expandColumn [j-k]) {
|
|
445 widths [last2=j-k] += delta2;
|
|
446 }
|
|
447 }
|
|
448 if (last2 > -1) widths [last2] += remainder2;
|
|
449 }
|
|
450 }
|
|
451 }
|
|
452 }
|
|
453 }
|
|
454 }
|
|
455 }
|
|
456 if (c is 0) break;
|
|
457 totalWidth = 0;
|
|
458 for (int i=0; i<columnCount; i++) {
|
|
459 totalWidth += widths [i];
|
|
460 }
|
|
461 delta = (availableWidth - totalWidth) / c;
|
|
462 remainder = (availableWidth - totalWidth) % c;
|
|
463 last = -1;
|
|
464 }
|
|
465 }
|
|
466 }
|
|
467
|
|
468 /* Wrapping */
|
|
469 GridData [] flush = null;
|
|
470 int flushLength = 0;
|
|
471 if (width !is SWT.DEFAULT) {
|
|
472 for (int j=0; j<columnCount; j++) {
|
|
473 for (int i=0; i<rowCount; i++) {
|
|
474 GridData data = getData (grid, i, j, rowCount, columnCount, false);
|
|
475 if (data !is null) {
|
|
476 if (data.heightHint is SWT.DEFAULT) {
|
|
477 Control child = grid [i][j];
|
|
478 //TEMPORARY CODE
|
|
479 int hSpan = Math.max (1, Math.min (data.horizontalSpan, columnCount));
|
|
480 int currentWidth = 0;
|
|
481 for (int k=0; k<hSpan; k++) {
|
|
482 currentWidth += widths [j-k];
|
|
483 }
|
|
484 currentWidth += (hSpan - 1) * horizontalSpacing - data.horizontalIndent;
|
|
485 if ((currentWidth !is data.cacheWidth && data.horizontalAlignment is SWT.FILL) || (data.cacheWidth > currentWidth)) {
|
|
486 int trim = 0;
|
|
487 if ( auto sa = cast(Scrollable)child ) {
|
|
488 Rectangle rect = sa.computeTrim (0, 0, 0, 0);
|
|
489 trim = rect.width;
|
|
490 } else {
|
|
491 trim = child.getBorderWidth () * 2;
|
|
492 }
|
|
493 data.cacheWidth = data.cacheHeight = SWT.DEFAULT;
|
|
494 data.computeSize (child, Math.max (0, currentWidth - trim), data.heightHint, false);
|
|
495 if (data.grabExcessVerticalSpace && data.minimumHeight > 0) {
|
|
496 data.cacheHeight = Math.max (data.cacheHeight, data.minimumHeight);
|
|
497 }
|
|
498 if (flush is null) flush = new GridData [count];
|
|
499 flush [flushLength++] = data;
|
|
500 }
|
|
501 }
|
|
502 }
|
|
503 }
|
|
504 }
|
|
505 }
|
|
506
|
|
507 /* Row heights */
|
|
508 int availableHeight = height - verticalSpacing * (rowCount - 1) - (marginTop + marginHeight * 2 + marginBottom);
|
|
509 expandCount = 0;
|
|
510 int [] heights = new int [rowCount];
|
|
511 int [] minHeights = new int [rowCount];
|
|
512 bool [] expandRow = new bool [rowCount];
|
|
513 for (int i=0; i<rowCount; i++) {
|
|
514 for (int j=0; j<columnCount; j++) {
|
|
515 GridData data = getData (grid, i, j, rowCount, columnCount, true);
|
|
516 if (data !is null) {
|
|
517 int vSpan = Math.max (1, Math.min (data.verticalSpan, rowCount));
|
|
518 if (vSpan is 1) {
|
|
519 int h = data.cacheHeight + data.verticalIndent;
|
|
520 heights [i] = Math.max (heights [i], h);
|
|
521 if (data.grabExcessVerticalSpace) {
|
|
522 if (!expandRow [i]) expandCount++;
|
|
523 expandRow [i] = true;
|
|
524 }
|
|
525 if (!data.grabExcessVerticalSpace || data.minimumHeight !is 0) {
|
|
526 h = !data.grabExcessVerticalSpace || data.minimumHeight is SWT.DEFAULT ? data.cacheHeight : data.minimumHeight;
|
|
527 h += data.verticalIndent;
|
|
528 minHeights [i] = Math.max (minHeights [i], h);
|
|
529 }
|
|
530 }
|
|
531 }
|
|
532 }
|
|
533 for (int j=0; j<columnCount; j++) {
|
|
534 GridData data = getData (grid, i, j, rowCount, columnCount, false);
|
|
535 if (data !is null) {
|
|
536 int vSpan = Math.max (1, Math.min (data.verticalSpan, rowCount));
|
|
537 if (vSpan > 1) {
|
|
538 int spanHeight = 0, spanMinHeight = 0, spanExpandCount = 0;
|
|
539 for (int k=0; k<vSpan; k++) {
|
|
540 spanHeight += heights [i-k];
|
|
541 spanMinHeight += minHeights [i-k];
|
|
542 if (expandRow [i-k]) spanExpandCount++;
|
|
543 }
|
|
544 if (data.grabExcessVerticalSpace && spanExpandCount is 0) {
|
|
545 expandCount++;
|
|
546 expandRow [i] = true;
|
|
547 }
|
|
548 int h = data.cacheHeight + data.verticalIndent - spanHeight - (vSpan - 1) * verticalSpacing;
|
|
549 if (h > 0) {
|
|
550 if (spanExpandCount is 0) {
|
|
551 heights [i] += h;
|
|
552 } else {
|
|
553 int delta = h / spanExpandCount;
|
|
554 int remainder = h % spanExpandCount, last = -1;
|
|
555 for (int k = 0; k < vSpan; k++) {
|
|
556 if (expandRow [i-k]) {
|
|
557 heights [last=i-k] += delta;
|
|
558 }
|
|
559 }
|
|
560 if (last > -1) heights [last] += remainder;
|
|
561 }
|
|
562 }
|
|
563 if (!data.grabExcessVerticalSpace || data.minimumHeight !is 0) {
|
|
564 h = !data.grabExcessVerticalSpace || data.minimumHeight is SWT.DEFAULT ? data.cacheHeight : data.minimumHeight;
|
|
565 h += data.verticalIndent - spanMinHeight - (vSpan - 1) * verticalSpacing;
|
|
566 if (h > 0) {
|
|
567 if (spanExpandCount is 0) {
|
|
568 minHeights [i] += h;
|
|
569 } else {
|
|
570 int delta = h / spanExpandCount;
|
|
571 int remainder = h % spanExpandCount, last = -1;
|
|
572 for (int k = 0; k < vSpan; k++) {
|
|
573 if (expandRow [i-k]) {
|
|
574 minHeights [last=i-k] += delta;
|
|
575 }
|
|
576 }
|
|
577 if (last > -1) minHeights [last] += remainder;
|
|
578 }
|
|
579 }
|
|
580 }
|
|
581 }
|
|
582 }
|
|
583 }
|
|
584 }
|
|
585 if (height !is SWT.DEFAULT && expandCount > 0) {
|
|
586 int totalHeight = 0;
|
|
587 for (int i=0; i<rowCount; i++) {
|
|
588 totalHeight += heights [i];
|
|
589 }
|
|
590 int c = expandCount;
|
|
591 int delta = (availableHeight - totalHeight) / c;
|
|
592 int remainder = (availableHeight - totalHeight) % c;
|
|
593 int last = -1;
|
|
594 while (totalHeight !is availableHeight) {
|
|
595 for (int i=0; i<rowCount; i++) {
|
|
596 if (expandRow [i]) {
|
|
597 if (heights [i] + delta > minHeights [i]) {
|
|
598 heights [last = i] = heights [i] + delta;
|
|
599 } else {
|
|
600 heights [i] = minHeights [i];
|
|
601 expandRow [i] = false;
|
|
602 c--;
|
|
603 }
|
|
604 }
|
|
605 }
|
|
606 if (last > -1) heights [last] += remainder;
|
|
607
|
|
608 for (int i=0; i<rowCount; i++) {
|
|
609 for (int j=0; j<columnCount; j++) {
|
|
610 GridData data = getData (grid, i, j, rowCount, columnCount, false);
|
|
611 if (data !is null) {
|
|
612 int vSpan = Math.max (1, Math.min (data.verticalSpan, rowCount));
|
|
613 if (vSpan > 1) {
|
|
614 if (!data.grabExcessVerticalSpace || data.minimumHeight !is 0) {
|
|
615 int spanHeight = 0, spanExpandCount = 0;
|
|
616 for (int k=0; k<vSpan; k++) {
|
|
617 spanHeight += heights [i-k];
|
|
618 if (expandRow [i-k]) spanExpandCount++;
|
|
619 }
|
|
620 int h = !data.grabExcessVerticalSpace || data.minimumHeight is SWT.DEFAULT ? data.cacheHeight : data.minimumHeight;
|
|
621 h += data.verticalIndent - spanHeight - (vSpan - 1) * verticalSpacing;
|
|
622 if (h > 0) {
|
|
623 if (spanExpandCount is 0) {
|
|
624 heights [i] += h;
|
|
625 } else {
|
|
626 int delta2 = h / spanExpandCount;
|
|
627 int remainder2 = h % spanExpandCount, last2 = -1;
|
|
628 for (int k = 0; k < vSpan; k++) {
|
|
629 if (expandRow [i-k]) {
|
|
630 heights [last2=i-k] += delta2;
|
|
631 }
|
|
632 }
|
|
633 if (last2 > -1) heights [last2] += remainder2;
|
|
634 }
|
|
635 }
|
|
636 }
|
|
637 }
|
|
638 }
|
|
639 }
|
|
640 }
|
|
641 if (c is 0) break;
|
|
642 totalHeight = 0;
|
|
643 for (int i=0; i<rowCount; i++) {
|
|
644 totalHeight += heights [i];
|
|
645 }
|
|
646 delta = (availableHeight - totalHeight) / c;
|
|
647 remainder = (availableHeight - totalHeight) % c;
|
|
648 last = -1;
|
|
649 }
|
|
650 }
|
|
651
|
|
652 /* Position the controls */
|
|
653 if (move) {
|
|
654 int gridY = y + marginTop + marginHeight;
|
|
655 for (int i=0; i<rowCount; i++) {
|
|
656 int gridX = x + marginLeft + marginWidth;
|
|
657 for (int j=0; j<columnCount; j++) {
|
|
658 GridData data = getData (grid, i, j, rowCount, columnCount, true);
|
|
659 if (data !is null) {
|
|
660 int hSpan = Math.max (1, Math.min (data.horizontalSpan, columnCount));
|
|
661 int vSpan = Math.max (1, data.verticalSpan);
|
|
662 int cellWidth = 0, cellHeight = 0;
|
|
663 for (int k=0; k<hSpan; k++) {
|
|
664 cellWidth += widths [j+k];
|
|
665 }
|
|
666 for (int k=0; k<vSpan; k++) {
|
|
667 cellHeight += heights [i+k];
|
|
668 }
|
|
669 cellWidth += horizontalSpacing * (hSpan - 1);
|
|
670 int childX = gridX + data.horizontalIndent;
|
|
671 int childWidth = Math.min (data.cacheWidth, cellWidth);
|
|
672 switch (data.horizontalAlignment) {
|
|
673 case SWT.CENTER:
|
|
674 case GridData.CENTER:
|
|
675 childX += Math.max (0, (cellWidth - data.horizontalIndent - childWidth) / 2);
|
|
676 break;
|
|
677 case SWT.RIGHT:
|
|
678 case SWT.END:
|
|
679 case GridData.END:
|
|
680 childX += Math.max (0, cellWidth - data.horizontalIndent - childWidth);
|
|
681 break;
|
|
682 case SWT.FILL:
|
|
683 childWidth = cellWidth - data.horizontalIndent;
|
|
684 break;
|
|
685 default:
|
|
686 }
|
|
687 cellHeight += verticalSpacing * (vSpan - 1);
|
|
688 int childY = gridY + data.verticalIndent;
|
|
689 int childHeight = Math.min (data.cacheHeight, cellHeight);
|
|
690 switch (data.verticalAlignment) {
|
|
691 case SWT.CENTER:
|
|
692 case GridData.CENTER:
|
|
693 childY += Math.max (0, (cellHeight - data.verticalIndent - childHeight) / 2);
|
|
694 break;
|
|
695 case SWT.BOTTOM:
|
|
696 case SWT.END:
|
|
697 case GridData.END:
|
|
698 childY += Math.max (0, cellHeight - data.verticalIndent - childHeight);
|
|
699 break;
|
|
700 case SWT.FILL:
|
|
701 childHeight = cellHeight - data.verticalIndent;
|
|
702 break;
|
|
703 default:
|
|
704 }
|
|
705 Control child = grid [i][j];
|
|
706 if (child !is null) {
|
|
707 child.setBounds (childX, childY, childWidth, childHeight);
|
|
708 }
|
|
709 }
|
|
710 gridX += widths [j] + horizontalSpacing;
|
|
711 }
|
|
712 gridY += heights [i] + verticalSpacing;
|
|
713 }
|
|
714 }
|
|
715
|
|
716 // clean up cache
|
|
717 for (int i = 0; i < flushLength; i++) {
|
|
718 flush [i].cacheWidth = flush [i].cacheHeight = -1;
|
|
719 }
|
|
720
|
|
721 int totalDefaultWidth = 0;
|
|
722 int totalDefaultHeight = 0;
|
|
723 for (int i=0; i<columnCount; i++) {
|
|
724 totalDefaultWidth += widths [i];
|
|
725 }
|
|
726 for (int i=0; i<rowCount; i++) {
|
|
727 totalDefaultHeight += heights [i];
|
|
728 }
|
|
729 totalDefaultWidth += horizontalSpacing * (columnCount - 1) + marginLeft + marginWidth * 2 + marginRight;
|
|
730 totalDefaultHeight += verticalSpacing * (rowCount - 1) + marginTop + marginHeight * 2 + marginBottom;
|
|
731 return new Point (totalDefaultWidth, totalDefaultHeight);
|
|
732 }
|
|
733
|
|
734 String getName () {
|
|
735 String string = this.classinfo.name;
|
|
736 int index = string.lastIndexOf('.');
|
|
737 if (index is -1 ) return string;
|
|
738 return string[ index + 1 .. string.length ];
|
|
739 }
|
|
740
|
|
741 /**
|
|
742 * Returns a string containing a concise, human-readable
|
|
743 * description of the receiver.
|
|
744 *
|
|
745 * @return a string representation of the layout
|
|
746 */
|
|
747 override public String toString () {
|
|
748 String string = getName ()~" {";
|
|
749 if (numColumns !is 1) string ~= "numColumns="~to!(String)(numColumns)~" ";
|
|
750 if (makeColumnsEqualWidth) string ~= "makeColumnsEqualWidth="~to!(String)(makeColumnsEqualWidth)~" ";
|
|
751 if (marginWidth !is 0) string ~= "marginWidth="~to!(String)(marginWidth)~" ";
|
|
752 if (marginHeight !is 0) string ~= "marginHeight="~to!(String)(marginHeight)~" ";
|
|
753 if (marginLeft !is 0) string ~= "marginLeft="~to!(String)(marginLeft)~" ";
|
|
754 if (marginRight !is 0) string ~= "marginRight="~to!(String)(marginRight)~" ";
|
|
755 if (marginTop !is 0) string ~= "marginTop="~to!(String)(marginTop)~" ";
|
|
756 if (marginBottom !is 0) string ~= "marginBottom="~to!(String)(marginBottom)~" ";
|
|
757 if (horizontalSpacing !is 0) string ~= "horizontalSpacing="~to!(String)(horizontalSpacing)~" ";
|
|
758 if (verticalSpacing !is 0) string ~= "verticalSpacing="~to!(String)(verticalSpacing)~" ";
|
|
759 string = string.trim();
|
|
760 string ~= "}";
|
|
761 return string;
|
|
762 }
|
|
763 }
|