comparison dwt/layout/FillLayout.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 649b8e223d5a
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
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.dwthelper.utils;
14
15 import dwt.DWT;
16 import dwt.graphics.Point;
17 import dwt.graphics.Rectangle;
18 import dwt.widgets.Composite;
19 import dwt.widgets.Control;
20 import dwt.widgets.Layout;
21 import dwt.widgets.Scrollable;
22
23 /**
24 * <code>FillLayout</code> is the simplest layout class. It lays out
25 * controls in a single row or column, forcing them to be the same size.
26 * <p>
27 * Initially, the controls will all be as tall as the tallest control,
28 * and as wide as the widest. <code>FillLayout</code> does not wrap,
29 * but you can specify margins and spacing. You might use it to
30 * lay out buttons in a task bar or tool bar, or to stack checkboxes
31 * in a <code>Group</code>. <code>FillLayout</code> can also be used
32 * when a <code>Composite</code> only has one child. For example,
33 * if a <code>Shell</code> has a single <code>Group</code> child,
34 * <code>FillLayout</code> will cause the <code>Group</code> to
35 * completely fill the <code>Shell</code> (if margins are 0).
36 * </p>
37 * <p>
38 * Example code: first a <code>FillLayout</code> is created and
39 * its type field is set, and then the layout is set into the
40 * <code>Composite</code>. Note that in a <code>FillLayout</code>,
41 * children are always the same size, and they fill all available space.
42 * <pre>
43 * FillLayout fillLayout = new FillLayout();
44 * fillLayout.type = DWT.VERTICAL;
45 * shell.setLayout(fillLayout);
46 * </pre>
47 * </p>
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 = DWT.HORIZONTAL;
62
63 /**
64 * marginWidth specifies the number of pixels 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 pixels 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 pixels 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 protected Point computeSize (Composite composite, int wHint, int hHint, bool flushCache) {
111 Control [] children = composite.getChildren ();
112 int count = children.length;
113 int maxWidth = 0, maxHeight = 0;
114 for (int i=0; i<count; i++) {
115 Control child = children [i];
116 int w = wHint, h = hHint;
117 if (count > 0) {
118 if (type is DWT.HORIZONTAL && wHint !is DWT.DEFAULT) {
119 w = Math.max (0, (wHint - (count - 1) * spacing) / count);
120 }
121 if (type is DWT.VERTICAL && hHint !is DWT.DEFAULT) {
122 h = Math.max (0, (hHint - (count - 1) * spacing) / count);
123 }
124 }
125 Point size = computeChildSize (child, w, h, flushCache);
126 maxWidth = Math.max (maxWidth, size.x);
127 maxHeight = Math.max (maxHeight, size.y);
128 }
129 int width = 0, height = 0;
130 if (type is DWT.HORIZONTAL) {
131 width = count * maxWidth;
132 if (count !is 0) width += (count - 1) * spacing;
133 height = maxHeight;
134 } else {
135 width = maxWidth;
136 height = count * maxHeight;
137 if (count !is 0) height += (count - 1) * spacing;
138 }
139 width += marginWidth * 2;
140 height += marginHeight * 2;
141 if (wHint !is DWT.DEFAULT) width = wHint;
142 if (hHint !is DWT.DEFAULT) height = hHint;
143 return new Point (width, height);
144 }
145
146 Point computeChildSize (Control control, int wHint, int hHint, bool flushCache) {
147 FillData data = (FillData)control.getLayoutData ();
148 if (data is null) {
149 data = new FillData ();
150 control.setLayoutData (data);
151 }
152 Point size = null;
153 if (wHint is DWT.DEFAULT && hHint is DWT.DEFAULT) {
154 size = data.computeSize (control, wHint, hHint, flushCache);
155 } else {
156 // TEMPORARY CODE
157 int trimX, trimY;
158 if (control instanceof Scrollable) {
159 Rectangle rect = ((Scrollable) control).computeTrim (0, 0, 0, 0);
160 trimX = rect.width;
161 trimY = rect.height;
162 } else {
163 trimX = trimY = control.getBorderWidth () * 2;
164 }
165 int w = wHint is DWT.DEFAULT ? wHint : Math.max (0, wHint - trimX);
166 int h = hHint is DWT.DEFAULT ? hHint : Math.max (0, hHint - trimY);
167 size = data.computeSize (control, w, h, flushCache);
168 }
169 return size;
170 }
171
172 protected bool flushCache (Control control) {
173 Object data = control.getLayoutData();
174 if (data !is null) ((FillData)data).flushCache();
175 return true;
176 }
177
178 String getName () {
179 String string = getClass ().getName ();
180 int index = string.lastIndexOf ('.');
181 if (index is -1) return string;
182 return string.substring (index + 1, string.length ());
183 }
184
185 protected void layout (Composite composite, bool flushCache) {
186 Rectangle rect = composite.getClientArea ();
187 Control [] children = composite.getChildren ();
188 int count = children.length;
189 if (count is 0) return;
190 int width = rect.width - marginWidth * 2;
191 int height = rect.height - marginHeight * 2;
192 if (type is DWT.HORIZONTAL) {
193 width -= (count - 1) * spacing;
194 int x = rect.x + marginWidth, extra = width % count;
195 int y = rect.y + marginHeight, cellWidth = width / count;
196 for (int i=0; i<count; i++) {
197 Control child = children [i];
198 int childWidth = cellWidth;
199 if (i is 0) {
200 childWidth += extra / 2;
201 } else {
202 if (i is count - 1) childWidth += (extra + 1) / 2;
203 }
204 child.setBounds (x, y, childWidth, height);
205 x += childWidth + spacing;
206 }
207 } else {
208 height -= (count - 1) * spacing;
209 int x = rect.x + marginWidth, cellHeight = height / count;
210 int y = rect.y + marginHeight, extra = height % count;
211 for (int i=0; i<count; i++) {
212 Control child = children [i];
213 int childHeight = cellHeight;
214 if (i is 0) {
215 childHeight += extra / 2;
216 } else {
217 if (i is count - 1) childHeight += (extra + 1) / 2;
218 }
219 child.setBounds (x, y, width, childHeight);
220 y += childHeight + spacing;
221 }
222 }
223 }
224
225 /**
226 * Returns a string containing a concise, human-readable
227 * description of the receiver.
228 *
229 * @return a string representation of the layout
230 */
231 public String toString () {
232 String string = getName ()+" {";
233 string += "type="+((type is DWT.VERTICAL) ? "DWT.VERTICAL" : "DWT.HORIZONTAL")+" ";
234 if (marginWidth !is 0) string += "marginWidth="+marginWidth+" ";
235 if (marginHeight !is 0) string += "marginHeight="+marginHeight+" ";
236 if (spacing !is 0) string += "spacing="+spacing+" ";
237 string = string.trim();
238 string += "}";
239 return string;
240 }
241 }