001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.swt.layout;
011:
012: import org.eclipse.swt.*;
013: import org.eclipse.swt.graphics.*;
014: import org.eclipse.swt.widgets.*;
015:
016: /**
017: * <code>FillLayout</code> is the simplest layout class. It lays out
018: * controls in a single row or column, forcing them to be the same size.
019: * <p>
020: * Initially, the controls will all be as tall as the tallest control,
021: * and as wide as the widest. <code>FillLayout</code> does not wrap,
022: * but you can specify margins and spacing. You might use it to
023: * lay out buttons in a task bar or tool bar, or to stack checkboxes
024: * in a <code>Group</code>. <code>FillLayout</code> can also be used
025: * when a <code>Composite</code> only has one child. For example,
026: * if a <code>Shell</code> has a single <code>Group</code> child,
027: * <code>FillLayout</code> will cause the <code>Group</code> to
028: * completely fill the <code>Shell</code> (if margins are 0).
029: * </p>
030: * <p>
031: * Example code: first a <code>FillLayout</code> is created and
032: * its type field is set, and then the layout is set into the
033: * <code>Composite</code>. Note that in a <code>FillLayout</code>,
034: * children are always the same size, and they fill all available space.
035: * <pre>
036: * FillLayout fillLayout = new FillLayout();
037: * fillLayout.type = SWT.VERTICAL;
038: * shell.setLayout(fillLayout);
039: * </pre>
040: * </p>
041: */
042: public final class FillLayout extends Layout {
043: /**
044: * type specifies how controls will be positioned
045: * within the layout.
046: *
047: * The default value is HORIZONTAL.
048: *
049: * Possible values are: <ul>
050: * <li>HORIZONTAL: Position the controls horizontally from left to right</li>
051: * <li>VERTICAL: Position the controls vertically from top to bottom</li>
052: * </ul>
053: */
054: public int type = SWT.HORIZONTAL;
055:
056: /**
057: * marginWidth specifies the number of pixels of horizontal margin
058: * that will be placed along the left and right edges of the layout.
059: *
060: * The default value is 0.
061: *
062: * @since 3.0
063: */
064: public int marginWidth = 0;
065:
066: /**
067: * marginHeight specifies the number of pixels of vertical margin
068: * that will be placed along the top and bottom edges of the layout.
069: *
070: * The default value is 0.
071: *
072: * @since 3.0
073: */
074: public int marginHeight = 0;
075:
076: /**
077: * spacing specifies the number of pixels between the edge of one cell
078: * and the edge of its neighbouring cell.
079: *
080: * The default value is 0.
081: *
082: * @since 3.0
083: */
084: public int spacing = 0;
085:
086: /**
087: * Constructs a new instance of this class.
088: */
089: public FillLayout() {
090: }
091:
092: /**
093: * Constructs a new instance of this class given the type.
094: *
095: * @param type the type of fill layout
096: *
097: * @since 2.0
098: */
099: public FillLayout(int type) {
100: this .type = type;
101: }
102:
103: protected Point computeSize(Composite composite, int wHint,
104: int hHint, boolean flushCache) {
105: Control[] children = composite.getChildren();
106: int count = children.length;
107: int maxWidth = 0, maxHeight = 0;
108: for (int i = 0; i < count; i++) {
109: Control child = children[i];
110: int w = wHint, h = hHint;
111: if (count > 0) {
112: if (type == SWT.HORIZONTAL && wHint != SWT.DEFAULT) {
113: w = Math.max(0, (wHint - (count - 1) * spacing)
114: / count);
115: }
116: if (type == SWT.VERTICAL && hHint != SWT.DEFAULT) {
117: h = Math.max(0, (hHint - (count - 1) * spacing)
118: / count);
119: }
120: }
121: Point size = computeChildSize(child, w, h, flushCache);
122: maxWidth = Math.max(maxWidth, size.x);
123: maxHeight = Math.max(maxHeight, size.y);
124: }
125: int width = 0, height = 0;
126: if (type == SWT.HORIZONTAL) {
127: width = count * maxWidth;
128: if (count != 0)
129: width += (count - 1) * spacing;
130: height = maxHeight;
131: } else {
132: width = maxWidth;
133: height = count * maxHeight;
134: if (count != 0)
135: height += (count - 1) * spacing;
136: }
137: width += marginWidth * 2;
138: height += marginHeight * 2;
139: if (wHint != SWT.DEFAULT)
140: width = wHint;
141: if (hHint != SWT.DEFAULT)
142: height = hHint;
143: return new Point(width, height);
144: }
145:
146: Point computeChildSize(Control control, int wHint, int hHint,
147: boolean flushCache) {
148: FillData data = (FillData) control.getLayoutData();
149: if (data == null) {
150: data = new FillData();
151: control.setLayoutData(data);
152: }
153: Point size = null;
154: if (wHint == SWT.DEFAULT && hHint == SWT.DEFAULT) {
155: size = data.computeSize(control, wHint, hHint, flushCache);
156: } else {
157: // TEMPORARY CODE
158: int trimX, trimY;
159: if (control instanceof Scrollable) {
160: Rectangle rect = ((Scrollable) control).computeTrim(0,
161: 0, 0, 0);
162: trimX = rect.width;
163: trimY = rect.height;
164: } else {
165: trimX = trimY = control.getBorderWidth() * 2;
166: }
167: int w = wHint == SWT.DEFAULT ? wHint : Math.max(0, wHint
168: - trimX);
169: int h = hHint == SWT.DEFAULT ? hHint : Math.max(0, hHint
170: - trimY);
171: size = data.computeSize(control, w, h, flushCache);
172: }
173: return size;
174: }
175:
176: protected boolean flushCache(Control control) {
177: Object data = control.getLayoutData();
178: if (data != null)
179: ((FillData) data).flushCache();
180: return true;
181: }
182:
183: String getName() {
184: String string = getClass().getName();
185: int index = string.lastIndexOf('.');
186: if (index == -1)
187: return string;
188: return string.substring(index + 1, string.length());
189: }
190:
191: protected void layout(Composite composite, boolean flushCache) {
192: Rectangle rect = composite.getClientArea();
193: Control[] children = composite.getChildren();
194: int count = children.length;
195: if (count == 0)
196: return;
197: int width = rect.width - marginWidth * 2;
198: int height = rect.height - marginHeight * 2;
199: if (type == SWT.HORIZONTAL) {
200: width -= (count - 1) * spacing;
201: int x = rect.x + marginWidth, extra = width % count;
202: int y = rect.y + marginHeight, cellWidth = width / count;
203: for (int i = 0; i < count; i++) {
204: Control child = children[i];
205: int childWidth = cellWidth;
206: if (i == 0) {
207: childWidth += extra / 2;
208: } else {
209: if (i == count - 1)
210: childWidth += (extra + 1) / 2;
211: }
212: child.setBounds(x, y, childWidth, height);
213: x += childWidth + spacing;
214: }
215: } else {
216: height -= (count - 1) * spacing;
217: int x = rect.x + marginWidth, cellHeight = height / count;
218: int y = rect.y + marginHeight, extra = height % count;
219: for (int i = 0; i < count; i++) {
220: Control child = children[i];
221: int childHeight = cellHeight;
222: if (i == 0) {
223: childHeight += extra / 2;
224: } else {
225: if (i == count - 1)
226: childHeight += (extra + 1) / 2;
227: }
228: child.setBounds(x, y, width, childHeight);
229: y += childHeight + spacing;
230: }
231: }
232: }
233:
234: /**
235: * Returns a string containing a concise, human-readable
236: * description of the receiver.
237: *
238: * @return a string representation of the layout
239: */
240: public String toString() {
241: String string = getName() + " {";
242: string += "type="
243: + ((type == SWT.VERTICAL) ? "SWT.VERTICAL"
244: : "SWT.HORIZONTAL") + " ";
245: if (marginWidth != 0)
246: string += "marginWidth=" + marginWidth + " ";
247: if (marginHeight != 0)
248: string += "marginHeight=" + marginHeight + " ";
249: if (spacing != 0)
250: string += "spacing=" + spacing + " ";
251: string = string.trim();
252: string += "}";
253: return string;
254: }
255: }
|