John Luke's patch. <john.luke@gmail.com>
[mono.git] / mcs / class / Mono.Cairo / Mono.Cairo / Matrix.cs
1 //
2 // Mono.Cairo.Matrix.cs
3 //
4 // Author: Duncan Mak
5 //         Hisham Mardam Bey (hisham.mardambey@gmail.com)
6 // (C) Ximian Inc, 2003 - 2005.
7 //
8 // This is an OO wrapper API for the Cairo API
9 //
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33 using System.Runtime.InteropServices;
34
35 namespace Cairo {
36    
37                   
38    [StructLayout(LayoutKind.Sequential)]
39    internal struct Matrix_T
40    {
41            public double xx; 
42            public double yx;
43            public double xy; 
44            public double yy;
45            public double x0; 
46            public double y0;       
47    }
48    
49    
50    
51         public class Matrix
52         {               
53                 internal Matrix_T matrix;
54                 
55                 public Matrix ()       
56                 {               
57                         //CreateIdentify();
58                 }
59                 
60                 internal Matrix (Matrix_T ptr)
61                 {
62                         //if (ptr == null)
63                         //  CreateIdentify ();
64                         
65                         matrix = ptr;
66                 }
67                 
68                 public void CreateIdentify ()
69                 {                       
70                         CairoAPI.cairo_matrix_init_identity (ref matrix);
71                 }
72                 
73                 public void Init (double xx, double yx, double xy, double yy,
74                                   double x0, double y0)
75                 {
76                         matrix.xx = xx; matrix.yx = yx; matrix.xy = xy;
77                         matrix.yy = yy; matrix.x0 = x0; matrix.y0 = y0;
78                 }
79                 
80                 public void InitTranslate (double tx, double ty)
81                 {               
82                         CairoAPI.cairo_matrix_init_translate (ref matrix, tx, ty);
83                 }               
84                                                
85                 public void Translate (double tx, double ty)
86                 {
87                         CairoAPI.cairo_matrix_translate (ref matrix, tx, ty);
88                 }
89                 
90                 public void InitScale (double sx, double sy)
91                 {
92                         CairoAPI.cairo_matrix_init_scale (ref matrix, sx, sy);
93                 }               
94                 
95                 public void Scale (double sx, double sy)
96                 {
97                         CairoAPI.cairo_matrix_scale (ref matrix, sx, sy);
98                 }
99
100                 public void InitRotate (double radians)
101                 {
102                         CairoAPI.cairo_matrix_init_rotate (ref matrix, radians);
103                 }               
104                 
105                 public void Rotate (double radians)
106                 {
107                         CairoAPI.cairo_matrix_rotate (ref matrix, radians);
108                 }
109
110                 public Cairo.Status Invert ()
111                 {
112                         return  CairoAPI.cairo_matrix_invert (ref matrix);
113                 }
114
115
116                 public static void Multiply (ref Cairo.Matrix res, 
117                                          ref Cairo.Matrix a, ref Cairo.Matrix b)
118                 {       
119                         if (res == null)
120                           res = new Matrix ();
121                                                 
122                         CairoAPI.cairo_matrix_multiply (ref res.matrix, 
123                                                         ref a.matrix, 
124                                                         ref b.matrix);
125                 }
126                 
127                 public void TransformDistance (ref double dx, ref double dy)
128                 {
129                         CairoAPI.cairo_matrix_transform_distance (ref matrix, ref dx, ref dy);
130                 }
131
132                 public void TransformPoint (ref double x, ref double y)
133                 {
134                         CairoAPI.cairo_matrix_transform_point (ref matrix, ref x, ref y);
135                 }
136                 
137                 internal Matrix_T Pointer {
138                         get { return matrix; }
139                         set { matrix = value; }
140                 }
141                 
142                 public IntPtr Raw {
143                         get {
144                                 IntPtr p = Marshal.AllocCoTaskMem ( Marshal.SizeOf (matrix));
145                                 Marshal.StructureToPtr (matrix, p, true);
146                                 return p;
147                         }
148                 }
149                                 
150         }
151 }