In .:
[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                 public ImageSurface (Format format, int width, int height)
43                 {
44                         surface = CairoAPI.cairo_image_surface_create (format, width, height);
45                         lock (surfaces.SyncRoot){
46                                 surfaces [surface] = this;
47                         }
48                 }
49
50                 public ImageSurface (string data, Cairo.Format format, int width, int height, int stride)
51                 {
52                         surface = CairoAPI.cairo_image_surface_create_for_data (data, format, width, height, stride);
53                         lock (surfaces.SyncRoot){
54                                 surfaces [surface] = this;
55                         }
56                 }
57                 
58                 public ImageSurface (string filename)
59                 {
60                         surface = CairoAPI.cairo_image_surface_create_from_png (filename);
61                         lock (surfaces.SyncRoot){
62                                 surfaces [surface] = this;
63                         }
64                 }
65                 
66                 public int Width {
67                         get { return CairoAPI.cairo_image_surface_get_width (surface); }
68                 }
69                 
70                 public int Height {
71                         get { return CairoAPI.cairo_image_surface_get_height (surface); }
72                 }
73                 
74         }
75
76         #if UNSTABLE
77         public class PdfSurface : Surface
78         {
79                 public PdfSurface (string filename, double width, double height)
80                 {
81                         surface = CairoAPI.cairo_pdf_surface_create (filename, width, height);
82                         lock (surfaces.SyncRoot){
83                                 surfaces [surface] = this;
84                         }
85                 }
86
87                 public void SetDPI (double x_dpi, double y_dpi)
88                 {
89                         CairoAPI.cairo_pdf_surface_set_dpi (surface, x_dpi, y_dpi);
90                 }
91         }
92
93         public class PostscriptSurface : Surface
94         {
95                 public PostscriptSurface (string filename, double width, double height)
96                 {
97                         surface = CairoAPI.cairo_ps_surface_create (filename, width, height);
98                         lock (surfaces.SyncRoot){
99                                 surfaces [surface] = this;
100                         }
101                 }
102
103                 public void SetDPI (double x_dpi, double y_dpi)
104                 {
105                         CairoAPI.cairo_ps_surface_set_dpi (surface, x_dpi, y_dpi);
106                 }
107         }
108         #endif
109
110         public class Win32Surface : Surface
111         {
112                 public Win32Surface (IntPtr hdc)
113                 {
114                         surface = CairoAPI.cairo_win32_surface_create (hdc);
115                         lock (surfaces.SyncRoot){
116                                 surfaces [surface] = this;
117                         }
118                 }
119         }
120
121         public class XlibSurface : Surface
122         {
123                 public XlibSurface (IntPtr display, IntPtr drawable, IntPtr visual, int width, int height)
124                 {
125                         surface = CairoAPI.cairo_xlib_surface_create (display, drawable, visual, width, height);
126                         lock (surfaces.SyncRoot){
127                                 surfaces [surface] = this;
128                         }
129                 }
130
131                 public XlibSurface (IntPtr ptr, bool own) : base (ptr, own)
132                 {
133                 }
134
135                 public static XlibSurface FromBitmap (IntPtr display, IntPtr bitmap, IntPtr screen, int width, int height)
136                 {
137                         IntPtr  ptr;
138
139                         ptr = CairoAPI.cairo_xlib_surface_create_for_bitmap (display, bitmap, screen, width, height);
140                         return new XlibSurface(ptr, true);
141                 }
142
143                 public void SetDrawable (IntPtr drawable, int width, int height)
144                 {
145                         CairoAPI.cairo_xlib_surface_set_drawable (surface, drawable, width, height);
146                 }
147
148                 public void SetSize (int width, int height)
149                 {
150                         CairoAPI.cairo_xlib_surface_set_size (surface, width, height);
151                 }
152         }
153    
154         public class Surface : IDisposable 
155         {                                               
156                 protected static Hashtable surfaces = new Hashtable ();
157                 internal IntPtr surface = IntPtr.Zero;
158
159                 protected Surface()
160                 {
161                 }
162                 
163                 protected Surface (IntPtr ptr, bool owns)
164                 {
165                         surface = ptr;
166                         lock (surfaces.SyncRoot){
167                                 surfaces [ptr] = this;
168                         }
169                         if (!owns)
170                                 CairoAPI.cairo_surface_reference (ptr);
171                 }
172
173                 static internal Surface LookupExternalSurface (IntPtr p)
174                 {
175                         lock (surfaces.SyncRoot){
176                                 object o = surfaces [p];
177                                 if (o == null){
178                                         return new Surface (p, false);
179                                 }
180                                 return (Surface) o;
181                         }
182                 }               
183                 
184                 [Obsolete ("Use an ImageSurface constructor instead.")]
185                 public static Cairo.Surface CreateForImage (
186                         string data, Cairo.Format format, int width, int height, int stride)
187                 {
188                         IntPtr p = CairoAPI.cairo_image_surface_create_for_data (
189                                 data, format, width, height, stride);
190                         
191                         return new Cairo.Surface (p, true);
192                 }
193
194                 [Obsolete ("Use an ImageSurface constructor instead.")]
195                 public static Cairo.Surface CreateForImage (
196                         Cairo.Format format, int width, int height)
197                 {
198                         IntPtr p = CairoAPI.cairo_image_surface_create (
199                                 format, width, height);
200
201                         return new Cairo.Surface (p, true);
202                 }
203
204
205                 public Cairo.Surface CreateSimilar (
206                         Cairo.Content content, int width, int height)
207                 {
208                         IntPtr p = CairoAPI.cairo_surface_create_similar (
209                                 this.Handle, content, width, height);
210
211                         return new Cairo.Surface (p, true);
212                 }
213
214                 ~Surface ()
215                 {
216                         Dispose (false);
217                 }
218
219                 public void Show (Context gr, int width, int height) 
220                 {
221                         CairoAPI.cairo_set_source_surface (gr.Handle, surface, width, height);
222                         CairoAPI.cairo_paint (gr.Handle);
223                 }
224
225                 void IDisposable.Dispose ()
226                 {
227                         Dispose (true);
228                         GC.SuppressFinalize (this);
229                 }
230
231                 protected virtual void Dispose (bool disposing)
232                 {
233                         if (surface == (IntPtr) 0)
234                                 return;
235                         
236                         lock (surfaces.SyncRoot)
237                                 surfaces.Remove (surface);
238
239                         CairoAPI.cairo_surface_destroy (surface);
240                         surface = (IntPtr) 0;
241                 }
242                 
243                 public Status Finish ()
244                 {
245                         CairoAPI.cairo_surface_finish (surface);
246                         return Status;
247                 }
248                 
249                 public void Flush ()
250                 {
251                         CairoAPI.cairo_surface_flush (surface);
252                 }
253                 
254                 public void MarkDirty ()
255                 {
256                         CairoAPI.cairo_surface_mark_dirty (Handle);
257                 }
258                 
259                 public void MarkDirty (Rectangle rectangle)
260                 {
261                         CairoAPI.cairo_surface_mark_dirty_rectangle (Handle, (int)rectangle.X, (int)rectangle.Y, (int)rectangle.Width, (int)rectangle.Height);
262                 }
263                 
264                 public IntPtr Handle {
265                         get {
266                                 return surface;
267                         }
268                 }
269
270                 public PointD DeviceOffset {
271                         set {
272                                 CairoAPI.cairo_surface_set_device_offset (surface, value.X, value.Y);
273                         }
274                 }
275                 
276                 public void Destroy()
277                 {
278                         CairoAPI.cairo_surface_destroy (surface);
279                 }
280
281                 public void WriteToPng (string filename)
282                 {
283                         CairoAPI.cairo_surface_write_to_png (surface, filename);
284                 }
285                 
286                 [Obsolete ("Use Handle instead.")]
287                 public IntPtr Pointer {
288                         get {
289                                 return surface;
290                         }
291                 }
292                 
293                 public Status Status {
294                         get { return CairoAPI.cairo_surface_status (surface); }
295                 }
296
297         }
298 }