Beispiel #1
0
 public CocoaContext(NSView focusWindow, NSGraphicsContext ctx, int width, int height)
 {
     this.focusWindow = focusWindow;
     this.ctx         = ctx;
     this.width       = width;
     this.height      = height;
 }
Beispiel #2
0
        public GraphicsHandler(NSView view)
        {
            this.view       = view;
            graphicsContext = NSGraphicsContext.FromWindow(view.Window);
            graphicsContext = graphicsContext.IsFlipped ? graphicsContext : NSGraphicsContext.FromGraphicsPort(graphicsContext.GraphicsPortHandle, true);
            disposeContext  = true;
            Control         = graphicsContext.GraphicsPort;

            view.PostsFrameChangedNotifications = true;
            AddObserver(NSView.FrameChangedNotification, FrameDidChange, view);

            // if control is in a scrollview, we need to trap when it's scrolled as well
            var parent = view.Superview;

            while (parent != null)
            {
                var scroll = parent as NSScrollView;
                if (scroll != null)
                {
                    scroll.ContentView.PostsBoundsChangedNotifications = true;
                    AddObserver(NSView.BoundsChangedNotification, FrameDidChange, scroll.ContentView);
                }
                parent = parent.Superview;
            }

            SetDefaults();
            InitializeContext(view.IsFlipped);
        }
        private NSImage CreateImageWithColor(string colorValue)
        {
            NSGraphicsContext.GlobalSaveGraphicsState();
            CGSize  size      = new CGSize(12, 12);
            NSImage tintImage = new NSImage(size);

            tintImage.LockFocus();

            float        cornerRadius = 5f;
            CGRect       rect         = new CGRect(0, 0, 10, 10);
            NSBezierPath path         = NSBezierPath.FromRoundedRect(rect, cornerRadius, cornerRadius);

            if (string.IsNullOrEmpty(colorValue))
            {
                NSColor.Grid.Set();
                path.Stroke();
            }
            else
            {
                Utility.ColorWithHexColorValue(colorValue, 1.0f).SetFill();
                path.Fill();
            }

            tintImage.UnlockFocus();
            CGContext context = NSGraphicsContext.CurrentContext.CGContext;

            return(tintImage);
        }
Beispiel #4
0
        public void DrawLayer(CALayer layer, CoreGraphics.CGContext context)
        {
            NSGraphicsContext.GlobalSaveGraphicsState();
            NSGraphicsContext graphicsContext = NSGraphicsContext.FromGraphicsPort(context, true);

            NSGraphicsContext.CurrentContext = graphicsContext;

            NSBezierPath path = new NSBezierPath();
            //ベジェ曲線
            var x1 = this.Frame.Left;
            var y1 = this.Frame.Top;
            var x2 = this.Frame.Right;
            var y2 = this.Frame.Bottom;

            path.MoveTo(new CoreGraphics.CGPoint(x1, y1));
            path.CurveTo(new CoreGraphics.CGPoint(x2, y1),
                         new CoreGraphics.CGPoint(x1, y2),
                         new CoreGraphics.CGPoint(x2, y2));
            //背景は白
            NSColor.White.Set();
            path.Fill();
            //線は青
            NSColor.Blue.Set();
            //線の太さ
            path.LineWidth = 2;
            path.Stroke();

            NSGraphicsContext.GlobalRestoreGraphicsState();
        }
Beispiel #5
0
        public static void CopyPath(CGPath cgpath)
        {
            var pdfpath = Path.GetTempPath() + ".pdf";
            var context = new CGContextPDF(NSUrl.FromFilename(pdfpath));

            CGRect rc = new CGRect(0, 0, 500, 500);

            context.BeginPage(rc);

            using (var gc = NSGraphicsContext.FromGraphicsPort(context, false))
            {
                var ctx = gc.CGContext;
                //ctx.SetStrokeColor(new CGColor(0, 0, 100));
                //ctx.SetLineWidth(2);
                //ctx.SetFillColor(new CGColor(100, 0, 0));

                ctx.AddPath(cgpath);
                ctx.DrawPath(CGPathDrawingMode.FillStroke);
            }

            context.EndPage();
            context.Close();

            var data = NSData.FromFile(pdfpath);

            NSPasteboard.GeneralPasteboard.ClearContents();
            NSPasteboard.GeneralPasteboard.SetDataForType(data, NSPasteboard.NSPdfType);
        }
