1 /*******************************************************************************
2  * Copyright (c) 2000, 2008 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  * Port to the D programming language:
11  *     Frank Benoit <benoit@tionex.de>
12  *******************************************************************************/
13 module org.eclipse.swt.layout.FillLayout;
14 
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.graphics.Point;
17 import org.eclipse.swt.graphics.Rectangle;
18 import org.eclipse.swt.widgets.Control;
19 import org.eclipse.swt.widgets.Layout;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Scrollable;
22 import org.eclipse.swt.layout.FillData;
23 
24 import java.lang.all;
25 
26 /**
27  * <code>FillLayout</code> is the simplest layout class. It lays out
28  * controls in a single row or column, forcing them to be the same size.
29  * <p>
30  * Initially, the controls will all be as tall as the tallest control,
31  * and as wide as the widest. <code>FillLayout</code> does not wrap,
32  * but you can specify margins and spacing. You might use it to
33  * lay out buttons in a task bar or tool bar, or to stack checkboxes
34  * in a <code>Group</code>. <code>FillLayout</code> can also be used
35  * when a <code>Composite</code> only has one child. For example,
36  * if a <code>Shell</code> has a single <code>Group</code> child,
37  * <code>FillLayout</code> will cause the <code>Group</code> to
38  * completely fill the <code>Shell</code> (if margins are 0).
39  * </p>
40  * <p>
41  * Example code: first a <code>FillLayout</code> is created and
42  * its type field is set, and then the layout is set into the
43  * <code>Composite</code>. Note that in a <code>FillLayout</code>,
44  * children are always the same size, and they fill all available space.
45  * <pre>
46  *      FillLayout fillLayout = new FillLayout();
47  *      fillLayout.type = SWT.VERTICAL;
48  *      shell.setLayout(fillLayout);
49  * </pre>
50  * </p>
51  *
52  * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: LayoutExample</a>
53  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
54  */
55 public final class FillLayout : Layout {
56     /**
57      * type specifies how controls will be positioned
58      * within the layout.
59      *
60      * The default value is HORIZONTAL.
61      *
62      * Possible values are: <ul>
63      *    <li>HORIZONTAL: Position the controls horizontally from left to right</li>
64      *    <li>VERTICAL: Position the controls vertically from top to bottom</li>
65      * </ul>
66      */
67     public int type = SWT.HORIZONTAL;
68 
69     /**
70      * marginWidth specifies the number of pixels of horizontal margin
71      * that will be placed along the left and right edges of the layout.
72      *
73      * The default value is 0.
74      *
75      * @since 3.0
76      */
77     public int marginWidth = 0;
78 
79     /**
80      * marginHeight specifies the number of pixels of vertical margin
81      * that will be placed along the top and bottom edges of the layout.
82      *
83      * The default value is 0.
84      *
85      * @since 3.0
86      */
87     public int marginHeight = 0;
88 
89     /**
90      * spacing specifies the number of pixels between the edge of one cell
91      * and the edge of its neighbouring cell.
92      *
93      * The default value is 0.
94      *
95      * @since 3.0
96      */
97     public int spacing = 0;
98 
99 /**
100  * Constructs a new instance of this class.
101  */
102 public this () {
103 }
104 
105 /**
106  * Constructs a new instance of this class given the type.
107  *
108  * @param type the type of fill layout
109  *
110  * @since 2.0
111  */
112 public this (int type) {
113     this.type = type;
114 }
115 
116 override protected Point computeSize (Composite composite, int wHint, int hHint, bool flushCache) {
117     Control [] children = composite.getChildren ();
118     int count = cast(int)/*64bit*/children.length;
119     int maxWidth = 0, maxHeight = 0;
120     for (int i=0; i<count; i++) {
121         Control child = children [i];
122         int w = wHint, h = hHint;
123         if (count > 0) {
124             if (type is SWT.HORIZONTAL && wHint !is SWT.DEFAULT) {
125                 w = Math.max (0, (wHint - (count - 1) * spacing) / count);
126             }
127             if (type is SWT.VERTICAL && hHint !is SWT.DEFAULT) {
128                 h = Math.max (0, (hHint - (count - 1) * spacing) / count);
129             }
130         }
131         Point size = computeChildSize (child, w, h, flushCache);
132         maxWidth = Math.max (maxWidth, size.x);
133         maxHeight = Math.max (maxHeight, size.y);
134     }
135     int width = 0, height = 0;
136     if (type is SWT.HORIZONTAL) {
137         width = count * maxWidth;
138         if (count !is 0) width += (count - 1) * spacing;
139         height = maxHeight;
140     } else {
141         width = maxWidth;
142         height = count * maxHeight;
143         if (count !is 0) height += (count - 1) * spacing;
144     }
145     width += marginWidth * 2;
146     height += marginHeight * 2;
147     if (wHint !is SWT.DEFAULT) width = wHint;
148     if (hHint !is SWT.DEFAULT) height = hHint;
149     return new Point (width, height);
150 }
151 
152 Point computeChildSize (Control control, int wHint, int hHint, bool flushCache) {
153     FillData data = cast(FillData)control.getLayoutData ();
154     if (data is null) {
155         data = new FillData ();
156         control.setLayoutData (data);
157     }
158     Point size = null;
159     if (wHint is SWT.DEFAULT && hHint is SWT.DEFAULT) {
160         size = data.computeSize (control, wHint, hHint, flushCache);
161     } else {
162         // TEMPORARY CODE
163         int trimX, trimY;
164         if ( auto sa = cast(Scrollable)control ) {
165             Rectangle rect = sa.computeTrim (0, 0, 0, 0);
166             trimX = rect.width;
167             trimY = rect.height;
168         } else {
169             trimX = trimY = control.getBorderWidth () * 2;
170         }
171         int w = wHint is SWT.DEFAULT ? wHint : Math.max (0, wHint - trimX);
172         int h = hHint is SWT.DEFAULT ? hHint : Math.max (0, hHint - trimY);
173         size = data.computeSize (control, w, h, flushCache);
174     }
175     return size;
176 }
177 
178 override protected bool flushCache (Control control) {
179     Object data = control.getLayoutData();
180     if (data !is null) (cast(FillData)data).flushCache();
181     return true;
182 }
183 
184 String getName () {
185     String string = this.classinfo.name;
186     int index = string.lastIndexOf( '.');
187     if (index is -1 ) return string;
188     return string[ index + 1 .. string.length ];
189 }
190 
191 override protected void layout (Composite composite, bool flushCache) {
192     Rectangle rect = composite.getClientArea ();
193     Control [] children = composite.getChildren ();
194     int count = cast(int)/*64bit*/children.length;
195     if (count is 0) return;
196     int width = rect.width - marginWidth * 2;
197     int height = rect.height - marginHeight * 2;
198     if (type is SWT.HORIZONTAL) {
199         width -= (count - 1) * spacing;
200         int x = rect.x + marginWidth, extra = width % count;
201         int y = rect.y + marginHeight, cellWidth = width / count;
202         for (int i=0; i<count; i++) {
203             Control child = children [i];
204             int childWidth = cellWidth;
205             if (i is 0) {
206                 childWidth += extra / 2;
207             } else {
208                 if (i is count - 1) childWidth += (extra + 1) / 2;
209             }
210             child.setBounds (x, y, childWidth, height);
211             x += childWidth + spacing;
212         }
213     } else {
214         height -= (count - 1) * spacing;
215         int x = rect.x + marginWidth, cellHeight = height / count;
216         int y = rect.y + marginHeight, extra = height % count;
217         for (int i=0; i<count; i++) {
218             Control child = children [i];
219             int childHeight = cellHeight;
220             if (i is 0) {
221                 childHeight += extra / 2;
222             } else {
223                 if (i is count - 1) childHeight += (extra + 1) / 2;
224             }
225             child.setBounds (x, y, width, childHeight);
226             y += childHeight + spacing;
227         }
228     }
229 }
230 
231 /**
232  * Returns a string containing a concise, human-readable
233  * description of the receiver.
234  *
235  * @return a string representation of the layout
236  */
237 override public String toString () {
238     String string = getName () ~ " {";
239     string ~= "type="~((type is SWT.VERTICAL) ? "SWT.VERTICAL" : "SWT.HORIZONTAL")~" ";
240     if (marginWidth !is 0) string ~= "marginWidth="~String_valueOf(marginWidth)~" ";
241     if (marginHeight !is 0) string ~= "marginHeight="~String_valueOf(marginHeight)~" ";
242     if (spacing !is 0) string ~= "spacing="~String_valueOf(spacing)~" ";
243     string = string.trim();
244     string ~= "}";
245     return string;
246 }
247 }