In corlib/System.Runtime.InteropServices:
[mono.git] / mcs / class / System.Drawing / System.Drawing.Drawing2D / Matrix.cs
index b56d9d33eb3b1264edb4a67c9f4b69a2e66da5f7..5b2472891752d3ddc42fc9cf153d73bda2c438b2 100644 (file)
@@ -8,11 +8,7 @@
 //   Ravindra (rkumar@novell.com)
 //
 // (C) Ximian, Inc.  http://www.ximian.com
-// (C) Novell, Inc.  http://www.novell.com
-//
-
-//
-// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2004, 2006 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -34,8 +30,6 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using System;
-using System.Drawing;
 using System.Runtime.InteropServices;
 
 namespace System.Drawing.Drawing2D
@@ -56,15 +50,25 @@ namespace System.Drawing.Drawing2D
                        GDIPlus.CheckStatus (status);
                 }
         
-                public Matrix (Rectangle rect , Point[] plgpts)
+                public Matrix (Rectangle rect, Point[] plgpts)
                 {
-                       Status status = GDIPlus.GdipCreateMatrix3I (rect, plgpts, out nativeMatrix);
+                       if (plgpts == null)
+                               throw new ArgumentNullException ("plgpts");
+                       if (plgpts.Length != 3)
+                               throw new ArgumentException ("plgpts");
+
+                       Status status = GDIPlus.GdipCreateMatrix3I (ref rect, plgpts, out nativeMatrix);
                        GDIPlus.CheckStatus (status);
                 }
         
-                public Matrix (RectangleF rect , PointF[] pa)
+                public Matrix (RectangleF rect, PointF[] plgpts)
                 {
-                       Status status = GDIPlus.GdipCreateMatrix3 (rect, pa, out nativeMatrix);
+                       if (plgpts == null)
+                               throw new ArgumentNullException ("plgpts");
+                       if (plgpts.Length != 3)
+                               throw new ArgumentException ("plgpts");
+
+                       Status status = GDIPlus.GdipCreateMatrix3 (ref rect, plgpts, out nativeMatrix);
                        GDIPlus.CheckStatus (status);
                 }
 
@@ -77,16 +81,17 @@ namespace System.Drawing.Drawing2D
                 // properties
                 public float[] Elements {
                         get {
-                                IntPtr tmp = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (float)) * 6);
-                                float [] retval = new float [6];
-
-                               Status status = GDIPlus.GdipGetMatrixElements (nativeMatrix, tmp);
-                               GDIPlus.CheckStatus (status);
-
-                                Marshal.Copy (tmp, retval, 0, 6);
-
-                                Marshal.FreeHGlobal (tmp);
-                                return retval;
+                               float [] retval = new float [6];
+                               IntPtr tmp = Marshal.AllocHGlobal (Marshal.SizeOf (typeof (float)) * 6);
+                               try {
+                                       Status status = GDIPlus.GdipGetMatrixElements (nativeMatrix, tmp);
+                                       GDIPlus.CheckStatus (status);
+                                       Marshal.Copy (tmp, retval, 0, 6);
+                               }
+                               finally {
+                                       Marshal.FreeHGlobal (tmp);
+                               }
+                               return retval;
                         }
                 }
         
@@ -131,8 +136,13 @@ namespace System.Drawing.Drawing2D
         
                 public void Dispose ()
                 {
-                       Status status = GDIPlus.GdipDeleteMatrix (nativeMatrix);
-                       GDIPlus.CheckStatus (status);
+                       if (nativeMatrix != IntPtr.Zero) {
+                               Status status = GDIPlus.GdipDeleteMatrix (nativeMatrix);
+                               GDIPlus.CheckStatus (status);
+                               nativeMatrix = IntPtr.Zero;
+                       }
+
+                       GC.SuppressFinalize (this);
                 }                       
         
                 public override bool Equals (object obj)
@@ -170,8 +180,11 @@ namespace System.Drawing.Drawing2D
                         Multiply (matrix, MatrixOrder.Prepend);
                 }
         
-                public void Multiply (Matrix matrix, MatrixOrder order)
-                {
+               public void Multiply (Matrix matrix, MatrixOrder order)
+               {
+                       if (matrix == null)
+                               throw new ArgumentNullException ("matrix");
+
                        Status status = GDIPlus.GdipMultiplyMatrix (nativeMatrix, matrix.nativeMatrix, order);
                        GDIPlus.CheckStatus (status);
                 }
@@ -200,6 +213,9 @@ namespace System.Drawing.Drawing2D
         
                 public void RotateAt (float angle, PointF point, MatrixOrder order)
                 {
+                       if ((order < MatrixOrder.Prepend) || (order > MatrixOrder.Append))
+                               throw new ArgumentException ("order");
+
                         angle *= (float) (Math.PI / 180.0);  // degrees to radians
                         float cos = (float) Math.Cos (angle);
                         float sin = (float) Math.Sin (angle);
@@ -252,24 +268,36 @@ namespace System.Drawing.Drawing2D
         
                 public void TransformPoints (Point[] pts)
                 {
+                       if (pts == null)
+                               throw new ArgumentNullException ("pts");
+
                        Status status = GDIPlus.GdipTransformMatrixPointsI (nativeMatrix, pts, pts.Length);
                        GDIPlus.CheckStatus (status);
                 }
         
                 public void TransformPoints (PointF[] pts)
                 {
+                       if (pts == null)
+                               throw new ArgumentNullException ("pts");
+
                        Status status = GDIPlus.GdipTransformMatrixPoints (nativeMatrix, pts, pts.Length);
                        GDIPlus.CheckStatus (status);
                 }
         
                 public void TransformVectors (Point[] pts)
                 {
+                       if (pts == null)
+                               throw new ArgumentNullException ("pts");
+
                        Status status = GDIPlus.GdipVectorTransformMatrixPointsI (nativeMatrix, pts, pts.Length);
                        GDIPlus.CheckStatus (status);
                 }
         
                 public void TransformVectors (PointF[] pts)
                 {
+                       if (pts == null)
+                               throw new ArgumentNullException ("pts");
+
                        Status status = GDIPlus.GdipVectorTransformMatrixPoints (nativeMatrix, pts, pts.Length);
                        GDIPlus.CheckStatus (status);
                 }