Beispiel #6
0
        public override void DrawRow(nint row, CGRect clipRect)
        {
            if (row >= RowCount)
            {
                return;
            }

            base.DrawRow(row, clipRect);

            NSGraphicsContext.GlobalSaveGraphicsState();
            NSGraphicsContext.CurrentContext.ShouldAntialias = false;

            CGRect rectRow    = RectForRow(row);
            CGRect rectColumn = RectForColumn(0);
            CGRect rect       = Frame;

            CGPoint start = new CGPoint(rectColumn.Left, rectRow.Top);
            CGPoint end   = new CGPoint(rectRow.Right, rectRow.Top);

            var linePath = new NSBezierPath();

            GridColor.Set();
            linePath.MoveTo(start);
            linePath.LineTo(end);
            linePath.ClosePath();
            linePath.Stroke();

            NSGraphicsContext.GlobalRestoreGraphicsState();
        }
Beispiel #7
0
        public void CreateFromImage(Bitmap image)
        {
            var handler = image.Handler as BitmapHandler;

            SourceImage = image;
#if OSX
            var rep = handler.Control.Representations().OfType <NSBitmapImageRep>().FirstOrDefault();
            if (rep.BitsPerPixel != 32)
            {
                // CoreGraphics only supports drawing to 32bpp, create a new 32-bpp image and copy back when disposed or flushed.
                DrawingImage = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppRgb);
                handler      = DrawingImage.Handler as BitmapHandler;
                rep          = handler.Control.Representations().OfType <NSBitmapImageRep>().FirstOrDefault();
            }
            graphicsContext = NSGraphicsContext.FromBitmap(rep);

            graphicsContext = graphicsContext.IsFlipped ? graphicsContext : NSGraphicsContext.FromGraphicsPort(graphicsContext.GraphicsPortHandle, true);
            disposeContext  = true;
            Control         = graphicsContext.GraphicsPort;
            PointsPerPixel  = (float)(rep.PixelsWide / handler.Control.Size.Width);
#elif IOS
            var cgimage = handler.Control.CGImage;
            Control        = new CGBitmapContext(handler.Data.MutableBytes, cgimage.Width, cgimage.Height, cgimage.BitsPerComponent, cgimage.BytesPerRow, cgimage.ColorSpace, cgimage.BitmapInfo);
            PointsPerPixel = (float)(cgimage.Width / handler.Control.Size.Width);
#endif

            height = image.Size.Height;
            SetDefaults();
            InitializeContext(true);
            if (DrawingImage != null && SourceImage != null)
            {
                // draw source image onto context, when source is incompatible for CoreGraphics drawing.
                DrawImage(SourceImage, 0, 0);
            }
        }
Beispiel #8
0
            public CGImage AsCGImage(IntPtr proposedDestRectPtr, NSGraphicsContext context, NSDictionary hints)
            {
                var proposedDestRect = Marshal.PtrToStructure <CGRect>(proposedDestRectPtr);
                var result           = Rep.AsCGImage(ref proposedDestRect, context, hints);

                Marshal.StructureToPtr(proposedDestRect, proposedDestRectPtr, false);
                return(result);
            }
Beispiel #9
0
        public static CGContext CGContext(this NSGraphicsContext context)
        {
#if MONOMAC
            return(context.GraphicsPort);
#elif XAMARINMAC
            return(context.CGContext);
#endif
        }
Beispiel #10
0
        void EndDrawing()
        {
            Control.RestoreState();
#if OSX
            NSGraphicsContext.GlobalRestoreGraphicsState();
#elif IOS
            UIGraphics.PopContext();
#endif
        }
Beispiel #11
0
 public GraphicsHandler(NSView view, NSGraphicsContext graphicsContext, float height, bool flipped)
 {
     DisplayView          = view;
     this.height          = height;
     this.graphicsContext = flipped != graphicsContext.IsFlipped ? graphicsContext : NSGraphicsContext.FromGraphicsPort(graphicsContext.GraphicsPortHandle, true);
     Control = this.graphicsContext.GraphicsPort;
     SetDefaults();
     InitializeContext(!flipped);
 }
