summaryrefslogtreecommitdiffstats
path: root/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/layout/FormData.java
blob: 866dc80a17c2269707783ccb84ce1b84a2b7ea6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.layout;

 
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;

/**
 * Instances of this class are used to define the attachments 
 * of a control in a <code>FormLayout</code>. 
 * <p>
 * To set a <code>FormData</code> object into a control, you use the 
 * <code>setLayoutData ()</code> method. To define attachments for the 
 * <code>FormData</code>, set the fields directly, like this:
 * <pre>
 * 		FormData data = new FormData();
 * 		data.left = new FormAttachment(0,5);
 * 		data.right = new FormAttachment(100,-5);
 * 		button.setLayoutData(formData);
 * </pre>
 * </p>
 * <p>
 * <code>FormData</code> contains the <code>FormAttachments</code> for 
 * each edge of the control that the <code>FormLayout</code> uses to
 * determine the size and position of the control. <code>FormData</code>
 * objects also allow you to set the width and height of controls within
 * a <code>FormLayout</code>. 
 * </p>
 * 
 * @see FormLayout
 * @see FormAttachment
 * 
 * @since 2.0
 */
public final class FormData {
	/**
	 * width specifies the preferred width in pixels. This value
	 * is the wHint passed into Control.computeSize(int, int, boolean) 
	 * to determine the preferred size of the control.
	 *
	 * The default value is SWT.DEFAULT.
	 *
	 * @see Control#computeSize(int, int, boolean)
	 */
	public int width = SWT.DEFAULT;
	/**
	 * height specifies the preferred height in pixels. This value
	 * is the hHint passed into Control.computeSize(int, int, boolean) 
	 * to determine the preferred size of the control.
	 *
	 * The default value is SWT.DEFAULT.
	 *
	 * @see Control#computeSize(int, int, boolean)
	 */
	public int height = SWT.DEFAULT;
	/**
	 * left specifies the attachment of the left side of 
	 * the control.
	 */
	public FormAttachment left;
	/**
	 * right specifies the attachment of the right side of
	 * the control.
	 */
	public FormAttachment right;
	/**
	 * top specifies the attachment of the top of the control.
	 */
	public FormAttachment top;
	/**
	 * bottom specifies the attachment of the bottom of the
	 * control.
	 */
	public FormAttachment bottom;
	
