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