Beispiel #12
0
        void StartDrawing()
        {
#if OSX
            NSGraphicsContext.GlobalSaveGraphicsState();
            NSGraphicsContext.CurrentContext = graphicsContext;
#elif IOS
            UIGraphics.PushContext(Control);
#endif
            Control.SaveState();
        }
Beispiel #13
0
        public override void DrawRect(CGRect dirtyRect)
        {
            NSGraphicsContext.GlobalSaveGraphicsState();
            NSColor.White.Set();
            NSGraphics.RectFill(dirtyRect);

            Subviews[1].Hidden  = !this.IsSelected;
            Subviews [2].Hidden = !this.IsSelected;
            NSGraphicsContext.GlobalRestoreGraphicsState();
        }
Beispiel #14
0
        public static void EndImageContext()
        {
            // Return to previous context
            if (PreviousContext != null) {
                NSGraphicsContext.CurrentContext = PreviousContext;
            }

            // Release memory
            Context = null;
            PreviousContext = null;
            ColorSpace = null;
            ImageSize = CGSize.Empty;
        }
Beispiel #15
0
            public override void DrawRect(System.Drawing.RectangleF dirtyRect)
            {
                var operation = NSPrintOperation.CurrentOperation;

                var context = new NSGraphicsContext(Messaging.IntPtr_objc_msgSend(classNSGraphicsContext, selCurrentContext));

                // this causes monomac to hang for some reason:
                //var context = NSGraphicsContext.CurrentContext;

                using (var graphics = new Graphics(Handler.Widget.Generator, new GraphicsHandler(this, context, this.Frame.Height, this.IsFlipped))) {
                    Handler.Widget.OnPrintPage(new PrintPageEventArgs(graphics, Platform.Conversions.ToEto(operation.PrintInfo.PaperSize), operation.CurrentPage - 1));
                }
            }
Beispiel #16
0
        public static NSImage Tint(this NSImage image, NSColor tint)
        {
            CIFilter colorGenerator = CIFilter.FromName("CIConstantColorGenerator");
            CIColor  color          = CIColor.FromCGColor(tint.ToCG());

            colorGenerator.SetValueForKey(color, CIFilterInputKey.Color);
            CIFilter colorFilter = CIFilter.FromName("CIColorControls");

            colorFilter.SetValueForKey(colorGenerator.ValueForKey(CIFilterOutputKey.Image), CIFilterInputKey.Image);
            colorFilter.SetValueForKey(NSNumber.FromFloat(3f), CIFilterInputKey.Saturation);
            colorFilter.SetValueForKey(NSNumber.FromFloat(0.35f), CIFilterInputKey.Brightness);
            colorFilter.SetValueForKey(NSNumber.FromFloat(1f), CIFilterInputKey.Contrast);

            CIFilter monochromeFilter = CIFilter.FromName("CIColorMonochrome");
            CIImage  baseImage        = CIImage.FromCGImage(image.CGImage);

            monochromeFilter.SetValueForKey(baseImage, CIFilterInputKey.Image);
            monochromeFilter.SetValueForKey(CIColor.FromRgb(0.75f, 0.75f, 0.75f), CIFilterInputKey.Color);
            monochromeFilter.SetValueForKey(NSNumber.FromFloat(1f), CIFilterInputKey.Intensity);

            CIFilter compositingFilter = CIFilter.FromName("CIMultiplyCompositing");

            compositingFilter.SetValueForKey(colorFilter.ValueForKey(CIFilterOutputKey.Image), CIFilterInputKey.Image);
            compositingFilter.SetValueForKey(monochromeFilter.ValueForKey(CIFilterOutputKey.Image), CIFilterInputKey.BackgroundImage);

            CIImage outputImage = (CIImage)compositingFilter.ValueForKey(CIFilterOutputKey.Image);
            var     extent      = outputImage.Extent;

            var newsize = sd.Size.Truncate(extent.Size);

            var tintedImage = new NSImage(newsize);
            var newrep      = new NSBitmapImageRep(IntPtr.Zero, newsize.Width, newsize.Height, 8, 4, true, false, NSColorSpace.DeviceRGB, 4 * newsize.Width, 32);

            tintedImage.AddRepresentation(newrep);

            var graphics = NSGraphicsContext.FromBitmap(newrep);

            NSGraphicsContext.GlobalSaveGraphicsState();
            NSGraphicsContext.CurrentContext = graphics;

            var ciContext = CIContext.FromContext(graphics.GraphicsPort, new CIContextOptions {
                UseSoftwareRenderer = true
            });

            ciContext.DrawImage(outputImage, extent, extent);

            NSGraphicsContext.GlobalRestoreGraphicsState();

            newrep.Size = image.Size;
            return(tintedImage);
        }
