comparison dwt/layout/FillLayout.d @ 0:5406a8f6526d

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