[Mono-winforms-list] More System.Graphics changes

kangaroo grompf@sublimeintervention.com
Thu, 9 Dec 2004 16:10:25 -0500


--Apple-Mail-16--234625588
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	format=flowed

I've found and implemented a way to support FromHwnd for the Quartz 
backend; however it involves some apple-only pinvoking in 
System.Graphics;  my question is; ok to commit as is; or should I split 
the internal structs and DllImports off into something like 
quartzFunctions.cs?

-kangaroo

--Apple-Mail-16--234625588
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream;
	x-unix-mode=0644;
	name="graph.diff"
Content-Disposition: attachment;
	filename=graph.diff

Index: System.Drawing/Graphics.cs
===================================================================
--- System.Drawing/Graphics.cs	(revision 37462)
+++ System.Drawing/Graphics.cs	(working copy)
@@ -1228,7 +1228,9 @@
 		public void Flush (FlushIntention intention)
 		{
 			Status status = GDIPlus.GdipFlush (nativeObject, intention);
-                        GDIPlus.CheckStatus (status);                     
+                        GDIPlus.CheckStatus (status);                    
+			if (use_quartz_drawable)
+				CGContextFlush (display);
 		}
 
 		[EditorBrowsable (EditorBrowsableState.Advanced)]		
@@ -1260,9 +1262,25 @@
 			IntPtr graphics;
 
 			if (use_quartz_drawable) {
-				QuartzContext qc = (QuartzContext) Marshal.PtrToStructure (hwnd, typeof (QuartzContext));
-				GDIPlus.GdipCreateFromQuartz_macosx (qc.cgContext, qc.width,qc.height, out graphics);
+				IntPtr port = GetWindowPort (GetControlOwner (hwnd));
+				IntPtr cgContext = IntPtr.Zero;
+				CreateCGContextForPort (port, ref cgContext);
+				QRect wBounds = new QRect ();
+				GetWindowBounds (GetControlOwner (hwnd), 32, ref wBounds);
+				HIRect vBounds = new HIRect ();
+				HIViewGetBounds (hwnd, ref vBounds);
+				HIViewConvertRect (ref vBounds, hwnd, IntPtr.Zero);
+				CGContextTranslateCTM (cgContext, vBounds.origin.x, (wBounds.bottom-wBounds.top)-(vBounds.origin.y+vBounds.size.height));
+				HIRect rcClip = new HIRect ();
+				rcClip.origin.x = 0;
+				rcClip.origin.y = 0;
+				rcClip.size.width = vBounds.size.width;
+				rcClip.size.height = vBounds.size.height;
+Console.WriteLine ("Clipping to {0}x{1}", rcClip.size.width, rcClip.size.height);
+				CGContextClipToRect (cgContext, rcClip);
+				GDIPlus.GdipCreateFromQuartz_macosx (cgContext, (int)vBounds.size.width, (int)vBounds.size.height, out graphics);
 				
+				display = cgContext;
 				return new Graphics (graphics);
 			}
 			if (use_x_drawable) {
@@ -1986,12 +2004,51 @@
 			}
 		}
 
-		internal struct QuartzContext
-		{
-			public IntPtr cgContext;
-			public int width;
-			public int height;
-		}
+		[DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
+                internal static extern int HIViewGetBounds (IntPtr vHnd, ref HIRect r);
+		[DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
+                internal static extern int HIViewConvertRect (ref HIRect r, IntPtr a, IntPtr b);
+
+		[DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
+		internal static extern IntPtr GetControlOwner (IntPtr aView);
+
+		[DllImport("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
+                internal static extern int GetWindowBounds (IntPtr wHnd, uint reg, ref QRect rect);
+		[DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
+		internal static extern IntPtr GetWindowPort (IntPtr hWnd);
+		[DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
+		internal static extern int CGContextClipToRect (IntPtr cgContext, HIRect clip);
+		[DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
+		internal static extern void CreateCGContextForPort (IntPtr port, ref IntPtr cgc);
+		[DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
+		internal static extern void CGContextTranslateCTM (IntPtr cgc, double tx, double ty);
+		[DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
+		internal static extern void CGContextScaleCTM (IntPtr cgc, double x, double y);
+		[DllImport ("/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon")]
+		internal static extern void CGContextFlush (IntPtr cgc);
 	}
+
+	internal struct CGSize {
+                public float width;
+                public float height;
+        }
+
+	internal struct CGPoint {
+                public float x;
+                public float y;
+        }
+
+        internal struct HIRect {
+                public CGPoint origin;
+                public CGSize size;
+        }
+	
+	internal struct QRect
+        {
+                public short top;
+                public short left;
+                public short bottom;
+                public short right;
+        }
 }
 

--Apple-Mail-16--234625588--