Beispiel #17
0
            public override void DrawRect(CGRect dirtyRect)
            {
                var operation = NSPrintOperation.CurrentOperation;

                var context = new NSGraphicsContext(Messaging.IntPtr_objc_msgSend(classNSGraphicsContext, selCurrentContext));

                // this causes monomac to hang for some reason:
                //var context = NSGraphicsContext.CurrentContext;

                using (var graphics = new Graphics(new GraphicsHandler(this, context, (float)Frame.Height, IsFlipped)))
                {
                    Handler.Callback.OnPrintPage(Handler.Widget, new PrintPageEventArgs(graphics, operation.PrintInfo.PaperSize.ToEto(), (int)operation.CurrentPage - 1));
                }
            }
Beispiel #18
0
        Graphics(NSGraphicsContext context)
        {
            var gc = context;

            if (gc.IsFlipped)
            {
                gc = NSGraphicsContext.FromGraphicsPort(gc.GraphicsPort, false);
            }

            screenScale  = 1;
            nativeObject = gc;
            isFlipped    = gc.IsFlipped;
            InitializeContext(gc.GraphicsPort);
        }
Beispiel #19
0
        public static void EndImageContext()
        {
            // Return to previous context
            if (PreviousContext != null)
            {
                NSGraphicsContext.CurrentContext = PreviousContext;
            }

            // Release memory
            Context         = null;
            PreviousContext = null;
            ColorSpace      = null;
            ImageSize       = CGSize.Empty;
        }
Beispiel #20
0
        public static NSImageRep Resize(this NSImageRep image, CGSize newsize, ImageInterpolation interpolation = ImageInterpolation.Default, CGSize?imageSize = null)
        {
            var newrep = new NSBitmapImageRep(IntPtr.Zero, (nint)newsize.Width, (nint)newsize.Height, 8, 4, true, false, NSColorSpace.DeviceRGB, 4 * (nint)newsize.Width, 32);

            newrep.Size = imageSize ?? newsize;

            var graphics = NSGraphicsContext.FromBitmap(newrep);

            NSGraphicsContext.GlobalSaveGraphicsState();
            NSGraphicsContext.CurrentContext           = graphics;
            graphics.GraphicsPort.InterpolationQuality = interpolation.ToCG();
            image.DrawInRect(new CGRect(CGPoint.Empty, newrep.Size), CGRect.Empty, NSCompositingOperation.SourceOver, 1f, true, DrawHints);
            NSGraphicsContext.GlobalRestoreGraphicsState();
            return(newrep);
        }
Beispiel #21
0
        public void CreateManager()
        {
            // This sets up the global context so our drawing doesn't produce error messages
            NSBitmapImageRep bitmap = new NSBitmapImageRep(IntPtr.Zero, 1000, 1000, 16, 4, true, false, NSColorSpace.DeviceRGB, 0, 0);

            NSGraphicsContext.CurrentContext = NSGraphicsContext.FromBitmap(bitmap);

            NSTextStorage   storage   = new NSTextStorage("Hello World");
            NSTextContainer container = new NSTextContainer();

            manager = new NSLayoutManager();

            manager.AddTextContainer(container);
            storage.AddLayoutManager(manager);
        }
