]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/layout/FormAttachment.java
c7bc0ef91801b410ee0a88f624e2dbfee560b8b4
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / layout / FormAttachment.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2018 IBM Corporation and others.
3  *
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/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.layout;
15
16 import org.eclipse.swt.*;
17 import org.eclipse.swt.widgets.*;
18
19 /**
20  * Instances of this class are used to define the edges of a control
21  * within a <code>FormLayout</code>.
22  * <p>
23  * <code>FormAttachments</code> are set into the top, bottom, left,
24  * and right fields of the <code>FormData</code> for a control.
25  * For example:
26  * </p>
27  * <pre>
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);
34  * </pre>
35  * <p>
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>
43  * <pre>
44  *              FormAttachment attach = new FormAttachment (20, -5);
45  * </pre>
46  * <p>
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.
49  * </p>
50  * <p>
51  * Control sides can also be attached to another control.
52  * For example:</p>
53  * <pre>
54  *              FormAttachment attach = new FormAttachment (button, 10);
55  * </pre>
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.
60  * For example:
61  * <pre>
62  *              FormData data = new FormData ();
63  *              data.left = new FormAttachment (button, 0, SWT.LEFT);
64  * </pre>
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:
69  * <pre>
70  *              data.left = new FormAttachment (button, 0, SWT.CENTER);
71  * </pre>
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.
75  *
76  * @see FormLayout
77  * @see FormData
78  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
79  *
80  * @since 2.0
81  */
82 public final class FormAttachment {
83         /**
84          * numerator specifies the numerator of the "a" term in the
85          * equation, y = ax + b, which defines the attachment.
86          */
87         public int numerator;
88
89         /**
90          * denominator specifies the denominator of the "a" term in the
91          * equation, y = ax + b, which defines the attachment.
92          *
93          * The default value is 100.
94          */
95         public int denominator = 100;
96
97         /**
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.
104          *
105          * This is equivalent to the "b" term in the equation y = ax + b.
106          * The default value is 0.
107          */
108         public int offset;
109
110         /**
111          * control specifies the control to which the control side is
112          * attached.
113          */
114         public Control control;
115
116         /**
117          * alignment specifies the alignment of the control side that is
118          * attached to a control.
119          * <p>
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.
123          * </p>
124          *
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>
132          * </ul>
133          */
134         public int alignment;
135
136 /**
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
141  * offset is zero.
142  *
143  * @since 3.2
144  */
145 public FormAttachment () {
146 }
147
148 /**
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.
153  *
154  * @param numerator the percentage of the position
155  *
156  * @since 3.0
157  */
158 public FormAttachment (int numerator) {
159         this (numerator, 100, 0);
160 }
161
162 /**
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.
167  *
168  * @param numerator the percentage of the position
169  * @param offset the offset of the side from the position
170  */
171 public FormAttachment (int numerator, int offset) {
172         this (numerator, 100, offset);
173 }
174
175 /**
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
179  * and denominator.
180  *
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
184  */
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;
190 }
191
192 /**
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
197  * used.
198  *
199  * @param control the control the side is attached to
200  */
201 public FormAttachment (Control control) {
202         this (control, 0, SWT.DEFAULT);
203 }
204
205 /**
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
209  * specified control.
210  *
211  * @param control the control the side is attached to
212  * @param offset the offset of the side from the control
213  */
214 public FormAttachment (Control control, int offset) {
215         this (control, offset, SWT.DEFAULT);
216 }
217
218 /**
219  * Constructs a new instance of this class given a control,
220  * an offset and an alignment.  The possible alignment values are:
221  * <dl>
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>
234  * </dl>
235  *
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
240  */
241 public FormAttachment (Control control, int offset, int alignment) {
242         this.control = control;
243         this.offset = offset;
244         this.alignment = alignment;
245 }
246
247 FormAttachment divide (int value) {
248         return new FormAttachment (numerator, denominator * value, offset / value);
249 }
250
251 int gcd (int m, int n) {
252         int temp;
253         m = Math.abs (m);
254         n = Math.abs (n);
255         if (m < n) {
256                 temp = m;
257                 m = n;
258                 n = temp;
259         }
260         while (n != 0){
261                 temp = m;
262                 m = n;
263                 n = temp % n;
264         }
265         return m;
266 }
267
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;
276         return solution;
277 }
278
279 FormAttachment minus (int value) {
280         return new FormAttachment (numerator, denominator, offset - value);
281 }
282
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;
291         return solution;
292 }
293
294 FormAttachment plus (int value) {
295         return new FormAttachment (numerator, denominator, offset + value);
296 }
297
298 int solveX (int value) {
299         if (denominator == 0) SWT.error (SWT.ERROR_CANNOT_BE_ZERO);
300         return ((numerator * value) / denominator) + offset;
301 }
302
303 int solveY (int value) {
304         if (numerator == 0) SWT.error (SWT.ERROR_CANNOT_BE_ZERO);
305         return (value - offset) * denominator / numerator;
306 }
307
308 /**
309  * Returns a string containing a concise, human-readable
310  * description of the receiver.
311  *
312  * @return a string representation of the FormAttachment
313  */
314 @Override
315 public String toString () {
316         String string = control != null ? control.toString () : numerator + "/" + denominator;
317         return "{y = (" + string + (offset >= 0 ? ")x + " + offset: ")x - " + (-offset))+"}";
318 }
319
320 }