comparison dwt/layout/FormAttachment.d @ 40:fbe68c33eeee

Sync layout with dwt-linux
author Frank Benoit <benoit@tionex.de>
date Tue, 07 Oct 2008 14:41:31 +0200
parents 1a8b3cb347e0
children d8635bb48c7c
comparison
equal deleted inserted replaced
39:43be986a1372 40:fbe68c33eeee
1 /******************************************************************************* 1 /*******************************************************************************
2 * Copyright (c) 2000, 2006 IBM Corporation and others. 2 * Copyright (c) 2000, 2008 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials 3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0 4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at 5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html 6 * http://www.eclipse.org/legal/epl-v10.html
7 * 7 *
8 * Contributors: 8 * Contributors:
9 * IBM Corporation - initial API and implementation 9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
10 *******************************************************************************/ 12 *******************************************************************************/
11 module dwt.layout.FormAttachment; 13 module dwt.layout.FormAttachment;
12 14
13 import dwt.dwthelper.utils;
14
15 import dwt.DWT; 15 import dwt.DWT;
16 import dwt.widgets.Control; 16 import dwt.widgets.Control;
17 import dwt.layout.FormLayout;
18 import dwt.layout.FormData;
19
20 import tango.text.convert.Format;
21 import dwt.dwthelper.utils;
17 22
18 /** 23 /**
19 * Instances of this class are used to define the edges of a control 24 * Instances of this class are used to define the edges of a control
20 * within a <code>FormLayout</code>. 25 * within a <code>FormLayout</code>.
21 * <p> 26 * <p>
22 * <code>FormAttachments</code> are set into the top, bottom, left, 27 * <code>FormAttachments</code> are set into the top, bottom, left,
23 * and right fields of the <code>FormData</code> for a control. 28 * and right fields of the <code>FormData</code> for a control.
24 * For example: 29 * For example:
25 * <pre> 30 * <pre>
31 * button.setLayoutData(data); 36 * button.setLayoutData(data);
32 * </pre> 37 * </pre>
33 * </p> 38 * </p>
34 * <p> 39 * <p>
35 * A <code>FormAttachment</code> defines where to attach the side of 40 * A <code>FormAttachment</code> defines where to attach the side of
36 * a control by using the equation, y = ax + b. The "a" term represents 41 * a control by using the equation, y = ax + b. The "a" term represents
37 * a fraction of the parent composite's width (from the left) or height 42 * a fraction of the parent composite's width (from the left) or height
38 * (from the top). It can be defined using a numerator and denominator, 43 * (from the top). It can be defined using a numerator and denominator,
39 * or just a percentage value. If a percentage is used, the denominator 44 * or just a percentage value. If a percentage is used, the denominator
40 * is set to 100. The "b" term in the equation represents an offset, in 45 * is set to 100. The "b" term in the equation represents an offset, in
41 * pixels, from the attachment position. For example: 46 * pixels, from the attachment position. For example:
42 * <pre> 47 * <pre>
43 * FormAttachment attach = new FormAttachment (20, -5); 48 * FormAttachment attach = new FormAttachment (20, -5);
44 * </pre> 49 * </pre>
50 * For example: 55 * For example:
51 * <pre> 56 * <pre>
52 * FormAttachment attach = new FormAttachment (button, 10); 57 * FormAttachment attach = new FormAttachment (button, 10);
53 * </pre> 58 * </pre>
54 * specifies that the side to which the <code>FormAttachment</code> 59 * specifies that the side to which the <code>FormAttachment</code>
55 * object belongs will lie in the same position as the adjacent side of 60 * object belongs will lie in the same position as the adjacent side of
56 * the <code>button</code> control, plus 10 pixels. The control side can 61 * the <code>button</code> control, plus 10 pixels. The control side can
57 * also be attached to the opposite side of the specified control. 62 * also be attached to the opposite side of the specified control.
58 * For example: 63 * For example:
59 * <pre> 64 * <pre>
60 * FormData data = new FormData (); 65 * FormData data = new FormData ();
61 * data.left = new FormAttachment (button, 0, DWT.LEFT); 66 * data.left = new FormAttachment (button, 0, DWT.LEFT);
62 * </pre> 67 * </pre>
63 * specifies that the left side of the control will lie in the same position 68 * specifies that the left side of the control will lie in the same position
64 * as the left side of the <code>button</code> control. The control can also 69 * as the left side of the <code>button</code> control. The control can also
65 * be attached in a position that will center the control on the specified 70 * be attached in a position that will center the control on the specified
66 * control. For example: 71 * control. For example:
67 * <pre> 72 * <pre>
68 * data.left = new FormAttachment (button, 0, DWT.CENTER); 73 * data.left = new FormAttachment (button, 0, DWT.CENTER);
69 * </pre> 74 * </pre>
70 * specifies that the left side of the control will be positioned so that it is 75 * specifies that the left side of the control will be positioned so that it is
71 * centered between the left and right sides of the <code>button</code> control. 76 * centered between the left and right sides of the <code>button</code> control.
72 * If the alignment is not specified, the default is to attach to the adjacent side. 77 * If the alignment is not specified, the default is to attach to the adjacent side.
73 * </p> 78 * </p>
74 * 79 *
75 * @see FormLayout 80 * @see FormLayout
76 * @see FormData 81 * @see FormData
77 * 82 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
83 *
78 * @since 2.0 84 * @since 2.0
79 */ 85 */
80 public final class FormAttachment { 86 public final class FormAttachment {
81 /** 87 /**
82 * numerator specifies the numerator of the "a" term in the 88 * numerator specifies the numerator of the "a" term in the
83 * equation, y = ax + b, which defines the attachment. 89 * equation, y = ax + b, which defines the attachment.
84 */ 90 */
85 public int numerator; 91 public int numerator;
86 92
87 /** 93 /**
88 * denominator specifies the denominator of the "a" term in the 94 * denominator specifies the denominator of the "a" term in the
89 * equation, y = ax + b, which defines the attachment. 95 * equation, y = ax + b, which defines the attachment.
90 * 96 *
91 * The default value is 100. 97 * The default value is 100.
92 */ 98 */
93 public int denominator = 100; 99 public int denominator = 100;
94 100
95 /** 101 /**
96 * offset specifies the offset, in pixels, of the control side 102 * offset specifies the offset, in pixels, of the control side
97 * from the attachment position. 103 * from the attachment position.
98 * If the offset is positive, then the control side is offset 104 * If the offset is positive, then the control side is offset
99 * to the right of or below the attachment position. If it is 105 * to the right of or below the attachment position. If it is
100 * negative, then the control side is offset to the left of or 106 * negative, then the control side is offset to the left of or
101 * above the attachment position. 107 * above the attachment position.
102 * 108 *
103 * This is equivalent to the "b" term in the equation y = ax + b. 109 * This is equivalent to the "b" term in the equation y = ax + b.
104 * The default value is 0. 110 * The default value is 0.
105 */ 111 */
106 public int offset; 112 public int offset;
107 113
108 /** 114 /**
109 * control specifies the control to which the control side is 115 * control specifies the control to which the control side is
110 * attached. 116 * attached.
111 */ 117 */
112 public Control control; 118 public Control control;
113 119
114 /** 120 /**
115 * alignment specifies the alignment of the control side that is 121 * alignment specifies the alignment of the control side that is
116 * attached to a control. 122 * attached to a control.
117 * <p> 123 * <p>
118 * For top and bottom attachments, TOP, BOTTOM and CENTER are used. For left 124 * For top and bottom attachments, TOP, BOTTOM and CENTER are used. For left
119 * and right attachments, LEFT, RIGHT and CENTER are used. If any other case 125 * and right attachments, LEFT, RIGHT and CENTER are used. If any other case
120 * occurs, the default will be used instead. 126 * occurs, the default will be used instead.
121 * </p> 127 * </p>
122 * 128 *
123 * <br>Possible values are: <ul> 129 * <br>Possible values are: <ul>
124 * <li>TOP: Attach the side to the top side of the specified control.</li> 130 * <li>{@link DWT#TOP}: Attach the side to the top side of the specified control.</li>
125 * <li>BOTTOM : Attach the side to the bottom side of the specified control.</li> 131 * <li>{@link DWT#BOTTOM}: Attach the side to the bottom side of the specified control.</li>
126 * <li>LEFT: Attach the side to the left side of the specified control.</li> 132 * <li>{@link DWT#LEFT}: Attach the side to the left side of the specified control.</li>
127 * <li>RIGHT: Attach the side to the right side of the specified control.</li> 133 * <li>{@link DWT#RIGHT}: Attach the side to the right side of the specified control.</li>
128 * <li>CENTER: Attach the side at a position which will center the control on the specified control.</li> 134 * <li>{@link DWT#CENTER}: Attach the side at a position which will center the control on the specified control.</li>
129 * <li>DEFAULT: Attach the side to the adjacent side of the specified control.</li> 135 * <li>{@link DWT#DEFAULT}: Attach the side to the adjacent side of the specified control.</li>
130 * </ul> 136 * </ul>
131 */ 137 */
132 public int alignment; 138 public int alignment;
133 139
134 /** 140 /**
135 * Constructs a new instance of this class. 141 * Constructs a new instance of this class.
136 * Since no numerator, denominator or offset is specified, 142 * Since no numerator, denominator or offset is specified,
137 * the attachment is treated as a percentage of the form. 143 * the attachment is treated as a percentage of the form.
138 * The numerator is zero, the denominator is 100 and the 144 * The numerator is zero, the denominator is 100 and the
139 * offset is zero. 145 * offset is zero.
140 * 146 *
141 * @since 3.2 147 * @since 3.2
142 */ 148 */
143 public this () { 149 public this () {
144 } 150 }
145 151
146 /** 152 /**
147 * Constructs a new instance of this class given a numerator 153 * Constructs a new instance of this class given a numerator
148 * Since no denominator or offset is specified, the default 154 * Since no denominator or offset is specified, the default
149 * is to treat the numerator as a percentage of the form, with a 155 * is to treat the numerator as a percentage of the form, with a
150 * denominator of 100. The offset is zero. 156 * denominator of 100. The offset is zero.
151 * 157 *
152 * @param numerator the percentage of the position 158 * @param numerator the percentage of the position
153 * 159 *
154 * @since 3.0 160 * @since 3.0
155 */ 161 */
156 public this (int numerator) { 162 public this (int numerator) {
157 this (numerator, 100, 0); 163 this (numerator, 100, 0);
158 } 164 }
159 165
160 /** 166 /**
161 * Constructs a new instance of this class given a numerator 167 * Constructs a new instance of this class given a numerator
162 * and an offset. Since no denominator is specified, the default 168 * and an offset. Since no denominator is specified, the default
163 * is to treat the numerator as a percentage of the form, with a 169 * is to treat the numerator as a percentage of the form, with a
164 * denominator of 100. 170 * denominator of 100.
165 * 171 *
166 * @param numerator the percentage of the position 172 * @param numerator the percentage of the position
167 * @param offset the offset of the side from the position 173 * @param offset the offset of the side from the position
168 */ 174 */
169 public this (int numerator, int offset) { 175 public this (int numerator, int offset) {
170 this (numerator, 100, offset); 176 this (numerator, 100, offset);
171 } 177 }
172 178
173 /** 179 /**
174 * Constructs a new instance of this class given a numerator 180 * Constructs a new instance of this class given a numerator
175 * and denominator and an offset. The position of the side is 181 * and denominator and an offset. The position of the side is
176 * given by the fraction of the form defined by the numerator 182 * given by the fraction of the form defined by the numerator
177 * and denominator. 183 * and denominator.
178 * 184 *
179 * @param numerator the numerator of the position 185 * @param numerator the numerator of the position
188 } 194 }
189 195
190 /** 196 /**
191 * Constructs a new instance of this class given a control. 197 * Constructs a new instance of this class given a control.
192 * Since no alignment is specified, the default alignment is 198 * Since no alignment is specified, the default alignment is
193 * to attach the side to the adjacent side of the specified 199 * to attach the side to the adjacent side of the specified
194 * control. Since no offset is specified, an offset of 0 is 200 * control. Since no offset is specified, an offset of 0 is
195 * used. 201 * used.
196 * 202 *
197 * @param control the control the side is attached to 203 * @param control the control the side is attached to
198 */ 204 */
199 public this (Control control) { 205 public this (Control control) {
200 this (control, 0, DWT.DEFAULT); 206 this (control, 0, DWT.DEFAULT);
201 } 207 }
202 208
203 /** 209 /**
204 * Constructs a new instance of this class given a control 210 * Constructs a new instance of this class given a control
205 * and an offset. Since no alignment is specified, the default 211 * and an offset. Since no alignment is specified, the default
206 * alignment is to attach the side to the adjacent side of the 212 * alignment is to attach the side to the adjacent side of the
207 * specified control. 213 * specified control.
208 * 214 *
209 * @param control the control the side is attached to 215 * @param control the control the side is attached to
210 * @param offset the offset of the side from the control 216 * @param offset the offset of the side from the control
211 */ 217 */
212 public this (Control control, int offset) { 218 public this (Control control, int offset) {
213 this (control, offset, DWT.DEFAULT); 219 this (control, offset, DWT.DEFAULT);
214 } 220 }
215 221
216 /** 222 /**
217 * Constructs a new instance of this class given a control, 223 * Constructs a new instance of this class given a control,
218 * an offset and an alignment. 224 * an offset and an alignment. The possible alignment values are:
219 * 225 * <dl>
226 * <dt><b>{@link DWT#TOP}</b></dt>
227 * <dd>the side will be attached to the top side of the specified control</dd>
228 * <dt><b>{@link DWT#BOTTOM}</b></dt>
229 * <dd>the side will be attached to the bottom side of the specified control</dd>
230 * <dt><b>{@link DWT#LEFT}</b></dt>
231 * <dd>the side will be attached to the left side of the specified control</dd>
232 * <dt><b>{@link DWT#RIGHT}</b></dt>
233 * <dd>the side will be attached to the right side of the specified control</dd>
234 * <dt><b>{@link DWT#CENTER}</b></dt>
235 * <dd>the side will be centered on the same side of the specified control</dd>
236 * <dt><b>{@link DWT#DEFAULT}</b></dt>
237 * <dd>the side will be attached to the adjacent side of the specified control</dd>
238 * </dl>
239 *
220 * @param control the control the side is attached to 240 * @param control the control the side is attached to
221 * @param offset the offset of the side from the control 241 * @param offset the offset of the side from the control
222 * @param alignment the alignment of the side to the control it is attached to 242 * @param alignment the alignment of the side to the control it is attached to,
243 * one of TOP, BOTTOM, LEFT, RIGHT, CENTER, or DEFAULT
223 */ 244 */
224 public this (Control control, int offset, int alignment) { 245 public this (Control control, int offset, int alignment) {
225 this.control = control; 246 this.control = control;
226 this.offset = offset; 247 this.offset = offset;
227 this.alignment = alignment; 248 this.alignment = alignment;
285 306
286 int solveY (int value) { 307 int solveY (int value) {
287 if (numerator is 0) DWT.error (DWT.ERROR_CANNOT_BE_ZERO); 308 if (numerator is 0) DWT.error (DWT.ERROR_CANNOT_BE_ZERO);
288 return (value - offset) * denominator / numerator; 309 return (value - offset) * denominator / numerator;
289 } 310 }
290 311
291 /** 312 /**
292 * Returns a string containing a concise, human-readable 313 * Returns a string containing a concise, human-readable
293 * description of the receiver. 314 * description of the receiver.
294 * 315 *
295 * @return a string representation of the FormAttachment 316 * @return a string representation of the FormAttachment
296 */ 317 */
297 public String toString () { 318 public override String toString () {
298 String string = control !is null ? control.toString () : numerator + "/" + denominator; 319 String string = control !is null ? control.toString () : Format( "{}/{}", numerator, denominator );
299 return "{y = (" + string + (offset >= 0 ? ")x + " + offset: ")x - " + (-offset))+"}"; 320 return Format("{{y = ({})x + {}}", string, ( offset >= 0 ? Format(")x + {}", offset ) : Format( ")x - {}", -offset)));
300 } 321 }
301 322
302 } 323 }