Beispiel #22
0
 public GraphicsHandler(NSView view, NSGraphicsContext graphicsContext, float height, bool flipped)
 {
     this.DisplayView             = view;
     this.height                  = height;
     this.graphicsContext         = graphicsContext;
     this.Control                 = graphicsContext.GraphicsPort;
     this.Flipped                 = flipped;
     Control.InterpolationQuality = CGInterpolationQuality.High;
     Control.SetAllowsSubpixelPositioning(false);
     Control.SaveState();
     if (!Flipped)
     {
         FlipDrawing();
     }
 }
            public override void DrawRect(RectangleF dirtyRect)
            {
                NSGraphicsContext nsgctxt = NSGraphicsContext.CurrentContext;

                nsgctxt.SaveGraphicsState();
                try {
                    var cg = nsgctxt.GraphicsPort;
                    cg.SetFillColor(new CGColor(0.25f, 1f));
                    cg.FillRect(dirtyRect);
                }
                finally {
                    nsgctxt.RestoreGraphicsState();
                }
                base.DrawRect(dirtyRect);
            }
        internal static NSBitmapImageRep Create()
        {
            float squareSize = 6;

            NSBitmapImageRep image = new NSBitmapImageRep(new CGRect(0, 0, squareSize * 2, squareSize * 2));

            using (CGContext gc = NSGraphicsContext.FromBitmap(image).GraphicsPort)
            {
                gc.SetFillColor(ImageDiffColors.TransparentBackgroundSquareColor);
                gc.FillRect(new CGRect(0, 0, squareSize, squareSize));
                gc.FillRect(new CGRect(squareSize, squareSize, squareSize, squareSize));
            }

            return(image);
        }
Beispiel #25
0
        public static void BeginImageContextWithOptions(CGSize size, bool opaque, nfloat scale)
        {
            // Create new image context
            ColorSpace = CGColorSpace.CreateDeviceRGB ();
            Context = new CGBitmapContext (null, (int)size.Width, (int)size.Height, 8, 0, ColorSpace, CGImageAlphaInfo.PremultipliedLast);

            // Flip context vertically
            var flipVertical = new  CGAffineTransform(1,0,0,-1,0,size.Height);
            Context.ConcatCTM (flipVertical);

            // Save previous context
            ImageSize = size;
            PreviousContext = NSGraphicsContext.CurrentContext;
            NSGraphicsContext.CurrentContext = NSGraphicsContext.FromCGContext (Context, true);
        }
Beispiel #26
0
        public static void BeginImageContextWithOptions(CGSize size, bool opaque, nfloat scale)
        {
            // Create new image context
            ColorSpace = CGColorSpace.CreateDeviceRGB();
            Context    = new CGBitmapContext(null, (int)size.Width, (int)size.Height, 8, 0, ColorSpace, CGImageAlphaInfo.PremultipliedLast);

            // Flip context vertically
            var flipVertical = new  CGAffineTransform(1, 0, 0, -1, 0, size.Height);

            Context.ConcatCTM(flipVertical);

            // Save previous context
            ImageSize       = size;
            PreviousContext = NSGraphicsContext.CurrentContext;
            NSGraphicsContext.CurrentContext = NSGraphicsContext.FromCGContext(Context, true);
        }
Beispiel #27
0
        public static NSImage Resize(this NSImage image, sd.Size newsize, ImageInterpolation interpolation = ImageInterpolation.Default)
        {
            var newimage = new NSImage(newsize);
            var newrep   = new NSBitmapImageRep(IntPtr.Zero, newsize.Width, newsize.Height, 8, 4, true, false, NSColorSpace.DeviceRGB, 4 * newsize.Width, 32);

            newimage.AddRepresentation(newrep);

            var graphics = NSGraphicsContext.FromBitmap(newrep);

            NSGraphicsContext.GlobalSaveGraphicsState();
            NSGraphicsContext.CurrentContext           = graphics;
            graphics.GraphicsPort.InterpolationQuality = interpolation.ToCG();
            image.DrawInRect(new sd.RectangleF(sd.PointF.Empty, newimage.Size), new sd.RectangleF(sd.PointF.Empty, image.Size), NSCompositingOperation.SourceOver, 1f);
            NSGraphicsContext.GlobalRestoreGraphicsState();
            return(newimage);
        }
        public override bool LockFocusIfCanDrawInContext(NSGraphicsContext context)
        {
            if (!NSThread.IsMain)
            {
                Console.WriteLine("Calling LockFocusIfCanDrawInContext from non UI thread!");
            }

            var traceInfo = GetTraceInfo();

            if (base.LockFocusIfCanDrawInContext(context))
            {
                ++traceInfo.lockFocusCounter;
                return(true);
            }
            return(false);
        }
        public override void DrawRect(CGRect dirtyRect)
        {
            base.DrawRect(dirtyRect);

            NSGraphicsContext.GlobalSaveGraphicsState();

            //backgound frame
            DrawCoverRect();

            //title frame
            DrawTitleRect();

            //bottom line
            DrawBottomLine();

            NSGraphicsContext.GlobalRestoreGraphicsState();
        }
