Mercurial > projects > dwt2
comparison org.eclipse.jface/src/org/eclipse/jface/util/Geometry.d @ 12:bc29606a740c
Added dwt-addons in original directory structure of eclipse.org
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 14 Mar 2009 18:23:29 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
11:43904fec5dca | 12:bc29606a740c |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2004, 2007 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 org.eclipse.jface.util.Geometry; | |
14 | |
15 import org.eclipse.swt.SWT; | |
16 import org.eclipse.swt.graphics.Point; | |
17 import org.eclipse.swt.graphics.Rectangle; | |
18 import org.eclipse.swt.widgets.Control; | |
19 | |
20 import java.lang.all; | |
21 import java.util.Set; | |
22 | |
23 /** | |
24 * Contains static methods for performing simple geometric operations | |
25 * on the SWT geometry classes. | |
26 * | |
27 * @since 3.0 | |
28 */ | |
29 public class Geometry { | |
30 | |
31 /** | |
32 * Prevent this class from being instantiated. | |
33 * | |
34 * @since 3.0 | |
35 */ | |
36 private this() { | |
37 //This is not instantiated | |
38 } | |
39 | |
40 /** | |
41 * Returns the square of the distance between two points. | |
42 * <p>This is preferred over the real distance when searching | |
43 * for the closest point, since it avoids square roots.</p> | |
44 * | |
45 * @param p1 first endpoint | |
46 * @param p2 second endpoint | |
47 * @return the square of the distance between the two points | |
48 * | |
49 * @since 3.0 | |
50 */ | |
51 public static int distanceSquared(Point p1, Point p2) { | |
52 int term1 = p1.x - p2.x; | |
53 int term2 = p1.y - p2.y; | |
54 return term1 * term1 + term2 * term2; | |
55 } | |
56 | |
57 /** | |
58 * Returns the magnitude of the given 2d vector (represented as a Point) | |
59 * | |
60 * @param p point representing the 2d vector whose magnitude is being computed | |
61 * @return the magnitude of the given 2d vector | |
62 * @since 3.0 | |
63 */ | |
64 public static double magnitude(Point p) { | |
65 return Math.sqrt( cast(real) magnitudeSquared(p)); | |
66 } | |
67 | |
68 /** | |
69 * Returns the square of the magnitude of the given 2-space vector (represented | |
70 * using a point) | |
71 * | |
72 * @param p the point whose magnitude is being computed | |
73 * @return the square of the magnitude of the given vector | |
74 * @since 3.0 | |
75 */ | |
76 public static int magnitudeSquared(Point p) { | |
77 return p.x * p.x + p.y * p.y; | |
78 } | |
79 | |
80 /** | |
81 * Returns the dot product of the given vectors (expressed as Points) | |
82 * | |
83 * @param p1 the first vector | |
84 * @param p2 the second vector | |
85 * @return the dot product of the two vectors | |
86 * @since 3.0 | |
87 */ | |
88 public static int dotProduct(Point p1, Point p2) { | |
89 return p1.x * p2.x + p1.y * p2.y; | |
90 } | |
91 | |
92 /** | |
93 * Returns a new point whose coordinates are the minimum of the coordinates of the | |
94 * given points | |
95 * | |
96 * @param p1 a Point | |
97 * @param p2 a Point | |
98 * @return a new point whose coordinates are the minimum of the coordinates of the | |
99 * given points | |
100 * @since 3.0 | |
101 */ | |
102 public static Point min(Point p1, Point p2) { | |
103 return new Point(Math.min(p1.x, p2.x), Math.min(p1.y, p2.y)); | |
104 } | |
105 | |
106 /** | |
107 * Returns a new point whose coordinates are the maximum of the coordinates | |
108 * of the given points | |
109 * @param p1 a Point | |
110 * @param p2 a Point | |
111 * @return point a new point whose coordinates are the maximum of the coordinates | |
112 * @since 3.0 | |
113 */ | |
114 public static Point max(Point p1, Point p2) { | |
115 return new Point(Math.max(p1.x, p2.x), Math.max(p1.y, p2.y)); | |
116 } | |
117 | |
118 /** | |
119 * Returns a vector in the given direction with the given | |
120 * magnitude. Directions are given using SWT direction constants, and | |
121 * the resulting vector is in the screen's coordinate system. That is, | |
122 * the vector (0, 1) is down and the vector (1, 0) is right. | |
123 * | |
124 * @param distance magnitude of the vector | |
125 * @param direction one of SWT.TOP, SWT.BOTTOM, SWT.LEFT, or SWT.RIGHT | |
126 * @return a point representing a vector in the given direction with the given magnitude | |
127 * @since 3.0 | |
128 */ | |
129 public static Point getDirectionVector(int distance, int direction) { | |
130 switch (direction) { | |
131 case SWT.TOP: | |
132 return new Point(0, -distance); | |
133 case SWT.BOTTOM: | |
134 return new Point(0, distance); | |
135 case SWT.LEFT: | |
136 return new Point(-distance, 0); | |
137 case SWT.RIGHT: | |
138 return new Point(distance, 0); | |
139 default: | |
140 } | |
141 | |
142 return new Point(0, 0); | |
143 } | |
144 | |
145 /** | |
146 * Returns the point in the center of the given rectangle. | |
147 * | |
148 * @param rect rectangle being computed | |
149 * @return a Point at the center of the given rectangle. | |
150 * @since 3.0 | |
151 */ | |
152 public static Point centerPoint(Rectangle rect) { | |
153 return new Point(rect.x + rect.width / 2, rect.y + rect.height / 2); | |
154 } | |
155 | |
156 /** | |
157 * Returns a copy of the given point | |
158 * | |
159 * @param toCopy point to copy | |
160 * @return a copy of the given point | |
161 */ | |
162 public static Point copy(Point toCopy) { | |
163 return new Point(toCopy.x, toCopy.y); | |
164 } | |
165 | |
166 /** | |
167 * Sets result equal to toCopy | |
168 * | |
169 * @param result object that will be modified | |
170 * @param toCopy object that will be copied | |
171 * @since 3.1 | |
172 */ | |
173 public static void set(Point result, Point toCopy) { | |
174 result.x = toCopy.x; | |
175 result.y = toCopy.y; | |
176 } | |
177 | |
178 /** | |
179 * Sets result equal to toCopy | |
180 * | |
181 * @param result object that will be modified | |
182 * @param toCopy object that will be copied | |
183 * @since 3.1 | |
184 */ | |
185 public static void set(Rectangle result, Rectangle toCopy) { | |
186 result.x = toCopy.x; | |
187 result.y = toCopy.y; | |
188 result.width = toCopy.width; | |
189 result.height = toCopy.height; | |
190 } | |
191 | |
192 /** | |
193 * <p>Returns a new difference Rectangle whose x, y, width, and height are equal to the difference of the corresponding | |
194 * attributes from the given rectangles</p> | |
195 * | |
196 * <p></p> | |
197 * <b>Example: Compute the margins for a given Composite, and apply those same margins to a new GridLayout</b> | |
198 * | |
199 * <code><pre> | |
200 * // Compute the client area, in the coordinate system of the input composite's parent | |
201 * Rectangle clientArea = Display.getCurrent().map(inputComposite, | |
202 * inputComposite.getParent(), inputComposite.getClientArea()); | |
203 * | |
204 * // Compute the margins for a given Composite by subtracting the client area from the composite's bounds | |
205 * Rectangle margins = Geometry.subtract(inputComposite.getBounds(), clientArea); | |
206 * | |
207 * // Now apply these margins to a new GridLayout | |
208 * GridLayout layout = GridLayoutFactory.fillDefaults().margins(margins).create(); | |
209 * </pre></code> | |
210 * | |
211 * @param rect1 first rectangle | |
212 * @param rect2 rectangle to subtract | |
213 * @return the difference between the two rectangles (computed as rect1 - rect2) | |
214 * @since 3.3 | |
215 */ | |
216 public static Rectangle subtract(Rectangle rect1, Rectangle rect2) { | |
217 return new Rectangle(rect1.x - rect2.x, rect1.y - rect2.y, rect1.width - rect2.width, rect1.height - rect2.height); | |
218 } | |
219 | |
220 /** | |
221 * <p>Returns a new Rectangle whose x, y, width, and height is the sum of the x, y, width, and height values of | |
222 * both rectangles respectively.</p> | |
223 * | |
224 * @param rect1 first rectangle to add | |
225 * @param rect2 second rectangle to add | |
226 * @return a new rectangle whose x, y, height, and width attributes are the sum of the corresponding attributes from | |
227 * the arguments. | |
228 * @since 3.3 | |
229 */ | |
230 public static Rectangle add(Rectangle rect1, Rectangle rect2) { | |
231 return new Rectangle(rect1.x + rect2.x, rect1.y + rect2.y, | |
232 rect1.width + rect2.width, rect1.height + rect2.height); | |
233 } | |
234 | |
235 /** | |
236 * Adds two points as 2d vectors. Returns a new point whose coordinates are | |
237 * the sum of the original two points. | |
238 * | |
239 * @param point1 the first point (not null) | |
240 * @param point2 the second point (not null) | |
241 * @return a new point whose coordinates are the sum of the given points | |
242 * @since 3.0 | |
243 */ | |
244 public static Point add(Point point1, Point point2) { | |
245 return new Point(point1.x + point2.x, point1.y + point2.y); | |
246 } | |
247 | |
248 /** | |
249 * Divides both coordinates of the given point by the given scalar. | |
250 * | |
251 * @since 3.1 | |
252 * | |
253 * @param toDivide point to divide | |
254 * @param scalar denominator | |
255 * @return a new Point whose coordinates are equal to the original point divided by the scalar | |
256 */ | |
257 public static Point divide(Point toDivide, int scalar) { | |
258 return new Point(toDivide.x / scalar, toDivide.y / scalar); | |
259 } | |
260 | |
261 | |
262 /** | |
263 * Performs vector subtraction on two points. Returns a new point equal to | |
264 * (point1 - point2). | |
265 * | |
266 * @param point1 initial point | |
267 * @param point2 vector to subtract | |
268 * @return the difference (point1 - point2) | |
269 * @since 3.0 | |
270 */ | |
271 public static Point subtract(Point point1, Point point2) { | |
272 return new Point(point1.x - point2.x, point1.y - point2.y); | |
273 } | |
274 | |
275 /** | |
276 * Swaps the X and Y coordinates of the given point. | |
277 * | |
278 * @param toFlip modifies this point | |
279 * @since 3.1 | |
280 */ | |
281 public static void flipXY(Point toFlip) { | |
282 int temp = toFlip.x; | |
283 toFlip.x = toFlip.y; | |
284 toFlip.y = temp; | |
285 } | |
286 | |
287 /** | |
288 * Swaps the X and Y coordinates of the given rectangle, along with the height and width. | |
289 * | |
290 * @param toFlip modifies this rectangle | |
291 * @since 3.1 | |
292 */ | |
293 public static void flipXY(Rectangle toFlip) { | |
294 int temp = toFlip.x; | |
295 toFlip.x = toFlip.y; | |
296 toFlip.y = temp; | |
297 | |
298 temp = toFlip.width; | |
299 toFlip.width = toFlip.height; | |
300 toFlip.height = temp; | |
301 } | |
302 | |
303 /** | |
304 * Returns the height or width of the given rectangle. | |
305 * | |
306 * @param toMeasure rectangle to measure | |
307 * @param width returns the width if true, and the height if false | |
308 * @return the width or height of the given rectangle | |
309 * @since 3.0 | |
310 */ | |
311 public static int getDimension(Rectangle toMeasure, bool width) { | |
312 if (width) { | |
313 return toMeasure.width; | |
314 } | |
315 return toMeasure.height; | |
316 } | |
317 | |
318 /** | |
319 * Returns the x or y coordinates of the given point. | |
320 * | |
321 * @param toMeasure point being measured | |
322 * @param width if true, returns x. Otherwise, returns y. | |
323 * @return the x or y coordinate | |
324 * @since 3.1 | |
325 */ | |
326 public static int getCoordinate(Point toMeasure, bool width) { | |
327 return width ? toMeasure.x : toMeasure.y; | |
328 } | |
329 | |
330 /** | |
331 * Returns the x or y coordinates of the given rectangle. | |
332 * | |
333 * @param toMeasure rectangle being measured | |
334 * @param width if true, returns x. Otherwise, returns y. | |
335 * @return the x or y coordinate | |
336 * @since 3.1 | |
337 */ | |
338 public static int getCoordinate(Rectangle toMeasure, bool width) { | |
339 return width ? toMeasure.x : toMeasure.y; | |
340 } | |
341 | |
342 /** | |
343 * Sets one dimension of the given rectangle. Modifies the given rectangle. | |
344 * | |
345 * @param toSet rectangle to modify | |
346 * @param width if true, the width is modified. If false, the height is modified. | |
347 * @param newCoordinate new value of the width or height | |
348 * @since 3.1 | |
349 */ | |
350 public static void setDimension(Rectangle toSet, bool width, int newCoordinate) { | |
351 if (width) { | |
352 toSet.width = newCoordinate; | |
353 } else { | |
354 toSet.height = newCoordinate; | |
355 } | |
356 } | |
357 | |
358 /** | |
359 * Sets one coordinate of the given rectangle. Modifies the given rectangle. | |
360 * | |
361 * @param toSet rectangle to modify | |
362 * @param width if true, the x coordinate is modified. If false, the y coordinate is modified. | |
363 * @param newCoordinate new value of the x or y coordinates | |
364 * @since 3.1 | |
365 */ | |
366 public static void setCoordinate(Rectangle toSet, bool width, int newCoordinate) { | |
367 if (width) { | |
368 toSet.x = newCoordinate; | |
369 } else { | |
370 toSet.y = newCoordinate; | |
371 } | |
372 } | |
373 | |
374 /** | |
375 * Sets one coordinate of the given point. Modifies the given point. | |
376 * | |
377 * @param toSet point to modify | |
378 * @param width if true, the x coordinate is modified. If false, the y coordinate is modified. | |
379 * @param newCoordinate new value of the x or y coordinates | |
380 * @since 3.1 | |
381 */ | |
382 public static void setCoordinate(Point toSet, bool width, int newCoordinate) { | |
383 if (width) { | |
384 toSet.x = newCoordinate; | |
385 } else { | |
386 toSet.y = newCoordinate; | |
387 } | |
388 } | |
389 | |
390 /** | |
391 * Returns the distance of the given point from a particular side of the given rectangle. | |
392 * Returns negative values for points outside the rectangle. | |
393 * | |
394 * @param rectangle a bounding rectangle | |
395 * @param testPoint a point to test | |
396 * @param edgeOfInterest side of the rectangle to test against | |
397 * @return the distance of the given point from the given edge of the rectangle | |
398 * @since 3.0 | |
399 */ | |
400 public static int getDistanceFromEdge(Rectangle rectangle, Point testPoint, | |
401 int edgeOfInterest) { | |
402 switch (edgeOfInterest) { | |
403 case SWT.TOP: | |
404 return testPoint.y - rectangle.y; | |
405 case SWT.BOTTOM: | |
406 return rectangle.y + rectangle.height - testPoint.y; | |
407 case SWT.LEFT: | |
408 return testPoint.x - rectangle.x; | |
409 case SWT.RIGHT: | |
410 return rectangle.x + rectangle.width - testPoint.x; | |
411 default: | |
412 } | |
413 | |
414 return 0; | |
415 } | |
416 | |
417 /** | |
418 * Extrudes the given edge inward by the given distance. That is, if one side of the rectangle | |
419 * was sliced off with a given thickness, this returns the rectangle that forms the slice. Note | |
420 * that the returned rectangle will be inside the given rectangle if size > 0. | |
421 * | |
422 * @param toExtrude the rectangle to extrude. The resulting rectangle will share three sides | |
423 * with this rectangle. | |
424 * @param size distance to extrude. A negative size will extrude outwards (that is, the resulting | |
425 * rectangle will overlap the original iff this is positive). | |
426 * @param orientation the side to extrude. One of SWT.LEFT, SWT.RIGHT, SWT.TOP, or SWT.BOTTOM. The | |
427 * resulting rectangle will always share this side with the original rectangle. | |
428 * @return a rectangle formed by extruding the given side of the rectangle by the given distance. | |
429 * @since 3.0 | |
430 */ | |
431 public static Rectangle getExtrudedEdge(Rectangle toExtrude, int size, | |
432 int orientation) { | |
433 Rectangle bounds = new Rectangle(toExtrude.x, toExtrude.y, | |
434 toExtrude.width, toExtrude.height); | |
435 | |
436 if (!isHorizontal(orientation)) { | |
437 bounds.width = size; | |
438 } else { | |
439 bounds.height = size; | |
440 } | |
441 | |
442 switch (orientation) { | |
443 case SWT.RIGHT: | |
444 bounds.x = toExtrude.x + toExtrude.width - bounds.width; | |
445 break; | |
446 case SWT.BOTTOM: | |
447 bounds.y = toExtrude.y + toExtrude.height - bounds.height; | |
448 break; | |
449 default: | |
450 } | |
451 | |
452 normalize(bounds); | |
453 | |
454 return bounds; | |
455 } | |
456 | |
457 /** | |
458 * Returns the opposite of the given direction. That is, returns SWT.LEFT if | |
459 * given SWT.RIGHT and visa-versa. | |
460 * | |
461 * @param swtDirectionConstant one of SWT.LEFT, SWT.RIGHT, SWT.TOP, or SWT.BOTTOM | |
462 * @return one of SWT.LEFT, SWT.RIGHT, SWT.TOP, or SWT.BOTTOM | |
463 * @since 3.0 | |
464 */ | |
465 public static int getOppositeSide(int swtDirectionConstant) { | |
466 switch (swtDirectionConstant) { | |
467 case SWT.TOP: | |
468 return SWT.BOTTOM; | |
469 case SWT.BOTTOM: | |
470 return SWT.TOP; | |
471 case SWT.LEFT: | |
472 return SWT.RIGHT; | |
473 case SWT.RIGHT: | |
474 return SWT.LEFT; | |
475 default: | |
476 } | |
477 | |
478 return swtDirectionConstant; | |
479 } | |
480 | |
481 /** | |
482 * Converts the given bool into an SWT orientation constant. | |
483 * | |
484 * @param horizontal if true, returns SWT.HORIZONTAL. If false, returns SWT.VERTICAL | |
485 * @return SWT.HORIZONTAL or SWT.VERTICAL. | |
486 * @since 3.0 | |
487 */ | |
488 public static int getSwtHorizontalOrVerticalConstant(bool horizontal) { | |
489 if (horizontal) { | |
490 return SWT.HORIZONTAL; | |
491 } | |
492 return SWT.VERTICAL; | |
493 } | |
494 | |
495 /** | |
496 * Returns true iff the given SWT side constant corresponds to a horizontal side | |
497 * of a rectangle. That is, returns true for the top and bottom but false for the | |
498 * left and right. | |
499 * | |
500 * @param swtSideConstant one of SWT.TOP, SWT.BOTTOM, SWT.LEFT, or SWT.RIGHT | |
501 * @return true iff the given side is horizontal. | |
502 * @since 3.0 | |
503 */ | |
504 public static bool isHorizontal(int swtSideConstant) { | |
505 return !(swtSideConstant is SWT.LEFT || swtSideConstant is SWT.RIGHT); | |
506 } | |
507 | |
508 /** | |
509 * Moves the given rectangle by the given delta. | |
510 * | |
511 * @param rect rectangle to move (will be modified) | |
512 * @param delta direction vector to move the rectangle by | |
513 * @since 3.0 | |
514 */ | |
515 public static void moveRectangle(Rectangle rect, Point delta) { | |
516 rect.x += delta.x; | |
517 rect.y += delta.y; | |
518 } | |
519 | |
520 /** | |
521 * Moves each edge of the given rectangle outward by the given amount. Negative values | |
522 * cause the rectangle to contract. Does not allow the rectangle's width or height to be | |
523 * reduced below zero. | |
524 * | |
525 * @param rect normalized rectangle to modify | |
526 * @param differenceRect difference rectangle to be added to rect | |
527 * @since 3.3 | |
528 */ | |
529 public static void expand(Rectangle rect, Rectangle differenceRect) { | |
530 rect.x += differenceRect.x; | |
531 rect.y += differenceRect.y; | |
532 rect.height = Math.max(0, rect.height + differenceRect.height); | |
533 rect.width = Math.max(0, rect.width + differenceRect.width); | |
534 } | |
535 | |
536 /** | |
537 * <p>Returns a rectangle which, when added to another rectangle, will expand each side | |
538 * by the given number of units.</p> | |
539 * | |
540 * <p>This is commonly used to store margin sizes. For example:</p> | |
541 * | |
542 * <code><pre> | |
543 * // Expands the left, right, top, and bottom | |
544 * // of the given control by 10, 5, 1, and 15 units respectively | |
545 * | |
546 * Rectangle margins = Geometry.createDifferenceRect(10,5,1,15); | |
547 * Rectangle bounds = someControl.getBounds(); | |
548 * someControl.setBounds(Geometry.add(bounds, margins)); | |
549 * </pre></code> | |
550 * | |
551 * @param left distance to expand the left side (negative values move the edge inward) | |
552 * @param right distance to expand the right side (negative values move the edge inward) | |
553 * @param top distance to expand the top (negative values move the edge inward) | |
554 * @param bottom distance to expand the bottom (negative values move the edge inward) | |
555 * | |
556 * @return a difference rectangle that, when added to another rectangle, will cause each | |
557 * side to expand by the given number of units | |
558 * @since 3.3 | |
559 */ | |
560 public static Rectangle createDiffRectangle(int left, int right, int top, int bottom) { | |
561 return new Rectangle(-left, -top, left + right, top + bottom); | |
562 } | |
563 | |
564 /** | |
565 * Moves each edge of the given rectangle outward by the given amount. Negative values | |
566 * cause the rectangle to contract. Does not allow the rectangle's width or height to be | |
567 * reduced below zero. | |
568 * | |
569 * @param rect normalized rectangle to modify | |
570 * @param left distance to move the left edge outward (negative values move the edge inward) | |
571 * @param right distance to move the right edge outward (negative values move the edge inward) | |
572 * @param top distance to move the top edge outward (negative values move the edge inward) | |
573 * @param bottom distance to move the bottom edge outward (negative values move the edge inward) | |
574 * @since 3.1 | |
575 */ | |
576 public static void expand(Rectangle rect, int left, int right, int top, int bottom) { | |
577 rect.x -= left; | |
578 rect.width = Math.max(0, rect.width + left + right); | |
579 rect.y -= top; | |
580 rect.height = Math.max(0, rect.height + top + bottom); | |
581 } | |
582 | |
583 /** | |
584 * Normalizes the given rectangle. That is, any rectangle with | |
585 * negative width or height becomes a rectangle with positive | |
586 * width or height that extends to the upper-left of the original | |
587 * rectangle. | |
588 * | |
589 * @param rect rectangle to modify | |
590 * @since 3.0 | |
591 */ | |
592 public static void normalize(Rectangle rect) { | |
593 if (rect.width < 0) { | |
594 rect.width = -rect.width; | |
595 rect.x -= rect.width; | |
596 } | |
597 | |
598 if (rect.height < 0) { | |
599 rect.height = -rect.height; | |
600 rect.y -= rect.height; | |
601 } | |
602 } | |
603 | |
604 /** | |
605 * Converts the given rectangle from display coordinates to the local coordinate system | |
606 * of the given object into display coordinates. | |
607 * | |
608 * @param coordinateSystem local coordinate system being converted to | |
609 * @param toConvert rectangle to convert | |
610 * @return a rectangle in control coordinates | |
611 * @since 3.0 | |
612 */ | |
613 public static Rectangle toControl(Control coordinateSystem, | |
614 Rectangle toConvert) { | |
615 return(coordinateSystem.getDisplay().map | |
616 (null,coordinateSystem,toConvert)); | |
617 } | |
618 | |
619 /** | |
620 * Converts the given rectangle from the local coordinate system of the given object | |
621 * into display coordinates. | |
622 * | |
623 * @param coordinateSystem local coordinate system being converted from | |
624 * @param toConvert rectangle to convert | |
625 * @return a rectangle in display coordinates | |
626 * @since 3.0 | |
627 */ | |
628 public static Rectangle toDisplay(Control coordinateSystem, | |
629 Rectangle toConvert) { | |
630 return(coordinateSystem.getDisplay().map | |
631 (coordinateSystem,null,toConvert)); | |
632 | |
633 } | |
634 | |
635 /** | |
636 * Determines where the given point lies with respect to the given rectangle. | |
637 * Returns a combination of SWT.LEFT, SWT.RIGHT, SWT.TOP, and SWT.BOTTOM, combined | |
638 * with bitwise or (for example, returns SWT.TOP | SWT.LEFT if the point is to the | |
639 * upper-left of the rectangle). Returns 0 if the point lies within the rectangle. | |
640 * Positions are in screen coordinates (ie: a point is to the upper-left of the | |
641 * rectangle if its x and y coordinates are smaller than any point in the rectangle) | |
642 * | |
643 * @param boundary normalized boundary rectangle | |
644 * @param toTest point whose relative position to the rectangle is being computed | |
645 * @return one of SWT.LEFT | SWT.TOP, SWT.TOP, SWT.RIGHT | SWT.TOP, SWT.LEFT, 0, | |
646 * SWT.RIGHT, SWT.LEFT | SWT.BOTTOM, SWT.BOTTOM, SWT.RIGHT | SWT.BOTTOM | |
647 * @since 3.0 | |
648 */ | |
649 public static int getRelativePosition(Rectangle boundary, Point toTest) { | |
650 int result = 0; | |
651 | |
652 if (toTest.x < boundary.x) { | |
653 result |= SWT.LEFT; | |
654 } else if (toTest.x >= boundary.x + boundary.width) { | |
655 result |= SWT.RIGHT; | |
656 } | |
657 | |
658 if (toTest.y < boundary.y) { | |
659 result |= SWT.TOP; | |
660 } else if (toTest.y >= boundary.y + boundary.height) { | |
661 result |= SWT.BOTTOM; | |
662 } | |
663 | |
664 return result; | |
665 } | |
666 | |
667 /** | |
668 * Returns the distance from the point to the nearest edge of the given | |
669 * rectangle. Returns negative values if the point lies outside the rectangle. | |
670 * | |
671 * @param boundary rectangle to test | |
672 * @param toTest point to test | |
673 * @return the distance between the given point and the nearest edge of the rectangle. | |
674 * Returns positive values for points inside the rectangle and negative values for points | |
675 * outside the rectangle. | |
676 * @since 3.1 | |
677 */ | |
678 public static int getDistanceFrom(Rectangle boundary, Point toTest) { | |
679 int side = getClosestSide(boundary, toTest); | |
680 return getDistanceFromEdge(boundary, toTest, side); | |
681 } | |
682 | |
683 /** | |
684 * Returns the edge of the given rectangle is closest to the given | |
685 * point. | |
686 * | |
687 * @param boundary rectangle to test | |
688 * @param toTest point to compare | |
689 * @return one of SWT.LEFT, SWT.RIGHT, SWT.TOP, or SWT.BOTTOM | |
690 * | |
691 * @since 3.0 | |
692 */ | |
693 public static int getClosestSide(Rectangle boundary, Point toTest) { | |
694 int[] sides = [ SWT.LEFT, SWT.RIGHT, SWT.TOP, SWT.BOTTOM ]; | |
695 | |
696 int closestSide = SWT.LEFT; | |
697 int closestDistance = Integer.MAX_VALUE; | |
698 | |
699 for (int idx = 0; idx < sides.length; idx++) { | |
700 int side = sides[idx]; | |
701 | |
702 int distance = getDistanceFromEdge(boundary, toTest, side); | |
703 | |
704 if (distance < closestDistance) { | |
705 closestDistance = distance; | |
706 closestSide = side; | |
707 } | |
708 } | |
709 | |
710 return closestSide; | |
711 } | |
712 | |
713 /** | |
714 * Returns a copy of the given rectangle | |
715 * | |
716 * @param toCopy rectangle to copy | |
717 * @return a copy of the given rectangle | |
718 * @since 3.0 | |
719 */ | |
720 public static Rectangle copy(Rectangle toCopy) { | |
721 return new Rectangle(toCopy.x, toCopy.y, toCopy.width, toCopy.height); | |
722 } | |
723 | |
724 /** | |
725 * Returns the size of the rectangle, as a Point | |
726 * | |
727 * @param rectangle rectangle whose size is being computed | |
728 * @return the size of the given rectangle | |
729 * @since 3.0 | |
730 */ | |
731 public static Point getSize(Rectangle rectangle) { | |
732 return new Point(rectangle.width, rectangle.height); | |
733 } | |
734 | |
735 /** | |
736 * Sets the size of the given rectangle to the given size | |
737 * | |
738 * @param rectangle rectangle to modify | |
739 * @param newSize new size of the rectangle | |
740 * @since 3.0 | |
741 */ | |
742 public static void setSize(Rectangle rectangle, Point newSize) { | |
743 rectangle.width = newSize.x; | |
744 rectangle.height = newSize.y; | |
745 } | |
746 | |
747 /** | |
748 * Sets the x,y position of the given rectangle. For a normalized | |
749 * rectangle (a rectangle with positive width and height), this will | |
750 * be the upper-left corner of the rectangle. | |
751 * | |
752 * @param rectangle rectangle to modify | |
753 * @param newLocation new location of the rectangle | |
754 * | |
755 * @since 3.0 | |
756 */ | |
757 public static void setLocation(Rectangle rectangle, Point newLocation) { | |
758 rectangle.x = newLocation.x; | |
759 rectangle.y = newLocation.y; | |
760 } | |
761 | |
762 /** | |
763 * Returns the x,y position of the given rectangle. For normalized rectangles | |
764 * (rectangles with positive width and height), this is the upper-left | |
765 * corner of the rectangle. | |
766 * | |
767 * @param toQuery rectangle to query | |
768 * @return a Point containing the x,y position of the rectangle | |
769 * | |
770 * @since 3.0 | |
771 */ | |
772 public static Point getLocation(Rectangle toQuery) { | |
773 return new Point(toQuery.x, toQuery.y); | |
774 } | |
775 | |
776 /** | |
777 * Returns a new rectangle with the given position and dimensions, expressed | |
778 * as points. | |
779 * | |
780 * @param position the (x,y) position of the rectangle | |
781 * @param size the size of the new rectangle, where (x,y) -> (width, height) | |
782 * @return a new Rectangle with the given position and size | |
783 * | |
784 * @since 3.0 | |
785 */ | |
786 public static Rectangle createRectangle(Point position, Point size) { | |
787 return new Rectangle(position.x, position.y, size.x, size.y); | |
788 } | |
789 | |
790 /** | |
791 * Repositions the 'inner' rectangle to lie completely within the bounds of the 'outer' | |
792 * rectangle if possible. One use for this is to ensure that, when setting a control's bounds, | |
793 * that they will always lie within its parent's client area (to avoid clipping). | |
794 * | |
795 * @param inner The 'inner' rectangle to be repositioned (should be smaller than the 'outer' rectangle) | |
796 * @param outer The 'outer' rectangle | |
797 */ | |
798 public static void moveInside(Rectangle inner, Rectangle outer) { | |
799 // adjust X | |
800 if (inner.x < outer.x) { | |
801 inner.x = outer.x; | |
802 } | |
803 if ((inner.x + inner.width) > (outer.x + outer.width)) { | |
804 inner.x -= (inner.x + inner.width) - (outer.x + outer.width); | |
805 } | |
806 | |
807 // Adjust Y | |
808 if (inner.y < outer.y) { | |
809 inner.y = outer.y; | |
810 } | |
811 if ((inner.y + inner.height) > (outer.y + outer.height)) { | |
812 inner.y -= (inner.y + inner.height) - (outer.y + outer.height); | |
813 } | |
814 } | |
815 | |
816 } |