Merge pull request #649 from DavidS/feature/implement-additional-reference-path
[mono.git] / mcs / class / Mono.Cairo / Mono.Cairo / Surface.cs
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.
11 //
12 // This is an OO wrapper API for the Cairo API
13 //
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 //
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.
34 //
35
36 using System;
37 using System.Collections;
38
39 namespace Cairo {
40
41         public class Surface : IDisposable
42         {
43                 [Obsolete]
44                 protected static Hashtable surfaces = new Hashtable ();
45
46                 IntPtr handle = IntPtr.Zero;
47
48                 [Obsolete]
49                 protected Surface()
50                 {
51                 }
52
53                 [Obsolete]
54                 protected Surface (IntPtr ptr) : this (ptr, true)
55                 {
56                 }
57
58                 protected Surface (IntPtr handle, bool owner)
59                 {
60                         this.handle = handle;
61                         if (!owner)
62                                 NativeMethods.cairo_surface_reference (handle);
63                         if (CairoDebug.Enabled)
64                                 CairoDebug.OnAllocated (handle);
65                 }
66
67                 public static Surface Lookup (IntPtr surface, bool owned)
68                 {
69                         SurfaceType st = NativeMethods.cairo_surface_get_type (surface);
70                         switch (st) {
71                         case SurfaceType.Image:
72                                 return new ImageSurface (surface, owned);
73                         case SurfaceType.Xlib:
74                                 return new XlibSurface (surface, owned);
75                         case SurfaceType.Xcb:
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);
81                         case SurfaceType.Pdf:
82                                 return new PdfSurface (surface, owned);
83                         case SurfaceType.PS:
84                                 return new PSSurface (surface, owned);
85                         case SurfaceType.DirectFB:
86                                 return new DirectFBSurface (surface, owned);
87                         case SurfaceType.Svg:
88                                 return new SvgSurface (surface, owned);
89                         default:
90                                 return new Surface (surface, owned);
91                         }
92                 }
93
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)
97                 {
98                         IntPtr p = NativeMethods.cairo_image_surface_create_for_data (
99                                 data, format, width, height, stride);
100
101                         return new Cairo.Surface (p, true);
102                 }
103
104                 [Obsolete ("Use an ImageSurface constructor instead.")]
105                 public static Cairo.Surface CreateForImage (
106                         Cairo.Format format, int width, int height)
107                 {
108                         IntPtr p = NativeMethods.cairo_image_surface_create (
109                                 format, width, height);
110
111                         return new Cairo.Surface (p, true);
112                 }
113
114
115                 public Cairo.Surface CreateSimilar (
116                         Cairo.Content content, int width, int height)
117                 {
118                         IntPtr p = NativeMethods.cairo_surface_create_similar (
119                                 this.Handle, content, width, height);
120
121                         return new Cairo.Surface (p, true);
122                 }
123
124                 ~Surface ()
125                 {
126                         Dispose (false);
127                 }
128
129                 //[Obsolete ("Use Context.SetSource() followed by Context.Paint()")]
130                 public void Show (Context gr, double x, double y)
131                 {
132                         NativeMethods.cairo_set_source_surface (gr.Handle, handle, x, y);
133                         NativeMethods.cairo_paint (gr.Handle);
134                 }
135
136                 public void Dispose ()
137                 {
138                         Dispose (true);
139                         GC.SuppressFinalize (this);
140                 }
141
142                 protected virtual void Dispose (bool disposing)
143                 {
144                         if (!disposing || CairoDebug.Enabled)
145                                 CairoDebug.OnDisposed<Surface> (handle, disposing);
146
147                         if (!disposing|| handle == IntPtr.Zero)
148                                 return;
149
150                         NativeMethods.cairo_surface_destroy (handle);
151                         handle = IntPtr.Zero;
152                 }
153
154                 public Status Finish ()
155                 {
156                         NativeMethods.cairo_surface_finish (handle);
157                         return Status;
158                 }
159
160                 public void Flush ()
161                 {
162                         NativeMethods.cairo_surface_flush (handle);
163                 }
164
165                 public void MarkDirty ()
166                 {
167                         NativeMethods.cairo_surface_mark_dirty (Handle);
168                 }
169
170                 public void MarkDirty (Rectangle rectangle)
171                 {
172                         NativeMethods.cairo_surface_mark_dirty_rectangle (Handle, (int)rectangle.X, (int)rectangle.Y, (int)rectangle.Width, (int)rectangle.Height);
173                 }
174
175                 public IntPtr Handle {
176                         get {
177                                 return handle;
178                         }
179                 }
180
181                 public PointD DeviceOffset {
182                         get {
183                                 double x, y;
184                                 NativeMethods.cairo_surface_get_device_offset (handle, out x, out y);
185                                 return new PointD (x, y);
186                         }
187
188                         set {
189                                 NativeMethods.cairo_surface_set_device_offset (handle, value.X, value.Y);
190                         }
191                 }
192
193                 [Obsolete ("Use Dispose()")]
194                 public void Destroy()
195                 {
196                         Dispose ();
197                 }
198
199                 public void SetFallbackResolution (double x, double y)
200                 {
201                         NativeMethods.cairo_surface_set_fallback_resolution (handle, x, y);
202                 }
203
204                 public void WriteToPng (string filename)
205                 {
206                         NativeMethods.cairo_surface_write_to_png (handle, filename);
207                 }
208
209                 [Obsolete ("Use Handle instead.")]
210                 public IntPtr Pointer {
211                         get {
212                                 return handle;
213                         }
214                 }
215
216                 public Status Status {
217                         get { return NativeMethods.cairo_surface_status (handle); }
218                 }
219
220                 public Content Content {
221                         get { return NativeMethods.cairo_surface_get_content (handle); }
222                 }
223
224                 public SurfaceType SurfaceType {
225                         get { return NativeMethods.cairo_surface_get_type (handle); }
226                 }
227
228                 public uint ReferenceCount {
229                         get { return NativeMethods.cairo_surface_get_reference_count (handle); }
230                 }
231         }
232 }