2007-02-28 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Drawing / System.Drawing / carbonFunctions.cs
1 //
2 // System.Drawing.carbonFunctions.cs
3 //
4 // Authors:
5 //      Geoff Norton (gnorton@customerdna.com>
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.Runtime.InteropServices;
30 using System.Security;
31
32 namespace System.Drawing {
33
34         [SuppressUnmanagedCodeSecurity]
35         internal class Carbon {
36
37                 internal static CarbonContext GetCGContextForNSView (IntPtr hwnd) {
38                         IntPtr cgContext = IntPtr.Zero;
39
40                         cgContext = objc_msgSend (objc_msgSend (objc_getClass ("NSGraphicsContext"), sel_registerName ("currentContext")), sel_registerName ("graphicsPort"));
41                         HIRect rect = new HIRect ();
42                         objc_msgSend_stret (ref rect, hwnd, sel_registerName ("bounds"));
43                         return new CarbonContext (cgContext, (int)rect.size.width, (int)rect.size.height);
44                 }
45                 internal static CarbonContext GetCGContextForView (IntPtr hwnd) {
46                         IntPtr cgContext = IntPtr.Zero;
47                         // Grab the window we're in
48                         IntPtr window = Carbon.GetControlOwner (hwnd);
49                         // Get the port of the window
50                         IntPtr port = Carbon.GetWindowPort (window);
51                         // Create a CGContext ref
52                         Carbon.CreateCGContextForPort (port, ref cgContext);
53                         
54                         // Get the bounds of the window
55                         QRect wBounds = new QRect ();
56                         Carbon.GetWindowBounds (window, 32, ref wBounds);
57                         
58                         // Get the bounds of the view
59                         HIRect vBounds = new HIRect ();
60                         Carbon.HIViewGetBounds (hwnd, ref vBounds);
61                         
62                         // Convert the view local bounds to window coordinates
63                         Carbon.HIViewConvertRect (ref vBounds, hwnd, IntPtr.Zero);
64                         Carbon.CGContextTranslateCTM (cgContext, vBounds.origin.x, (wBounds.bottom-wBounds.top)-(vBounds.origin.y+vBounds.size.height));
65                         /* FIXME: Do we need this or is it inherintly clipped */
66                         HIRect rcClip = new HIRect ();
67                         rcClip.origin.x = 0;
68                         rcClip.origin.y = 0;
69                         rcClip.size.width = vBounds.size.width;
70                         rcClip.size.height = vBounds.size.height;
71                         Carbon.CGContextClipToRect (cgContext, rcClip);
72                         return new CarbonContext (cgContext, (int)vBounds.size.width, (int)vBounds.size.height);
73                 }
74                 #region Cocoa Methods
75                 [DllImport("libobjc.dylib")]
76                 public static extern IntPtr objc_getClass(string className); 
77                 [DllImport("libobjc.dylib")]
78                 public static extern IntPtr objc_msgSend(IntPtr basePtr, IntPtr selector, string argument);  
79                 [DllImport("libobjc.dylib")]
80                 public static extern IntPtr objc_msgSend(IntPtr basePtr, IntPtr selector);        
81                 [DllImport("libobjc.dylib")]
82                 public static extern void objc_msgSend_stret(ref HIRect arect, IntPtr basePtr, IntPtr selector);        
83                 [DllImport("libobjc.dylib")]
84                 public static extern IntPtr sel_registerName(string selectorName);         
85                 #endregion
86
87                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
88                 internal static extern int HIViewGetBounds (IntPtr vHnd, ref HIRect r);
89                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
90                 internal static extern int HIViewConvertRect (ref HIRect r, IntPtr a, IntPtr b);
91
92                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
93                 internal static extern IntPtr GetControlOwner (IntPtr aView);
94
95                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
96                 internal static extern int GetWindowBounds (IntPtr wHnd, uint reg, ref QRect rect);
97                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
98                 internal static extern IntPtr GetWindowPort (IntPtr hWnd);
99                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
100                 internal static extern int CGContextClipToRect (IntPtr cgContext, HIRect clip);
101                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
102                 internal static extern void CreateCGContextForPort (IntPtr port, ref IntPtr cgc);
103                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
104                 internal static extern void CGContextTranslateCTM (IntPtr cgc, double tx, double ty);
105                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
106                 internal static extern void CGContextScaleCTM (IntPtr cgc, double x, double y);
107                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
108                 internal static extern void CGContextFlush (IntPtr cgc);
109                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
110                 internal static extern void CGContextSynchronize (IntPtr cgc);
111         }
112
113         internal struct CGSize {
114                 public float width;
115                 public float height;
116         }
117
118         internal struct CGPoint {
119                 public float x;
120                 public float y;
121         }
122
123         internal struct HIRect {
124                 public CGPoint origin;
125                 public CGSize size;
126         }
127
128         internal struct QRect
129         {
130                 public short top;
131                 public short left;
132                 public short bottom;
133                 public short right;
134         }
135
136         internal struct CarbonContext
137         {
138                 public IntPtr ctx;
139                 public int width;
140                 public int height;
141
142                 public CarbonContext (IntPtr ctx, int width, int height)
143                 {
144                         this.ctx = ctx;
145                         this.width = width;
146                         this.height = height;
147                 }
148         }
149 }