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