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