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