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