1 /*******************************************************************************
2 * Copyright (c) 2000, 2018 IBM Corporation and others.
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/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.layout;
16 import org.eclipse.swt.*;
17 import org.eclipse.swt.graphics.*;
18 import org.eclipse.swt.widgets.*;
21 * Instances of this class lay out the control children of a
22 * <code>Composite</code> in a grid.
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.
30 * The following code creates a shell managed by a <code>GridLayout</code>
33 * Display display = new Display();
34 * Shell shell = new Shell(display);
35 * GridLayout gridLayout = new GridLayout();
36 * gridLayout.numColumns = 3;
37 * shell.setLayout(gridLayout);
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>.
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>
51 public final class GridLayout extends Layout {
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.
58 * The default value is 1.
60 public int numColumns = 1;
63 * makeColumnsEqualWidth specifies whether all columns in the layout
64 * will be forced to have the same width.
66 * The default value is false.
68 public boolean makeColumnsEqualWidth = false;
71 * marginWidth specifies the number of points of horizontal margin
72 * that will be placed along the left and right edges of the layout.
74 * The default value is 5.
76 public int marginWidth = 5;
79 * marginHeight specifies the number of points of vertical margin
80 * that will be placed along the top and bottom edges of the layout.
82 * The default value is 5.
84 public int marginHeight = 5;
87 * marginLeft specifies the number of points of horizontal margin
88 * that will be placed along the left edge of the layout.
90 * The default value is 0.
94 public int marginLeft = 0;
97 * marginTop specifies the number of points of vertical margin
98 * that will be placed along the top edge of the layout.
100 * The default value is 0.
104 public int marginTop = 0;
107 * marginRight specifies the number of points of horizontal margin
108 * that will be placed along the right edge of the layout.
110 * The default value is 0.
114 public int marginRight = 0;
117 * marginBottom specifies the number of points of vertical margin
118 * that will be placed along the bottom edge of the layout.
120 * The default value is 0.
124 public int marginBottom = 0;
127 * horizontalSpacing specifies the number of points between the right
128 * edge of one cell and the left edge of its neighbouring cell to
131 * The default value is 5.
133 public int horizontalSpacing = 5;
136 * verticalSpacing specifies the number of points between the bottom
137 * edge of one cell and the top edge of its neighbouring cell underneath.
139 * The default value is 5.
141 public int verticalSpacing = 5;
144 * Constructs a new instance of this class
145 * with a single column.
147 public GridLayout () {}
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.
156 * @param numColumns the number of columns in the grid
157 * @param makeColumnsEqualWidth whether or not the columns will have equal width
161 public GridLayout (int numColumns, boolean makeColumnsEqualWidth) {
162 this.numColumns = numColumns;
163 this.makeColumnsEqualWidth = makeColumnsEqualWidth;
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;
175 protected boolean flushCache (Control control) {
176 Object data = control.getLayoutData ();
177 if (data != null) ((GridData) data).flushCache ();
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;
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);
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);
208 Control [] children = composite.getChildren ();
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];
218 return new Point (marginLeft + marginWidth * 2 + marginRight, marginTop + marginHeight * 2 + marginBottom);
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) {
230 if (child instanceof Scrollable) {
231 Rectangle rect = ((Scrollable) child).computeTrim (0, 0, 0, 0);
234 trim = child.getBorderWidth () * 2;
236 data.cacheWidth = data.cacheHeight = SWT.DEFAULT;
237 data.computeSize (child, Math.max (0, data.minimumWidth - trim), data.heightHint, false);
240 if (data.grabExcessVerticalSpace && data.minimumHeight > 0) {
241 data.cacheHeight = Math.max (data.cacheHeight, data.minimumHeight);
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);
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);
260 if (grid [row] == null) {
261 grid [row] = new Control [columnCount];
263 while (column < columnCount && grid [row] [column] != null) {
266 int endCount = column + hSpan;
267 if (endCount <= columnCount) {
269 while (index < endCount && grid [row] [index] == null) {
272 if (index == endCount) break;
275 if (column + hSpan >= columnCount) {
280 for (int j=0; j<vSpan; j++) {
281 if (grid [row + j] == null) {
282 grid [row + j] = new Control [columnCount];
284 for (int k=0; k<hSpan; k++) {
285 grid [row + j] [column + k] = child;
288 rowCount = Math.max (rowCount, row + vSpan);
293 int availableWidth = width - horizontalSpacing * (columnCount - 1) - (marginLeft + marginWidth * 2 + marginRight);
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);
302 int hSpan = Math.max (1, Math.min (data.horizontalSpan, columnCount));
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;
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);
318 for (int i=0; i<rowCount; i++) {
319 GridData data = getData (grid, i, j, rowCount, columnCount, false);
321 int hSpan = Math.max (1, Math.min (data.horizontalSpan, columnCount));
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++;
329 if (data.grabExcessHorizontalSpace && spanExpandCount == 0) {
331 expandColumn [j] = true;
333 int w = data.cacheWidth + data.horizontalIndent - spanWidth - (hSpan - 1) * horizontalSpacing;
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]);
341 if (last > -1) widths [last] += remainder;
343 if (spanExpandCount == 0) {
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;
353 if (last > -1) widths [last] += remainder;
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;
361 if (spanExpandCount == 0) {
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;
371 if (last > -1) minWidths [last] += remainder;
379 if (makeColumnsEqualWidth) {
380 int minColumnWidth = 0;
382 for (int i=0; i<columnCount; i++) {
383 minColumnWidth = Math.max (minColumnWidth, minWidths [i]);
384 columnWidth = Math.max (columnWidth, widths [i]);
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;
392 if (width != SWT.DEFAULT && expandCount > 0) {
394 for (int i=0; i<columnCount; i++) {
395 totalWidth += widths [i];
398 int delta = (availableWidth - totalWidth) / c;
399 int remainder = (availableWidth - totalWidth) % c;
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;
407 widths [j] = minWidths [j];
408 expandColumn [j] = false;
413 if (last > -1) widths [last] += remainder;
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);
419 int hSpan = Math.max (1, Math.min (data.horizontalSpan, columnCount));
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++;
427 int w = !data.grabExcessHorizontalSpace || data.minimumWidth == SWT.DEFAULT ? data.cacheWidth : data.minimumWidth;
428 w += data.horizontalIndent - spanWidth - (hSpan - 1) * horizontalSpacing;
430 if (spanExpandCount == 0) {
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;
440 if (last2 > -1) widths [last2] += remainder2;
450 for (int i=0; i<columnCount; i++) {
451 totalWidth += widths [i];
453 delta = (availableWidth - totalWidth) / c;
454 remainder = (availableWidth - totalWidth) % c;
461 GridData [] flush = null;
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);
468 if (data.heightHint == SWT.DEFAULT) {
469 Control child = grid [i][j];
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];
476 currentWidth += (hSpan - 1) * horizontalSpacing - data.horizontalIndent;
477 if ((currentWidth != data.cacheWidth && data.horizontalAlignment == SWT.FILL) || (data.cacheWidth > currentWidth)) {
479 if (child instanceof Scrollable) {
480 Rectangle rect = ((Scrollable) child).computeTrim (0, 0, 0, 0);
483 trim = child.getBorderWidth () * 2;
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);
490 if (flush == null) flush = new GridData [count];
491 flush [flushLength++] = data;
500 int availableHeight = height - verticalSpacing * (rowCount - 1) - (marginTop + marginHeight * 2 + marginBottom);
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);
509 int vSpan = Math.max (1, Math.min (data.verticalSpan, rowCount));
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;
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);
525 for (int j=0; j<columnCount; j++) {
526 GridData data = getData (grid, i, j, rowCount, columnCount, false);
528 int vSpan = Math.max (1, Math.min (data.verticalSpan, rowCount));
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++;
536 if (data.grabExcessVerticalSpace && spanExpandCount == 0) {
538 expandRow [i] = true;
540 int h = data.cacheHeight + data.verticalIndent - spanHeight - (vSpan - 1) * verticalSpacing;
542 if (spanExpandCount == 0) {
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;
552 if (last > -1) heights [last] += remainder;
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;
559 if (spanExpandCount == 0) {
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;
569 if (last > -1) minHeights [last] += remainder;
577 if (height != SWT.DEFAULT && expandCount > 0) {
579 for (int i=0; i<rowCount; i++) {
580 totalHeight += heights [i];
583 int delta = (availableHeight - totalHeight) / c;
584 int remainder = (availableHeight - totalHeight) % c;
586 while (totalHeight != availableHeight) {
587 for (int i=0; i<rowCount; i++) {
589 if (heights [i] + delta > minHeights [i]) {
590 heights [last = i] = heights [i] + delta;
592 heights [i] = minHeights [i];
593 expandRow [i] = false;
598 if (last > -1) heights [last] += remainder;
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);
604 int vSpan = Math.max (1, Math.min (data.verticalSpan, rowCount));
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++;
612 int h = !data.grabExcessVerticalSpace || data.minimumHeight == SWT.DEFAULT ? data.cacheHeight : data.minimumHeight;
613 h += data.verticalIndent - spanHeight - (vSpan - 1) * verticalSpacing;
615 if (spanExpandCount == 0) {
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;
625 if (last2 > -1) heights [last2] += remainder2;
635 for (int i=0; i<rowCount; i++) {
636 totalHeight += heights [i];
638 delta = (availableHeight - totalHeight) / c;
639 remainder = (availableHeight - totalHeight) % c;
644 /* Position the controls */
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);
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];
658 for (int k=0; k<vSpan; k++) {
659 cellHeight += heights [i+k];
661 cellWidth += horizontalSpacing * (hSpan - 1);
662 int childX = gridX + data.horizontalIndent;
663 int childWidth = Math.min (data.cacheWidth, cellWidth);
664 switch (data.horizontalAlignment) {
666 case GridData.CENTER:
667 childX += Math.max (0, (cellWidth - data.horizontalIndent - childWidth) / 2);
672 childX += Math.max (0, cellWidth - data.horizontalIndent - childWidth);
675 childWidth = cellWidth - data.horizontalIndent;
678 cellHeight += verticalSpacing * (vSpan - 1);
679 int childY = gridY + data.verticalIndent;
680 int childHeight = Math.min (data.cacheHeight, cellHeight);
681 switch (data.verticalAlignment) {
683 case GridData.CENTER:
684 childY += Math.max (0, (cellHeight - data.verticalIndent - childHeight) / 2);
689 childY += Math.max (0, cellHeight - data.verticalIndent - childHeight);
692 childHeight = cellHeight - data.verticalIndent;
695 Control child = grid [i][j];
697 child.setBounds (childX, childY, childWidth, childHeight);
700 gridX += widths [j] + horizontalSpacing;
702 gridY += heights [i] + verticalSpacing;
707 for (int i = 0; i < flushLength; i++) {
708 flush [i].cacheWidth = flush [i].cacheHeight = -1;
711 int totalDefaultWidth = 0;
712 int totalDefaultHeight = 0;
713 for (int i=0; i<columnCount; i++) {
714 totalDefaultWidth += widths [i];
716 for (int i=0; i<rowCount; i++) {
717 totalDefaultHeight += heights [i];
719 totalDefaultWidth += horizontalSpacing * (columnCount - 1) + marginLeft + marginWidth * 2 + marginRight;
720 totalDefaultHeight += verticalSpacing * (rowCount - 1) + marginTop + marginHeight * 2 + marginBottom;
721 return new Point (totalDefaultWidth, totalDefaultHeight);
725 String string = getClass ().getName ();
726 int index = string.lastIndexOf ('.');
727 if (index == -1) return string;
728 return string.substring (index + 1, string.length ());
732 * Returns a string containing a concise, human-readable
733 * description of the receiver.
735 * @return a string representation of the layout
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();