diff dwt/graphics/Transform.d @ 45:d8635bb48c7c

Merge with SWT 3.5
author Jacob Carlborg <doob@me.com>
date Mon, 01 Dec 2008 17:07:00 +0100
parents db5a898b2119
children cfa563df4fdd
line wrap: on
line diff
--- a/dwt/graphics/Transform.d	Tue Oct 21 15:20:04 2008 +0200
+++ b/dwt/graphics/Transform.d	Mon Dec 01 17:07:00 2008 +0100
@@ -1,5 +1,5 @@
-/*******************************************************************************
- * Copyright (c) 2000, 2007 IBM Corporation and others.
+/*******************************************************************************
+ * Copyright (c) 2000, 2008 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -9,7 +9,7 @@
  *     IBM Corporation - initial API and implementation
  *     
  * Port to the D programming language:
- *     Jacob Carlborg <jacob.carlborg@gmail.com>
+ *     Jacob Carlborg <doob@me.com>
  *******************************************************************************/
 module dwt.graphics.Transform;
 
@@ -18,7 +18,9 @@
 import dwt.DWTException;
 import dwt.internal.cocoa.NSAffineTransform;
 import dwt.internal.cocoa.NSAffineTransformStruct;
+import dwt.internal.cocoa.NSAutoreleasePool;
 import dwt.internal.cocoa.NSPoint;
+import dwt.internal.cocoa.NSThread;
 
 import tango.text.convert.Format;
 
@@ -39,6 +41,9 @@
  * which may not be available on some platforms.
  * </p>
  * 
+ * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: GraphicsExample</a>
+ * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
+ * 
  * @since 3.1
  */
 public class Transform : Resource {
@@ -143,11 +148,17 @@
  */
 public this (Device device, float m11, float m12, float m21, float m22, float dx, float dy) {
     super(device);
-    handle = NSAffineTransform.transform();
-    if (handle is null) DWT.error(DWT.ERROR_NO_HANDLES);
-    handle.retain();
-    setElements(m11, m12, m21, m22, dx, dy);
+    NSAutoreleasePool pool = null;
+    if (!NSThread.isMainThread()) pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init();
+    try {
+        handle = NSAffineTransform.transform();
+        if (handle is null) DWT.error(DWT.ERROR_NO_HANDLES);
+        handle.retain();
+        setElements(m11, m12, m21, m22, dx, dy);
     init_();
+    } finally {
+        if (pool !is null) pool.release();
+    }
 }
 
 static float[] checkTransform(float[] elements) {
@@ -179,25 +190,46 @@
     if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
     if (elements is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
     if (elements.length < 6) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
-    NSAffineTransformStruct structt = handle.transformStruct();
-    elements[0] = structt.m11;
-    elements[1] = structt.m12;
-    elements[2] = structt.m21;
-    elements[3] = structt.m22;
-    elements[4] = structt.tX;
-    elements[5] = structt.tY;
-}
-
-public void identity() {
-    if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
-    NSAffineTransformStruct structt = NSAffineTransformStruct();
-    structt.m11 = 1;
-    structt.m22 = 1;
-    handle.setTransformStruct(structt);
+    NSAutoreleasePool pool = null;
+    if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
+    try {
+        NSAffineTransformStruct struct_ = handle.transformStruct();
+        elements[0] = (float)/*64*/struct_.m11;
+        elements[1] = (float)/*64*/struct_.m12;
+        elements[2] = (float)/*64*/struct_.m21;
+        elements[3] = (float)/*64*/struct_.m22;
+        elements[4] = (float)/*64*/struct_.tX;
+        elements[5] = (float)/*64*/struct_.tY;
+    } finally {
+        if (pool !is null) pool.release();
+    }
 }
 
 /**
  * Modifies the receiver such that the matrix it represents becomes the
+ * identity matrix. 
+ *
+ * @exception DWTException <ul>
+ *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
+ * </ul>
+ * 
+ * @since 3.4
+ */
+public void identity() {
+    if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
+    NSAutoreleasePool pool = null;
+    try {
+        NSAffineTransformStruct struct_ = NSAffineTransformStruct();
+        struct_.m11 = 1;
+        struct_.m22 = 1;
+        handle.setTransformStruct(structt);
+    } finally {
+        if (pool !is null) pool.release();
+    }
+}
+
+/**
+ * Modifies the receiver such that the matrix it represents becomes
  * the mathematical inverse of the matrix it previously represented. 
  *
  * @exception DWTException <ul>
@@ -207,11 +239,17 @@
  */
 public void invert() {
     if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
-    NSAffineTransformStruct structt = handle.transformStruct();
-    if ((structt.m11 * structt.m22 - structt.m12 * structt.m21) is 0) {
-        DWT.error(DWT.ERROR_CANNOT_INVERT_MATRIX);
+    NSAutoreleasePool pool = null;
+    if (!NSThread.isMainThread()) pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init();
+    try {
+        NSAffineTransformStruct struct_ = handle.transformStruct();
+        if ((struct_.m11 * struct_.m22 - struct_.m12 * struct_.m21) is 0) {
+            DWT.error(DWT.ERROR_CANNOT_INVERT_MATRIX);
+        }
+        handle.invert();
+    } finally {
+        if (pool !is null) pool.release();
     }
-    handle.invert();
 }
 
 /**
@@ -236,8 +274,14 @@
  */
 public bool isIdentity() {
     if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
-    NSAffineTransformStruct structt = handle.transformStruct();
-    return structt.m11 is 1 && structt.m12 is 0 && structt.m21 is 0 && structt.m22 is 1 && structt.tX is 0 && structt.tY is 0;
+    NSAutoreleasePool pool = null;
+    if (!NSThread.isMainThread()) pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init();
+    try {
+        NSAffineTransformStruct struct = handle.transformStruct();
+        return struct.m11 is 1 && struct.m12 is 0 && struct.m21 is 0 && struct.m22 is 1 && struct.tX is 0 && struct.tY is 0;
+    } finally {
+        if (pool !is null) pool.release();
+    }
 }
 
 /**
@@ -259,7 +303,13 @@
     if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
     if (matrix is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
     if (matrix.isDisposed()) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
-    handle.prependTransform(matrix.handle);
+    NSAutoreleasePool pool = null;
+    if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
+    try {
+        handle.prependTransform(matrix.handle);
+    } finally {
+        if (pool !is null) pool.release();
+    }
 }
 
 /**
@@ -277,7 +327,13 @@
  */
 public void rotate(float angle) {
     if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
-    handle.rotateByDegrees(angle);
+    NSAutoreleasePool pool = null;
+    if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
+    try {
+        handle.rotateByDegrees(angle);
+    } finally {
+        if (pool !is null) pool.release();
+    }
 }
 
 /**
@@ -293,7 +349,13 @@
  */
 public void scale(float scaleX, float scaleY) {
     if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
-    handle.scaleXBy(scaleX, scaleY);
+    NSAutoreleasePool pool = null;
+    if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
+    try {
+        handle.scaleXBy(scaleX, scaleY);
+    } finally {
+        if (pool !is null) pool.release();
+    }
 }
 
 /**
@@ -313,26 +375,51 @@
  */
 public void setElements(float m11, float m12, float m21, float m22, float dx, float dy) {
     if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
-    NSAffineTransformStruct structt = NSAffineTransformStruct();
-    structt.m11 = m11;
-    structt.m12 = m12;
-    structt.m21 = m21;
-    structt.m22 = m22;
-    structt.tX = dx;
-    structt.tY = dy;
-    handle.setTransformStruct(structt);
+    NSAutoreleasePool pool = null;
+    if (!NSThread.isMainThread()) pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init();
+    try {
+        NSAffineTransformStruct struct_ = NSAffineTransformStruct();
+        struct_.m11 = m11;
+        struct_.m12 = m12;
+        struct_.m21 = m21;
+        struct_.m22 = m22;
+        struct_.tX = dx;
+        struct_.tY = dy;
+        handle.setTransformStruct(struct_);
+    } finally {
+        if (pool !is null) pool.release();
+    }
 }
 
+/**
+ * Modifies the receiver so that it represents a transformation that is
+ * equivalent to its previous transformation sheared by (shearX, shearY).
+ * 
+ * @param shearX the shear factor in the X direction
+ * @param shearY the shear factor in the Y direction
+ * 
+ * @exception DWTException <ul>
+ *    <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
+ * </ul>
+ * 
+ * @since 3.4
+ */
 public void shear(float shearX, float shearY) {
     if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
-    NSAffineTransformStruct structt = NSAffineTransformStruct();
-    structt.m11 = 1;
-    structt.m12 = shearX;
-    structt.m21 = shearY;
-    structt.m22 = 1;
-    NSAffineTransform matrix = NSAffineTransform.transform();
-    matrix.setTransformStruct(structt);
-    handle.prependTransform(matrix);
+    NSAutoreleasePool pool = null;
+    if (!NSThread.isMainThread()) pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init();
+    try {
+        NSAffineTransformStruct struct_ =  NSAffineTransformStruct();
+        struct_.m11 = 1;
+        struct_.m12 = shearX;
+        struct_.m21 = shearY;
+        struct_.m22 = 1;
+        NSAffineTransform matrix = NSAffineTransform.transform();
+        matrix.setTransformStruct(struct_);
+        handle.prependTransform(matrix);
+    } finally {
+        if (pool !is null) pool.release();
+    }
 }
 
 /** 
@@ -352,14 +439,20 @@
 public void transform(float[] pointArray) {
     if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
     if (pointArray is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
-    NSPoint point = NSPoint();
-    size_t length = pointArray.length / 2;
-    for (int i = 0, j = 0; i < length; i++, j += 2) {
-        point.x = pointArray[j];
-        point.y = pointArray[j + 1];
-        point = handle.transformPoint(point);
-        pointArray[j] = point.x;                
-        pointArray[j + 1] = point.y;                
+    NSAutoreleasePool pool = null;
+    if (!NSThread.isMainThread()) pool = cast(NSAutoreleasePool) (new NSAutoreleasePool()).alloc().init();
+    try {
+        NSPoint point = NSPoint();
+        size_t length = pointArray.length / 2;
+        for (size_t i = 0, j = 0; i < length; i++, j += 2) {
+            point.x = pointArray[j];
+            point.y = pointArray[j + 1];
+            point = handle.transformPoint(point);
+            pointArray[j] = (float)/*64*/point.x;               
+            pointArray[j + 1] = (float)/*64*/point.y;               
+        }
+    } finally {
+        if (pool !is null) pool.release();
     }
 }
 
@@ -376,7 +469,13 @@
  */
 public void translate(float offsetX, float offsetY) {
     if (isDisposed()) DWT.error(DWT.ERROR_GRAPHIC_DISPOSED);
-    handle.translateXBy(offsetX, offsetY);
+    NSAutoreleasePool pool = null;
+    if (!NSThread.isMainThread()) pool = (NSAutoreleasePool) new NSAutoreleasePool().alloc().init();
+    try {
+        handle.translateXBy(offsetX, offsetY);
+    } finally {
+        if (pool !is null) pool.release();
+    }
 }
 
 /**