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