2 // Mono.Cairo.CairoSurfaceObject.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 ImageSurface : Surface
42 public ImageSurface (Format format, int width, int height)
44 surface = CairoAPI.cairo_image_surface_create (format, width, height);
45 lock (surfaces.SyncRoot){
46 surfaces [surface] = this;
50 public ImageSurface (string data, Cairo.Format format, int width, int height, int stride)
52 surface = CairoAPI.cairo_image_surface_create_for_data (data, format, width, height, stride);
53 lock (surfaces.SyncRoot){
54 surfaces [surface] = this;
58 public ImageSurface (string filename)
60 surface = CairoAPI.cairo_image_surface_create_from_png (filename);
61 lock (surfaces.SyncRoot){
62 surfaces [surface] = this;
67 get { return CairoAPI.cairo_image_surface_get_width (surface); }
71 get { return CairoAPI.cairo_image_surface_get_height (surface); }
77 public class PdfSurface : Surface
79 public PdfSurface (string filename, double width, double height)
81 surface = CairoAPI.cairo_pdf_surface_create (filename, width, height);
82 lock (surfaces.SyncRoot){
83 surfaces [surface] = this;
87 public void SetDPI (double x_dpi, double y_dpi)
89 CairoAPI.cairo_pdf_surface_set_dpi (surface, x_dpi, y_dpi);
93 public class PostscriptSurface : Surface
95 public PostscriptSurface (string filename, double width, double height)
97 surface = CairoAPI.cairo_ps_surface_create (filename, width, height);
98 lock (surfaces.SyncRoot){
99 surfaces [surface] = this;
103 public void SetDPI (double x_dpi, double y_dpi)
105 CairoAPI.cairo_ps_surface_set_dpi (surface, x_dpi, y_dpi);
110 public class Win32Surface : Surface
112 public Win32Surface (IntPtr hdc)
114 surface = CairoAPI.cairo_win32_surface_create (hdc);
115 lock (surfaces.SyncRoot){
116 surfaces [surface] = this;
121 public class XlibSurface : Surface
123 public XlibSurface (IntPtr display, IntPtr drawable, IntPtr visual, int width, int height)
125 surface = CairoAPI.cairo_xlib_surface_create (display, drawable, visual, width, height);
126 lock (surfaces.SyncRoot){
127 surfaces [surface] = this;
131 /* FIXME: has the same parameters as above
132 public XlibSurface (IntPtr display, IntPtr bitmap, IntPtr screen, int width, int height)
134 surface = CairoAPI.cairo_xlib_surface_create_for_bitmap (display, bitmap, screen, width, height);
135 lock (surfaces.SyncRoot){
136 surfaces [surface] = this;
141 public void SetDrawable (IntPtr drawable, int width, int height)
143 CairoAPI.cairo_xlib_surface_set_drawable (surface, drawable, width, height);
146 public void SetSize (int width, int height)
148 CairoAPI.cairo_xlib_surface_set_size (surface, width, height);
152 public class Surface : IDisposable
154 protected static Hashtable surfaces = new Hashtable ();
155 internal IntPtr surface = IntPtr.Zero;
161 private Surface (IntPtr ptr, bool owns)
164 lock (surfaces.SyncRoot){
165 surfaces [ptr] = this;
168 CairoAPI.cairo_surface_reference (ptr);
171 static internal Surface LookupExternalSurface (IntPtr p)
173 lock (surfaces.SyncRoot){
174 object o = surfaces [p];
176 return new Surface (p, false);
182 [Obsolete ("Use an ImageSurface constructor instead.")]
183 public static Cairo.Surface CreateForImage (
184 string data, Cairo.Format format, int width, int height, int stride)
186 IntPtr p = CairoAPI.cairo_image_surface_create_for_data (
187 data, format, width, height, stride);
189 return new Cairo.Surface (p, true);
192 [Obsolete ("Use an ImageSurface constructor instead.")]
193 public static Cairo.Surface CreateForImage (
194 Cairo.Format format, int width, int height)
196 IntPtr p = CairoAPI.cairo_image_surface_create (
197 format, width, height);
199 return new Cairo.Surface (p, true);
203 public Cairo.Surface CreateSimilar (
204 Cairo.Content content, int width, int height)
206 IntPtr p = CairoAPI.cairo_surface_create_similar (
207 this.Handle, content, width, height);
209 return new Cairo.Surface (p, true);
217 public void Show (Context gr, int width, int height)
219 CairoAPI.cairo_set_source_surface (gr.Handle, surface, width, height);
220 CairoAPI.cairo_paint (gr.Handle);
223 void IDisposable.Dispose ()
226 GC.SuppressFinalize (this);
229 protected virtual void Dispose (bool disposing)
231 if (surface == (IntPtr) 0)
234 lock (surfaces.SyncRoot)
235 surfaces.Remove (surface);
237 CairoAPI.cairo_surface_destroy (surface);
238 surface = (IntPtr) 0;
241 public Cairo.Status Finish ()
243 return CairoAPI.cairo_surface_finish (surface);
246 public IntPtr Handle {
252 public PointD DeviceOffset {
254 CairoAPI.cairo_surface_set_device_offset (surface, value.X, value.Y);
258 public void Destroy()
260 CairoAPI.cairo_surface_destroy (surface);
263 public void WriteToPng (string filename)
265 CairoAPI.cairo_surface_write_to_png (surface, filename);
268 public IntPtr Pointer {
274 public Status Status {
275 get { return CairoAPI.cairo_surface_status (surface); }