diff dwt/graphics/Path.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 e831403a80a9
children db5a898b2119
line wrap: on
line diff
--- a/dwt/graphics/Path.d	Fri Sep 12 13:53:21 2008 +0200
+++ b/dwt/graphics/Path.d	Sun Sep 14 01:45:57 2008 +0200
@@ -7,10 +7,12 @@
  *
  * Contributors:
  *     IBM Corporation - initial API and implementation
+ *     
+ * Port to the D programming language:
+ *     Jacob Carlborg <jacob.carlborg@gmail.com>
  *******************************************************************************/
 module dwt.graphics.Path;
 
-import dwt.dwthelper.utils;
 
 import dwt.DWT;
 import dwt.DWTError;
@@ -27,6 +29,18 @@
 import dwt.internal.cocoa.NSTextStorage;
 import dwt.internal.cocoa.OS;
 
+import tango.text.convert.Format;
+
+import dwt.dwthelper.utils;
+import dwt.graphics.Device;
+import dwt.graphics.Font;
+import dwt.graphics.GC;
+import dwt.graphics.GCData;
+import dwt.graphics.PathData;
+import dwt.graphics.Resource;
+import dwt.internal.cocoa.CGFloat;
+import dwt.internal.cocoa.NSFont : NSGlyph;
+
 /**
  * Instances of this class represent paths through the two-dimensional
  * coordinate system. Paths do not have to be continuous, and can be
@@ -85,7 +99,7 @@
     handle = NSBezierPath.bezierPath();
     if (handle is null) DWT.error(DWT.ERROR_NO_HANDLES);
     handle.retain();
-    handle.moveToPoint(new NSPoint());
+    handle.moveToPoint(NSPoint());
     init();
 }
 
@@ -95,9 +109,9 @@
     if (path.isDisposed()) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
     flatness = Math.max(0, flatness);
     if (flatness is 0) {
-        handle = new NSBezierPath(path.handle.copy().id);
+        handle = new NSBezierPath(path.handle.copy().id_);
     } else {
-        float defaultFlatness = NSBezierPath.defaultFlatness();
+        CGFloat defaultFlatness = NSBezierPath.defaultFlatness();
         NSBezierPath.setDefaultFlatness(flatness);
         handle = path.handle.bezierPathByFlatteningPath();
         NSBezierPath.setDefaultFlatness(defaultFlatness);       
@@ -147,9 +161,9 @@
     transform.translateXBy(x + width / 2f, y + height / 2f);
     transform.scaleXBy(width / 2f, height / 2f);
     NSBezierPath path = NSBezierPath.bezierPath();
-    NSPoint center = new NSPoint();
-    float sAngle = -startAngle;
-    float eAngle = -(startAngle + arcAngle);
+    NSPoint center = NSPoint();
+    CGFloat sAngle = -startAngle;
+    CGFloat eAngle = -(startAngle + arcAngle);
     path.appendBezierPathWithArcWithCenter_radius_startAngle_endAngle_clockwise_(center, 1, sAngle,  eAngle, arcAngle>0);
     path.transformUsingAffineTransform(transform);
     handle.appendBezierPath(path);
@@ -189,7 +203,7 @@
  */
 public void addRectangle(float x, float y, float width, float height) {
     if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
-    NSRect rect = new NSRect();
+    NSRect rect = NSRect();
     rect.x = x;
     rect.y = y;
     rect.width = width;
@@ -201,7 +215,7 @@
  * Adds to the receiver the pattern of glyphs generated by drawing
  * the given string using the given font starting at the point (x, y).
  *
- * @param string the text to use
+ * @param stri the text to use
  * @param x the x coordinate of the starting point
  * @param y the y coordinate of the starting point
  * @param font the font to use
@@ -214,32 +228,32 @@
  *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
  * </ul>
  */
-public void addString(String string, float x, float y, Font font) {
+public void addString(String stri, float x, float y, Font font) {
     if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
     if (font is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
     if (font.isDisposed()) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
-    NSString str = NSString.stringWith(string);
-    NSTextStorage textStorage = (cast(NSTextStorage)new NSTextStorage().alloc());
+    NSString str = NSString.stringWith(stri);
+    NSTextStorage textStorage = (cast(NSTextStorage)(new NSTextStorage()).alloc());
     textStorage.initWithString_(str);
-    NSLayoutManager layoutManager = cast(NSLayoutManager)new NSLayoutManager().alloc().init();
-    NSTextContainer textContainer = cast(NSTextContainer)new NSTextContainer().alloc();
-    NSSize size = new NSSize();
-    size.width = Float.MAX_VALUE;
-    size.height = Float.MAX_VALUE;
+    NSLayoutManager layoutManager = cast(NSLayoutManager)(new NSLayoutManager()).alloc().init();
+    NSTextContainer textContainer = cast(NSTextContainer)(new NSTextContainer()).alloc();
+    NSSize size = NSSize();
+    size.width = CGFloat.max; //Float.MAX_VALUE;
+    size.height = CGFloat.max; //Float.MAX_VALUE;
     textContainer.initWithContainerSize(size);
     textStorage.addLayoutManager(layoutManager);
     layoutManager.addTextContainer(textContainer);
-    NSRange range = new NSRange();
+    NSRange range = NSRange();
     range.length = str.length();
     textStorage.beginEditing();
     textStorage.addAttribute(OS.NSFontAttributeName(), font.handle, range);
     textStorage.endEditing();
     range = layoutManager.glyphRangeForTextContainer(textContainer);
     if (range.length !is 0) {
-        int glyphs = OS.malloc(4 * range.length * 2);
+        NSGlyph* glyphs = OS.malloc(4 * range.length * 2);
         layoutManager.getGlyphs(glyphs, range);
         NSBezierPath path = NSBezierPath.bezierPath();
-        NSPoint point = new NSPoint();
+        NSPoint point = NSPoint();
         point.x = x;
         point.y = y;
         path.moveToPoint(point);
@@ -299,7 +313,7 @@
     if (gc.isDisposed()) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
 //  gc.checkGC(GC.LINE_CAP | GC.LINE_JOIN | GC.LINE_STYLE | GC.LINE_WIDTH);
     //TODO outline
-    NSPoint point = new NSPoint();
+    NSPoint point = NSPoint();
     point.x = x;
     point.y = y;
     return handle.containsPoint(point);
@@ -321,13 +335,13 @@
  */
 public void cubicTo(float cx1, float cy1, float cx2, float cy2, float x, float y) {
     if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
-    NSPoint pt = new NSPoint();
+    NSPoint pt = NSPoint();
     pt.x = x;
     pt.y = y;
-    NSPoint ct1 = new NSPoint();
+    NSPoint ct1 = NSPoint();
     ct1.x = cx1;
     ct1.y = cy1;
-    NSPoint ct2 = new NSPoint();
+    NSPoint ct2 = NSPoint();
     ct2.x = cx2;
     ct2.y = cy2;
     handle.curveToPoint(pt, ct1, ct2);
@@ -404,11 +418,11 @@
     int pointCount = 0, typeCount = 0;
     byte[] types = new byte[count];
     float[] pointArray = new float[count * 6];
-    int points = OS.malloc(3 * NSPoint.sizeof);
+    NSPointArray points = OS.malloc(3 * NSPoint.sizeof);
     if (points is 0) DWT.error(DWT.ERROR_NO_HANDLES);
-    NSPoint pt = new NSPoint();
-    for (int i = 0; i < count; i++) {
-        int element = handle.elementAtIndex_associatedPoints_(i, points);
+    NSPoint pt = NSPoint();
+    for (NSInteger i = 0; i < count; i++) {
+        NSBezierPathElement  element = handle.elementAtIndex_associatedPoints_(i, points);
         switch (element) {
             case OS.NSMoveToBezierPathElement:
                 types[typeCount++] = DWT.PATH_MOVE_TO;
@@ -505,7 +519,7 @@
  */
 public void lineTo(float x, float y) {
     if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
-    NSPoint pt = new NSPoint();
+    NSPoint pt = NSPoint();
     pt.x = x;
     pt.y = y;
     handle.lineToPoint(pt);
@@ -525,7 +539,7 @@
  */
 public void moveTo(float x, float y) {
     if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
-    NSPoint pt = new NSPoint();
+    NSPoint pt = NSPoint();
     pt.x = x;
     pt.y = y;
     handle.moveToPoint(pt);
@@ -546,10 +560,10 @@
  */
 public void quadTo(float cx, float cy, float x, float y) {
     if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
-    NSPoint pt = new NSPoint();
+    NSPoint pt = NSPoint();
     pt.x = x;
     pt.y = y;
-    NSPoint ct = new NSPoint();
+    NSPoint ct = NSPoint();
     ct.x = cx;
     ct.y = cy;
     handle.curveToPoint(pt, ct, ct);
@@ -563,7 +577,7 @@
  */
 public String toString () {
     if (isDisposed()) return "Path {*DISPOSED*}";
-    return "Path {" + handle + "}";
+    return Format("Path {{}{}" , handle , "}");
 }
 
 }