]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/layout/FillLayout.java
f66f280bc8b2dbb5059de5c07a24b9f50d1802e7
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / layout / FillLayout.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  * <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.
23  * <p>
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).
33  * </p>
34  * <p>
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.
39  * </p>
40  * <pre>
41  *              FillLayout fillLayout = new FillLayout();
42  *              fillLayout.type = SWT.VERTICAL;
43  *              shell.setLayout(fillLayout);
44  * </pre>
45  *
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>
48  */
49 public final class FillLayout extends Layout {
50         /**
51          * type specifies how controls will be positioned
52          * within the layout.
53          *
54          * The default value is HORIZONTAL.
55          *
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>
59          * </ul>
60          */
61         public int type = SWT.HORIZONTAL;
62
63         /**
64          * marginWidth specifies the number of points of horizontal margin
65          * that will be placed along the left and right edges of the layout.
66          *
67          * The default value is 0.
68          *
69          * @since 3.0
70          */
71         public int marginWidth = 0;
72
73         /**
74          * marginHeight specifies the number of points of vertical margin
75          * that will be placed along the top and bottom edges of the layout.
76          *
77          * The default value is 0.
78          *
79          * @since 3.0
80          */
81         public int marginHeight = 0;
82
83         /**
84          * spacing specifies the number of points between the edge of one cell
85          * and the edge of its neighbouring cell.
86          *
87          * The default value is 0.
88          *
89          * @since 3.0
90          */
91         public int spacing = 0;
92
93 /**
94  * Constructs a new instance of this class.
95  */
96 public FillLayout () {
97 }
98
99 /**
100  * Constructs a new instance of this class given the type.
101  *
102  * @param type the type of fill layout
103  *
104  * @since 2.0
105  */
106 public FillLayout (int type) {
107         this.type = type;
108 }
109
110 @Override
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;
118                 if (count > 0) {
119                         if (type == SWT.HORIZONTAL && wHint != SWT.DEFAULT) {
120                                 w = Math.max (0, (wHint - (count - 1) * spacing) / count);
121                         }
122                         if (type == SWT.VERTICAL && hHint != SWT.DEFAULT) {
123                                 h = Math.max (0, (hHint - (count - 1) * spacing) / count);
124                         }
125                 }
126                 Point size = computeChildSize (child, w, h, flushCache);
127                 maxWidth = Math.max (maxWidth, size.x);
128                 maxHeight = Math.max (maxHeight, size.y);
129         }
130         int width = 0, height = 0;
131         if (type == SWT.HORIZONTAL) {
132                 width = count * maxWidth;
133                 if (count != 0) width += (count - 1) * spacing;
134                 height = maxHeight;
135         } else {
136                 width = maxWidth;
137                 height = count * maxHeight;
138                 if (count != 0) height += (count - 1) * spacing;
139         }
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);
145 }
146
147 Point computeChildSize (Control control, int wHint, int hHint, 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, 0, 0, 0);
161                         trimX = rect.width;
162                         trimY = rect.height;
163                 } else {
164                         trimX = trimY = control.getBorderWidth () * 2;
165                 }
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);
169         }
170         return size;
171 }
172
173 @Override
174 protected boolean flushCache (Control control) {
175         Object data = control.getLayoutData();
176         if (data != null) ((FillData)data).flushCache();
177         return true;
178 }
179
180 String getName () {
181         String string = getClass ().getName ();
182         int index = string.lastIndexOf ('.');
183         if (index == -1) return string;
184         return string.substring (index + 1, string.length ());
185 }
186
187 @Override
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;
202                         if (i == 0) {
203                                 childWidth += extra / 2;
204                         } else {
205                                 if (i == count - 1) childWidth += (extra + 1) / 2;
206                         }
207                         child.setBounds (x, y, childWidth, height);
208                         x += childWidth + spacing;
209                 }
210         } else {
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;
217                         if (i == 0) {
218                                 childHeight += extra / 2;
219                         } else {
220                                 if (i == count - 1) childHeight += (extra + 1) / 2;
221                         }
222                         child.setBounds (x, y, width, childHeight);
223                         y += childHeight + spacing;
224                 }
225         }
226 }
227
228 /**
229  * Returns a string containing a concise, human-readable
230  * description of the receiver.
231  *
232  * @return a string representation of the layout
233  */
234 @Override
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();
242         string += "}";
243         return string;
244 }
245 }