2010-05-27 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Mono.Cairo / Mono.Cairo / Surface.cs
blob862cbdc606d85ac92f2556106359da1aa97ae3f7
1 //
2 // Mono.Cairo.Surface.cs
3 //
4 // Authors:
5 // Duncan Mak
6 // Miguel de Icaza.
7 // Alp Toker
8 //
9 // (C) Ximian Inc, 2003.
10 // (C) Novell, Inc. 2003.
12 // This is an OO wrapper API for the Cairo API
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
23 //
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 //
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System;
37 using System.Collections;
39 namespace Cairo {
41 public class Surface : IDisposable
43 protected static Hashtable surfaces = new Hashtable ();
44 internal IntPtr surface = IntPtr.Zero;
46 protected Surface()
50 protected Surface (IntPtr ptr, bool owns)
52 surface = ptr;
53 lock (surfaces.SyncRoot){
54 surfaces [ptr] = this;
56 if (!owns)
57 NativeMethods.cairo_surface_reference (ptr);
60 static internal Surface LookupExternalSurface (IntPtr p)
62 lock (surfaces.SyncRoot){
63 object o = surfaces [p];
64 if (o == null){
65 return new Surface (p, false);
67 return (Surface) o;
71 static internal Surface LookupSurface (IntPtr surface)
73 SurfaceType st = NativeMethods.cairo_surface_get_type (surface);
74 switch (st) {
75 case SurfaceType.Image:
76 return new ImageSurface (surface, true);
77 case SurfaceType.Xlib:
78 return new XlibSurface (surface, true);
79 case SurfaceType.Xcb:
80 return new XcbSurface (surface, true);
81 case SurfaceType.Glitz:
82 return new GlitzSurface (surface, true);
83 case SurfaceType.Win32:
84 return new Win32Surface (surface, true);
86 case SurfaceType.Pdf:
87 return new PdfSurface (surface, true);
88 case SurfaceType.PS:
89 return new PSSurface (surface, true);
90 case SurfaceType.DirectFB:
91 return new DirectFBSurface (surface, true);
92 case SurfaceType.Svg:
93 return new SvgSurface (surface, true);
95 default:
96 return Surface.LookupExternalSurface (surface);
100 [Obsolete ("Use an ImageSurface constructor instead.")]
101 public static Cairo.Surface CreateForImage (
102 ref byte[] data, Cairo.Format format, int width, int height, int stride)
104 IntPtr p = NativeMethods.cairo_image_surface_create_for_data (
105 data, format, width, height, stride);
107 return new Cairo.Surface (p, true);
110 [Obsolete ("Use an ImageSurface constructor instead.")]
111 public static Cairo.Surface CreateForImage (
112 Cairo.Format format, int width, int height)
114 IntPtr p = NativeMethods.cairo_image_surface_create (
115 format, width, height);
117 return new Cairo.Surface (p, true);
121 public Cairo.Surface CreateSimilar (
122 Cairo.Content content, int width, int height)
124 IntPtr p = NativeMethods.cairo_surface_create_similar (
125 this.Handle, content, width, height);
127 return new Cairo.Surface (p, true);
130 ~Surface ()
132 Dispose (false);
135 //[Obsolete ("Use Context.SetSource() followed by Context.Paint()")]
136 public void Show (Context gr, double x, double y)
138 NativeMethods.cairo_set_source_surface (gr.Handle, surface, x, y);
139 NativeMethods.cairo_paint (gr.Handle);
142 public void Dispose ()
144 Dispose (true);
145 GC.SuppressFinalize (this);
148 protected virtual void Dispose (bool disposing)
150 if (surface == IntPtr.Zero)
151 return;
153 lock (surfaces.SyncRoot)
154 surfaces.Remove (surface);
156 NativeMethods.cairo_surface_destroy (surface);
157 surface = IntPtr.Zero;
160 public Status Finish ()
162 NativeMethods.cairo_surface_finish (surface);
163 return Status;
166 public void Flush ()
168 NativeMethods.cairo_surface_flush (surface);
171 public void MarkDirty ()
173 NativeMethods.cairo_surface_mark_dirty (Handle);
176 public void MarkDirty (Rectangle rectangle)
178 NativeMethods.cairo_surface_mark_dirty_rectangle (Handle, (int)rectangle.X, (int)rectangle.Y, (int)rectangle.Width, (int)rectangle.Height);
181 public IntPtr Handle {
182 get {
183 return surface;
187 public PointD DeviceOffset {
188 get {
189 double x, y;
190 NativeMethods.cairo_surface_get_device_offset (surface, out x, out y);
191 return new PointD (x, y);
194 set {
195 NativeMethods.cairo_surface_set_device_offset (surface, value.X, value.Y);
199 public void Destroy()
201 Dispose (true);
204 public void SetFallbackResolution (double x, double y)
206 NativeMethods.cairo_surface_set_fallback_resolution (surface, x, y);
209 public void WriteToPng (string filename)
211 NativeMethods.cairo_surface_write_to_png (surface, filename);
214 [Obsolete ("Use Handle instead.")]
215 public IntPtr Pointer {
216 get {
217 return surface;
221 public Status Status {
222 get { return NativeMethods.cairo_surface_status (surface); }
225 public Content Content {
226 get { return NativeMethods.cairo_surface_get_content (surface); }
229 public SurfaceType SurfaceType {
230 get { return NativeMethods.cairo_surface_get_type (surface); }
233 public uint ReferenceCount {
234 get { return NativeMethods.cairo_surface_get_reference_count (surface); }