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