2 // Mono.Cairo.Surface.cs
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:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
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.
37 using System.Collections;
41 public class Surface : IDisposable
44 protected static Hashtable surfaces = new Hashtable ();
46 IntPtr handle = IntPtr.Zero;
54 protected Surface (IntPtr ptr) : this (ptr, true)
58 protected Surface (IntPtr handle, bool owner)
62 NativeMethods.cairo_surface_reference (handle);
63 if (CairoDebug.Enabled)
64 CairoDebug.OnAllocated (handle);
67 public static Surface Lookup (IntPtr surface, bool owned)
69 SurfaceType st = NativeMethods.cairo_surface_get_type (surface);
71 case SurfaceType.Image:
72 return new ImageSurface (surface, owned);
73 case SurfaceType.Xlib:
74 return new XlibSurface (surface, owned);
76 return new XcbSurface (surface, owned);
77 case SurfaceType.Glitz:
78 return new GlitzSurface (surface, owned);
79 case SurfaceType.Win32:
80 return new Win32Surface (surface, owned);
82 return new PdfSurface (surface, owned);
84 return new PSSurface (surface, owned);
85 case SurfaceType.DirectFB:
86 return new DirectFBSurface (surface, owned);
88 return new SvgSurface (surface, owned);
90 return new Surface (surface, owned);
94 [Obsolete ("Use an ImageSurface constructor instead.")]
95 public static Cairo.Surface CreateForImage (
96 ref byte[] data, Cairo.Format format, int width, int height, int stride)
98 IntPtr p = NativeMethods.cairo_image_surface_create_for_data (
99 data, format, width, height, stride);
101 return new Cairo.Surface (p, true);
104 [Obsolete ("Use an ImageSurface constructor instead.")]
105 public static Cairo.Surface CreateForImage (
106 Cairo.Format format, int width, int height)
108 IntPtr p = NativeMethods.cairo_image_surface_create (
109 format, width, height);
111 return new Cairo.Surface (p, true);
115 public Cairo.Surface CreateSimilar (
116 Cairo.Content content, int width, int height)
118 IntPtr p = NativeMethods.cairo_surface_create_similar (
119 this.Handle, content, width, height);
121 return new Cairo.Surface (p, true);
129 //[Obsolete ("Use Context.SetSource() followed by Context.Paint()")]
130 public void Show (Context gr, double x, double y)
132 NativeMethods.cairo_set_source_surface (gr.Handle, handle, x, y);
133 NativeMethods.cairo_paint (gr.Handle);
136 public void Dispose ()
139 GC.SuppressFinalize (this);
142 protected virtual void Dispose (bool disposing)
144 if (!disposing || CairoDebug.Enabled)
145 CairoDebug.OnDisposed<Surface> (handle, disposing);
147 if (!disposing|| handle == IntPtr.Zero)
150 NativeMethods.cairo_surface_destroy (handle);
151 handle = IntPtr.Zero;
154 public Status Finish ()
156 NativeMethods.cairo_surface_finish (handle);
162 NativeMethods.cairo_surface_flush (handle);
165 public void MarkDirty ()
167 NativeMethods.cairo_surface_mark_dirty (Handle);
170 public void MarkDirty (Rectangle rectangle)
172 NativeMethods.cairo_surface_mark_dirty_rectangle (Handle, (int)rectangle.X, (int)rectangle.Y, (int)rectangle.Width, (int)rectangle.Height);
175 public IntPtr Handle {
181 public PointD DeviceOffset {
184 NativeMethods.cairo_surface_get_device_offset (handle, out x, out y);
185 return new PointD (x, y);
189 NativeMethods.cairo_surface_set_device_offset (handle, value.X, value.Y);
193 [Obsolete ("Use Dispose()")]
194 public void Destroy()
199 public void SetFallbackResolution (double x, double y)
201 NativeMethods.cairo_surface_set_fallback_resolution (handle, x, y);
204 public void WriteToPng (string filename)
206 NativeMethods.cairo_surface_write_to_png (handle, filename);
209 [Obsolete ("Use Handle instead.")]
210 public IntPtr Pointer {
216 public Status Status {
217 get { return NativeMethods.cairo_surface_status (handle); }
220 public Content Content {
221 get { return NativeMethods.cairo_surface_get_content (handle); }
224 public SurfaceType SurfaceType {
225 get { return NativeMethods.cairo_surface_get_type (handle); }
228 public uint ReferenceCount {
229 get { return NativeMethods.cairo_surface_get_reference_count (handle); }