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 * <code>FillLayout</code> is the simplest layout class. It lays out
22 * controls in a single row or column, forcing them to be the same size.
24 * Initially, the controls will all be as tall as the tallest control,
25 * and as wide as the widest. <code>FillLayout</code> does not wrap,
26 * but you can specify margins and spacing. You might use it to
27 * lay out buttons in a task bar or tool bar, or to stack checkboxes
28 * in a <code>Group</code>. <code>FillLayout</code> can also be used
29 * when a <code>Composite</code> only has one child. For example,
30 * if a <code>Shell</code> has a single <code>Group</code> child,
31 * <code>FillLayout</code> will cause the <code>Group</code> to
32 * completely fill the <code>Shell</code> (if margins are 0).
35 * Example code: first a <code>FillLayout</code> is created and
36 * its type field is set, and then the layout is set into the
37 * <code>Composite</code>. Note that in a <code>FillLayout</code>,
38 * children are always the same size, and they fill all available space.
41 * FillLayout fillLayout = new FillLayout();
42 * fillLayout.type = SWT.VERTICAL;
43 * shell.setLayout(fillLayout);
46 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: LayoutExample</a>
47 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
49 public final class FillLayout extends Layout {
51 * type specifies how controls will be positioned
54 * The default value is HORIZONTAL.
56 * Possible values are: <ul>
57 * <li>HORIZONTAL: Position the controls horizontally from left to right</li>
58 * <li>VERTICAL: Position the controls vertically from top to bottom</li>
61 public int type = SWT.HORIZONTAL;
64 * marginWidth specifies the number of points of horizontal margin
65 * that will be placed along the left and right edges of the layout.
67 * The default value is 0.
71 public int marginWidth = 0;
74 * marginHeight specifies the number of points of vertical margin
75 * that will be placed along the top and bottom edges of the layout.
77 * The default value is 0.
81 public int marginHeight = 0;
84 * spacing specifies the number of points between the edge of one cell
85 * and the edge of its neighbouring cell.
87 * The default value is 0.
91 public int spacing = 0;
94 * Constructs a new instance of this class.
96 public FillLayout () {
100 * Constructs a new instance of this class given the type.
102 * @param type the type of fill layout
106 public FillLayout (int type) {
111 protected Point computeSize (Composite composite, int wHint, int hHint, boolean flushCache) {
112 Control [] children = composite.getChildren ();
113 int count = children.length;
114 int maxWidth = 0, maxHeight = 0;
115 for (int i=0; i<count; i++) {
116 Control child = children [i];
117 int w = wHint, h = hHint;
119 if (type == SWT.HORIZONTAL && wHint != SWT.DEFAULT) {
120 w = Math.max (0, (wHint - (count - 1) * spacing) / count);
122 if (type == SWT.VERTICAL && hHint != SWT.DEFAULT) {
123 h = Math.max (0, (hHint - (count - 1) * spacing) / count);
126 Point size = computeChildSize (child, w, h, flushCache);
127 maxWidth = Math.max (maxWidth, size.x);
128 maxHeight = Math.max (maxHeight, size.y);
130 int width = 0, height = 0;
131 if (type == SWT.HORIZONTAL) {
132 width = count * maxWidth;
133 if (count != 0) width += (count - 1) * spacing;
137 height = count * maxHeight;
138 if (count != 0) height += (count - 1) * spacing;
140 width += marginWidth * 2;
141 height += marginHeight * 2;
142 if (wHint != SWT.DEFAULT) width = wHint;
143 if (hHint != SWT.DEFAULT) height = hHint;
144 return new Point (width, height);
147 Point computeChildSize (Control control, int wHint, int hHint, boolean flushCache) {
148 FillData data = (FillData)control.getLayoutData ();
150 data = new FillData ();
151 control.setLayoutData (data);
154 if (wHint == SWT.DEFAULT && hHint == SWT.DEFAULT) {
155 size = data.computeSize (control, wHint, hHint, flushCache);
159 if (control instanceof Scrollable) {
160 Rectangle rect = ((Scrollable) control).computeTrim (0, 0, 0, 0);
164 trimX = trimY = control.getBorderWidth () * 2;
166 int w = wHint == SWT.DEFAULT ? wHint : Math.max (0, wHint - trimX);
167 int h = hHint == SWT.DEFAULT ? hHint : Math.max (0, hHint - trimY);
168 size = data.computeSize (control, w, h, flushCache);
174 protected boolean flushCache (Control control) {
175 Object data = control.getLayoutData();
176 if (data != null) ((FillData)data).flushCache();
181 String string = getClass ().getName ();
182 int index = string.lastIndexOf ('.');
183 if (index == -1) return string;
184 return string.substring (index + 1, string.length ());
188 protected void layout (Composite composite, boolean flushCache) {
189 Rectangle rect = composite.getClientArea ();
190 Control [] children = composite.getChildren ();
191 int count = children.length;
192 if (count == 0) return;
193 int width = rect.width - marginWidth * 2;
194 int height = rect.height - marginHeight * 2;
195 if (type == SWT.HORIZONTAL) {
196 width -= (count - 1) * spacing;
197 int x = rect.x + marginWidth, extra = width % count;
198 int y = rect.y + marginHeight, cellWidth = width / count;
199 for (int i=0; i<count; i++) {
200 Control child = children [i];
201 int childWidth = cellWidth;
203 childWidth += extra / 2;
205 if (i == count - 1) childWidth += (extra + 1) / 2;
207 child.setBounds (x, y, childWidth, height);
208 x += childWidth + spacing;
211 height -= (count - 1) * spacing;
212 int x = rect.x + marginWidth, cellHeight = height / count;
213 int y = rect.y + marginHeight, extra = height % count;
214 for (int i=0; i<count; i++) {
215 Control child = children [i];
216 int childHeight = cellHeight;
218 childHeight += extra / 2;
220 if (i == count - 1) childHeight += (extra + 1) / 2;
222 child.setBounds (x, y, width, childHeight);
223 y += childHeight + spacing;
229 * Returns a string containing a concise, human-readable
230 * description of the receiver.
232 * @return a string representation of the layout
235 public String toString () {
236 String string = getName ()+" {";
237 string += "type="+((type == SWT.VERTICAL) ? "SWT.VERTICAL" : "SWT.HORIZONTAL")+" ";
238 if (marginWidth != 0) string += "marginWidth="+marginWidth+" ";
239 if (marginHeight != 0) string += "marginHeight="+marginHeight+" ";
240 if (spacing != 0) string += "spacing="+spacing+" ";
241 string = string.trim();