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