1 11 package org.eclipse.swt.graphics; 12 13 14 import org.eclipse.swt.internal.carbon.*; 15 import org.eclipse.swt.*; 16 17 27 public final class Region extends Resource { 28 38 public int handle; 39 40 47 public Region() { 48 this(null); 49 } 50 51 70 public Region(Device device) { 71 if (device == null) device = Device.getDevice(); 72 if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 73 this.device = device; 74 handle = OS.NewRgn(); 75 if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES); 76 } 77 78 Region(Device device, int handle) { 79 this.device = device; 80 this.handle = handle; 81 } 82 83 99 public void add (int[] pointArray) { 100 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 101 if (pointArray == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 102 if (pointArray.length < 2) return; 103 int polyRgn = OS.NewRgn(); 104 OS.OpenRgn(); 105 OS.MoveTo((short)pointArray[0], (short)pointArray[1]); 106 for (int i = 1; i < pointArray.length / 2; i++) { 107 OS.LineTo((short)pointArray[2 * i], (short)pointArray[2 * i + 1]); 108 } 109 OS.LineTo((short)pointArray[0], (short)pointArray[1]); 110 OS.CloseRgn(polyRgn); 111 OS.UnionRgn(handle, polyRgn, handle); 112 OS.DisposeRgn(polyRgn); 113 } 114 115 129 public void add(Rectangle rect) { 130 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 131 if (rect == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 132 if (rect.width < 0 || rect.height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 133 add (rect.x, rect.y, rect.width, rect.height); 134 } 135 136 154 public void add(int x, int y, int width, int height) { 155 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 156 if (width < 0 || height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 157 int rectRgn = OS.NewRgn(); 158 Rect r = new Rect(); 159 OS.SetRect(r, (short)x, (short)y, (short)(x + width),(short)(y + height)); 160 OS.RectRgn(rectRgn, r); 161 OS.UnionRgn(handle, rectRgn, handle); 162 OS.DisposeRgn(rectRgn); 163 } 164 165 180 public void add(Region region) { 181 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 182 if (region == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 183 if (region.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 184 OS.UnionRgn(handle, region.handle, handle); 185 } 186 187 200 public boolean contains(int x, int y) { 201 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 202 org.eclipse.swt.internal.carbon.Point point = new org.eclipse.swt.internal.carbon.Point(); 203 OS.SetPt(point, (short)x, (short)y); 204 return OS.PtInRgn(point, handle); 205 } 206 207 222 public boolean contains(Point pt) { 223 if (pt == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 224 return contains(pt.x, pt.y); 225 } 226 231 public void dispose() { 232 if (handle == 0) return; 233 OS.DisposeRgn(handle); 234 handle = 0; 235 device = null; 236 } 237 238 248 public boolean equals(Object object) { 249 if (this == object) return true; 250 if (!(object instanceof Region)) return false; 251 Region region = (Region)object; 252 return handle == region.handle; 253 } 254 255 268 public Rectangle getBounds() { 269 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 270 Rect bounds = new Rect(); 271 OS.GetRegionBounds(handle, bounds); 272 int width = bounds.right - bounds.left; 273 int height = bounds.bottom - bounds.top; 274 return new Rectangle(bounds.left, bounds.top, width, height); 275 } 276 277 public static Region carbon_new(Device device, int handle) { 278 return new Region(device, handle); 279 } 280 281 291 public int hashCode() { 292 return handle; 293 } 294 295 311 public void intersect(Rectangle rect) { 312 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 313 if (rect == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 314 intersect (rect.x, rect.y, rect.width, rect.height); 315 } 316 317 335 public void intersect(int x, int y, int width, int height) { 336 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 337 if (width < 0 || height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 338 int rectRgn = OS.NewRgn(); 339 Rect r = new Rect(); 340 OS.SetRect(r, (short)x, (short)y, (short)(x + width),(short)(y + height)); 341 OS.RectRgn(rectRgn, r); 342 OS.SectRgn(handle, rectRgn, handle); 343 OS.DisposeRgn(rectRgn); 344 } 345 346 363 public void intersect(Region region) { 364 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 365 if (region == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 366 if (region.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 367 OS.SectRgn(handle, region.handle, handle); 368 } 369 370 387 public boolean intersects (int x, int y, int width, int height) { 388 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 389 Rect rect = new Rect(); 390 OS.SetRect(rect, (short)x, (short)y, (short)(x + width),(short)(y + height)); 391 return OS.RectInRgn(rect, handle); 392 } 393 394 411 public boolean intersects(Rectangle rect) { 412 if (rect == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 413 return intersects(rect.x, rect.y, rect.width, rect.height); 414 } 415 416 426 public boolean isDisposed() { 427 return handle == 0; 428 } 429 430 441 public boolean isEmpty() { 442 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 443 return OS.EmptyRgn(handle); 444 } 445 446 461 public void subtract (int[] pointArray) { 462 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 463 if (pointArray == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 464 if (pointArray.length < 2) return; 465 int polyRgn = OS.NewRgn(); 466 OS.OpenRgn(); 467 OS.MoveTo((short)pointArray[0], (short)pointArray[1]); 468 for (int i = 1; i < pointArray.length / 2; i++) { 469 OS.LineTo((short)pointArray[2 * i], (short)pointArray[2 * i + 1]); 470 } 471 OS.LineTo((short)pointArray[0], (short)pointArray[1]); 472 OS.CloseRgn(polyRgn); 473 OS.DiffRgn(handle, polyRgn, handle); 474 OS.DisposeRgn(polyRgn); 475 } 476 477 493 public void subtract(Rectangle rect) { 494 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 495 if (rect == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 496 subtract (rect.x, rect.y, rect.width, rect.height); 497 } 498 499 517 public void subtract(int x, int y, int width, int height) { 518 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 519 if (width < 0 || height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 520 int rectRgn = OS.NewRgn(); 521 Rect r = new Rect(); 522 OS.SetRect(r, (short)x, (short)y, (short)(x + width),(short)(y + height)); 523 OS.RectRgn(rectRgn, r); 524 OS.DiffRgn(handle, rectRgn, handle); 525 OS.DisposeRgn(rectRgn); 526 } 527 528 545 public void subtract(Region region) { 546 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 547 if (region == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 548 if (region.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 549 OS.DiffRgn(handle, region.handle, handle); 550 } 551 552 565 public void translate (int x, int y) { 566 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 567 OS.OffsetRgn (handle, (short)x, (short)y); 568 } 569 570 585 public void translate (Point pt) { 586 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); 587 if (pt == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 588 translate (pt.x, pt.y); 589 } 590 591 597 public String toString () { 598 if (isDisposed()) return "Region {*DISPOSED*}"; 599 return "Region {" + handle + "}"; 600 } 601 } 602 | Popular Tags |