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