Beispiel #30
0
            public void Dispose()
            {
                if (null == ctx)
                {
                    return;
                }

                Flush();
                NSGraphicsContext.GlobalRestoreGraphicsState();                 //   RestoreGraphicsState_c ();

                if (null != focusWindow)
                {
                    focusWindow.UnlockFocus();
                }

                ctx         = null;
                focusWindow = null;
            }
            public override void DrawBackgrounn(RectangleF dirtyRect)
            {
                base.DrawBackgrounn(dirtyRect);
                NSGraphicsContext nsgctxt = NSGraphicsContext.CurrentContext;

                nsgctxt.SaveGraphicsState();
                try {
                    var     cg = nsgctxt.GraphicsPort;
                    float[] comps;
                    this.CustomBackgroundColor.GetComponents(out comps);
                    cg.SetFillColor(comps);
                    cg.FillRect(dirtyRect);
                }
                finally {
                    nsgctxt.RestoreGraphicsState();
                }
                //base.DrawRect (dirtyRect);
            }
Beispiel #32
0
        public static NSImageRep Resize(this NSImageRep image, CGSize newsize, ImageInterpolation interpolation = ImageInterpolation.Default, CGSize?imageSize = null)
        {
            var newrep = new NSBitmapImageRep(IntPtr.Zero, (nint)newsize.Width, (nint)newsize.Height, 8, 4, true, false, NSColorSpace.DeviceRGB, 4 * (nint)newsize.Width, 32);

            newrep.Size = imageSize ?? newsize;

            var graphics = NSGraphicsContext.FromBitmap(newrep);

            NSGraphicsContext.GlobalSaveGraphicsState();
            NSGraphicsContext.CurrentContext           = graphics;
            graphics.GraphicsPort.InterpolationQuality = interpolation.ToCG();
#if XAMMAC
            // Xamarin.Mac doesn't allow null for hints, remove this when it does.
            Messaging.bool_objc_msgSend_CGRect_CGRect_UIntPtr_nfloat_bool_IntPtr(image.Handle, selDrawInRect_FromRect_Operation_Fraction_RespectFlipped_Hints_Handle, new CGRect(CGPoint.Empty, newrep.Size), CGRect.Empty, (UIntPtr)(ulong)NSCompositingOperation.SourceOver, 1f, true, IntPtr.Zero);
#else
            image.DrawInRect(new CGRect(CGPoint.Empty, newrep.Size), CGRect.Empty, NSCompositingOperation.SourceOver, 1f, true, null);
#endif
            NSGraphicsContext.GlobalRestoreGraphicsState();
            return(newrep);
        }
        private Graphics(NSGraphicsContext context)
        {
            var gc = context;

            if (gc.IsFlipped)
                gc = NSGraphicsContext.FromGraphicsPort (gc.GraphicsPort, false);

            // testing for now
            //			var attribs = gc.Attributes;
            //			attribs = NSScreen.MainScreen.DeviceDescription;
            //			NSValue asdf = (NSValue)attribs["NSDeviceResolution"];
            //			var size = asdf.SizeFValue;
            // ----------------------
            screenScale = 1;
            nativeObject = gc;

            isFlipped = gc.IsFlipped;

            InitializeContext(gc.GraphicsPort);
        }
 public static Graphics FromContext(NSGraphicsContext context)
 {
     return new Graphics (context);
 }
		public UIGraphicsContext (NSGraphicsContext context) : base () {
			// Initialize
			this.context = context;
		}