internal static CocoaContext GetCGContextForNSView(IntPtr handle) { if (handle == IntPtr.Zero) { return(null); } IntPtr focusView = objc_msgSend(objc_getClass("NSView"), sel_registerName("focusView")); IntPtr focusHandle = IntPtr.Zero; if (focusView != handle) { if (!bool_objc_msgSend(handle, sel_registerName("lockFocusIfCanDraw"))) { return(null); } focusHandle = handle; } IntPtr windowHandle = objc_msgSend(handle, sel_registerName("window")); IntPtr graphicsContext = objc_msgSend(windowHandle, sel_registerName("graphicsContext")); IntPtr ctx = objc_msgSend(graphicsContext, sel_registerName("graphicsPort")); bool isFlipped = bool_objc_msgSend(handle, sel_registerName("isFlipped")); Size size; CGContextSaveGState(ctx); if (IntPtr.Size == 4) { CGRect32 bounds = new CGRect32(); objc_msgSend_stret(ref bounds, handle, sel_registerName("bounds")); if (isFlipped) { CGContextTranslateCTM32(ctx, bounds.origin.x, bounds.size.height); CGContextScaleCTM32(ctx, 1.0f, -1.0f); } size = new Size((int)bounds.size.width, (int)bounds.size.height); } else { CGRect64 bounds = new CGRect64(); objc_msgSend_stret(ref bounds, handle, sel_registerName("bounds")); if (isFlipped) { CGContextTranslateCTM64(ctx, bounds.origin.x, bounds.size.height); CGContextScaleCTM64(ctx, 1.0f, -1.0f); } size = new Size((int)bounds.size.width, (int)bounds.size.height); } return(new CocoaContext(focusHandle, ctx, size.Width, size.Height)); }
public static extern void objc_msgSend_stret(ref CGRect64 arect, IntPtr basePtr, IntPtr selector);