| 1 |
/******************************************************************************* |
|---|
| 2 |
* Copyright (c) 2000, 2008 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 |
import dwt.dwthelper.utils; |
|---|
| 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 |
* @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a> |
|---|
| 83 |
* |
|---|
| 84 |
* @since 2.0 |
|---|
| 85 |
*/ |
|---|
| 86 |
public final class FormAttachment { |
|---|
| 87 |
/** |
|---|
| 88 |
* numerator specifies the numerator of the "a" term in the |
|---|
| 89 |
* equation, y = ax + b, which defines the attachment. |
|---|
| 90 |
*/ |
|---|
| 91 |
public int numerator; |
|---|
| 92 |
|
|---|
| 93 |
/** |
|---|
| 94 |
* denominator specifies the denominator of the "a" term in the |
|---|
| 95 |
* equation, y = ax + b, which defines the attachment. |
|---|
| 96 |
* |
|---|
| 97 |
* The default value is 100. |
|---|
| 98 |
*/ |
|---|
| 99 |
public int denominator = 100; |
|---|
| 100 |
|
|---|
| 101 |
/** |
|---|
| 102 |
* offset specifies the offset, in pixels, of the control side |
|---|
| 103 |
* from the attachment position. |
|---|
| 104 |
* If the offset is positive, then the control side is offset |
|---|
| 105 |
* to the right of or below the attachment position. If it is |
|---|
| 106 |
* negative, then the control side is offset to the left of or |
|---|
| 107 |
* above the attachment position. |
|---|
| 108 |
* |
|---|
| 109 |
* This is equivalent to the "b" term in the equation y = ax + b. |
|---|
| 110 |
* The default value is 0. |
|---|
| 111 |
*/ |
|---|
| 112 |
public int offset; |
|---|
| 113 |
|
|---|
| 114 |
/** |
|---|
| 115 |
* control specifies the control to which the control side is |
|---|
| 116 |
* attached. |
|---|
| 117 |
*/ |
|---|
| 118 |
public Control control; |
|---|
| 119 |
|
|---|
| 120 |
/** |
|---|
| 121 |
* alignment specifies the alignment of the control side that is |
|---|
| 122 |
* attached to a control. |
|---|
| 123 |
* <p> |
|---|
| 124 |
* For top and bottom attachments, TOP, BOTTOM and CENTER are used. For left |
|---|
| 125 |
* and right attachments, LEFT, RIGHT and CENTER are used. If any other case |
|---|
| 126 |
* occurs, the default will be used instead. |
|---|
| 127 |
* </p> |
|---|
| 128 |
* |
|---|
| 129 |
* <br>Possible values are: <ul> |
|---|
| 130 |
* <li>{@link DWT#TOP}: Attach the side to the top side of the specified control.</li> |
|---|
| 131 |
* <li>{@link DWT#BOTTOM}: Attach the side to the bottom side of the specified control.</li> |
|---|
| 132 |
* <li>{@link DWT#LEFT}: Attach the side to the left side of the specified control.</li> |
|---|
| 133 |
* <li>{@link DWT#RIGHT}: Attach the side to the right side of 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> |
|---|
| 135 |
* <li>{@link DWT#DEFAULT}: Attach the side to the adjacent side of the specified control.</li> |
|---|
| 136 |
* </ul> |
|---|
| 137 |
*/ |
|---|
| 138 |
public int alignment; |
|---|
| 139 |
|
|---|
| 140 |
/** |
|---|
| 141 |
* Constructs a new instance of this class. |
|---|
| 142 |
* Since no numerator, denominator or offset is specified, |
|---|
| 143 |
* the attachment is treated as a percentage of the form. |
|---|
| 144 |
* The numerator is zero, the denominator is 100 and the |
|---|
| 145 |
* offset is zero. |
|---|
| 146 |
* |
|---|
| 147 |
* @since 3.2 |
|---|
| 148 |
*/ |
|---|
| 149 |
public this () { |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
/** |
|---|
| 153 |
* Constructs a new instance of this class given a numerator |
|---|
| 154 |
* Since no denominator or offset is specified, the default |
|---|
| 155 |
* is to treat the numerator as a percentage of the form, with a |
|---|
| 156 |
* denominator of 100. The offset is zero. |
|---|
| 157 |
* |
|---|
| 158 |
* @param numerator the percentage of the position |
|---|
| 159 |
* |
|---|
| 160 |
* @since 3.0 |
|---|
| 161 |
*/ |
|---|
| 162 |
public this (int numerator) { |
|---|
| 163 |
this (numerator, 100, 0); |
|---|
| 164 |
} |
|---|
| 165 |
|
|---|
| 166 |
/** |
|---|
| 167 |
* Constructs a new instance of this class given a numerator |
|---|
| 168 |
* and an offset. Since no denominator is specified, the default |
|---|
| 169 |
* is to treat the numerator as a percentage of the form, with a |
|---|
| 170 |
* denominator of 100. |
|---|
| 171 |
* |
|---|
| 172 |
* @param numerator the percentage of the position |
|---|
| 173 |
* @param offset the offset of the side from the position |
|---|
| 174 |
*/ |
|---|
| 175 |
public this (int numerator, int offset) { |
|---|
| 176 |
this (numerator, 100, offset); |
|---|
| 177 |
} |
|---|
| 178 |
|
|---|
| 179 |
/** |
|---|
| 180 |
* Constructs a new instance of this class given a numerator |
|---|
| 181 |
* and denominator and an offset. The position of the side is |
|---|
| 182 |
* given by the fraction of the form defined by the numerator |
|---|
| 183 |
* and denominator. |
|---|
| 184 |
* |
|---|
| 185 |
* @param numerator the numerator of the position |
|---|
| 186 |
* @param denominator the denominator of the position |
|---|
| 187 |
* @param offset the offset of the side from the position |
|---|
| 188 |
*/ |
|---|
| 189 |
public this (int numerator, int denominator, int offset) { |
|---|
| 190 |
if (denominator is 0) DWT.error (DWT.ERROR_CANNOT_BE_ZERO); |
|---|
| 191 |
this.numerator = numerator; |
|---|
| 192 |
this.denominator = denominator; |
|---|
| 193 |
this.offset = offset; |
|---|
| 194 |
} |
|---|
| 195 |
|
|---|
| 196 |
/** |
|---|
| 197 |
* Constructs a new instance of this class given a control. |
|---|
| 198 |
* Since no alignment is specified, the default alignment is |
|---|
| 199 |
* to attach the side to the adjacent side of the specified |
|---|
| 200 |
* control. Since no offset is specified, an offset of 0 is |
|---|
| 201 |
* used. |
|---|
| 202 |
* |
|---|
| 203 |
* @param control the control the side is attached to |
|---|
| 204 |
*/ |
|---|
| 205 |
public this (Control control) { |
|---|
| 206 |
this (control, 0, DWT.DEFAULT); |
|---|
| 207 |
} |
|---|
| 208 |
|
|---|
| 209 |
/** |
|---|
| 210 |
* Constructs a new instance of this class given a control |
|---|
| 211 |
* and an offset. Since no alignment is specified, the default |
|---|
| 212 |
* alignment is to attach the side to the adjacent side of the |
|---|
| 213 |
* specified control. |
|---|
| 214 |
* |
|---|
| 215 |
* @param control the control the side is attached to |
|---|
| 216 |
* @param offset the offset of the side from the control |
|---|
| 217 |
*/ |
|---|
| 218 |
public this (Control control, int offset) { |
|---|
| 219 |
this (control, offset, DWT.DEFAULT); |
|---|
| 220 |
} |
|---|
| 221 |
|
|---|
| 222 |
/** |
|---|
| 223 |
* Constructs a new instance of this class given a control, |
|---|
| 224 |
* an offset and an alignment. The possible alignment values are: |
|---|
| 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 |
* |
|---|
| 240 |
* @param control the control the side is attached to |
|---|
| 241 |
* @param offset the offset of the side from the control |
|---|
| 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 |
|---|
| 244 |
*/ |
|---|
| 245 |
public this (Control control, int offset, int alignment) { |
|---|
| 246 |
this.control = control; |
|---|
| 247 |
this.offset = offset; |
|---|
| 248 |
this.alignment = alignment; |
|---|
| 249 |
} |
|---|
| 250 |
|
|---|
| 251 |
FormAttachment divide (int value) { |
|---|
| 252 |
return new FormAttachment (numerator, denominator * value, offset / value); |
|---|
| 253 |
} |
|---|
| 254 |
|
|---|
| 255 |
int gcd (int m, int n) { |
|---|
| 256 |
int temp; |
|---|
| 257 |
m = Math.abs (m); |
|---|
| 258 |
n = Math.abs (n); |
|---|
| 259 |
if (m < n) { |
|---|
| 260 |
temp = m; |
|---|
| 261 |
m = n; |
|---|
| 262 |
n = temp; |
|---|
| 263 |
} |
|---|
| 264 |
while (n !is 0){ |
|---|
| 265 |
temp = m; |
|---|
| 266 |
m = n; |
|---|
| 267 |
n = temp % n; |
|---|
| 268 |
} |
|---|
| 269 |
return m; |
|---|
| 270 |
} |
|---|
| 271 |
|
|---|
| 272 |
FormAttachment minus (FormAttachment attachment) { |
|---|
| 273 |
FormAttachment solution = new FormAttachment (); |
|---|
| 274 |
solution.numerator = numerator * attachment.denominator - denominator * attachment.numerator; |
|---|
| 275 |
solution.denominator = denominator * attachment.denominator; |
|---|
| 276 |
int gcd = gcd (solution.denominator, solution.numerator); |
|---|
| 277 |
solution.numerator = solution.numerator / gcd; |
|---|
| 278 |
solution.denominator = solution.denominator / gcd; |
|---|
| 279 |
solution.offset = offset - attachment.offset; |
|---|
| 280 |
return solution; |
|---|
| 281 |
} |
|---|
| 282 |
|
|---|
| 283 |
FormAttachment minus (int value) { |
|---|
| 284 |
return new FormAttachment (numerator, denominator, offset - value); |
|---|
| 285 |
} |
|---|
| 286 |
|
|---|
| 287 |
FormAttachment plus (FormAttachment attachment) { |
|---|
| 288 |
FormAttachment solution = new FormAttachment (); |
|---|
| 289 |
solution.numerator = numerator * attachment.denominator + denominator * attachment.numerator; |
|---|
| 290 |
solution.denominator = denominator * attachment.denominator; |
|---|
| 291 |
int gcd = gcd (solution.denominator, solution.numerator); |
|---|
| 292 |
solution.numerator = solution.numerator / gcd; |
|---|
| 293 |
solution.denominator = solution.denominator / gcd; |
|---|
| 294 |
solution.offset = offset + attachment.offset; |
|---|
| 295 |
return solution; |
|---|
| 296 |
} |
|---|
| 297 |
|
|---|
| 298 |
FormAttachment plus (int value) { |
|---|
| 299 |
return new FormAttachment (numerator, denominator, offset + value); |
|---|
| 300 |
} |
|---|
| 301 |
|
|---|
| 302 |
int solveX (int value) { |
|---|
| 303 |
if (denominator is 0) DWT.error (DWT.ERROR_CANNOT_BE_ZERO); |
|---|
| 304 |
return ((numerator * value) / denominator) + offset; |
|---|
| 305 |
} |
|---|
| 306 |
|
|---|
| 307 |
int solveY (int value) { |
|---|
| 308 |
if (numerator is 0) DWT.error (DWT.ERROR_CANNOT_BE_ZERO); |
|---|
| 309 |
return (value - offset) * denominator / numerator; |
|---|
| 310 |
} |
|---|
| 311 |
|
|---|
| 312 |
/** |
|---|
| 313 |
* Returns a string containing a concise, human-readable |
|---|
| 314 |
* description of the receiver. |
|---|
| 315 |
* |
|---|
| 316 |
* @return a string representation of the FormAttachment |
|---|
| 317 |
*/ |
|---|
| 318 |
public override String toString () { |
|---|
| 319 |
String string = control !is null ? control.toString () : Format( "{}/{}", numerator, denominator ); |
|---|
| 320 |
return Format("{{y = ({})x + {}}", string, ( offset >= 0 ? Format(")x + {}", offset ) : Format( ")x - {}", -offset))); |
|---|
| 321 |
} |
|---|
| 322 |
|
|---|
| 323 |
} |
|---|