1 /*******************************************************************************
2 * Copyright (c) 2000, 2018 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.layout;
16 import org.eclipse.swt.*;
17 import org.eclipse.swt.widgets.*;
20 * Instances of this class are used to define the edges of a control
21 * within a <code>FormLayout</code>.
23 * <code>FormAttachments</code> are set into the top, bottom, left,
24 * and right fields of the <code>FormData</code> for a control.
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);
36 * A <code>FormAttachment</code> defines where to attach the side of
37 * a control by using the equation, y = ax + b. The "a" term represents
38 * a fraction of the parent composite's width (from the left) or height
39 * (from the top). It can be defined using a numerator and denominator,
40 * or just a percentage value. If a percentage is used, the denominator
41 * is set to 100. The "b" term in the equation represents an offset, in
42 * points, from the attachment position. For example:</p>
44 * FormAttachment attach = new FormAttachment (20, -5);
47 * specifies that the side to which the <code>FormAttachment</code>
48 * object belongs will lie at 20% of the parent composite, minus 5 points.
51 * Control sides can also be attached to another control.
54 * FormAttachment attach = new FormAttachment (button, 10);
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 points. The control side can
59 * also be attached to the opposite side of the specified control.
62 * FormData data = new FormData ();
63 * data.left = new FormAttachment (button, 0, SWT.LEFT);
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:
70 * data.left = new FormAttachment (button, 0, SWT.CENTER);
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.
78 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
82 public final class FormAttachment {
84 * numerator specifies the numerator of the "a" term in the
85 * equation, y = ax + b, which defines the attachment.
90 * denominator specifies the denominator of the "a" term in the
91 * equation, y = ax + b, which defines the attachment.
93 * The default value is 100.
95 public int denominator = 100;
98 * offset specifies the offset, in points, 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.
105 * This is equivalent to the "b" term in the equation y = ax + b.
106 * The default value is 0.
111 * control specifies the control to which the control side is
114 public Control control;
117 * alignment specifies the alignment of the control side that is
118 * attached to a control.
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.
125 * <br>Possible values are: <ul>
126 * <li>{@link SWT#TOP}: Attach the side to the top side of the specified control.</li>
127 * <li>{@link SWT#BOTTOM}: Attach the side to the bottom side of the specified control.</li>
128 * <li>{@link SWT#LEFT}: Attach the side to the left side of the specified control.</li>
129 * <li>{@link SWT#RIGHT}: Attach the side to the right side of the specified control.</li>
130 * <li>{@link SWT#CENTER}: Attach the side at a position which will center the control on the specified control.</li>
131 * <li>{@link SWT#DEFAULT}: Attach the side to the adjacent side of the specified control.</li>
134 public int alignment;
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
145 public FormAttachment () {
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.
154 * @param numerator the percentage of the position
158 public FormAttachment (int numerator) {
159 this (numerator, 100, 0);
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.
168 * @param numerator the percentage of the position
169 * @param offset the offset of the side from the position
171 public FormAttachment (int numerator, int offset) {
172 this (numerator, 100, offset);
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
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
185 public FormAttachment (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;
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
199 * @param control the control the side is attached to
201 public FormAttachment (Control control) {
202 this (control, 0, SWT.DEFAULT);
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
211 * @param control the control the side is attached to
212 * @param offset the offset of the side from the control
214 public FormAttachment (Control control, int offset) {
215 this (control, offset, SWT.DEFAULT);
219 * Constructs a new instance of this class given a control,
220 * an offset and an alignment. The possible alignment values are:
222 * <dt><b>{@link SWT#TOP}</b></dt>
223 * <dd>the side will be attached to the top side of the specified control</dd>
224 * <dt><b>{@link SWT#BOTTOM}</b></dt>
225 * <dd>the side will be attached to the bottom side of the specified control</dd>
226 * <dt><b>{@link SWT#LEFT}</b></dt>
227 * <dd>the side will be attached to the left side of the specified control</dd>
228 * <dt><b>{@link SWT#RIGHT}</b></dt>
229 * <dd>the side will be attached to the right side of the specified control</dd>
230 * <dt><b>{@link SWT#CENTER}</b></dt>
231 * <dd>the side will be centered on the same side of the specified control</dd>
232 * <dt><b>{@link SWT#DEFAULT}</b></dt>
233 * <dd>the side will be attached to the adjacent side of the specified control</dd>
236 * @param control the control the side is attached to
237 * @param offset the offset of the side from the control
238 * @param alignment the alignment of the side to the control it is attached to,
239 * one of TOP, BOTTOM, LEFT, RIGHT, CENTER, or DEFAULT
241 public FormAttachment (Control control, int offset, int alignment) {
242 this.control = control;
243 this.offset = offset;
244 this.alignment = alignment;
247 FormAttachment divide (int value) {
248 return new FormAttachment (numerator, denominator * value, offset / value);
251 int gcd (int m, int n) {
268 FormAttachment minus (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;
279 FormAttachment minus (int value) {
280 return new FormAttachment (numerator, denominator, offset - value);
283 FormAttachment plus (FormAttachment attachment) {
284 FormAttachment solution = new FormAttachment ();
285 solution.numerator = numerator * attachment.denominator + denominator * attachment.numerator;
286 solution.denominator = denominator * attachment.denominator;
287 int gcd = gcd (solution.denominator, solution.numerator);
288 solution.numerator = solution.numerator / gcd;
289 solution.denominator = solution.denominator / gcd;
290 solution.offset = offset + attachment.offset;
294 FormAttachment plus (int value) {
295 return new FormAttachment (numerator, denominator, offset + value);
298 int solveX (int value) {
299 if (denominator == 0) SWT.error (SWT.ERROR_CANNOT_BE_ZERO);
300 return ((numerator * value) / denominator) + offset;
303 int solveY (int value) {
304 if (numerator == 0) SWT.error (SWT.ERROR_CANNOT_BE_ZERO);
305 return (value - offset) * denominator / numerator;
309 * Returns a string containing a concise, human-readable
310 * description of the receiver.
312 * @return a string representation of the FormAttachment
315 public String toString () {
316 String string = control != null ? control.toString () : numerator + "/" + denominator;
317 return "{y = (" + string + (offset >= 0 ? ")x + " + offset: ")x - " + (-offset))+"}";