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