Mercurial > projects > dwt-linux
comparison dwt/layout/FormAttachment.d @ 50:d48f7334742c
FormLayout, FormData, FormAttachment
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 11 Jan 2008 10:44:12 +0100 |
parents | |
children | 8cec8f536af3 |
comparison
equal
deleted
inserted
replaced
49:a67796650ad4 | 50:d48f7334742c |
---|---|
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 *******************************************************************************/ | |
11 module dwt.layout.FormAttachment; | |
12 | |
13 import dwt.SWT; | |
14 import dwt.widgets.Control; | |
15 import dwt.layout.FormLayout; | |
16 import dwt.layout.FormData; | |
17 | |
18 import tango.text.convert.Format; | |
19 | |
20 /** | |
21 * Instances of this class are used to define the edges of a control | |
22 * within a <code>FormLayout</code>. | |
23 * <p> | |
24 * <code>FormAttachments</code> are set into the top, bottom, left, | |
25 * and right fields of the <code>FormData</code> for a control. | |
26 * For example: | |
27 * <pre> | |
28 * FormData data = new FormData(); | |
29 * data.top = new FormAttachment(0,5); | |
30 * data.bottom = new FormAttachment(100,-5); | |
31 * data.left = new FormAttachment(0,5); | |
32 * data.right = new FormAttachment(100,-5); | |
33 * button.setLayoutData(data); | |
34 * </pre> | |
35 * </p> | |
36 * <p> | |
37 * A <code>FormAttachment</code> defines where to attach the side of | |
38 * a control by using the equation, y = ax + b. The "a" term represents | |
39 * a fraction of the parent composite's width (from the left) or height | |
40 * (from the top). It can be defined using a numerator and denominator, | |
41 * or just a percentage value. If a percentage is used, the denominator | |
42 * is set to 100. The "b" term in the equation represents an offset, in | |
43 * pixels, from the attachment position. For example: | |
44 * <pre> | |
45 * FormAttachment attach = new FormAttachment (20, -5); | |
46 * </pre> | |
47 * specifies that the side to which the <code>FormAttachment</code> | |
48 * object belongs will lie at 20% of the parent composite, minus 5 pixels. | |
49 * </p> | |
50 * <p> | |
51 * Control sides can also be attached to another control. | |
52 * For example: | |
53 * <pre> | |
54 * FormAttachment attach = new FormAttachment (button, 10); | |
55 * </pre> | |
56 * specifies that the side to which the <code>FormAttachment</code> | |
57 * object belongs will lie in the same position as the adjacent side of | |
58 * the <code>button</code> control, plus 10 pixels. The control side can | |
59 * also be attached to the opposite side of the specified control. | |
60 * For example: | |
61 * <pre> | |
62 * FormData data = new FormData (); | |
63 * data.left = new FormAttachment (button, 0, SWT.LEFT); | |
64 * </pre> | |
65 * specifies that the left side of the control will lie in the same position | |
66 * as the left side of the <code>button</code> control. The control can also | |
67 * be attached in a position that will center the control on the specified | |
68 * control. For example: | |
69 * <pre> | |
70 * data.left = new FormAttachment (button, 0, SWT.CENTER); | |
71 * </pre> | |
72 * specifies that the left side of the control will be positioned so that it is | |
73 * centered between the left and right sides of the <code>button</code> control. | |
74 * If the alignment is not specified, the default is to attach to the adjacent side. | |
75 * </p> | |
76 * | |
77 * @see FormLayout | |
78 * @see FormData | |
79 * | |
80 * @since 2.0 | |
81 */ | |
82 public final class FormAttachment { | |
83 /** | |
84 * numerator specifies the numerator of the "a" term in the | |
85 * equation, y = ax + b, which defines the attachment. | |
86 */ | |
87 public int numerator; | |
88 | |
89 /** | |
90 * denominator specifies the denominator of the "a" term in the | |
91 * equation, y = ax + b, which defines the attachment. | |
92 * | |
93 * The default value is 100. | |
94 */ | |
95 public int denominator = 100; | |
96 | |
97 /** | |
98 * offset specifies the offset, in pixels, of the control side | |
99 * from the attachment position. | |
100 * If the offset is positive, then the control side is offset | |
101 * to the right of or below the attachment position. If it is | |
102 * negative, then the control side is offset to the left of or | |
103 * above the attachment position. | |
104 * | |
105 * This is equivalent to the "b" term in the equation y = ax + b. | |
106 * The default value is 0. | |
107 */ | |
108 public int offset; | |
109 | |
110 /** | |
111 * control specifies the control to which the control side is | |
112 * attached. | |
113 */ | |
114 public Control control; | |
115 | |
116 /** | |
117 * alignment specifies the alignment of the control side that is | |
118 * attached to a control. | |
119 * <p> | |
120 * For top and bottom attachments, TOP, BOTTOM and CENTER are used. For left | |
121 * and right attachments, LEFT, RIGHT and CENTER are used. If any other case | |
122 * occurs, the default will be used instead. | |
123 * </p> | |
124 * | |
125 * <br>Possible values are: <ul> | |
126 * <li>TOP: Attach the side to the top side of the specified control.</li> | |
127 * <li>BOTTOM : Attach the side to the bottom side of the specified control.</li> | |
128 * <li>LEFT: Attach the side to the left side of the specified control.</li> | |
129 * <li>RIGHT: Attach the side to the right side of the specified control.</li> | |
130 * <li>CENTER: Attach the side at a position which will center the control on the specified control.</li> | |
131 * <li>DEFAULT: Attach the side to the adjacent side of the specified control.</li> | |
132 * </ul> | |
133 */ | |
134 public int alignment; | |
135 | |
136 /** | |
137 * Constructs a new instance of this class. | |
138 * Since no numerator, denominator or offset is specified, | |
139 * the attachment is treated as a percentage of the form. | |
140 * The numerator is zero, the denominator is 100 and the | |
141 * offset is zero. | |
142 * | |
143 * @since 3.2 | |
144 */ | |
145 public this () { | |
146 } | |
147 | |
148 /** | |
149 * Constructs a new instance of this class given a numerator | |
150 * Since no denominator or offset is specified, the default | |
151 * is to treat the numerator as a percentage of the form, with a | |
152 * denominator of 100. The offset is zero. | |
153 * | |
154 * @param numerator the percentage of the position | |
155 * | |
156 * @since 3.0 | |
157 */ | |
158 public this (int numerator) { | |
159 this (numerator, 100, 0); | |
160 } | |
161 | |
162 /** | |
163 * Constructs a new instance of this class given a numerator | |
164 * and an offset. Since no denominator is specified, the default | |
165 * is to treat the numerator as a percentage of the form, with a | |
166 * denominator of 100. | |
167 * | |
168 * @param numerator the percentage of the position | |
169 * @param offset the offset of the side from the position | |
170 */ | |
171 public this (int numerator, int offset) { | |
172 this (numerator, 100, offset); | |
173 } | |
174 | |
175 /** | |
176 * Constructs a new instance of this class given a numerator | |
177 * and denominator and an offset. The position of the side is | |
178 * given by the fraction of the form defined by the numerator | |
179 * and denominator. | |
180 * | |
181 * @param numerator the numerator of the position | |
182 * @param denominator the denominator of the position | |
183 * @param offset the offset of the side from the position | |
184 */ | |
185 public this (int numerator, int denominator, int offset) { | |
186 if (denominator == 0) SWT.error (SWT.ERROR_CANNOT_BE_ZERO); | |
187 this.numerator = numerator; | |
188 this.denominator = denominator; | |
189 this.offset = offset; | |
190 } | |
191 | |
192 /** | |
193 * Constructs a new instance of this class given a control. | |
194 * Since no alignment is specified, the default alignment is | |
195 * to attach the side to the adjacent side of the specified | |
196 * control. Since no offset is specified, an offset of 0 is | |
197 * used. | |
198 * | |
199 * @param control the control the side is attached to | |
200 */ | |
201 public this (Control control) { | |
202 this (control, 0, SWT.DEFAULT); | |
203 } | |
204 | |
205 /** | |
206 * Constructs a new instance of this class given a control | |
207 * and an offset. Since no alignment is specified, the default | |
208 * alignment is to attach the side to the adjacent side of the | |
209 * specified control. | |
210 * | |
211 * @param control the control the side is attached to | |
212 * @param offset the offset of the side from the control | |
213 */ | |
214 public this (Control control, int offset) { | |
215 this (control, offset, SWT.DEFAULT); | |
216 } | |
217 | |
218 /** | |
219 * Constructs a new instance of this class given a control, | |
220 * an offset and an alignment. | |
221 * | |
222 * @param control the control the side is attached to | |
223 * @param offset the offset of the side from the control | |
224 * @param alignment the alignment of the side to the control it is attached to | |
225 */ | |
226 public this (Control control, int offset, int alignment) { | |
227 this.control = control; | |
228 this.offset = offset; | |
229 this.alignment = alignment; | |
230 } | |
231 | |
232 FormAttachment divide (int value) { | |
233 return new FormAttachment (numerator, denominator * value, offset / value); | |
234 } | |
235 | |
236 int gcd (int m, int n) { | |
237 int temp; | |
238 m = Math.abs (m); | |
239 n = Math.abs (n); | |
240 if (m < n) { | |
241 temp = m; | |
242 m = n; | |
243 n = temp; | |
244 } | |
245 while (n != 0){ | |
246 temp = m; | |
247 m = n; | |
248 n = temp % n; | |
249 } | |
250 return m; | |
251 } | |
252 | |
253 FormAttachment minus (FormAttachment attachment) { | |
254 FormAttachment solution = new FormAttachment (); | |
255 solution.numerator = numerator * attachment.denominator - denominator * attachment.numerator; | |
256 solution.denominator = denominator * attachment.denominator; | |
257 int gcd = gcd (solution.denominator, solution.numerator); | |
258 solution.numerator = solution.numerator / gcd; | |
259 solution.denominator = solution.denominator / gcd; | |
260 solution.offset = offset - attachment.offset; | |
261 return solution; | |
262 } | |
263 | |
264 FormAttachment minus (int value) { | |
265 return new FormAttachment (numerator, denominator, offset - value); | |
266 } | |
267 | |
268 FormAttachment plus (FormAttachment attachment) { | |
269 FormAttachment solution = new FormAttachment (); | |
270 solution.numerator = numerator * attachment.denominator + denominator * attachment.numerator; | |
271 solution.denominator = denominator * attachment.denominator; | |
272 int gcd = gcd (solution.denominator, solution.numerator); | |
273 solution.numerator = solution.numerator / gcd; | |
274 solution.denominator = solution.denominator / gcd; | |
275 solution.offset = offset + attachment.offset; | |
276 return solution; | |
277 } | |
278 | |
279 FormAttachment plus (int value) { | |
280 return new FormAttachment (numerator, denominator, offset + value); | |
281 } | |
282 | |
283 int solveX (int value) { | |
284 if (denominator == 0) SWT.error (SWT.ERROR_CANNOT_BE_ZERO); | |
285 return ((numerator * value) / denominator) + offset; | |
286 } | |
287 | |
288 int solveY (int value) { | |
289 if (numerator == 0) SWT.error (SWT.ERROR_CANNOT_BE_ZERO); | |
290 return (value - offset) * denominator / numerator; | |
291 } | |
292 | |
293 /** | |
294 * Returns a string containing a concise, human-readable | |
295 * description of the receiver. | |
296 * | |
297 * @return a string representation of the FormAttachment | |
298 */ | |
299 public char[] toString () { | |
300 char[] string = control != null ? control.toString () : Format( "{}/{}", numerator, denominator ); | |
301 return Format("{{y = ({})x + {}}", string, ( offset >= 0 ? Format(")x + {}", offset ) : Format( ")x - {}", -offset))); | |
302 } | |
303 | |
304 } |