2005-08-16 Marek Safar <marek.safar@seznam.cz>
[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.Drawing;
34 using System.Runtime.InteropServices;
35 using Cairo;
36
37 namespace Cairo {
38    
39                   
40    [StructLayout(LayoutKind.Sequential)]
41    public struct Matrix_T
42    {
43            public double xx; 
44            public double yx;
45            public double xy; 
46            public double yy;
47            public double x0; 
48            public double y0;       
49    }
50    
51    
52    
53         public class Matrix
54         {               
55                 internal Matrix_T matrix;
56                 
57                 public Matrix ()       
58                 {               
59                         //CreateIdentify();
60                 }
61                 
62                 internal Matrix (Matrix_T ptr)
63                 {
64                         //if (ptr == null)
65                         //  CreateIdentify ();
66                         
67                         matrix = ptr;
68                 }
69                 
70                 public void CreateIdentify ()
71                 {                       
72                         CairoAPI.cairo_matrix_init_identity (ref matrix);
73                 }
74                 
75                 public void Init (double xx, double yx, double xy, double yy,
76                                   double x0, double y0)
77                 {
78                         matrix.xx = xx; matrix.yx = yx; matrix.xy = xy;
79                         matrix.yy = yy; matrix.x0 = x0; matrix.y0 = y0;
80                 }
81                 
82                 public void InitTranslate (double tx, double ty)
83                 {               
84                         CairoAPI.cairo_matrix_init_translate (ref matrix, tx, ty);
85                 }               
86                                                
87                 public void Translate (double tx, double ty)
88                 {
89                         CairoAPI.cairo_matrix_translate (ref matrix, tx, ty);
90                 }
91                 
92                 public void InitScale (double sx, double sy)
93                 {
94                         CairoAPI.cairo_matrix_init_scale (ref matrix, sx, sy);
95                 }               
96                 
97                 public void Scale (double sx, double sy)
98                 {
99                         CairoAPI.cairo_matrix_scale (ref matrix, sx, sy);
100                 }
101
102                 public void InitRotate (double radians)
103                 {
104                         CairoAPI.cairo_matrix_init_rotate (ref matrix, radians);
105                 }               
106                 
107                 public void Rotate (double radians)
108                 {
109                         CairoAPI.cairo_matrix_rotate (ref matrix, radians);
110                 }
111
112                 public Cairo.Status Invert ()
113                 {
114                         return  CairoAPI.cairo_matrix_invert (ref matrix);
115                 }
116
117
118                 public static void Multiply (ref Cairo.Matrix res, 
119                                          ref Cairo.Matrix a, ref Cairo.Matrix b)
120                 {       
121                         if (res == null)
122                           res = new Matrix ();
123                                                 
124                         CairoAPI.cairo_matrix_multiply (ref res.matrix, 
125                                                         ref a.matrix, 
126                                                         ref b.matrix);
127                 }
128                 
129                 public void TransformDistance (ref double dx, ref double dy)
130                 {
131                         CairoAPI.cairo_matrix_transform_distance (ref matrix, ref dx, ref dy);
132                 }
133
134                 public void TransformPoint (ref double x, ref double y)
135                 {
136                         CairoAPI.cairo_matrix_transform_point (ref matrix, ref x, ref y);
137                 }
138                 
139                 public Matrix_T Pointer {
140                         get { return matrix; }
141                         set { matrix = value; }
142                 }
143                 
144                 public IntPtr Raw {
145                         get {
146                                 IntPtr p = Marshal.AllocCoTaskMem ( Marshal.SizeOf (matrix));
147                                 Marshal.StructureToPtr (matrix, p, true);
148                                 return p;
149                         }
150                 }
151                                 
152         }
153 }