Exemplo n.º 1
0
        void InitializeContext(bool viewFlipped)
        {
            Control.SaveState();

                        #if OSX
            // os x has different flipped states (depending on layers, etc), so compensate to make 0,0 at the top-left
            if (view != null)
            {
                // we have a view (drawing directly to the screen), so adjust to where it is
                Control.ClipToRect(view.ConvertRectToView(view.VisibleRect(), null));
                var pos = view.ConvertPointToView(CGPoint.Empty, null);
                if (!viewFlipped)
                {
                    pos.Y += view.Frame.Height;
                }
                Control.ConcatCTM(new CGAffineTransform(1, 0, 0, -1, (float)pos.X, (float)pos.Y));
            }
            else
            {
                // drawing to a bitmap or during a drawRect operation
                if (viewFlipped)
                {
                    Control.ConcatCTM(new CGAffineTransform(1, 0, 0, -1, 0, ViewHeight));
                }
            }

            phase = null;
                        #elif IOS
            if (viewFlipped)
            {
                // on ios, we flip the context if we're drawing on a bitmap otherwise we don't need to
                Control.ConcatCTM(new CGAffineTransform(1, 0, 0, -1, 0, ViewHeight));
            }
                        #endif

            ApplyAll();
        }
Exemplo n.º 2
0
        public SD.RectangleF TranslateView(SD.RectangleF rect, bool halfers = false)
        {
            if (halfers)
            {
                rect.X      += 0.5F;
                rect.Y      += 0.5F;
                rect.Width  -= 0.5F;
                rect.Height -= 0.5F;
            }

            if (view != null)
            {
                if (!Flipped)
                {
                    rect.Y = view.Bounds.Height - rect.Y - rect.Height;
                }
                rect = view.ConvertRectToView(rect, null);
            }
            else if (!Flipped)
            {
                rect.Y = this.height - rect.Y - rect.Height;
            }
            return(rect);
        }
Exemplo n.º 3
0
        public void Show(Popover.Position orientation, Widget referenceWidget, Rectangle positionRect, Widget child)
        {
            var refBackend = Toolkit.GetBackend(referenceWidget) as IWidgetBackend;

            NSView refView = (refBackend as EmbedNativeWidgetBackend)?.EmbeddedView;

            if (refView == null)
            {
                refView = (refBackend as ViewBackend)?.Widget;
            }

            if (refView == null)
            {
                if (referenceWidget.Surface.ToolkitEngine.Type == ToolkitType.Gtk)
                {
                    try {
                        refView = GtkQuartz.GetView(refBackend.NativeWidget);
                        var rLocation = refView.ConvertRectToView(refView.Frame, null).Location.ToXwtPoint();
                        if (referenceWidget.WindowBounds.Location != rLocation)
                        {
                            positionRect.X += referenceWidget.WindowBounds.Location.X - rLocation.X;
                            positionRect.Y += referenceWidget.WindowBounds.Location.Y - rLocation.Y;
                        }
                    } catch (Exception ex) {
                        throw new ArgumentException("Widget belongs to an unsupported Toolkit", nameof(referenceWidget), ex);
                    }
                }
                else if (referenceWidget.Surface.ToolkitEngine != ApplicationContext.Toolkit)
                {
                    throw new ArgumentException("Widget belongs to an unsupported Toolkit", nameof(referenceWidget));
                }
            }

            // If the rect is empty, the coordinates of the rect will be ignored.
            // Set the width and height, for the positioning to function correctly.
            if (Math.Abs(positionRect.Width) < double.Epsilon)
            {
                positionRect.Width = referenceWidget.Size.Width;
            }
            if (Math.Abs(positionRect.Height) < double.Epsilon)
            {
                positionRect.Height = referenceWidget.Size.Height;
            }

            DestroyPopover();

            popover = new NSAppearanceCustomizationPopover {
                Behavior = NSPopoverBehavior.Transient
            };
            controller = new FactoryViewController(this, child, popover)
            {
                BackgroundColor = backgroundColor
            };
            popover.ContentViewController = controller;
            popover.Delegate = controller;

            // if the reference has a custom appearance, use it for the popover
            if (refView.EffectiveAppearance.Name != NSAppearance.NameAqua)
            {
                controller.EffectiveAppearanceName = refView.EffectiveAppearance.Name;

                if (popover is INSAppearanceCustomization)
                {
                    ((INSAppearanceCustomization)popover).SetAppearance(refView.EffectiveAppearance);
                }
            }

            popover.Show(positionRect.ToCGRect(),
                         refView,
                         ToRectEdge(orientation));
        }
Exemplo n.º 4
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));
        }