svn path=/branches/mono-1-1-9/mcs/; revision=51206
[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.Collections;
37
38 namespace Cairo {
39
40         public class ImageSurface : Surface
41         {
42                 
43                 public ImageSurface (string filename)
44                 {
45                         surface = CairoAPI.cairo_image_surface_create_from_png (filename);
46                         lock (surfaces.SyncRoot){
47                                 surfaces [surface] = this;
48                         }
49                 }
50                 
51                 public int Width {
52                         get { return CairoAPI.cairo_image_surface_get_width (surface); }
53                 }
54                 
55                 public int Height {
56                         get { return CairoAPI.cairo_image_surface_get_height (surface); }
57                 }
58                 
59         }
60
61         #if UNSTABLE
62         public class PdfSurface : Surface
63         {
64                 public PdfSurface (string filename, double width, double height)
65                 {
66                         surface = CairoAPI.cairo_pdf_surface_create (filename, width, height);
67                         lock (surfaces.SyncRoot){
68                                 surfaces [surface] = this;
69                         }
70                 }
71
72                 public void SetDPI (double x_dpi, double y_dpi)
73                 {
74                         CairoAPI.cairo_pdf_surface_set_dpi (surface, x_dpi, y_dpi);
75                 }
76         }
77
78         public class PostscriptSurface : Surface
79         {
80                 public PostscriptSurface (string filename, double width, double height)
81                 {
82                         surface = CairoAPI.cairo_ps_surface_create (filename, width, height);
83                         lock (surfaces.SyncRoot){
84                                 surfaces [surface] = this;
85                         }
86                 }
87
88                 public void SetDPI (double x_dpi, double y_dpi)
89                 {
90                         CairoAPI.cairo_ps_surface_set_dpi (surface, x_dpi, y_dpi);
91                 }
92         }
93         #endif
94
95         public class Win32Surface : Surface
96         {
97                 public Win32Surface (IntPtr hdc)
98                 {
99                         surface = CairoAPI.cairo_win32_surface_create (hdc);
100                         lock (surfaces.SyncRoot){
101                                 surfaces [surface] = this;
102                         }
103                 }
104         }
105
106         public class XlibSurface : Surface
107         {
108                 public XlibSurface (IntPtr display, IntPtr drawable, IntPtr visual, int width, int height)
109                 {
110                         surface = CairoAPI.cairo_xlib_surface_create (display, drawable, visual, width, height);
111                         lock (surfaces.SyncRoot){
112                                 surfaces [surface] = this;
113                         }
114                 }
115
116                 /* FIXME: has the same parameters as above
117                 public XlibSurface (IntPtr display, IntPtr bitmap, IntPtr screen, int width, int height)
118                 {
119                         surface = CairoAPI.cairo_xlib_surface_create_for_bitmap (display, bitmap, screen, width, height);
120                         lock (surfaces.SyncRoot){
121                                 surfaces [surface] = this;
122                         }
123                 }
124                 */
125
126                 public void SetDrawable (IntPtr drawable, int width, int height)
127                 {
128                         CairoAPI.cairo_xlib_surface_set_drawable (surface, drawable, width, height);
129                 }
130
131                 public void SetSize (int width, int height)
132                 {
133                         CairoAPI.cairo_xlib_surface_set_size (surface, width, height);
134                 }
135         }
136    
137         public class Surface : IDisposable 
138         {                                               
139                 protected static Hashtable surfaces = new Hashtable ();
140                 internal IntPtr surface = IntPtr.Zero;
141
142                 protected Surface()
143                 {}
144                 
145                 private Surface (IntPtr ptr, bool owns)
146                 {
147                         surface = ptr;
148                         lock (surfaces.SyncRoot){
149                                 surfaces [ptr] = this;
150                         }
151                         if (!owns)
152                                 CairoAPI.cairo_surface_reference (ptr);
153                 }
154
155                 static internal Surface LookupExternalSurface (IntPtr p)
156                 {
157                         lock (surfaces.SyncRoot){
158                                 object o = surfaces [p];
159                                 if (o == null){
160                                         return new Surface (p, false);
161                                 }
162                                 return (Surface) o;
163                         }
164                 }               
165                 
166                 public static Cairo.Surface CreateForImage (
167                         string data, Cairo.Format format, int width, int height, int stride)
168                 {
169                         IntPtr p = CairoAPI.cairo_image_surface_create_for_data (
170                                 data, format, width, height, stride);
171                         
172                         return new Cairo.Surface (p, true);
173                 }
174
175                 public static Cairo.Surface CreateForImage (
176                         Cairo.Format format, int width, int height)
177                 {
178                         IntPtr p = CairoAPI.cairo_image_surface_create (
179                                 format, width, height);
180
181                         return new Cairo.Surface (p, true);
182                 }
183
184
185                 public Cairo.Surface CreateSimilar (
186                         Cairo.Content content, int width, int height)
187                 {
188                         IntPtr p = CairoAPI.cairo_surface_create_similar (
189                                 this.Handle, content, width, height);
190
191                         return new Cairo.Surface (p, true);
192                 }
193
194                 ~Surface ()
195                 {
196                         Dispose (false);
197                 }
198
199                 public void Show (Graphics gr, int width, int height) 
200                 {
201                         CairoAPI.cairo_set_source_surface (gr.Handle, surface, width, height);
202                         CairoAPI.cairo_paint (gr.Handle);
203                 }
204
205                 void IDisposable.Dispose ()
206                 {
207                         Dispose (true);
208                         GC.SuppressFinalize (this);
209                 }
210
211                 protected virtual void Dispose (bool disposing)
212                 {
213                         if (surface == (IntPtr) 0)
214                                 return;
215                         lock (surfaces.SyncRoot){
216                                 surfaces.Remove (surface);
217                         }
218                         CairoAPI.cairo_surface_destroy (surface);
219                         surface = (IntPtr) 0;
220                 }
221                 
222                 public Cairo.Status Finish ()
223                 {
224                         return CairoAPI.cairo_surface_finish (surface);
225                 }
226                 
227                 public IntPtr Handle {
228                         get { return surface; }
229                 }
230
231                 public PointD DeviceOffset {
232                         set { CairoAPI.cairo_surface_set_device_offset (surface,
233                                                                     value.X,
234                                                                     value.Y);
235                         }
236                 }
237                 
238                 public void Destroy()
239                 {
240                         CairoAPI.cairo_surface_destroy (surface);
241                 }
242
243                 public void WriteToPng (string filename)
244                 {
245                         CairoAPI.cairo_surface_write_to_png (surface, filename);
246                 }
247                 
248                 public IntPtr Pointer {
249                         get { return surface; }
250                 }
251
252                                 public Status Status {
253                                         get { return CairoAPI.cairo_surface_status (surface); }
254                                 }
255
256         }
257 }