* Mono.Cairo/Cairo.cs: p/invoke the windows dll name
[mono.git] / mcs / class / Mono.Cairo / Mono.Cairo / Surface.cs
1 //
2 // Mono.Cairo.CairoSurfaceObject.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 SurfaceImage : Surface
41         {
42                 
43                 public SurfaceImage (string filename)
44                 {
45                         surface = CairoAPI.cairo_image_surface_create_from_png (filename);
46                         lock (surfaces.SyncRoot){
47                                 surfaces [surface] = this;
48                         }
49                         
50                         CairoAPI.cairo_surface_reference (surface);
51                 }
52                 
53                 public int Width {
54                         get { return CairoAPI.cairo_image_surface_get_width (surface); }
55                 }
56                 
57                 public int Height {
58                         get { return CairoAPI.cairo_image_surface_get_height (surface); }
59                 }
60                 
61         }
62    
63         public class Surface : IDisposable 
64         {                                               
65                 protected static Hashtable surfaces = new Hashtable ();
66                 internal IntPtr surface = IntPtr.Zero;
67
68                 protected Surface()
69                 {}
70                 
71                 private Surface (IntPtr ptr, bool owns)
72                 {
73                         surface = ptr;
74                         lock (surfaces.SyncRoot){
75                                 surfaces [ptr] = this;
76                         }
77                         if (!owns)
78                                 CairoAPI.cairo_surface_reference (ptr);
79                 }
80
81                 static internal Surface LookupExternalSurface (IntPtr p)
82                 {
83                         lock (surfaces.SyncRoot){
84                                 object o = surfaces [p];
85                                 if (o == null){
86                                         return new Surface (p, false);
87                                 }
88                                 return (Surface) o;
89                         }
90                 }               
91                 
92                 public static Cairo.Surface CreateForXlib (IntPtr display, IntPtr win,
93                                                            IntPtr visual, int w, 
94                                                            int h)
95                 {
96                         IntPtr p = CairoAPI.cairo_xlib_surface_create (display, win,
97                                                                    visual, w, h);
98                         if(p == IntPtr.Zero) System.Console.WriteLine("Failed creating surface");
99                         return new Cairo.Surface (p, false);
100                 }
101                 
102                 public static Cairo.Surface CreateForImage (
103                         string data, Cairo.Format format, int width, int height, int stride)
104                 {
105                         IntPtr p = CairoAPI.cairo_image_surface_create_for_data (
106                                 data, format, width, height, stride);
107                         
108                         return new Cairo.Surface (p, true);
109                 }
110
111                 public static Cairo.Surface CreateForImage (
112                         Cairo.Format format, int width, int height)
113                 {
114                         IntPtr p = CairoAPI.cairo_image_surface_create (
115                                 format, width, height);
116
117                         return new Cairo.Surface (p, true);
118                 }
119
120
121                 public static Cairo.Surface CreateSimilar (
122                         Cairo.Surface surface, Cairo.Format format, int width, int height)
123                 {
124                         IntPtr p = CairoAPI.cairo_surface_create_similar (
125                                 surface.Handle, format, width, height);
126
127                         return new Cairo.Surface (p, true);
128                 }
129
130                 public static Cairo.Surface CreateSimilarSolid (
131                         Cairo.Surface surface, Cairo.Format format,
132                         int width, int height, double red, double green, double blue, double alpha)
133                 {
134                         IntPtr p = CairoAPI.cairo_surface_create_similar_solid (
135                                 surface.Handle, format, width, height, red, green, blue, alpha);
136
137                         return new Cairo.Surface (p, true);
138                 }
139
140                 ~Surface ()
141                 {
142                         Dispose (false);
143                 }
144
145                 public void Show (Graphics gr, int width, int height) 
146                 {
147                         CairoAPI.cairo_set_source_surface (gr.Handle, surface, width, height);
148                         CairoAPI.cairo_paint (gr.Handle);
149                 }
150
151                 void IDisposable.Dispose ()
152                 {
153                         Dispose (true);
154                         GC.SuppressFinalize (this);
155                 }
156
157                 protected virtual void Dispose (bool disposing)
158                 {
159                         if (surface == (IntPtr) 0)
160                                 return;
161                         lock (surfaces.SyncRoot){
162                                 surfaces.Remove (surface);
163                         }
164                         CairoAPI.cairo_surface_destroy (surface);
165                         surface = (IntPtr) 0;
166                 }
167                 
168                 public Cairo.Status Finish ()
169                 {
170                         return CairoAPI.cairo_surface_finish (surface);
171                 }
172                 
173                 public IntPtr Handle {
174                         get { return surface; }
175                 }
176
177                 public PointD DeviceOffset {
178                         set { CairoAPI.cairo_surface_set_device_offset (surface,
179                                                                     value.X,
180                                                                     value.Y);
181                         }
182                 }
183                 
184                 public void XlibSetSize (int w, int h)
185                 {
186                         CairoAPI.cairo_xlib_surface_set_size (surface, w, h);
187                 }
188                 
189                 public void Destroy()
190                 {
191                         CairoAPI.cairo_surface_destroy (surface);
192                 }
193
194                 public void WriteToPng (string filename)
195                 {
196                         CairoAPI.cairo_surface_write_to_png (surface, filename);
197                 }
198                 
199                 public IntPtr Pointer {
200                         get { return surface; }
201                 }
202
203         }
204 }