Mercurial > projects > dwt-mac
comparison dwt/layout/FormData.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 | 1a8b3cb347e0 |
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.FormData; | |
12 | |
13 import dwt.dwthelper.utils; | |
14 | |
15 | |
16 import dwt.DWT; | |
17 import dwt.graphics.Point; | |
18 import dwt.widgets.Control; | |
19 | |
20 /** | |
21 * Instances of this class are used to define the attachments | |
22 * of a control in a <code>FormLayout</code>. | |
23 * <p> | |
24 * To set a <code>FormData</code> object into a control, you use the | |
25 * <code>setLayoutData ()</code> method. To define attachments for the | |
26 * <code>FormData</code>, set the fields directly, like this: | |
27 * <pre> | |
28 * FormData data = new FormData(); | |
29 * data.left = new FormAttachment(0,5); | |
30 * data.right = new FormAttachment(100,-5); | |
31 * button.setLayoutData(formData); | |
32 * </pre> | |
33 * </p> | |
34 * <p> | |
35 * <code>FormData</code> contains the <code>FormAttachments</code> for | |
36 * each edge of the control that the <code>FormLayout</code> uses to | |
37 * determine the size and position of the control. <code>FormData</code> | |
38 * objects also allow you to set the width and height of controls within | |
39 * a <code>FormLayout</code>. | |
40 * </p> | |
41 * | |
42 * @see FormLayout | |
43 * @see FormAttachment | |
44 * | |
45 * @since 2.0 | |
46 */ | |
47 public final class FormData { | |
48 /** | |
49 * width specifies the preferred width in pixels. This value | |
50 * is the wHint passed into Control.computeSize(int, int, bool) | |
51 * to determine the preferred size of the control. | |
52 * | |
53 * The default value is DWT.DEFAULT. | |
54 * | |
55 * @see Control#computeSize(int, int, bool) | |
56 */ | |
57 public int width = DWT.DEFAULT; | |
58 /** | |
59 * height specifies the preferred height in pixels. This value | |
60 * is the hHint passed into Control.computeSize(int, int, bool) | |
61 * to determine the preferred size of the control. | |
62 * | |
63 * The default value is DWT.DEFAULT. | |
64 * | |
65 * @see Control#computeSize(int, int, bool) | |
66 */ | |
67 public int height = DWT.DEFAULT; | |
68 /** | |
69 * left specifies the attachment of the left side of | |
70 * the control. | |
71 */ | |
72 public FormAttachment left; | |
73 /** | |
74 * right specifies the attachment of the right side of | |
75 * the control. | |
76 */ | |
77 public FormAttachment right; | |
78 /** | |
79 * top specifies the attachment of the top of the control. | |
80 */ | |
81 public FormAttachment top; | |
82 /** | |
83 * bottom specifies the attachment of the bottom of the | |
84 * control. | |
85 */ | |
86 public FormAttachment bottom; | |
87 | |
88 int cacheWidth = -1, cacheHeight = -1; | |
89 int defaultWhint, defaultHhint, defaultWidth = -1, defaultHeight = -1; | |
90 int currentWhint, currentHhint, currentWidth = -1, currentHeight = -1; | |
91 FormAttachment cacheLeft, cacheRight, cacheTop, cacheBottom; | |
92 bool isVisited, needed; | |
93 | |
94 /** | |
95 * Constructs a new instance of FormData using | |
96 * default values. | |
97 */ | |
98 public FormData () { | |
99 } | |
100 | |
101 /** | |
102 * Constructs a new instance of FormData according to the parameters. | |
103 * A value of DWT.DEFAULT indicates that no minimum width or | |
104 * no minimum height is specified. | |
105 * | |
106 * @param width a minimum width for the control | |
107 * @param height a minimum height for the control | |
108 */ | |
109 public FormData (int width, int height) { | |
110 this.width = width; | |
111 this.height = height; | |
112 } | |
113 | |
114 void computeSize (Control control, int wHint, int hHint, bool flushCache) { | |
115 if (cacheWidth !is -1 && cacheHeight !is -1) return; | |
116 if (wHint is this.width && hHint is this.height) { | |
117 if (defaultWidth is -1 || defaultHeight is -1 || wHint !is defaultWhint || hHint !is defaultHhint) { | |
118 Point size = control.computeSize (wHint, hHint, flushCache); | |
119 defaultWhint = wHint; | |
120 defaultHhint = hHint; | |
121 defaultWidth = size.x; | |
122 defaultHeight = size.y; | |
123 } | |
124 cacheWidth = defaultWidth; | |
125 cacheHeight = defaultHeight; | |
126 return; | |
127 } | |
128 if (currentWidth is -1 || currentHeight is -1 || wHint !is currentWhint || hHint !is currentHhint) { | |
129 Point size = control.computeSize (wHint, hHint, flushCache); | |
130 currentWhint = wHint; | |
131 currentHhint = hHint; | |
132 currentWidth = size.x; | |
133 currentHeight = size.y; | |
134 } | |
135 cacheWidth = currentWidth; | |
136 cacheHeight = currentHeight; | |
137 } | |
138 | |
139 void flushCache () { | |
140 cacheWidth = cacheHeight = -1; | |
141 defaultHeight = defaultWidth = -1; | |
142 currentHeight = currentWidth = -1; | |
143 } | |
144 | |
145 int getWidth (Control control, bool flushCache) { | |
146 needed = true; | |
147 computeSize (control, width, height, flushCache); | |
148 return cacheWidth; | |
149 } | |
150 | |
151 int getHeight (Control control, bool flushCache) { | |
152 computeSize (control, width, height, flushCache); | |
153 return cacheHeight; | |
154 } | |
155 | |
156 FormAttachment getBottomAttachment (Control control, int spacing, bool flushCache) { | |
157 if (cacheBottom !is null) return cacheBottom; | |
158 if (isVisited) return cacheBottom = new FormAttachment (0, getHeight (control, flushCache)); | |
159 if (bottom is null) { | |
160 if (top is null) return cacheBottom = new FormAttachment (0, getHeight (control, flushCache)); | |
161 return cacheBottom = getTopAttachment (control, spacing, flushCache).plus (getHeight (control, flushCache)); | |
162 } | |
163 Control bottomControl = bottom.control; | |
164 if (bottomControl !is null) { | |
165 if (bottomControl.isDisposed ()) { | |
166 bottom.control = bottomControl = null; | |
167 } else { | |
168 if (bottomControl.getParent () !is control.getParent ()) { | |
169 bottomControl = null; | |
170 } | |
171 } | |
172 } | |
173 if (bottomControl is null) return cacheBottom = bottom; | |
174 isVisited = true; | |
175 FormData bottomData = (FormData) bottomControl.getLayoutData (); | |
176 FormAttachment bottomAttachment = bottomData.getBottomAttachment (bottomControl, spacing, flushCache); | |
177 switch (bottom.alignment) { | |
178 case DWT.BOTTOM: | |
179 cacheBottom = bottomAttachment.plus (bottom.offset); | |
180 break; | |
181 case DWT.CENTER: { | |
182 FormAttachment topAttachment = bottomData.getTopAttachment (bottomControl, spacing, flushCache); | |
183 FormAttachment bottomHeight = bottomAttachment.minus (topAttachment); | |
184 cacheBottom = bottomAttachment.minus (bottomHeight.minus (getHeight (control, flushCache)).divide (2)); | |
185 break; | |
186 } | |
187 default: { | |
188 FormAttachment topAttachment = bottomData.getTopAttachment (bottomControl, spacing, flushCache); | |
189 cacheBottom = topAttachment.plus (bottom.offset - spacing); | |
190 break; | |
191 } | |
192 } | |
193 isVisited = false; | |
194 return cacheBottom; | |
195 } | |
196 | |
197 FormAttachment getLeftAttachment (Control control, int spacing, bool flushCache) { | |
198 if (cacheLeft !is null) return cacheLeft; | |
199 if (isVisited) return cacheLeft = new FormAttachment (0, 0); | |
200 if (left is null) { | |
201 if (right is null) return cacheLeft = new FormAttachment (0, 0); | |
202 return cacheLeft = getRightAttachment (control, spacing, flushCache).minus (getWidth (control, flushCache)); | |
203 } | |
204 Control leftControl = left.control; | |
205 if (leftControl !is null) { | |
206 if (leftControl.isDisposed ()) { | |
207 left.control = leftControl = null; | |
208 } else { | |
209 if (leftControl.getParent () !is control.getParent ()) { | |
210 leftControl = null; | |
211 } | |
212 } | |
213 } | |
214 if (leftControl is null) return cacheLeft = left; | |
215 isVisited = true; | |
216 FormData leftData = (FormData) leftControl.getLayoutData (); | |
217 FormAttachment leftAttachment = leftData.getLeftAttachment (leftControl, spacing, flushCache); | |
218 switch (left.alignment) { | |
219 case DWT.LEFT: | |
220 cacheLeft = leftAttachment.plus (left.offset); | |
221 break; | |
222 case DWT.CENTER: { | |
223 FormAttachment rightAttachment = leftData.getRightAttachment (leftControl, spacing, flushCache); | |
224 FormAttachment leftWidth = rightAttachment.minus (leftAttachment); | |
225 cacheLeft = leftAttachment.plus (leftWidth.minus (getWidth (control, flushCache)).divide (2)); | |
226 break; | |
227 } | |
228 default: { | |
229 FormAttachment rightAttachment = leftData.getRightAttachment (leftControl, spacing, flushCache); | |
230 cacheLeft = rightAttachment.plus (left.offset + spacing); | |
231 } | |
232 } | |
233 isVisited = false; | |
234 return cacheLeft; | |
235 } | |
236 | |
237 String getName () { | |
238 String string = getClass ().getName (); | |
239 int index = string.lastIndexOf ('.'); | |
240 if (index is -1) return string; | |
241 return string.substring (index + 1, string.length ()); | |
242 } | |
243 | |
244 FormAttachment getRightAttachment (Control control, int spacing, bool flushCache) { | |
245 if (cacheRight !is null) return cacheRight; | |
246 if (isVisited) return cacheRight = new FormAttachment (0, getWidth (control, flushCache)); | |
247 if (right is null) { | |
248 if (left is null) return cacheRight = new FormAttachment (0, getWidth (control, flushCache)); | |
249 return cacheRight = getLeftAttachment (control, spacing, flushCache).plus (getWidth (control, flushCache)); | |
250 } | |
251 Control rightControl = right.control; | |
252 if (rightControl !is null) { | |
253 if (rightControl.isDisposed ()) { | |
254 right.control = rightControl = null; | |
255 } else { | |
256 if (rightControl.getParent () !is control.getParent ()) { | |
257 rightControl = null; | |
258 } | |
259 } | |
260 } | |
261 if (rightControl is null) return cacheRight = right; | |
262 isVisited = true; | |
263 FormData rightData = (FormData) rightControl.getLayoutData (); | |
264 FormAttachment rightAttachment = rightData.getRightAttachment (rightControl, spacing, flushCache); | |
265 switch (right.alignment) { | |
266 case DWT.RIGHT: | |
267 cacheRight = rightAttachment.plus (right.offset); | |
268 break; | |
269 case DWT.CENTER: { | |
270 FormAttachment leftAttachment = rightData.getLeftAttachment (rightControl, spacing, flushCache); | |
271 FormAttachment rightWidth = rightAttachment.minus (leftAttachment); | |
272 cacheRight = rightAttachment.minus (rightWidth.minus (getWidth (control, flushCache)).divide (2)); | |
273 break; | |
274 } | |
275 default: { | |
276 FormAttachment leftAttachment = rightData.getLeftAttachment (rightControl, spacing, flushCache); | |
277 cacheRight = leftAttachment.plus (right.offset - spacing); | |
278 break; | |
279 } | |
280 } | |
281 isVisited = false; | |
282 return cacheRight; | |
283 } | |
284 | |
285 FormAttachment getTopAttachment (Control control, int spacing, bool flushCache) { | |
286 if (cacheTop !is null) return cacheTop; | |
287 if (isVisited) return cacheTop = new FormAttachment (0, 0); | |
288 if (top is null) { | |
289 if (bottom is null) return cacheTop = new FormAttachment (0, 0); | |
290 return cacheTop = getBottomAttachment (control, spacing, flushCache).minus (getHeight (control, flushCache)); | |
291 } | |
292 Control topControl = top.control; | |
293 if (topControl !is null) { | |
294 if (topControl.isDisposed ()) { | |
295 top.control = topControl = null; | |
296 } else { | |
297 if (topControl.getParent () !is control.getParent ()) { | |
298 topControl = null; | |
299 } | |
300 } | |
301 } | |
302 if (topControl is null) return cacheTop = top; | |
303 isVisited = true; | |
304 FormData topData = (FormData) topControl.getLayoutData (); | |
305 FormAttachment topAttachment = topData.getTopAttachment (topControl, spacing, flushCache); | |
306 switch (top.alignment) { | |
307 case DWT.TOP: | |
308 cacheTop = topAttachment.plus (top.offset); | |
309 break; | |
310 case DWT.CENTER: { | |
311 FormAttachment bottomAttachment = topData.getBottomAttachment (topControl, spacing, flushCache); | |
312 FormAttachment topHeight = bottomAttachment.minus (topAttachment); | |
313 cacheTop = topAttachment.plus (topHeight.minus (getHeight (control, flushCache)).divide (2)); | |
314 break; | |
315 } | |
316 default: { | |
317 FormAttachment bottomAttachment = topData.getBottomAttachment (topControl, spacing, flushCache); | |
318 cacheTop = bottomAttachment.plus (top.offset + spacing); | |
319 break; | |
320 } | |
321 } | |
322 isVisited = false; | |
323 return cacheTop; | |
324 } | |
325 | |
326 /** | |
327 * Returns a string containing a concise, human-readable | |
328 * description of the receiver. | |
329 * | |
330 * @return a string representation of the FormData object | |
331 */ | |
332 public String toString () { | |
333 String string = getName()+" {"; | |
334 if (width !is DWT.DEFAULT) string += "width="+width+" "; | |
335 if (height !is DWT.DEFAULT) string += "height="+height+" "; | |
336 if (left !is null) string += "left="+left+" "; | |
337 if (right !is null) string += "right="+right+" "; | |
338 if (top !is null) string += "top="+top+" "; | |
339 if (bottom !is null) string += "bottom="+bottom+" "; | |
340 string = string.trim(); | |
341 string += "}"; | |
342 return string; | |
343 } | |
344 | |
345 } |