2 // Mono.Cairo.Surface.cs
8 // (C) Ximian Inc, 2003.
9 // (C) Novell, Inc. 2003.
11 // This is an OO wrapper API for the Cairo API
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
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.
36 using System.Collections;
40 public class Surface : IDisposable
42 protected static Hashtable surfaces = new Hashtable ();
43 internal IntPtr surface = IntPtr.Zero;
49 protected Surface (IntPtr ptr, bool owns)
52 lock (surfaces.SyncRoot){
53 surfaces [ptr] = this;
56 CairoAPI.cairo_surface_reference (ptr);
59 static internal Surface LookupExternalSurface (IntPtr p)
61 lock (surfaces.SyncRoot){
62 object o = surfaces [p];
64 return new Surface (p, false);
70 [Obsolete ("Use an ImageSurface constructor instead.")]
71 public static Cairo.Surface CreateForImage (
72 ref byte[] data, Cairo.Format format, int width, int height, int stride)
74 IntPtr p = CairoAPI.cairo_image_surface_create_for_data (
75 data, format, width, height, stride);
77 return new Cairo.Surface (p, true);
80 [Obsolete ("Use an ImageSurface constructor instead.")]
81 public static Cairo.Surface CreateForImage (
82 Cairo.Format format, int width, int height)
84 IntPtr p = CairoAPI.cairo_image_surface_create (
85 format, width, height);
87 return new Cairo.Surface (p, true);
91 public Cairo.Surface CreateSimilar (
92 Cairo.Content content, int width, int height)
94 IntPtr p = CairoAPI.cairo_surface_create_similar (
95 this.Handle, content, width, height);
97 return new Cairo.Surface (p, true);
105 public void Show (Context gr, double x, double y)
107 CairoAPI.cairo_set_source_surface (gr.Handle, surface, x, y);
108 CairoAPI.cairo_paint (gr.Handle);
111 void IDisposable.Dispose ()
114 GC.SuppressFinalize (this);
117 protected virtual void Dispose (bool disposing)
119 if (surface == (IntPtr) 0)
122 lock (surfaces.SyncRoot)
123 surfaces.Remove (surface);
125 CairoAPI.cairo_surface_destroy (surface);
126 surface = (IntPtr) 0;
129 public Status Finish ()
131 CairoAPI.cairo_surface_finish (surface);
137 CairoAPI.cairo_surface_flush (surface);
140 public void MarkDirty ()
142 CairoAPI.cairo_surface_mark_dirty (Handle);
145 public void MarkDirty (Rectangle rectangle)
147 CairoAPI.cairo_surface_mark_dirty_rectangle (Handle, (int)rectangle.X, (int)rectangle.Y, (int)rectangle.Width, (int)rectangle.Height);
150 public IntPtr Handle {
156 public PointD DeviceOffset {
160 CairoAPI.cairo_surface_get_device_offset (surface, out x, out y);
161 return new PointD (x, y);
165 CairoAPI.cairo_surface_set_device_offset (surface, value.X, value.Y);
169 public void Destroy()
175 public void SetFallbackResolution (double x, double y)
177 CairoAPI.cairo_surface_set_fallback_resolution (surface, x, y);
181 public void WriteToPng (string filename)
183 CairoAPI.cairo_surface_write_to_png (surface, filename);
186 [Obsolete ("Use Handle instead.")]
187 public IntPtr Pointer {
193 public Status Status {
194 get { return CairoAPI.cairo_surface_status (surface); }
198 public Content Content {
199 get { return CairoAPI.cairo_surface_get_content (surface); }
202 public SurfaceType SurfaceType {
203 get { return CairoAPI.cairo_surface_get_type (surface); }