2006-06-23 John Luke <john.luke@gmail.com>
[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 //
8 // (C) Ximian Inc, 2003.
9 // (C) Novell, Inc. 2003.
10 //
11 // This is an OO wrapper API for the Cairo API
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
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:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
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.
33 //
34
35 using System;
36 using System.Collections;
37
38 namespace Cairo {
39
40         public class Surface : IDisposable 
41         {                                               
42                 protected static Hashtable surfaces = new Hashtable ();
43                 internal IntPtr surface = IntPtr.Zero;
44
45                 protected Surface()
46                 {
47                 }
48                 
49                 protected Surface (IntPtr ptr, bool owns)
50                 {
51                         surface = ptr;
52                         lock (surfaces.SyncRoot){
53                                 surfaces [ptr] = this;
54                         }
55                         if (!owns)
56                                 CairoAPI.cairo_surface_reference (ptr);
57                 }
58
59                 static internal Surface LookupExternalSurface (IntPtr p)
60                 {
61                         lock (surfaces.SyncRoot){
62                                 object o = surfaces [p];
63                                 if (o == null){
64                                         return new Surface (p, false);
65                                 }
66                                 return (Surface) o;
67                         }
68                 }               
69                 
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)
73                 {
74                         IntPtr p = CairoAPI.cairo_image_surface_create_for_data (
75                                 data, format, width, height, stride);
76                         
77                         return new Cairo.Surface (p, true);
78                 }
79
80                 [Obsolete ("Use an ImageSurface constructor instead.")]
81                 public static Cairo.Surface CreateForImage (
82                         Cairo.Format format, int width, int height)
83                 {
84                         IntPtr p = CairoAPI.cairo_image_surface_create (
85                                 format, width, height);
86
87                         return new Cairo.Surface (p, true);
88                 }
89
90
91                 public Cairo.Surface CreateSimilar (
92                         Cairo.Content content, int width, int height)
93                 {
94                         IntPtr p = CairoAPI.cairo_surface_create_similar (
95                                 this.Handle, content, width, height);
96
97                         return new Cairo.Surface (p, true);
98                 }
99
100                 ~Surface ()
101                 {
102                         Dispose (false);
103                 }
104
105                 public void Show (Context gr, int width, int height) 
106                 {
107                         CairoAPI.cairo_set_source_surface (gr.Handle, surface, width, height);
108                         CairoAPI.cairo_paint (gr.Handle);
109                 }
110
111                 void IDisposable.Dispose ()
112                 {
113                         Dispose (true);
114                         GC.SuppressFinalize (this);
115                 }
116
117                 protected virtual void Dispose (bool disposing)
118                 {
119                         if (surface == (IntPtr) 0)
120                                 return;
121                         
122                         lock (surfaces.SyncRoot)
123                                 surfaces.Remove (surface);
124
125                         CairoAPI.cairo_surface_destroy (surface);
126                         surface = (IntPtr) 0;
127                 }
128                 
129                 public Status Finish ()
130                 {
131                         CairoAPI.cairo_surface_finish (surface);
132                         return Status;
133                 }
134                 
135                 public void Flush ()
136                 {
137                         CairoAPI.cairo_surface_flush (surface);
138                 }
139                 
140                 public void MarkDirty ()
141                 {
142                         CairoAPI.cairo_surface_mark_dirty (Handle);
143                 }
144                 
145                 public void MarkDirty (Rectangle rectangle)
146                 {
147                         CairoAPI.cairo_surface_mark_dirty_rectangle (Handle, (int)rectangle.X, (int)rectangle.Y, (int)rectangle.Width, (int)rectangle.Height);
148                 }
149                 
150                 public IntPtr Handle {
151                         get {
152                                 return surface;
153                         }
154                 }
155
156                 public PointD DeviceOffset {
157                         set {
158                                 CairoAPI.cairo_surface_set_device_offset (surface, value.X, value.Y);
159                         }
160                 }
161                 
162                 public void Destroy()
163                 {
164                         CairoAPI.cairo_surface_destroy (surface);
165                 }
166
167                 public void WriteToPng (string filename)
168                 {
169                         CairoAPI.cairo_surface_write_to_png (surface, filename);
170                 }
171                 
172                 [Obsolete ("Use Handle instead.")]
173                 public IntPtr Pointer {
174                         get {
175                                 return surface;
176                         }
177                 }
178                 
179                 public Status Status {
180                         get { return CairoAPI.cairo_surface_status (surface); }
181                 }
182
183         }
184 }