2005-04-10 Geoff Norton <gnorton@customerdna.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
31 namespace System.Drawing {
32         internal class Carbon {
33
34                 internal static CarbonContext GetCGContextForView (IntPtr hwnd) {
35                         IntPtr cgContext = IntPtr.Zero;
36                         // Grab the window we're in
37                         IntPtr window = Carbon.GetControlOwner (hwnd);
38                         // Get the port of the window
39                         IntPtr port = Carbon.GetWindowPort (window);
40                         // Create a CGContext ref
41                         Carbon.CreateCGContextForPort (port, ref cgContext);
42                         
43                         // Get the bounds of the window
44                         QRect wBounds = new QRect ();
45                         Carbon.GetWindowBounds (window, 32, ref wBounds);
46                         
47                         // Get the bounds of the view
48                         HIRect vBounds = new HIRect ();
49                         Carbon.HIViewGetBounds (hwnd, ref vBounds);
50                         
51                         // Convert the view local bounds to window coordinates
52                         Carbon.HIViewConvertRect (ref vBounds, hwnd, IntPtr.Zero);
53                         Carbon.CGContextTranslateCTM (cgContext, vBounds.origin.x, (wBounds.bottom-wBounds.top)-(vBounds.origin.y+vBounds.size.height));
54                         /* FIXME: Do we need this or is it inherintly clipped */
55                         HIRect rcClip = new HIRect ();
56                         rcClip.origin.x = 0;
57                         rcClip.origin.y = 0;
58                         rcClip.size.width = vBounds.size.width;
59                         rcClip.size.height = vBounds.size.height;
60                         Carbon.CGContextClipToRect (cgContext, rcClip);
61                         return new CarbonContext (cgContext, (int)vBounds.size.width, (int)vBounds.size.height);
62                 }
63                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
64                 internal static extern int HIViewGetBounds (IntPtr vHnd, ref HIRect r);
65                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
66                 internal static extern int HIViewConvertRect (ref HIRect r, IntPtr a, IntPtr b);
67
68                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
69                 internal static extern IntPtr GetControlOwner (IntPtr aView);
70
71                 [DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
72                 internal static extern int GetWindowBounds (IntPtr wHnd, uint reg, ref QRect rect);
73                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
74                 internal static extern IntPtr GetWindowPort (IntPtr hWnd);
75                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
76                 internal static extern int CGContextClipToRect (IntPtr cgContext, HIRect clip);
77                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
78                 internal static extern void CreateCGContextForPort (IntPtr port, ref IntPtr cgc);
79                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
80                 internal static extern void CGContextTranslateCTM (IntPtr cgc, double tx, double ty);
81                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
82                 internal static extern void CGContextScaleCTM (IntPtr cgc, double x, double y);
83                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
84                 internal static extern void CGContextFlush (IntPtr cgc);
85                 [DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
86                 internal static extern void CGContextSynchronize (IntPtr cgc);
87         }
88
89         internal struct CGSize {
90                 public float width;
91                 public float height;
92         }
93
94         internal struct CGPoint {
95                 public float x;
96                 public float y;
97         }
98
99         internal struct HIRect {
100                 public CGPoint origin;
101                 public CGSize size;
102         }
103
104         internal struct QRect
105         {
106                 public short top;
107                 public short left;
108                 public short bottom;
109                 public short right;
110         }
111
112         internal struct CarbonContext
113         {
114                 public IntPtr ctx;
115                 public int width;
116                 public int height;
117
118                 public CarbonContext (IntPtr ctx, int width, int height)
119                 {
120                         this.ctx = ctx;
121                         this.width = width;
122                         this.height = height;
123                 }
124         }
125 }