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