1 /*******************************************************************************
2  * Copyright (c) 2000, 2017 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.graphics;
15 
16 
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.internal.*;
19 import org.eclipse.swt.internal.cocoa.*;
20 
21 /**
22  * Instances of this class represent areas of an x-y coordinate
23  * system that are aggregates of the areas covered by a number
24  * of polygons.
25  * <p>
26  * Application code must explicitly invoke the <code>Region.dispose()</code>
27  * method to release the operating system resources managed by each instance
28  * when those instances are no longer required.
29  * </p>
30  *
31  * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: GraphicsExample</a>
32  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
33  */
34 public final class Region extends Resource {
35 	/**
36 	 * the OS resource for the region
37 	 * (Warning: This field is platform dependent)
38 	 * <p>
39 	 * <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT
40 	 * public API. It is marked public only so that it can be shared
41 	 * within the packages provided by SWT. It is not available on all
42 	 * platforms and should never be accessed from application code.
43 	 * </p>
44 	 *
45 	 * @noreference This field is not intended to be referenced by clients.
46 	 */
47 	public long handle;
48 
49 /**
50  * Constructs a new empty region.
51  * <p>
52  * You must dispose the region when it is no longer required.
53  * </p>
54  *
55  * @exception SWTError <ul>
56  *    <li>ERROR_NO_HANDLES if a handle could not be obtained for region creation</li>
57  * </ul>
58  *
59  * @see #dispose()
60  */
Region()61 public Region() {
62 	this(null);
63 }
64 
65 /**
66  * Constructs a new empty region.
67  * <p>
68  * You must dispose the region when it is no longer required.
69  * </p>
70  *
71  * @param device the device on which to allocate the region
72  *
73  * @exception IllegalArgumentException <ul>
74  *    <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
75  * </ul>
76  * @exception SWTError <ul>
77  *    <li>ERROR_NO_HANDLES if a handle could not be obtained for region creation</li>
78  * </ul>
79  *
80  * @see #dispose()
81  *
82  * @since 3.0
83  */
Region(Device device)84 public Region(Device device) {
85 	super(device);
86 	NSAutoreleasePool pool = null;
87 	if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
88 	try {
89 		handle = OS.NewRgn();
90 		if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
91 		init();
92 	} finally {
93 		if (pool != null) pool.release();
94 	}
95 }
96 
Region(Device device, long handle)97 Region(Device device, long handle) {
98 	super(device);
99 	this.handle = handle;
100 }
101 
102 /**
103  * Invokes platform specific functionality to allocate a new region.
104  * <p>
105  * <b>IMPORTANT:</b> This method is <em>not</em> part of the public
106  * API for <code>Region</code>. It is marked public only so that it
107  * can be shared within the packages provided by SWT. It is not
108  * available on all platforms, and should never be called from
109  * application code.
110  * </p>
111  *
112  * @param device the device on which to allocate the region
113  * @param handle the handle for the region
114  * @return a new region object containing the specified device and handle
115  *
116  * @noreference This method is not intended to be referenced by clients.
117  */
cocoa_new(Device device, long handle)118 public static Region cocoa_new(Device device, long handle) {
119 	return new Region(device, handle);
120 }
121 
polyToRgn(int[] poly, int length)122 static long polyToRgn(int[] poly, int length) {
123 	short[] r = new short[4];
124 	long polyRgn = OS.NewRgn(), rectRgn = OS.NewRgn();
125 	int minY = poly[1], maxY = poly[1];
126 	for (int y = 3; y < length; y += 2) {
127 		if (poly[y] < minY) minY = poly[y];
128 		if (poly[y] > maxY) maxY = poly[y];
129 	}
130 	int[] inter = new int[length + 1];
131 	for (int y = minY; y <= maxY; y++) {
132 		int count = 0;
133 		int x1 = poly[0], y1 = poly[1];
134 		for (int p = 2; p < length; p += 2) {
135 			int x2 = poly[p], y2 = poly[p + 1];
136 			if (y1 != y2 && ((y1 <= y && y < y2) || (y2 <= y && y < y1))) {
137 				inter[count++] = (int)((((y - y1) / (float)(y2 - y1)) * (x2 - x1)) + x1 + 0.5f);
138 			}
139 			x1 = x2;
140 			y1 = y2;
141 		}
142 		int x2 = poly[0], y2 = poly[1];
143 		if (y1 != y2 && ((y1 <= y && y < y2) || (y2 <= y && y < y1))) {
144 			inter[count++] = (int)((((y - y1) / (float)(y2 - y1)) * (x2 - x1)) + x1 + 0.5f);
145 		}
146 		for (int gap=count/2; gap>0; gap/=2) {
147 			for (int i=gap; i<count; i++) {
148 				for (int j=i-gap; j>=0; j-=gap) {
149 					if ((inter[j] - inter[j + gap]) <= 0)
150 						break;
151 					int temp = inter[j];
152 					inter[j] = inter[j + gap];
153 					inter[j + gap] = temp;
154 				}
155 			}
156 		}
157 		for (int i = 0; i < count; i += 2) {
158 			OS.SetRect(r, (short)inter[i], (short)y, (short)(inter[i + 1]),(short)(y + 1));
159 			OS.RectRgn(rectRgn, r);
160 			OS.UnionRgn(polyRgn, rectRgn, polyRgn);
161 		}
162 	}
163 	OS.DisposeRgn(rectRgn);
164 	return polyRgn;
165 }
166 
polyRgn(int[] pointArray, int count)167 static long polyRgn(int[] pointArray, int count) {
168 	NSAutoreleasePool pool = null;
169 	if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
170 	try {
171 		long polyRgn;
172 		if (C.PTR_SIZEOF == 4) {
173 			polyRgn = OS.NewRgn();
174 			OS.OpenRgn();
175 			OS.MoveTo((short)pointArray[0], (short)pointArray[1]);
176 			for (int i = 1; i < count / 2; i++) {
177 				OS.LineTo((short)pointArray[2 * i], (short)pointArray[2 * i + 1]);
178 			}
179 			OS.LineTo((short)pointArray[0], (short)pointArray[1]);
180 			OS.CloseRgn(polyRgn);
181 		} else {
182 			polyRgn = polyToRgn(pointArray, count);
183 		}
184 		return polyRgn;
185 	} finally {
186 		if (pool != null) pool.release();
187 	}
188 }
189 
190 /**
191  * Adds the given polygon to the collection of polygons
192  * the receiver maintains to describe its area.
193  *
194  * @param pointArray points that describe the polygon to merge with the receiver
195  *
196  * @exception IllegalArgumentException <ul>
197  *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
198  * </ul>
199  * @exception SWTException <ul>
200  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
201  * </ul>
202  *
203  * @since 3.0
204  *
205  */
add(int[] pointArray)206 public void add (int[] pointArray) {
207 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
208 	if (pointArray == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
209 	NSAutoreleasePool pool = null;
210 	if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
211 	try {
212 		add(pointArray, pointArray.length);
213 	} finally {
214 		if (pool != null) pool.release();
215 	}
216 }
217 
add(int[] pointArray, int count)218 void add(int[] pointArray, int count) {
219 	count = count / 2 * 2;
220 	if (count <= 2) return;
221 	NSAutoreleasePool pool = null;
222 	if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
223 	try {
224 		long polyRgn = polyRgn(pointArray, count);
225 		OS.UnionRgn(handle, polyRgn, handle);
226 		OS.DisposeRgn(polyRgn);
227 	} finally {
228 		if (pool != null) pool.release();
229 	}
230 }
231 
232 /**
233  * Adds the given rectangle to the collection of polygons
234  * the receiver maintains to describe its area.
235  *
236  * @param rect the rectangle to merge with the receiver
237  *
238  * @exception IllegalArgumentException <ul>
239  *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
240  *    <li>ERROR_INVALID_ARGUMENT - if the rectangle's width or height is negative</li>
241  * </ul>
242  * @exception SWTException <ul>
243  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
244  * </ul>
245  */
add(Rectangle rect)246 public void add(Rectangle rect) {
247 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
248 	if (rect == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
249 	if (rect.width < 0 || rect.height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
250 	NSAutoreleasePool pool = null;
251 	if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
252 	try {
253 		add (rect.x, rect.y, rect.width, rect.height);
254 	} finally {
255 		if (pool != null) pool.release();
256 	}
257 }
258 
259 /**
260  * Adds the given rectangle to the collection of polygons
261  * the receiver maintains to describe its area.
262  *
263  * @param x the x coordinate of the rectangle
264  * @param y the y coordinate of the rectangle
265  * @param width the width coordinate of the rectangle
266  * @param height the height coordinate of the rectangle
267  *
268  * @exception IllegalArgumentException <ul>
269  *    <li>ERROR_INVALID_ARGUMENT - if the rectangle's width or height is negative</li>
270  * </ul>
271  * @exception SWTException <ul>
272  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
273  * </ul>
274  *
275  * @since 3.1
276  */
add(int x, int y, int width, int height)277 public void add(int x, int y, int width, int height) {
278 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
279 	if (width < 0 || height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
280 	NSAutoreleasePool pool = null;
281 	if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
282 	try {
283 		long rectRgn = OS.NewRgn();
284 		short[] r = new short[4];
285 		OS.SetRect(r, (short)x, (short)y, (short)(x + width),(short)(y + height));
286 		OS.RectRgn(rectRgn, r);
287 		OS.UnionRgn(handle, rectRgn, handle);
288 		OS.DisposeRgn(rectRgn);
289 	} finally {
290 		if (pool != null) pool.release();
291 	}
292 }
293 
294 /**
295  * Adds all of the polygons which make up the area covered
296  * by the argument to the collection of polygons the receiver
297  * maintains to describe its area.
298  *
299  * @param region the region to merge
300  *
301  * @exception IllegalArgumentException <ul>
302  *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
303  *    <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
304  * </ul>
305  * @exception SWTException <ul>
306  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
307  * </ul>
308  */
add(Region region)309 public void add(Region region) {
310 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
311 	if (region == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
312 	if (region.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
313 	NSAutoreleasePool pool = null;
314 	if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
315 	try {
316 		OS.UnionRgn(handle, region.handle, handle);
317 	} finally {
318 		if (pool != null) pool.release();
319 	}
320 }
321 
322 /**
323  * Returns <code>true</code> if the point specified by the
324  * arguments is inside the area specified by the receiver,
325  * and <code>false</code> otherwise.
326  *
327  * @param x the x coordinate of the point to test for containment
328  * @param y the y coordinate of the point to test for containment
329  * @return <code>true</code> if the region contains the point and <code>false</code> otherwise
330  *
331  * @exception SWTException <ul>
332  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
333  * </ul>
334  */
contains(int x, int y)335 public boolean contains(int x, int y) {
336 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
337 	NSAutoreleasePool pool = null;
338 	if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
339 	try {
340 		short[] point = new short[]{(short)y, (short)x};
341 		return OS.PtInRgn(point, handle);
342 	} finally {
343 		if (pool != null) pool.release();
344 	}
345 }
346 
347 /**
348  * Returns <code>true</code> if the given point is inside the
349  * area specified by the receiver, and <code>false</code>
350  * otherwise.
351  *
352  * @param pt the point to test for containment
353  * @return <code>true</code> if the region contains the point and <code>false</code> otherwise
354  *
355  * @exception IllegalArgumentException <ul>
356  *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
357  * </ul>
358  * @exception SWTException <ul>
359  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
360  * </ul>
361  */
contains(Point pt)362 public boolean contains(Point pt) {
363 	if (pt == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
364 	return contains(pt.x, pt.y);
365 }
366 
367 NSAffineTransform transform;
convertRgn(NSAffineTransform transform)368 void convertRgn(NSAffineTransform transform) {
369 	long newRgn = OS.NewRgn();
370 	Callback callback = new Callback(this, "convertRgn", 4);
371 	this.transform = transform;
372 	OS.QDRegionToRects(handle, OS.kQDParseRegionFromTopLeft, callback.getAddress(), newRgn);
373 	this.transform = null;
374 	callback.dispose();
375 	OS.CopyRgn(newRgn, handle);
376 	OS.DisposeRgn(newRgn);
377 }
378 
convertRgn(long message, long rgn, long r, long newRgn)379 long convertRgn(long message, long rgn, long r, long newRgn) {
380 	if (message == OS.kQDRegionToRectsMsgParse) {
381 		short[] rect = new short[4];
382 		C.memmove(rect, r, rect.length * 2);
383 		int i = 0;
384 		NSPoint point = new NSPoint();
385 		int[] points = new int[10];
386 		point.x = rect[1];
387 		point.y = rect[0];
388 		point = transform.transformPoint(point);
389 		short startX, startY;
390 		points[i++] = startX = (short)point.x;
391 		points[i++] = startY = (short)point.y;
392 		point.x = rect[3];
393 		point.y = rect[0];
394 		point = transform.transformPoint(point);
395 		points[i++] = (short)Math.round(point.x);
396 		points[i++] = (short)point.y;
397 		point.x = rect[3];
398 		point.y = rect[2];
399 		point = transform.transformPoint(point);
400 		points[i++] = (short)Math.round(point.x);
401 		points[i++] = (short)Math.round(point.y);
402 		point.x = rect[1];
403 		point.y = rect[2];
404 		point = transform.transformPoint(point);
405 		points[i++] = (short)point.x;
406 		points[i++] = (short)Math.round(point.y);
407 		points[i++] = startX;
408 		points[i++] = startY;
409 		long polyRgn = polyRgn(points, points.length);
410 		OS.UnionRgn(newRgn, polyRgn, newRgn);
411 		OS.DisposeRgn(polyRgn);
412 	}
413 	return 0;
414 }
415 
416 @Override
destroy()417 void destroy() {
418 	OS.DisposeRgn(handle);
419 	handle = 0;
420 }
421 
422 /**
423  * Compares the argument to the receiver, and returns true
424  * if they represent the <em>same</em> object using a class
425  * specific comparison.
426  *
427  * @param object the object to compare with this object
428  * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
429  *
430  * @see #hashCode
431  */
432 @Override
equals(Object object)433 public boolean equals(Object object) {
434 	if (this == object) return true;
435 	if (!(object instanceof Region)) return false;
436 	Region region = (Region)object;
437 	return handle == region.handle;
438 }
439 
440 /**
441  * Returns a rectangle which represents the rectangular
442  * union of the collection of polygons the receiver
443  * maintains to describe its area.
444  *
445  * @return a bounding rectangle for the region
446  *
447  * @exception SWTException <ul>
448  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
449  * </ul>
450  *
451  * @see Rectangle#union
452  */
getBounds()453 public Rectangle getBounds() {
454 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
455 	NSAutoreleasePool pool = null;
456 	if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
457 	try {
458 		short[] bounds = new short[4];
459 		OS.GetRegionBounds(handle, bounds);
460 		int width = bounds[3] - bounds[1];
461 		int height = bounds[2] - bounds[0];
462 		return new Rectangle(bounds[1], bounds[0], width, height);
463 	} finally {
464 		if (pool != null) pool.release();
465 	}
466 }
467 
getPath()468 NSBezierPath getPath() {
469 	Callback callback = new Callback(this, "regionToRects", 4);
470 	NSBezierPath path = NSBezierPath.bezierPath();
471 	path.retain();
472 	OS.QDRegionToRects(handle, OS.kQDParseRegionFromTopLeft, callback.getAddress(), path.id);
473 	callback.dispose();
474 	if (path.isEmpty()) path.appendBezierPathWithRect(new NSRect());
475 	return path;
476 }
477 
478 NSPoint pt = new NSPoint();
479 short[] rect = new short[4];
regionToRects(long message, long rgn, long r, long path)480 long regionToRects(long message, long rgn, long r, long path) {
481 	if (message == OS.kQDRegionToRectsMsgParse) {
482 		C.memmove(rect, r, rect.length * 2);
483 		pt.x = rect[1];
484 		pt.y = rect[0];
485 		OS.objc_msgSend(path, OS.sel_moveToPoint_, pt);
486 		pt.x = rect[3];
487 		OS.objc_msgSend(path, OS.sel_lineToPoint_, pt);
488 		pt.x = rect[3];
489 		pt.y = rect[2];
490 		OS.objc_msgSend(path, OS.sel_lineToPoint_, pt);
491 		pt.x = rect[1];
492 		OS.objc_msgSend(path, OS.sel_lineToPoint_, pt);
493 		OS.objc_msgSend(path, OS.sel_closePath);
494 	}
495 	return 0;
496 }
497 
498 /**
499  * Returns an integer hash code for the receiver. Any two
500  * objects that return <code>true</code> when passed to
501  * <code>equals</code> must return the same value for this
502  * method.
503  *
504  * @return the receiver's hash
505  *
506  * @see #equals
507  */
508 @Override
hashCode()509 public int hashCode() {
510 	return (int)handle;
511 }
512 
513 /**
514  * Intersects the given rectangle to the collection of polygons
515  * the receiver maintains to describe its area.
516  *
517  * @param rect the rectangle to intersect with the receiver
518  *
519  * @exception IllegalArgumentException <ul>
520  *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
521  *    <li>ERROR_INVALID_ARGUMENT - if the rectangle's width or height is negative</li>
522  * </ul>
523  * @exception SWTException <ul>
524  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
525  * </ul>
526  *
527  * @since 3.0
528  */
intersect(Rectangle rect)529 public void intersect(Rectangle rect) {
530 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
531 	if (rect == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
532 	intersect (rect.x, rect.y, rect.width, rect.height);
533 }
534 
535 /**
536  * Intersects the given rectangle to the collection of polygons
537  * the receiver maintains to describe its area.
538  *
539  * @param x the x coordinate of the rectangle
540  * @param y the y coordinate of the rectangle
541  * @param width the width coordinate of the rectangle
542  * @param height the height coordinate of the rectangle
543  *
544  * @exception IllegalArgumentException <ul>
545  *    <li>ERROR_INVALID_ARGUMENT - if the rectangle's width or height is negative</li>
546  * </ul>
547  * @exception SWTException <ul>
548  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
549  * </ul>
550  *
551  * @since 3.1
552  */
intersect(int x, int y, int width, int height)553 public void intersect(int x, int y, int width, int height) {
554 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
555 	if (width < 0 || height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
556 	NSAutoreleasePool pool = null;
557 	if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
558 	try {
559 		long rectRgn = OS.NewRgn();
560 		short[] r = new short[4];
561 		OS.SetRect(r, (short)x, (short)y, (short)(x + width),(short)(y + height));
562 		OS.RectRgn(rectRgn, r);
563 		OS.SectRgn(handle, rectRgn, handle);
564 		OS.DisposeRgn(rectRgn);
565 	} finally {
566 		if (pool != null) pool.release();
567 	}
568 }
569 
570 /**
571  * Intersects all of the polygons which make up the area covered
572  * by the argument to the collection of polygons the receiver
573  * maintains to describe its area.
574  *
575  * @param region the region to intersect
576  *
577  * @exception IllegalArgumentException <ul>
578  *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
579  *    <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
580  * </ul>
581  * @exception SWTException <ul>
582  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
583  * </ul>
584  *
585  * @since 3.0
586  */
intersect(Region region)587 public void intersect(Region region) {
588 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
589 	if (region == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
590 	if (region.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
591 	NSAutoreleasePool pool = null;
592 	if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
593 	try {
594 		OS.SectRgn(handle, region.handle, handle);
595 	} finally {
596 		if (pool != null) pool.release();
597 	}
598 }
599 
600 /**
601  * Returns <code>true</code> if the rectangle described by the
602  * arguments intersects with any of the polygons the receiver
603  * maintains to describe its area, and <code>false</code> otherwise.
604  *
605  * @param x the x coordinate of the origin of the rectangle
606  * @param y the y coordinate of the origin of the rectangle
607  * @param width the width of the rectangle
608  * @param height the height of the rectangle
609  * @return <code>true</code> if the rectangle intersects with the receiver, and <code>false</code> otherwise
610  *
611  * @exception SWTException <ul>
612  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
613  * </ul>
614  *
615  * @see Rectangle#intersects(Rectangle)
616  */
intersects(int x, int y, int width, int height)617 public boolean intersects (int x, int y, int width, int height) {
618 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
619 	NSAutoreleasePool pool = null;
620 	if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
621 	try {
622 		short[] r = new short[4];
623 		OS.SetRect(r, (short)x, (short)y, (short)(x + width),(short)(y + height));
624 		return OS.RectInRgn(r, handle);
625 	} finally {
626 		if (pool != null) pool.release();
627 	}
628 }
629 
630 /**
631  * Returns <code>true</code> if the given rectangle intersects
632  * with any of the polygons the receiver maintains to describe
633  * its area and <code>false</code> otherwise.
634  *
635  * @param rect the rectangle to test for intersection
636  * @return <code>true</code> if the rectangle intersects with the receiver, and <code>false</code> otherwise
637  *
638  * @exception IllegalArgumentException <ul>
639  *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
640  * </ul>
641  * @exception SWTException <ul>
642  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
643  * </ul>
644  *
645  * @see Rectangle#intersects(Rectangle)
646  */
intersects(Rectangle rect)647 public boolean intersects(Rectangle rect) {
648 	if (rect == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
649 	return intersects(rect.x, rect.y, rect.width, rect.height);
650 }
651 
652 /**
653  * Returns <code>true</code> if the region has been disposed,
654  * and <code>false</code> otherwise.
655  * <p>
656  * This method gets the dispose state for the region.
657  * When a region has been disposed, it is an error to
658  * invoke any other method (except {@link #dispose()}) using the region.
659  *
660  * @return <code>true</code> when the region is disposed, and <code>false</code> otherwise
661  */
662 @Override
isDisposed()663 public boolean isDisposed() {
664 	return handle == 0;
665 }
666 
667 /**
668  * Returns <code>true</code> if the receiver does not cover any
669  * area in the (x, y) coordinate plane, and <code>false</code> if
670  * the receiver does cover some area in the plane.
671  *
672  * @return <code>true</code> if the receiver is empty, and <code>false</code> otherwise
673  *
674  * @exception SWTException <ul>
675  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
676  * </ul>
677  */
isEmpty()678 public boolean isEmpty() {
679 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
680 	NSAutoreleasePool pool = null;
681 	if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
682 	try {
683 		return OS.EmptyRgn(handle);
684 	} finally {
685 		if (pool != null) pool.release();
686 	}
687 }
688 
689 /**
690  * Subtracts the given polygon from the collection of polygons
691  * the receiver maintains to describe its area.
692  *
693  * @param pointArray points that describe the polygon to merge with the receiver
694  *
695  * @exception IllegalArgumentException <ul>
696  *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
697  * </ul>
698  * @exception SWTException <ul>
699  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
700  * </ul>
701  *
702  * @since 3.0
703  */
subtract(int[] pointArray)704 public void subtract (int[] pointArray) {
705 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
706 	if (pointArray == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
707 	if (pointArray.length < 2) return;
708 	NSAutoreleasePool pool = null;
709 	if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
710 	try {
711 		long polyRgn = polyRgn(pointArray, pointArray.length);
712 		OS.DiffRgn(handle, polyRgn, handle);
713 		OS.DisposeRgn(polyRgn);
714 	} finally {
715 		if (pool != null) pool.release();
716 	}
717 }
718 
719 /**
720  * Subtracts the given rectangle from the collection of polygons
721  * the receiver maintains to describe its area.
722  *
723  * @param rect the rectangle to subtract from the receiver
724  *
725  * @exception IllegalArgumentException <ul>
726  *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
727  *    <li>ERROR_INVALID_ARGUMENT - if the rectangle's width or height is negative</li>
728  * </ul>
729  * @exception SWTException <ul>
730  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
731  * </ul>
732  *
733  * @since 3.0
734  */
subtract(Rectangle rect)735 public void subtract(Rectangle rect) {
736 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
737 	if (rect == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
738 	subtract (rect.x, rect.y, rect.width, rect.height);
739 }
740 
741 /**
742  * Subtracts the given rectangle from the collection of polygons
743  * the receiver maintains to describe its area.
744  *
745  * @param x the x coordinate of the rectangle
746  * @param y the y coordinate of the rectangle
747  * @param width the width coordinate of the rectangle
748  * @param height the height coordinate of the rectangle
749  *
750  * @exception IllegalArgumentException <ul>
751  *    <li>ERROR_INVALID_ARGUMENT - if the rectangle's width or height is negative</li>
752  * </ul>
753  * @exception SWTException <ul>
754  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
755  * </ul>
756  *
757  * @since 3.1
758  */
subtract(int x, int y, int width, int height)759 public void subtract(int x, int y, int width, int height) {
760 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
761 	if (width < 0 || height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
762 	NSAutoreleasePool pool = null;
763 	if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
764 	try {
765 		long rectRgn = OS.NewRgn();
766 		short[] r = new short[4];
767 		OS.SetRect(r, (short)x, (short)y, (short)(x + width),(short)(y + height));
768 		OS.RectRgn(rectRgn, r);
769 		OS.DiffRgn(handle, rectRgn, handle);
770 		OS.DisposeRgn(rectRgn);
771 	} finally {
772 		if (pool != null) pool.release();
773 	}
774 }
775 
776 /**
777  * Subtracts all of the polygons which make up the area covered
778  * by the argument from the collection of polygons the receiver
779  * maintains to describe its area.
780  *
781  * @param region the region to subtract
782  *
783  * @exception IllegalArgumentException <ul>
784  *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
785  *    <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
786  * </ul>
787  * @exception SWTException <ul>
788  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
789  * </ul>
790  *
791  * @since 3.0
792  */
subtract(Region region)793 public void subtract(Region region) {
794 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
795 	if (region == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
796 	if (region.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
797 	NSAutoreleasePool pool = null;
798 	if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
799 	try {
800 		OS.DiffRgn(handle, region.handle, handle);
801 	} finally {
802 		if (pool != null) pool.release();
803 	}
804 }
805 
806 /**
807  * Translate all of the polygons the receiver maintains to describe
808  * its area by the specified point.
809  *
810  * @param x the x coordinate of the point to translate
811  * @param y the y coordinate of the point to translate
812  *
813  * @exception SWTException <ul>
814  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
815  * </ul>
816  *
817  * @since 3.1
818  */
translate(int x, int y)819 public void translate (int x, int y) {
820 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
821 	NSAutoreleasePool pool = null;
822 	if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
823 	try {
824 		OS.OffsetRgn (handle, (short)x, (short)y);
825 	} finally {
826 		if (pool != null) pool.release();
827 	}
828 }
829 
830 /**
831  * Translate all of the polygons the receiver maintains to describe
832  * its area by the specified point.
833  *
834  * @param pt the point to translate
835  *
836  * @exception IllegalArgumentException <ul>
837  *    <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
838  * </ul>
839  * @exception SWTException <ul>
840  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
841  * </ul>
842  *
843  * @since 3.1
844  */
translate(Point pt)845 public void translate (Point pt) {
846 	if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
847 	if (pt == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
848 	NSAutoreleasePool pool = null;
849 	if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
850 	try {
851 		translate (pt.x, pt.y);
852 	} finally {
853 		if (pool != null) pool.release();
854 	}
855 }
856 
857 /**
858  * Returns a string containing a concise, human-readable
859  * description of the receiver.
860  *
861  * @return a string representation of the receiver
862  */
863 @Override
toString()864 public String toString () {
865 	if (isDisposed()) return "Region {*DISPOSED*}";
866 	return "Region {" + handle + "}";
867 }
868 }
869