internal static CarbonContext GetCGContextForView(IntPtr handle) { IntPtr context = IntPtr.Zero; IntPtr port = IntPtr.Zero; IntPtr window = IntPtr.Zero; window = GetControlOwner(handle); if (handle == IntPtr.Zero || window == IntPtr.Zero) { // FIXME: Can we actually get a CGContextRef for the desktop? this makes context IntPtr.Zero port = GetQDGlobalsThePort(); CreateCGContextForPort(port, ref context); Rect desktop_bounds = CGDisplayBounds(CGMainDisplayID()); return(new CarbonContext(port, context, (int)desktop_bounds.size.width, (int)desktop_bounds.size.height)); } QDRect window_bounds = new QDRect(); Rect view_bounds = new Rect(); port = GetWindowPort(window); context = GetContext(port); GetWindowBounds(window, 32, ref window_bounds); HIViewGetBounds(handle, ref view_bounds); HIViewConvertRect(ref view_bounds, handle, IntPtr.Zero); if (view_bounds.size.height < 0) { view_bounds.size.height = 0; } if (view_bounds.size.width < 0) { view_bounds.size.width = 0; } CGContextTranslateCTM(context, view_bounds.origin.x, (window_bounds.bottom - window_bounds.top) - (view_bounds.origin.y + view_bounds.size.height)); // Create the original rect path and clip to it Rect rc_clip = new Rect(0, 0, view_bounds.size.width, view_bounds.size.height); CGContextSaveGState(context); Rectangle [] clip_rectangles = (Rectangle [])hwnd_delegate.DynamicInvoke(new object [] { handle }); if (clip_rectangles != null && clip_rectangles.Length > 0) { int length = clip_rectangles.Length; CGContextBeginPath(context); CGContextAddRect(context, rc_clip); for (int i = 0; i < length; i++) { CGContextAddRect(context, new Rect(clip_rectangles [i].X, view_bounds.size.height - clip_rectangles [i].Y - clip_rectangles [i].Height, clip_rectangles [i].Width, clip_rectangles [i].Height)); } CGContextClosePath(context); CGContextEOClip(context); #if DEBUG_CLIPPING if (clip_rectangles.Length >= debug_threshold) { CGContextSetRGBFillColor(context, red, green, blue, 0.5f); CGContextFillRect(context, rc_clip); CGContextFlush(context); System.Threading.Thread.Sleep(500); if (red == 1.0f) { red = 0.0f; blue = 1.0f; } else if (blue == 1.0f) { blue = 0.0f; green = 1.0f; } else if (green == 1.0f) { green = 0.0f; red = 1.0f; } } #endif } else { CGContextBeginPath(context); CGContextAddRect(context, rc_clip); CGContextClosePath(context); CGContextClip(context); } return(new CarbonContext(port, context, (int)view_bounds.size.width, (int)view_bounds.size.height)); }
internal static extern int GetWindowBounds(IntPtr wHnd, uint reg, ref QDRect rect);