Пример #1
0
        internal static INativeContex GetCGContextForView(IntPtr handle)
        {
//			IntPtr context = IntPtr.Zero;
            NSView focusWindow = null;
            // JV - je to OK??
            //NSObject whoWrapper = NSObject.Lookup (handle);
            NSObject whoWrapper    = MonoMac.ObjCRuntime.Runtime.GetNSObject(handle);
            NSView   viewWrapper   = whoWrapper as NSView;
            NSWindow windowWrapper = null;

            if (null == viewWrapper)
            {
                windowWrapper = whoWrapper as NSWindow;
            }
            else
            {
                windowWrapper = viewWrapper.Window;
            }

            IntPtr            window = windowWrapper.Handle;
            NSGraphicsContext gcw    = null;

            if (handle == IntPtr.Zero || window == IntPtr.Zero)
            {
                // FIXME: Can we actually get a CGContextRef for the desktop?  this makes context IntPtr.Zero
                gcw = NSApplication.SharedApplication.Context;
//				context = gcw.graphicsPort();

                var desktop_bounds = NSRect.Empty;
                // NSScreen.mainScreen () returns the screen the the user is currently interacting with.
                // To get the screen identified by CGMainDisplayID (), get the 0th element of this array.
                NSScreen[] screens = NSScreen.Screens;

                if (null != screens && 0 < screens.Length)
                {
                    NSScreen screenWrap = screens[0];
                    desktop_bounds = screenWrap.Frame;
                }

                return(new CocoaContext(focusWindow, gcw, (int)desktop_bounds.Width,
                                        (int)desktop_bounds.Height));
            }

            if (null != viewWrapper && viewWrapper != NSView.FocusView())
            {
                if (!viewWrapper.LockFocusIfCanDraw())
                {
                    return(null);
                }

                focusWindow = viewWrapper;
            }

            gcw = windowWrapper.GraphicsContext;
            gcw.SaveGraphicsState();
//			context = gcw.graphicsPort();

//			NSRect winRect = windowWrapper.frame();
//			QDRect window_bounds = new QDRect (winRect.Top, winRect.Left, winRect.Bottom, winRect.Right);

            var vuRect = windowWrapper.Frame;

            if (null != viewWrapper)
            {
                vuRect = viewWrapper.Bounds;
                vuRect = viewWrapper.ConvertRectToView(vuRect, null);
            }
//			Rect view_bounds = new Rect (vuRect.origin.x, vuRect.origin.y, vuRect.size.width, vuRect.size.height);

            if (vuRect.Height < 0)
            {
                vuRect.Height = 0;
            }
            if (vuRect.Width < 0)
            {
                vuRect.Width = 0;
            }

//ASSUMPTION! lockFocus did the translating and clipping.
//ASSUMPTION! The NSView isFlipped.
//			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);
//
//
//			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 CocoaContext(focusWindow, gcw, (int)vuRect.Width, (int)vuRect.Height));
        }