Copied remotely
[mono.git] / mcs / class / Mono.Cairo / Mono.Cairo / Surface.cs
1 //
2 // Mono.Cairo.CairoSurfaceObject.cs
3 //
4 // Authors:
5 //    Duncan Mak
6 //    Miguel de Icaza.
7 //
8 // (C) Ximian Inc, 2003.
9 // (C) Novell, Inc. 2003.
10 //
11 // This is an OO wrapper API for the Cairo API
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System;
36 using System.Drawing;
37 using System.Runtime.InteropServices;
38 using System.Collections;
39 using Cairo;
40
41 namespace Cairo {
42
43         public class Surface : IDisposable 
44         {
45                 static Hashtable surfaces = new Hashtable ();
46                 internal IntPtr surface = IntPtr.Zero;
47
48                 private Surface (IntPtr ptr, bool owns)
49                 {
50                         surface = ptr;
51                         lock (typeof (Surface)){
52                                 surfaces [ptr] = this;
53                         }
54                         if (!owns)
55                                 CairoAPI.cairo_surface_reference (ptr);
56                 }
57
58                 static internal Surface LookupExternalSurface (IntPtr p)
59                 {
60                         lock (typeof (Surface)){
61                                 object o = surfaces [p];
62                                 if (o == null){
63                                         return new Surface (p, false);
64                                 }
65                                 return (Surface) o;
66                         }
67                 }
68
69                 public static Cairo.Surface CreateForImage (
70                         string data, Cairo.Format format, int width, int height, int stride)
71                 {
72                         IntPtr p = CairoAPI.cairo_surface_create_for_image (
73                                 data, format, width, height, stride);
74                         
75                         return new Cairo.Surface (p, true);
76                 }
77
78                 public static Cairo.Surface CreateForImage (
79                         Cairo.Format format, int width, int height)
80                 {
81                         IntPtr p = CairoAPI.cairo_image_surface_create (
82                                 format, width, height);
83
84                         return new Cairo.Surface (p, true);
85                 }
86
87
88                 public static Cairo.Surface CreateSimilar (
89                         Cairo.Surface surface, Cairo.Format format, int width, int height)
90                 {
91                         IntPtr p = CairoAPI.cairo_surface_create_similar (
92                                 surface.Handle, format, width, height);
93
94                         return new Cairo.Surface (p, true);
95                 }
96
97                 public static Cairo.Surface CreateSimilarSolid (
98                         Cairo.Surface surface, Cairo.Format format,
99                         int width, int height, double red, double green, double blue, double alpha)
100                 {
101                         IntPtr p = CairoAPI.cairo_surface_create_similar_solid (
102                                 surface.Handle, format, width, height, red, green, blue, alpha);
103
104                         return new Cairo.Surface (p, true);
105                 }
106
107                 ~Surface ()
108                 {
109                         Dispose (false);
110                 }
111
112                 public void Show (Graphics gr, int width, int height) 
113                 {
114                         CairoAPI.cairo_show_surface (gr.Handle, surface, width,  height);
115                 }
116
117                 void IDisposable.Dispose ()
118                 {
119                         Dispose (true);
120                         GC.SuppressFinalize (this);
121                 }
122
123                 protected virtual void Dispose (bool disposing)
124                 {
125                         if (surface == (IntPtr) 0)
126                                 return;
127                         lock (typeof (Surface)){
128                                 surfaces.Remove (surface);
129                         }
130                         CairoAPI.cairo_surface_destroy (surface);
131                         surface = (IntPtr) 0;
132                 }
133                 
134                 public IntPtr Handle {
135                         get { return surface; }
136                 }
137
138                 public int Repeat {
139                         set {
140                                 CairoAPI.cairo_surface_set_repeat (surface, value);
141                         } 
142                 }
143
144                 public Cairo.Matrix Matrix {
145                         set {
146                                 CairoAPI.cairo_surface_set_matrix (surface, value.Pointer);
147                         }
148
149                         get {
150                                 IntPtr p = IntPtr.Zero;
151                                 CairoAPI.cairo_surface_get_matrix (surface, out p);
152                                 return new Cairo.Matrix (p);
153                         }
154                 }
155
156                 public Cairo.Filter Filter {
157                         set {
158                                 CairoAPI.cairo_surface_set_filter (surface, value);
159                         }
160                 }
161
162                 public IntPtr Pointer {
163                         get { return surface; }
164                 }
165
166
167
168         }
169 }