All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.swt.layout.GridLayout Maven / Gradle / Ivy

Go to download

The Rich Ajax Platform lets you build rich, Ajax-enabled Web applications.

There is a newer version: 3.29.0
Show newest version
/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.layout;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Layout;
import org.eclipse.swt.widgets.Scrollable;

/**
 * Instances of this class lay out the control children of a 
 * Composite in a grid. 
 * 

* GridLayout has a number of configuration fields, and the * controls it lays out can have an associated layout data object, called * GridData. The power of GridLayout lies in the * ability to configure GridData for each control in the layout. *

*

* The following code creates a shell managed by a GridLayout * with 3 columns: *

 *    Display display = new Display();
 *    Shell shell = new Shell(display);
 *    GridLayout gridLayout = new GridLayout();
 *    gridLayout.numColumns = 3;
 *    shell.setLayout(gridLayout);
 * 
* The numColumns field is the most important field in a * GridLayout. Widgets are laid out in columns from left * to right, and a new row is created when numColumns + 1 * controls are added to the Composite. *

* * @see GridData * @see GridLayout snippets * @see SWT Example: LayoutExample * @see Sample code and further information * @since 1.0 */ public final class GridLayout extends Layout { /** * numColumns specifies the number of cell columns in the layout. * If numColumns has a value less than 1, the layout will not * set the size and position of any controls. * * The default value is 1. */ public int numColumns = 1; /** * makeColumnsEqualWidth specifies whether all columns in the layout * will be forced to have the same width. * * The default value is false. */ public boolean makeColumnsEqualWidth = false; /** * marginWidth specifies the number of pixels of horizontal margin * that will be placed along the left and right edges of the layout. * * The default value is 5. */ public int marginWidth = 5; /** * marginHeight specifies the number of pixels of vertical margin * that will be placed along the top and bottom edges of the layout. * * The default value is 5. */ public int marginHeight = 5; /** * marginLeft specifies the number of pixels of horizontal margin * that will be placed along the left edge of the layout. * * The default value is 0. */ public int marginLeft = 0; /** * marginTop specifies the number of pixels of vertical margin * that will be placed along the top edge of the layout. * * The default value is 0. */ public int marginTop = 0; /** * marginRight specifies the number of pixels of horizontal margin * that will be placed along the right edge of the layout. * * The default value is 0. */ public int marginRight = 0; /** * marginBottom specifies the number of pixels of vertical margin * that will be placed along the bottom edge of the layout. * * The default value is 0. */ public int marginBottom = 0; /** * horizontalSpacing specifies the number of pixels between the right * edge of one cell and the left edge of its neighbouring cell to * the right. * * The default value is 5. */ public int horizontalSpacing = 5; /** * verticalSpacing specifies the number of pixels between the bottom * edge of one cell and the top edge of its neighbouring cell underneath. * * The default value is 5. */ public int verticalSpacing = 5; /** * Constructs a new instance of this class * with a single column. */ public GridLayout () {} /** * Constructs a new instance of this class given the * number of columns, and whether or not the columns * should be forced to have the same width. * If numColumns has a value less than 1, the layout will not * set the size and position of any controls. * * @param numColumns the number of columns in the grid * @param makeColumnsEqualWidth whether or not the columns will have equal width */ public GridLayout (int numColumns, boolean makeColumnsEqualWidth) { this.numColumns = numColumns; this.makeColumnsEqualWidth = makeColumnsEqualWidth; } protected Point computeSize (Composite composite, int wHint, int hHint, boolean flushCache) { Point size = layout (composite, false, 0, 0, wHint, hHint, flushCache); if (wHint != SWT.DEFAULT) size.x = wHint; if (hHint != SWT.DEFAULT) size.y = hHint; return size; } protected boolean flushCache (Control control) { Object data = control.getLayoutData (); if (data != null) ((GridData) data).flushCache (); return true; } GridData getData (Control [][] grid, int row, int column, int rowCount, int columnCount, boolean first) { Control control = grid [row] [column]; if (control != null) { GridData data = (GridData) control.getLayoutData (); int hSpan = Math.max (1, Math.min (data.horizontalSpan, columnCount)); int vSpan = Math.max (1, data.verticalSpan); int i = first ? row + vSpan - 1 : row - vSpan + 1; int j = first ? column + hSpan - 1 : column - hSpan + 1; if (0 <= i && i < rowCount) { if (0 <= j && j < columnCount) { if (control == grid [i][j]) return data; } } } return null; } protected void layout (Composite composite, boolean flushCache) { Rectangle rect = composite.getClientArea (); layout (composite, true, rect.x, rect.y, rect.width, rect.height, flushCache); } Point layout (Composite composite, boolean move, int x, int y, int width, int height, boolean flushCache) { if (numColumns < 1) { return new Point (marginLeft + marginWidth * 2 + marginRight, marginTop + marginHeight * 2 + marginBottom); } Control [] children = composite.getChildren (); int count = 0; for (int i=0; i 0) { if (data.cacheWidth < data.minimumWidth) { int trim = 0; //TEMPORARY CODE if (child instanceof Scrollable) { Rectangle rect = ((Scrollable) child).computeTrim (0, 0, 0, 0); trim = rect.width; } else { trim = child.getBorderWidth () * 2; } data.cacheWidth = data.cacheHeight = SWT.DEFAULT; data.computeSize (child, Math.max (0, data.minimumWidth - trim), data.heightHint, false); } } if (data.grabExcessVerticalSpace && data.minimumHeight > 0) { data.cacheHeight = Math.max (data.cacheHeight, data.minimumHeight); } } /* Build the grid */ int row = 0, column = 0, rowCount = 0, columnCount = numColumns; Control [][] grid = new Control [4] [columnCount]; for (int i=0; i= grid.length) { Control [][] newGrid = new Control [lastRow + 4] [columnCount]; System.arraycopy (grid, 0, newGrid, 0, grid.length); grid = newGrid; } if (grid [row] == null) { grid [row] = new Control [columnCount]; } while (column < columnCount && grid [row] [column] != null) { column++; } int endCount = column + hSpan; if (endCount <= columnCount) { int index = column; while (index < endCount && grid [row] [index] == null) { index++; } if (index == endCount) break; column = index; } if (column + hSpan >= columnCount) { column = 0; row++; } } for (int j=0; j 1) { int spanWidth = 0, spanMinWidth = 0, spanExpandCount = 0; for (int k=0; k 0) { if (makeColumnsEqualWidth) { int equalWidth = (w + spanWidth) / hSpan; int remainder = (w + spanWidth) % hSpan, last = -1; for (int k = 0; k < hSpan; k++) { widths [last=j-k] = Math.max (equalWidth, widths [j-k]); } if (last > -1) widths [last] += remainder; } else { if (spanExpandCount == 0) { widths [j] += w; } else { int delta = w / spanExpandCount; int remainder = w % spanExpandCount, last = -1; for (int k = 0; k < hSpan; k++) { if (expandColumn [j-k]) { widths [last=j-k] += delta; } } if (last > -1) widths [last] += remainder; } } } if (!data.grabExcessHorizontalSpace || data.minimumWidth != 0) { w = !data.grabExcessHorizontalSpace || data.minimumWidth == SWT.DEFAULT ? data.cacheWidth : data.minimumWidth; w += data.horizontalIndent - spanMinWidth - (hSpan - 1) * horizontalSpacing; if (w > 0) { if (spanExpandCount == 0) { minWidths [j] += w; } else { int delta = w / spanExpandCount; int remainder = w % spanExpandCount, last = -1; for (int k = 0; k < hSpan; k++) { if (expandColumn [j-k]) { minWidths [last=j-k] += delta; } } if (last > -1) minWidths [last] += remainder; } } } } } } } if (makeColumnsEqualWidth) { int minColumnWidth = 0; int columnWidth = 0; for (int i=0; i 0; widths [i] = columnWidth; } } else { if (width != SWT.DEFAULT && expandCount > 0) { int totalWidth = 0; for (int i=0; i minWidths [j]) { widths [last = j] = widths [j] + delta; } else { widths [j] = minWidths [j]; expandColumn [j] = false; c--; } } } if (last > -1) widths [last] += remainder; for (int j=0; j 1) { if (!data.grabExcessHorizontalSpace || data.minimumWidth != 0) { int spanWidth = 0, spanExpandCount = 0; for (int k=0; k 0) { if (spanExpandCount == 0) { widths [j] += w; } else { int delta2 = w / spanExpandCount; int remainder2 = w % spanExpandCount, last2 = -1; for (int k = 0; k < hSpan; k++) { if (expandColumn [j-k]) { widths [last2=j-k] += delta2; } } if (last2 > -1) widths [last2] += remainder2; } } } } } } } if (c == 0) break; totalWidth = 0; for (int i=0; i currentWidth)) { int trim = 0; if (child instanceof Scrollable) { Rectangle rect = ((Scrollable) child).computeTrim (0, 0, 0, 0); trim = rect.width; } else { trim = child.getBorderWidth () * 2; } data.cacheWidth = data.cacheHeight = SWT.DEFAULT; data.computeSize (child, Math.max (0, currentWidth - trim), data.heightHint, false); if (data.grabExcessVerticalSpace && data.minimumHeight > 0) { data.cacheHeight = Math.max (data.cacheHeight, data.minimumHeight); } if (flush == null) flush = new GridData [count]; flush [flushLength++] = data; } } } } } } /* Row heights */ int availableHeight = height - verticalSpacing * (rowCount - 1) - (marginTop + marginHeight * 2 + marginBottom); expandCount = 0; int [] heights = new int [rowCount]; int [] minHeights = new int [rowCount]; boolean [] expandRow = new boolean [rowCount]; for (int i=0; i 1) { int spanHeight = 0, spanMinHeight = 0, spanExpandCount = 0; for (int k=0; k 0) { if (spanExpandCount == 0) { heights [i] += h; } else { int delta = h / spanExpandCount; int remainder = h % spanExpandCount, last = -1; for (int k = 0; k < vSpan; k++) { if (expandRow [i-k]) { heights [last=i-k] += delta; } } if (last > -1) heights [last] += remainder; } } if (!data.grabExcessVerticalSpace || data.minimumHeight != 0) { h = !data.grabExcessVerticalSpace || data.minimumHeight == SWT.DEFAULT ? data.cacheHeight : data.minimumHeight; h += data.verticalIndent - spanMinHeight - (vSpan - 1) * verticalSpacing; if (h > 0) { if (spanExpandCount == 0) { minHeights [i] += h; } else { int delta = h / spanExpandCount; int remainder = h % spanExpandCount, last = -1; for (int k = 0; k < vSpan; k++) { if (expandRow [i-k]) { minHeights [last=i-k] += delta; } } if (last > -1) minHeights [last] += remainder; } } } } } } } if (height != SWT.DEFAULT && expandCount > 0) { int totalHeight = 0; for (int i=0; i minHeights [i]) { heights [last = i] = heights [i] + delta; } else { heights [i] = minHeights [i]; expandRow [i] = false; c--; } } } if (last > -1) heights [last] += remainder; for (int i=0; i 1) { if (!data.grabExcessVerticalSpace || data.minimumHeight != 0) { int spanHeight = 0, spanExpandCount = 0; for (int k=0; k 0) { if (spanExpandCount == 0) { heights [i] += h; } else { int delta2 = h / spanExpandCount; int remainder2 = h % spanExpandCount, last2 = -1; for (int k = 0; k < vSpan; k++) { if (expandRow [i-k]) { heights [last2=i-k] += delta2; } } if (last2 > -1) heights [last2] += remainder2; } } } } } } } if (c == 0) break; totalHeight = 0; for (int i=0; i