comparison dwt/graphics/Region.d @ 36:db5a898b2119

Fixed a lot of compile errors
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Tue, 07 Oct 2008 12:56:18 +0200
parents 5123b17c98ef
children 642f460a0908
comparison
equal deleted inserted replaced
35:7d135fe0caf2 36:db5a898b2119
28 import dwt.dwthelper.utils; 28 import dwt.dwthelper.utils;
29 import dwt.graphics.Device; 29 import dwt.graphics.Device;
30 import dwt.graphics.Point; 30 import dwt.graphics.Point;
31 import dwt.graphics.Rectangle; 31 import dwt.graphics.Rectangle;
32 import dwt.graphics.Resource; 32 import dwt.graphics.Resource;
33 import dwt.internal.c.carboncore.MacTypes;
33 import dwt.internal.c.qd.Quickdraw; 34 import dwt.internal.c.qd.Quickdraw;
34 import dwt.internal.c.qd.QuickdrawTypes; 35 static import QuickdrawTypes = dwt.internal.c.qd.QuickdrawTypes;
35 36
36 /** 37 /**
37 * Instances of this class represent areas of an x-y coordinate 38 * Instances of this class represent areas of an x-y coordinate
38 * system that are aggregates of the areas covered by a number 39 * system that are aggregates of the areas covered by a number
39 * of polygons. 40 * of polygons.
42 * method to release the operating system resources managed by each instance 43 * method to release the operating system resources managed by each instance
43 * when those instances are no longer required. 44 * when those instances are no longer required.
44 * </p> 45 * </p>
45 */ 46 */
46 public final class Region : Resource { 47 public final class Region : Resource {
48
49 alias Resource.init_ init_;
50
47 /** 51 /**
48 * the OS resource for the region 52 * the OS resource for the region
49 * (Warning: This field is platform dependent) 53 * (Warning: This field is platform dependent)
50 * <p> 54 * <p>
51 * <b>IMPORTANT:</b> This field is <em>not</em> part of the DWT 55 * <b>IMPORTANT:</b> This field is <em>not</em> part of the DWT
52 * public API. It is marked public only so that it can be shared 56 * public API. It is marked public only so that it can be shared
53 * within the packages provided by DWT. It is not available on all 57 * within the packages provided by DWT. It is not available on all
54 * platforms and should never be accessed from application code. 58 * platforms and should never be accessed from application code.
55 * </p> 59 * </p>
56 */ 60 */
57 public RgnHandle handle; 61 public QuickdrawTypes.RgnHandle handle;
58 62
59 /** 63 /**
60 * Constructs a new empty region. 64 * Constructs a new empty region.
61 * 65 *
62 * @exception DWTError <ul> 66 * @exception DWTError <ul>
88 */ 92 */
89 public this(Device device) { 93 public this(Device device) {
90 super(device); 94 super(device);
91 handle = OS.NewRgn(); 95 handle = OS.NewRgn();
92 if (handle is null) DWT.error(DWT.ERROR_NO_HANDLES); 96 if (handle is null) DWT.error(DWT.ERROR_NO_HANDLES);
93 init(); 97 init_();
94 } 98 }
95 99
96 this(Device device, RgnHandle handle) { 100 this(Device device, QuickdrawTypes.RgnHandle handle) {
97 super(device); 101 super(device);
98 this.handle = handle; 102 this.handle = handle;
99 } 103 }
100 104
101 /** 105 /**
120 add(pointArray, pointArray.length); 124 add(pointArray, pointArray.length);
121 } 125 }
122 126
123 void add(int[] pointArray, int count) { 127 void add(int[] pointArray, int count) {
124 if (count <= 2) return; 128 if (count <= 2) return;
125 RgnHandle polyRgn = OS.NewRgn(); 129 QuickdrawTypes.RgnHandle polyRgn = OS.NewRgn();
126 OS.OpenRgn(); 130 OS.OpenRgn();
127 OS.MoveTo(cast(short)pointArray[0], cast(short)pointArray[1]); 131 OS.MoveTo(cast(short)pointArray[0], cast(short)pointArray[1]);
128 for (int i = 1; i < count / 2; i++) { 132 for (int i = 1; i < count / 2; i++) {
129 OS.LineTo(cast(short)pointArray[2 * i], cast(short)pointArray[2 * i + 1]); 133 OS.LineTo(cast(short)pointArray[2 * i], cast(short)pointArray[2 * i + 1]);
130 } 134 }
174 * @since 3.1 178 * @since 3.1
175 */ 179 */
176 public void add(int x, int y, int width, int height) { 180 public void add(int x, int y, int width, int height) {
177 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 181 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
178 if (width < 0 || height < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT); 182 if (width < 0 || height < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
179 RgnHandle rectRgn = OS.NewRgn(); 183 QuickdrawTypes.RgnHandle rectRgn = OS.NewRgn();
180 Rect r; 184 Rect r;
181 OS.SetRect(&r, cast(short)x, cast(short)y, cast(short)(x + width),cast(short)(y + height)); 185 OS.SetRect(&r, cast(short)x, cast(short)y, cast(short)(x + width),cast(short)(y + height));
182 OS.RectRgn(rectRgn, &r); 186 OS.RectRgn(rectRgn, &r);
183 OS.UnionRgn(handle, rectRgn, handle); 187 OS.UnionRgn(handle, rectRgn, handle);
184 OS.DisposeRgn(rectRgn); 188 OS.DisposeRgn(rectRgn);
219 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> 223 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
220 * </ul> 224 * </ul>
221 */ 225 */
222 public bool contains(int x, int y) { 226 public bool contains(int x, int y) {
223 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 227 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
224 Point point = Point(cast(short)x, cast(short)y); 228 QuickdrawTypes.Point point = {cast(short)x, cast(short)y};
225 return OS.PtInRgn(point, handle); 229 return OS.PtInRgn(point, handle);
226 } 230 }
227 231
228 /** 232 /**
229 * Returns <code>true</code> if the given point is inside the 233 * Returns <code>true</code> if the given point is inside the
245 return contains(pt.x, pt.y); 249 return contains(pt.x, pt.y);
246 } 250 }
247 251
248 NSAffineTransform transform; 252 NSAffineTransform transform;
249 void convertRgn(NSAffineTransform transform) { 253 void convertRgn(NSAffineTransform transform) {
250 RgnHandle newRgn = OS.NewRgn(); 254 QuickdrawTypes.RgnHandle newRgn = OS.NewRgn();
251 RegionToRectsUPP proc = &convertRgn; 255 RegionToRectsUPP proc = &Region.convertRgn_;
252 this.transform = transform; 256 this.transform = transform;
253 OS.QDRegionToRects(handle, OS.kQDParseRegionFromTopLeft, proc, newRgn); 257 OS.QDRegionToRects(handle, OS.kQDParseRegionFromTopLeft, proc, newRgn);
254 this.transform = null; 258 this.transform = null;
255 OS.CopyRgn(newRgn, handle); 259 OS.CopyRgn(newRgn, handle);
256 OS.DisposeRgn(newRgn); 260 OS.DisposeRgn(newRgn);
257 } 261 }
258 262
259 extern(C) private static OSStatus* convertRgn(ushort message, RgnHandle rgn, Rect* r, void* newRgn) { 263 extern(C) private static OSStatus* convertRgn_(ushort message, QuickdrawTypes.RgnHandle rgn, Rect* r, void* newRgn) {
260 if (message is OS.kQDRegionToRectsMsgParse) { 264 if (message is OS.kQDRegionToRectsMsgParse) {
261 Rect rect; 265 Rect rect;
262 OS.memmove(&rect, r, rect.sizeof); 266 OS.memmove(&rect, r, rect.sizeof);
263 NSPoint point = NSPoint(); 267 NSPoint point = NSPoint();
264 RgnHandle polyRgn = OS.NewRgn(); 268 QuickdrawTypes.RgnHandle polyRgn = OS.NewRgn();
265 OS.OpenRgn(); 269 OS.OpenRgn();
266 point.x = rect.left; 270 point.x = rect.left;
267 point.y = rect.top; 271 point.y = rect.top;
268 point = transform.transformPoint(point); 272 point = transform.transformPoint(point);
269 short startX, startY; 273 short startX, startY;
280 point.y = rect.bottom; 284 point.y = rect.bottom;
281 point = transform.transformPoint(point); 285 point = transform.transformPoint(point);
282 OS.LineTo(cast(short)point.x, cast(short)Math.round(point.y)); 286 OS.LineTo(cast(short)point.x, cast(short)Math.round(point.y));
283 OS.LineTo(startX, startY); 287 OS.LineTo(startX, startY);
284 OS.CloseRgn(polyRgn); 288 OS.CloseRgn(polyRgn);
285 OS.UnionRgn(newRgn, polyRgn, newRgn); 289 OS.UnionRgn(cast(QuickdrawTypes.RgnHandle) newRgn, polyRgn, cast(QuickdrawTypes.RgnHandle) newRgn);
286 OS.DisposeRgn(polyRgn); 290 OS.DisposeRgn(polyRgn);
287 } 291 }
288 return null; 292 return null;
289 } 293 }
290 294
291 void destroy() { 295 void destroy() {
292 OS.DisposeRgn(handle); 296 OS.DisposeRgn(handle);
293 handle = 0; 297 handle = null;
294 } 298 }
295 299
296 /** 300 /**
297 * Compares the argument to the receiver, and returns true 301 * Compares the argument to the receiver, and returns true
298 * if they represent the <em>same</em> object using a class 302 * if they represent the <em>same</em> object using a class
335 } 339 }
336 340
337 NSBezierPath getPath() { 341 NSBezierPath getPath() {
338 NSBezierPath path = NSBezierPath.bezierPath(); 342 NSBezierPath path = NSBezierPath.bezierPath();
339 path.retain(); 343 path.retain();
340 OS.QDRegionToRects(handle, OS.kQDParseRegionFromTopLeft, &regionToRects, path.id_); 344 OS.QDRegionToRects(handle, OS.kQDParseRegionFromTopLeft, &Region.regionToRects, path.id_);
341 if (path.isEmpty()) path.appendBezierPathWithRect(NSRect()); 345 if (path.isEmpty()) path.appendBezierPathWithRect(NSRect());
342 return path; 346 return path;
343 } 347 }
344 348
345 NSPoint pt = NSPoint(); 349 static NSPoint pt = NSPoint();
346 Rect rect; 350 static Rect rect;
347 extern(C) private static OSStatus* regionToRects(ushort message, RgnHandle rgn, Rect* r, void* path) { 351 extern(C) private static OSStatus* regionToRects(ushort message, QuickdrawTypes.RgnHandle rgn, Rect* r, void* path) {
348 if (message is OS.kQDRegionToRectsMsgParse) { 352 if (message is OS.kQDRegionToRectsMsgParse) {
349 OS.memmove(rect, r, rect.sizeof); 353 OS.memmove(&rect, r, rect.sizeof);
350 pt.x = rect.left; 354 pt.x = rect.left;
351 pt.y = rect.top; 355 pt.y = rect.top;
352 OS.objc_msgSend(path, OS.sel_moveToPoint_1, pt); 356 OS.objc_msgSend(cast(objc.id) path, OS.sel_moveToPoint_1, pt);
353 pt.x = rect.right; 357 pt.x = rect.right;
354 OS.objc_msgSend(path, OS.sel_lineToPoint_1, pt); 358 OS.objc_msgSend(cast(objc.id) path, OS.sel_lineToPoint_1, pt);
355 pt.x = rect.right; 359 pt.x = rect.right;
356 pt.y = rect.bottom; 360 pt.y = rect.bottom;
357 OS.objc_msgSend(path, OS.sel_lineToPoint_1, pt); 361 OS.objc_msgSend(cast(objc.id) path, OS.sel_lineToPoint_1, pt);
358 pt.x = rect.left; 362 pt.x = rect.left;
359 OS.objc_msgSend(path, OS.sel_lineToPoint_1, pt); 363 OS.objc_msgSend(cast(objc.id) path, OS.sel_lineToPoint_1, pt);
360 OS.objc_msgSend(path, OS.sel_closePath); 364 OS.objc_msgSend(cast(objc.id) path, OS.sel_closePath);
361 } 365 }
362 return null; 366 return null;
363 } 367 }
364 368
365 public static Region carbon_new(Device device, RgnHandle handle) { 369 public static Region carbon_new(Device device, QuickdrawTypes.RgnHandle handle) {
366 return new Region(device, handle); 370 return new Region(device, handle);
367 } 371 }
368 372
369 /** 373 /**
370 * Returns an integer hash code for the receiver. Any two 374 * Returns an integer hash code for the receiver. Any two
375 * @return the receiver's hash 379 * @return the receiver's hash
376 * 380 *
377 * @see #equals 381 * @see #equals
378 */ 382 */
379 public hash_t toHash() { 383 public hash_t toHash() {
380 return handle; 384 return cast(hash_t) handle;
381 } 385 }
382 386
383 alias toHash hashCode; 387 alias toHash hashCode;
384 388
385 /** 389 /**
423 * @since 3.1 427 * @since 3.1
424 */ 428 */
425 public void intersect(int x, int y, int width, int height) { 429 public void intersect(int x, int y, int width, int height) {
426 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 430 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
427 if (width < 0 || height < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT); 431 if (width < 0 || height < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
428 RgnHandle rectRgn = OS.NewRgn(); 432 QuickdrawTypes.RgnHandle rectRgn = OS.NewRgn();
429 Rect r; 433 Rect r;
430 OS.SetRect(&r, cast(short)x, cast(short)y, cast(short)(x + width),cast(short)(y + height)); 434 OS.SetRect(&r, cast(short)x, cast(short)y, cast(short)(x + width),cast(short)(y + height));
431 OS.RectRgn(rectRgn, &r); 435 OS.RectRgn(rectRgn, &r);
432 OS.SectRgn(handle, rectRgn, handle); 436 OS.SectRgn(handle, rectRgn, handle);
433 OS.DisposeRgn(rectRgn); 437 OS.DisposeRgn(rectRgn);
550 */ 554 */
551 public void subtract (int[] pointArray) { 555 public void subtract (int[] pointArray) {
552 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 556 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
553 if (pointArray is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); 557 if (pointArray is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
554 if (pointArray.length < 2) return; 558 if (pointArray.length < 2) return;
555 RgnHandle polyRgn = OS.NewRgn(); 559 QuickdrawTypes.RgnHandle polyRgn = OS.NewRgn();
556 OS.OpenRgn(); 560 OS.OpenRgn();
557 OS.MoveTo(cast(short)pointArray[0], cast(short)pointArray[1]); 561 OS.MoveTo(cast(short)pointArray[0], cast(short)pointArray[1]);
558 for (int i = 1; i < pointArray.length / 2; i++) { 562 for (int i = 1; i < pointArray.length / 2; i++) {
559 OS.LineTo(cast(short)pointArray[2 * i], cast(short)pointArray[2 * i + 1]); 563 OS.LineTo(cast(short)pointArray[2 * i], cast(short)pointArray[2 * i + 1]);
560 } 564 }
605 * @since 3.1 609 * @since 3.1
606 */ 610 */
607 public void subtract(int x, int y, int width, int height) { 611 public void subtract(int x, int y, int width, int height) {
608 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); 612 if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
609 if (width < 0 || height < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT); 613 if (width < 0 || height < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
610 RgnHandle rectRgn = OS.NewRgn(); 614 QuickdrawTypes.RgnHandle rectRgn = OS.NewRgn();
611 Rect r; 615 Rect r;
612 OS.SetRect(r, cast(short)x, cast(short)y, cast(short)(x + width),cast(short)(y + height)); 616 OS.SetRect(&r, cast(short)x, cast(short)y, cast(short)(x + width),cast(short)(y + height));
613 OS.RectRgn(rectRgn, &r); 617 OS.RectRgn(rectRgn, &r);
614 OS.DiffRgn(handle, rectRgn, handle); 618 OS.DiffRgn(handle, rectRgn, handle);
615 OS.DisposeRgn(rectRgn); 619 OS.DisposeRgn(rectRgn);
616 } 620 }
617 621