Mercurial > projects > dwt-mac
diff dwt/graphics/Region.d @ 34:5123b17c98ef
Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sun, 14 Sep 2008 01:45:57 +0200 |
parents | a9ab4c738ed8 |
children | db5a898b2119 |
line wrap: on
line diff
--- a/dwt/graphics/Region.d Fri Sep 12 13:53:21 2008 +0200 +++ b/dwt/graphics/Region.d Sun Sep 14 01:45:57 2008 +0200 @@ -7,22 +7,32 @@ * * Contributors: * IBM Corporation - initial API and implementation + * + * Port to the D programming language: + * Jacob Carlborg <jacob.carlborg@gmail.com> *******************************************************************************/ module dwt.graphics.Region; -import dwt.dwthelper.utils; - import dwt.DWT; import dwt.DWTError; import dwt.DWTException; -import dwt.internal.Callback; import dwt.internal.cocoa.NSAffineTransform; import dwt.internal.cocoa.NSBezierPath; import dwt.internal.cocoa.NSPoint; import dwt.internal.cocoa.NSRect; import dwt.internal.cocoa.OS; +import tango.text.convert.Format; + +import dwt.dwthelper.utils; +import dwt.graphics.Device; +import dwt.graphics.Point; +import dwt.graphics.Rectangle; +import dwt.graphics.Resource; +import dwt.internal.c.qd.Quickdraw; +import dwt.internal.c.qd.QuickdrawTypes; + /** * Instances of this class represent areas of an x-y coordinate * system that are aggregates of the areas covered by a number @@ -44,7 +54,7 @@ * platforms and should never be accessed from application code. * </p> */ - public int handle; + public RgnHandle handle; /** * Constructs a new empty region. @@ -79,11 +89,11 @@ public this(Device device) { super(device); handle = OS.NewRgn(); - if (handle is 0) DWT.error(DWT.ERROR_NO_HANDLES); + if (handle is null) DWT.error(DWT.ERROR_NO_HANDLES); init(); } -this(Device device, int handle) { +this(Device device, RgnHandle handle) { super(device); this.handle = handle; } @@ -112,7 +122,7 @@ void add(int[] pointArray, int count) { if (count <= 2) return; - int polyRgn = OS.NewRgn(); + RgnHandle polyRgn = OS.NewRgn(); OS.OpenRgn(); OS.MoveTo(cast(short)pointArray[0], cast(short)pointArray[1]); for (int i = 1; i < count / 2; i++) { @@ -166,10 +176,10 @@ public void add(int x, int y, int width, int height) { if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); if (width < 0 || height < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT); - int rectRgn = OS.NewRgn(); - short[] r = new short[4]; - OS.SetRect(r, cast(short)x, cast(short)y, cast(short)(x + width),cast(short)(y + height)); - OS.RectRgn(rectRgn, r); + RgnHandle rectRgn = OS.NewRgn(); + Rect r; + OS.SetRect(&r, cast(short)x, cast(short)y, cast(short)(x + width),cast(short)(y + height)); + OS.RectRgn(rectRgn, &r); OS.UnionRgn(handle, rectRgn, handle); OS.DisposeRgn(rectRgn); } @@ -211,7 +221,7 @@ */ public bool contains(int x, int y) { if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); - short[] point = new short[]{cast(short)x, cast(short)y}; + Point point = Point(cast(short)x, cast(short)y); return OS.PtInRgn(point, handle); } @@ -237,40 +247,37 @@ NSAffineTransform transform; void convertRgn(NSAffineTransform transform) { - int newRgn = OS.NewRgn(); - Callback callback = new Callback(this, "convertRgn", 4); - int proc = callback.getAddress(); - if (proc is 0) DWT.error(DWT.ERROR_NO_MORE_CALLBACKS); + RgnHandle newRgn = OS.NewRgn(); + RegionToRectsUPP proc = &convertRgn; this.transform = transform; OS.QDRegionToRects(handle, OS.kQDParseRegionFromTopLeft, proc, newRgn); this.transform = null; - callback.dispose(); OS.CopyRgn(newRgn, handle); OS.DisposeRgn(newRgn); } -int convertRgn(int message, int rgn, int r, int newRgn) { +extern(C) private static OSStatus* convertRgn(ushort message, RgnHandle rgn, Rect* r, void* newRgn) { if (message is OS.kQDRegionToRectsMsgParse) { - short[] rect = new short[4]; - OS.memmove(rect, r, rect.length * 2); - NSPoint point = new NSPoint(); - int polyRgn = OS.NewRgn(); + Rect rect; + OS.memmove(&rect, r, rect.sizeof); + NSPoint point = NSPoint(); + RgnHandle polyRgn = OS.NewRgn(); OS.OpenRgn(); - point.x = rect[1]; - point.y = rect[0]; + point.x = rect.left; + point.y = rect.top; point = transform.transformPoint(point); short startX, startY; OS.MoveTo(startX = cast(short)point.x, startY = cast(short)point.y); - point.x = rect[3]; - point.y = rect[0]; + point.x = rect.right; + point.y = rect.top; point = transform.transformPoint(point); OS.LineTo(cast(short)Math.round(point.x), cast(short)point.y); - point.x = rect[3]; - point.y = rect[2]; + point.x = rect.right; + point.y = rect.bottom; point = transform.transformPoint(point); OS.LineTo(cast(short)Math.round(point.x), cast(short)Math.round(point.y)); - point.x = rect[1]; - point.y = rect[2]; + point.x = rect.left; + point.y = rect.bottom; point = transform.transformPoint(point); OS.LineTo(cast(short)point.x, cast(short)Math.round(point.y)); OS.LineTo(startX, startY); @@ -278,7 +285,7 @@ OS.UnionRgn(newRgn, polyRgn, newRgn); OS.DisposeRgn(polyRgn); } - return 0; + return null; } void destroy() { @@ -296,13 +303,15 @@ * * @see #hashCode */ -public bool equals(Object object) { +public int opEquals(Object object) { if (this is object) return true; if (!( null !is cast(Region)object )) return false; Region region = cast(Region)object; return handle is region.handle; } +alias opEquals equals; + /** * Returns a rectangle which represents the rectangular * union of the collection of polygons the receiver @@ -318,45 +327,42 @@ */ public Rectangle getBounds() { if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); - short[] bounds = new short[4]; - OS.GetRegionBounds(handle, bounds); - int width = bounds[3] - bounds[1]; - int height = bounds[2] - bounds[0]; - return new Rectangle(bounds[1], bounds[0], width, height); + Rect bounds; + OS.GetRegionBounds(handle, &bounds); + int width = bounds.right - bounds.left; + int height = bounds.bottom - bounds.left; + return new Rectangle(bounds.left, bounds.top, width, height); } NSBezierPath getPath() { - Callback callback = new Callback(this, "regionToRects", 4); - if (callback.getAddress() is 0) DWT.error(DWT.ERROR_NO_MORE_CALLBACKS); NSBezierPath path = NSBezierPath.bezierPath(); path.retain(); - OS.QDRegionToRects(handle, OS.kQDParseRegionFromTopLeft, callback.getAddress(), path.id); - callback.dispose(); - if (path.isEmpty()) path.appendBezierPathWithRect(new NSRect()); + OS.QDRegionToRects(handle, OS.kQDParseRegionFromTopLeft, ®ionToRects, path.id_); + if (path.isEmpty()) path.appendBezierPathWithRect(NSRect()); return path; } -NSPoint pt = new NSPoint(); -short[] rect = new short[4]; -int regionToRects(int message, int rgn, int r, int path) { +NSPoint pt = NSPoint(); +Rect rect; +extern(C) private static OSStatus* regionToRects(ushort message, RgnHandle rgn, Rect* r, void* path) { if (message is OS.kQDRegionToRectsMsgParse) { - OS.memmove(rect, r, rect.length * 2); - pt.x = rect[1]; - pt.y = rect[0]; + OS.memmove(rect, r, rect.sizeof); + pt.x = rect.left; + pt.y = rect.top; OS.objc_msgSend(path, OS.sel_moveToPoint_1, pt); - pt.x = rect[3]; + pt.x = rect.right; OS.objc_msgSend(path, OS.sel_lineToPoint_1, pt); - pt.x = rect[3]; - pt.y = rect[2]; + pt.x = rect.right; + pt.y = rect.bottom; OS.objc_msgSend(path, OS.sel_lineToPoint_1, pt); - pt.x = rect[1]; + pt.x = rect.left; OS.objc_msgSend(path, OS.sel_lineToPoint_1, pt); OS.objc_msgSend(path, OS.sel_closePath); } - return 0; + return null; } -public static Region carbon_new(Device device, int handle) { +public static Region carbon_new(Device device, RgnHandle handle) { return new Region(device, handle); } @@ -370,10 +376,12 @@ * * @see #equals */ -public int hashCode() { +public hash_t toHash() { return handle; } +alias toHash hashCode; + /** * Intersects the given rectangle to the collection of polygons * the receiver maintains to describe its area. @@ -417,10 +425,10 @@ public void intersect(int x, int y, int width, int height) { if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); if (width < 0 || height < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT); - int rectRgn = OS.NewRgn(); - short[] r = new short[4]; - OS.SetRect(r, cast(short)x, cast(short)y, cast(short)(x + width),cast(short)(y + height)); - OS.RectRgn(rectRgn, r); + RgnHandle rectRgn = OS.NewRgn(); + Rect r; + OS.SetRect(&r, cast(short)x, cast(short)y, cast(short)(x + width),cast(short)(y + height)); + OS.RectRgn(rectRgn, &r); OS.SectRgn(handle, rectRgn, handle); OS.DisposeRgn(rectRgn); } @@ -468,9 +476,9 @@ */ public bool intersects (int x, int y, int width, int height) { if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); - short[] r = new short[4]; - OS.SetRect(r, cast(short)x, cast(short)y, cast(short)(x + width),cast(short)(y + height)); - return OS.RectInRgn(rect, handle); + Rect r; + OS.SetRect(&r, cast(short)x, cast(short)y, cast(short)(x + width),cast(short)(y + height)); + return OS.RectInRgn(&rect, handle); } /** @@ -506,7 +514,7 @@ * @return <code>true</code> when the region is disposed, and <code>false</code> otherwise */ public bool isDisposed() { - return handle is 0; + return handle is null; } /** @@ -544,7 +552,7 @@ if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); if (pointArray is null) DWT.error(DWT.ERROR_NULL_ARGUMENT); if (pointArray.length < 2) return; - int polyRgn = OS.NewRgn(); + RgnHandle polyRgn = OS.NewRgn(); OS.OpenRgn(); OS.MoveTo(cast(short)pointArray[0], cast(short)pointArray[1]); for (int i = 1; i < pointArray.length / 2; i++) { @@ -599,10 +607,10 @@ public void subtract(int x, int y, int width, int height) { if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED); if (width < 0 || height < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT); - int rectRgn = OS.NewRgn(); - short[] r = new short[4]; + RgnHandle rectRgn = OS.NewRgn(); + Rect r; OS.SetRect(r, cast(short)x, cast(short)y, cast(short)(x + width),cast(short)(y + height)); - OS.RectRgn(rectRgn, r); + OS.RectRgn(rectRgn, &r); OS.DiffRgn(handle, rectRgn, handle); OS.DisposeRgn(rectRgn); } @@ -678,6 +686,6 @@ */ public String toString () { if (isDisposed()) return "Region {*DISPOSED*}"; - return "Region {" + handle + "}"; + return Format("Region {{}{}" , handle , "}"); } }