Added tests for Task.WhenAll w/ empty list
[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                 protected static Hashtable surfaces = new Hashtable ();
44                 internal IntPtr surface = IntPtr.Zero;
45
46                 protected Surface()
47                 {
48                 }
49                 
50                 protected Surface (IntPtr ptr, bool owns)
51                 {
52                         surface = ptr;
53                         lock (surfaces.SyncRoot){
54                                 surfaces [ptr] = this;
55                         }
56                         if (!owns)
57                                 NativeMethods.cairo_surface_reference (ptr);
58                 }
59
60                 static internal Surface LookupExternalSurface (IntPtr p)
61                 {
62                         lock (surfaces.SyncRoot){
63                                 object o = surfaces [p];
64                                 if (o == null){
65                                         return new Surface (p, false);
66                                 }
67                                 return (Surface) o;
68                         }
69                 }               
70
71                 static internal Surface LookupSurface (IntPtr surface)
72                 {
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);
85
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);
94
95                         default:
96                                 return Surface.LookupExternalSurface (surface);
97                         }
98                 }
99                 
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)
103                 {
104                         IntPtr p = NativeMethods.cairo_image_surface_create_for_data (
105                                 data, format, width, height, stride);
106                         
107                         return new Cairo.Surface (p, true);
108                 }
109
110                 [Obsolete ("Use an ImageSurface constructor instead.")]
111                 public static Cairo.Surface CreateForImage (
112                         Cairo.Format format, int width, int height)
113                 {
114                         IntPtr p = NativeMethods.cairo_image_surface_create (
115                                 format, width, height);
116
117                         return new Cairo.Surface (p, true);
118                 }
119
120
121                 public Cairo.Surface CreateSimilar (
122                         Cairo.Content content, int width, int height)
123                 {
124                         IntPtr p = NativeMethods.cairo_surface_create_similar (
125                                 this.Handle, content, width, height);
126
127                         return new Cairo.Surface (p, true);
128                 }
129
130                 ~Surface ()
131                 {
132                         Dispose (false);
133                 }
134
135                 //[Obsolete ("Use Context.SetSource() followed by Context.Paint()")]
136                 public void Show (Context gr, double x, double y) 
137                 {
138                         NativeMethods.cairo_set_source_surface (gr.Handle, surface, x, y);
139                         NativeMethods.cairo_paint (gr.Handle);
140                 }
141
142                 public void Dispose ()
143                 {
144                         Dispose (true);
145                         GC.SuppressFinalize (this);
146                 }
147
148                 protected virtual void Dispose (bool disposing)
149                 {
150                         if (surface == IntPtr.Zero)
151                                 return;
152                         
153                         lock (surfaces.SyncRoot)
154                                 surfaces.Remove (surface);
155
156                         NativeMethods.cairo_surface_destroy (surface);
157                         surface = IntPtr.Zero;
158                 }
159                 
160                 public Status Finish ()
161                 {
162                         NativeMethods.cairo_surface_finish (surface);
163                         return Status;
164                 }
165                 
166                 public void Flush ()
167                 {
168                         NativeMethods.cairo_surface_flush (surface);
169                 }
170                 
171                 public void MarkDirty ()
172                 {
173                         NativeMethods.cairo_surface_mark_dirty (Handle);
174                 }
175                 
176                 public void MarkDirty (Rectangle rectangle)
177                 {
178                         NativeMethods.cairo_surface_mark_dirty_rectangle (Handle, (int)rectangle.X, (int)rectangle.Y, (int)rectangle.Width, (int)rectangle.Height);
179                 }
180                 
181                 public IntPtr Handle {
182                         get {
183                                 return surface;
184                         }
185                 }
186
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);
192                         }
193
194                         set {
195                                 NativeMethods.cairo_surface_set_device_offset (surface, value.X, value.Y);
196                         }
197                 }
198                 
199                 public void Destroy()
200                 {
201                         Dispose (true);
202                 }
203
204                 public void SetFallbackResolution (double x, double y)
205                 {
206                         NativeMethods.cairo_surface_set_fallback_resolution (surface, x, y);
207                 }
208
209                 public void WriteToPng (string filename)
210                 {
211                         NativeMethods.cairo_surface_write_to_png (surface, filename);
212                 }
213                 
214                 [Obsolete ("Use Handle instead.")]
215                 public IntPtr Pointer {
216                         get {
217                                 return surface;
218                         }
219                 }
220                 
221                 public Status Status {
222                         get { return NativeMethods.cairo_surface_status (surface); }
223                 }
224
225                 public Content Content {
226                         get { return NativeMethods.cairo_surface_get_content (surface); }
227                 }
228
229                 public SurfaceType SurfaceType {
230                         get { return NativeMethods.cairo_surface_get_type (surface); }
231                 }
232
233                 public uint ReferenceCount {
234                         get { return NativeMethods.cairo_surface_get_reference_count (surface); }
235                 }
236         }
237 }