* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / Mono.Cairo / Samples / gtk / sysdraw.cs
1 //
2 //
3 //      GDK-X11 interface
4 //
5
6 //
7 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Reflection;
31 using System.Runtime.InteropServices;
32 using Gtk;
33 using Cairo;
34
35 namespace Gdk 
36 {               
37         public class Context
38         {               
39                 //Use [DllImport("libgdk-win32-2.0-0.dll")] for  Win32 
40                 [DllImport("libgdk-x11-2.0.so")]
41                   internal static extern IntPtr gdk_x11_drawable_get_xdisplay (IntPtr raw);
42                 
43                 [DllImport("libgdk-x11-2.0.so")]
44                   internal static extern IntPtr gdk_x11_drawable_get_xid (IntPtr raw);
45                 
46                 [DllImport("libgdk-x11-2.0.so")]
47                   internal static extern IntPtr gdk_drawable_get_visual (IntPtr raw);
48                 
49                 [DllImport("libgdk-x11-2.0.so")]
50                   internal static extern IntPtr gdk_x11_visual_get_xvisual (IntPtr raw);
51                 
52                 [DllImport("libgdk-x11-2.0.so")]
53                   internal static extern IntPtr gdk_cairo_create (IntPtr raw);
54                 
55                 public static Cairo.Context CreateDrawable (Gdk.Drawable drawable)
56                 {
57                         IntPtr x_drawable = IntPtr.Zero;
58                         int x_off = 0, y_off = 0;                       
59                         
60                         int x, y, w, h, d;
61                         ((Gdk.Window)drawable).GetGeometry(out x, out y, out w, out h, out d);
62
63                         bool is_gdk_window = drawable is Gdk.Window;
64                         if (is_gdk_window)
65                                 ((Gdk.Window) drawable).GetInternalPaintInfo(out drawable, 
66                                                                              out x_off, out y_off);
67                         
68                         x_drawable = drawable.Handle;                   
69                         IntPtr visual = gdk_drawable_get_visual(x_drawable);
70                         
71                         IntPtr Xdisplay = gdk_x11_drawable_get_xdisplay(x_drawable);
72                         IntPtr Xvisual = gdk_x11_visual_get_xvisual(visual);
73                         IntPtr Xdrawable = gdk_x11_drawable_get_xid (x_drawable);
74                         
75                         Cairo.XlibSurface s = new Cairo.XlibSurface (Xdisplay,
76                                                                    Xdrawable,
77                                                                    Xvisual,
78                                                                    w, h);
79                         
80                         Cairo.Context g = new Cairo.Context (s);
81                         
82                         // this can be safely removed now, just keep it for a bit more
83                         //Cairo.Context g = new Cairo.Context (
84                         //                    gdk_cairo_create (x_drawable ));
85                         
86                         if (is_gdk_window)
87                                 g.Translate (-(double)x_off,-(double)y_off);
88                         return g;
89                 }
90         }
91 }