	int cacheWidth = -1, cacheHeight = -1;
	int defaultWhint, defaultHhint, defaultWidth = -1, defaultHeight = -1;
	int currentWhint, currentHhint, currentWidth = -1, currentHeight = -1;
	FormAttachment cacheLeft, cacheRight, cacheTop, cacheBottom;
	boolean isVisited, needed;
	
/**
 * Constructs a new instance of FormData using
 * default values.
 */
public FormData () {
}
	
/**
 * Constructs a new instance of FormData according to the parameters.
 * A value of SWT.DEFAULT indicates that no minimum width or
 * no minimum height is specified.
 * 
 * @param width a minimum width for the control
 * @param height a minimum height for the control
 */
public FormData (int width, int height) {
	this.width = width;
	this.height = height;
}

void computeSize (Control control, int wHint, int hHint, boolean flushCache) {
	if (cacheWidth != -1 && cacheHeight != -1) return;
	if (wHint == this.width && hHint == this.height) {
		if (defaultWidth == -1 || defaultHeight == -1 || wHint != defaultWhint || hHint != defaultHhint) {
			Point size =  control.computeSize (wHint, hHint, flushCache);
			defaultWhint = wHint;
			defaultHhint = hHint;
			defaultWidth = size.x;
			defaultHeight = size.y;
		}
		cacheWidth = defaultWidth;
		cacheHeight = defaultHeight;
		return;
	}
	if (currentWidth == -1 || currentHeight == -1 || wHint != currentWhint || hHint != currentHhint) {
		Point size =  control.computeSize (wHint, hHint, flushCache);
		currentWhint = wHint;
		currentHhint = hHint;
		currentWidth = size.x;
		currentHeight = size.y;
	}
	cacheWidth = currentWidth;
	cacheHeight = currentHeight;
}

void flushCache () {
	cacheWidth = cacheHeight = -1;
	defaultHeight = defaultWidth = -1;
	currentHeight = currentWidth = -1;
}

int getWidth (Control control, boolean flushCache) {
	needed = true;
	computeSize (control, width, height, flushCache);
	return cacheWidth;
}

int getHeight (Control control, boolean flushCache) {
	computeSize (control, width, height, flushCache);
	return cacheHeight;
}

FormAttachment getBottomAttachment (Control control, int spacing, boolean flushCache) {
	if (cacheBottom != null) return cacheBottom;
	if (isVisited) return cacheBottom = new FormAttachment (0, getHeight (control, flushCache));
	if (bottom == null) {
		if (top == null) return cacheBottom = new FormAttachment (0, getHeight (control, flushCache));
		return cacheBottom = getTopAttachment (control, spacing, flushCache).plus (getHeight (control, flushCache));
	}
	Control bottomControl = bottom.control;
	if (bottomControl != null) {
		if (bottomControl.isDisposed ()) {
			bottom.control = bottomControl = null;
		} else {
			if (bottomControl.getParent () != control.getParent ()) {
				bottomControl = null;
			}
		}
	}
	if (bottomControl == null) return cacheBottom = bottom;
	isVisited = true;
	FormData bottomData = (FormData) bottomControl.getLayoutData ();
	FormAttachment bottomAttachment = bottomData.getBottomAttachment (bottomControl, spacing, flushCache);
	switch (bottom.alignment) {
		case SWT.BOTTOM: 
			cacheBottom = bottomAttachment.plus (bottom.offset);
			break;
		case SWT.CENTER: {
			FormAttachment topAttachment = bottomData.getTopAttachment (bottomControl, spacing, flushCache);
			FormAttachment bottomHeight = bottomAttachment.minus (topAttachment);
			cacheBottom = bottomAttachment.minus (bottomHeight.minus (getHeight (control, flushCache)).divide (2));
			break;
		}
		default: {
			FormAttachment topAttachment = bottomData.getTopAttachment (bottomControl, spacing, flushCache);
			cacheBottom = topAttachment.plus (bottom.offset - spacing);	
			break;
		}
	}
	isVisited = false;
	return cacheBottom;
}

FormAttachment getLeftAttachment (Control control, int spacing, boolean flushCache) {
	if (cacheLeft != null) return cacheLeft;
	if (isVisited) return cacheLeft = new FormAttachment (0, 0);
	if (left == null) {
		if (right == null) return cacheLeft = new FormAttachment (0, 0);
		return cacheLeft = getRightAttachment (control, spacing, flushCache).minus (getWidth (control, flushCache));
	}
	Control leftControl = left.control;
	if (leftControl != null) {
		if (leftControl.isDisposed ()) {
			left.control = leftControl = null;
		} else {
			if (leftControl.getParent () != control.getParent ()) {
				leftControl = null;
			}
		}
	}
	if (leftControl == null) return cacheLeft = left;
	isVisited = true;
	FormData leftData = (FormData) leftControl.getLayoutData ();
	FormAttachment leftAttachment = leftData.getLeftAttachment (leftControl, spacing, flushCache);
	switch (left.alignment) {
		case SWT.LEFT:
			cacheLeft = leftAttachment.plus (left.offset);
			break;
		case SWT.CENTER: {
			FormAttachment rightAttachment = leftData.getRightAttachment (leftControl, spacing, flushCache);
			FormAttachment leftWidth = rightAttachment.minus (leftAttachment);
			cacheLeft = leftAttachment.plus (leftWidth.minus (getWidth (control, flushCache)).divide (2));
			break;
		}
		default: {
			FormAttachment rightAttachment = leftData.getRightAttachment (leftControl, spacing, flushCache);
			cacheLeft = rightAttachment.plus (left.offset + spacing); 
		}
	}
	isVisited = false; 
	return cacheLeft;
}
	
String getName () {
	String string = getClass ().getName ();
	int index = string.lastIndexOf ('.');
	if (index == -1) return string;
	return string.substring (index + 1, string.length ());
}

FormAttachment getRightAttachment (Control control, int spacing, boolean flushCache) {
	if (cacheRight != null) return cacheRight;
	if (isVisited) return cacheRight = new FormAttachment (0, getWidth (control, flushCache));
	if (right == null) {
		if (left == null) return cacheRight = new FormAttachment (0, getWidth (control, flushCache));
		return cacheRight = getLeftAttachment (control, spacing, flushCache).plus (getWidth (control, flushCache));
	}
	Control rightControl = right.control;
	if (rightControl != null) {
		if (rightControl.isDisposed ()) {
			right.control = rightControl = null;
		} else {
			if (rightControl.getParent () != control.getParent ()) {
				rightControl = null;
			}
		}
	}
	if (rightControl == null) return cacheRight = right;
	isVisited = true;
	FormData rightData = (FormData) rightControl.getLayoutData ();
	FormAttachment rightAttachment = rightData.getRightAttachment (rightControl, spacing, flushCache);
	switch (right.alignment) {
		case SWT.RIGHT: 
			cacheRight = rightAttachment.plus (right.offset);
			break;
		case SWT.CENTER: {
			FormAttachment leftAttachment = rightData.getLeftAttachment (rightControl, spacing, flushCache);
			FormAttachment rightWidth = rightAttachment.minus (leftAttachment);
			cacheRight = rightAttachment.minus (rightWidth.minus (getWidth (control, flushCache)).divide (2));
			break;
		}
		default: {
			FormAttachment leftAttachment = rightData.getLeftAttachment (rightControl, spacing, flushCache);
			cacheRight = leftAttachment.plus (right.offset - spacing);
			break;
		}
	}
	isVisited = false;
	return cacheRight;
}

FormAttachment getTopAttachment (Control control, int spacing, boolean flushCache) {
	if (cacheTop != null) return cacheTop;
	if (isVisited) return cacheTop = new FormAttachment (0, 0);
	if (top == null) {
		if (bottom == null) return cacheTop = new FormAttachment (0, 0);
		return cacheTop = getBottomAttachment (control, spacing, flushCache).minus (getHeight (control, flushCache));
	}
	Control topControl = top.control;
	if (topControl != null) {
		if (topControl.isDisposed ()) {
			top.control = topControl = null;
		} else {
			if (topControl.getParent () != control.getParent ()) {
				topControl = null;
			}
		}
	}
	if (topControl == null) return cacheTop = top;
	isVisited = true;
	FormData topData = (FormData) topControl.getLayoutData ();
	FormAttachment topAttachment = topData.getTopAttachment (topControl, spacing, flushCache);
	switch (top.alignment) {
		case SWT.TOP:
			cacheTop = topAttachment.plus (top.offset);
			break;
		case SWT.CENTER: {
			FormAttachment bottomAttachment = topData.getBottomAttachment (topControl, spacing, flushCache);
			FormAttachment topHeight = bottomAttachment.minus (topAttachment);
			cacheTop = topAttachment.plus (topHeight.minus (getHeight (control, flushCache)).divide (2));
			break;
		}
		default: {
			FormAttachment bottomAttachment = topData.getBottomAttachment (topControl, spacing, flushCache);
			cacheTop = bottomAttachment.plus (top.offset + spacing);
			break;
		}
	}
	isVisited = false;
	return cacheTop;
}

/**
 * Returns a string containing a concise, human-readable
 * description of the receiver.
 *
 * @return a string representation of the FormData object
 */
public String toString () {
 	String string = getName()+" {";
 	if (width != SWT.DEFAULT) string += "width="+width+" ";
	if (height != SWT.DEFAULT) string += "height="+height+" ";
 	if (left != null) string += "left="+left+" ";
 	if (right != null) string += "right="+right+" ";
 	if (top != null) string += "top="+top+" ";
 	if (bottom != null) string += "bottom="+bottom+" ";
 	string = string.trim();
 	string += "}";
	return string;
}

}