Beispiel #1
0
		public void CopyImage(NSObject sender) {

			// Grab the current image
			var image = ImageView.Image;

			// Anything to process?
			if (image != null) {
				// Get the standard pasteboard
				var pasteboard = NSPasteboard.GeneralPasteboard;

				// Empty the current contents
				pasteboard.ClearContents();

				// Add the current image to the pasteboard
				pasteboard.WriteObjects (new NSImage[] {image});

				// Save the custom data class to the pastebaord
				pasteboard.WriteObjects (new ImageInfo[] { Info });

				// Using a Pasteboard Item
				NSPasteboardItem item = new NSPasteboardItem();
				string[] writableTypes = {"public.text"};

				// Add a data provier to the item
				ImageInfoDataProvider dataProvider = new ImageInfoDataProvider (Info.Name, Info.ImageType);
				var ok = item.SetDataProviderForTypes (dataProvider, writableTypes);

				// Save to pasteboard
				if (ok) {
					pasteboard.WriteObjects (new NSPasteboardItem[] { item });
				}
			}

		}
Beispiel #2
0
        internal static void Store(NSPasteboard pboard, object data, int id)
        {
            if (id == 0)
            {
                pboard.ClearContents();
                managed.Clear();
                providers.Clear();
                return;
            }
            if (data == null)
            {
                return;
            }

            var name = DataFormats.GetFormat(id)?.Name ?? String.Empty;

            if (name == Clipboard.IDataObjectFormat)
            {
                managed[name] = data;

                var provider = new DataObjectProvider((IDataObject)data);
                var item     = new NSPasteboardItem();
                item.SetDataProviderForTypes(provider, provider.Types);
                pboard.WriteObject(item);
                providers.Add(provider);

                // Set flag that clipboard contains our data
                // Do it as the last step, to avoid fooling the FileMaker (and possibly other apps).
                pboard.SetStringForType(name, name);
            }
        }
Beispiel #3
0
        internal NSPasteboardItem NewPasteboardItem()
        {
            var item = new NSPasteboardItem();

            item.SetDataForType(new NSData(), IDataObjectPboardType);             // Tells NSMonoView to look for XplatIUCocoa.DraggedData
            return(item);
        }
Beispiel #4
0
        public override void MouseDragged(NSEvent ev)
        {
            _callbacks.OnDragged().MatchWith(
                data =>
            {
                var pasteBoardItem = new NSPasteboardItem();
                pasteBoardItem.SetStringForType("fuse-drag-n-drop", "public.data");
                var draggingItem = new NSDraggingItem(pasteBoardItem);

                var imgCache = BitmapImageRepForCachingDisplayInRect(Bounds);
                CacheDisplay(Bounds, imgCache);
                var img = new NSImage(imgCache.Size);
                img.LockFocus();
                imgCache.DrawInRect(Bounds, Bounds, NSCompositingOperation.Copy, 0.5f, true, new NSDictionary());
                img.UnlockFocus();

                draggingItem.SetDraggingFrame(new CGRect(Bounds.Location, img.Size), img);

                BeginDraggingSession(new[] { draggingItem }, ev, new DragSource(this, _space, _callbacks, data));
            },
                () =>
            {
                BeginDraggingSession(new NSDraggingItem[0], ev, new DragSource(this, _space, _callbacks, null));
            });
        }
Beispiel #5
0
        public void CopyImage(NSObject sender)
        {
            // Grab the current image
            var image = ImageView.Image;

            // Anything to process?
            if (image != null)
            {
                // Get the standard pasteboard
                var pasteboard = NSPasteboard.GeneralPasteboard;

                // Empty the current contents
                pasteboard.ClearContents();

                // Add the current image to the pasteboard
                pasteboard.WriteObjects(new NSImage[] { image });

                // Save the custom data class to the pastebaord
                pasteboard.WriteObjects(new ImageInfo[] { Info });

                // Using a Pasteboard Item
                NSPasteboardItem item          = new NSPasteboardItem();
                string[]         writableTypes = { "public.text" };

                // Add a data provier to the item
                ImageInfoDataProvider dataProvider = new ImageInfoDataProvider(Info.Name, Info.ImageType);
                var ok = item.SetDataProviderForTypes(dataProvider, writableTypes);

                // Save to pasteboard
                if (ok)
                {
                    pasteboard.WriteObjects(new NSPasteboardItem[] { item });
                }
            }
        }
Beispiel #6
0
 protected void ProvidePng(NSPasteboard pboard, NSPasteboardItem item, string type)
 {
     if (data.GetData(DataFormats.Bitmap) is Image image)
     {
         item.SetDataForType(image.ToNSData(ImageFormat.Png), Pasteboard.NSPasteboardTypePNG);
     }
 }
Beispiel #7
0
        protected void ProvideWebArchive(NSPasteboard pboard, NSPasteboardItem item, string type)
        {
            // This method does not seem to work any more. Disabled by not returning appropriate type in Types getter.
            var s = GetHtmlWithoutMetadata();

            if (s == null)
            {
                return;
            }

            var mainRsrc = new NSDictionary
            {
                ["WebResourceData"]             = NSData.FromString(s, NSStringEncoding.UTF8),
                ["WebResourceFrameName"]        = (NSString)"",
                ["WebResourceMIMEType"]         = (NSString)"text/html",
                ["WebResourceTextEncodingName"] = (NSString)"UTF-8",
                ["WebResourceURL"] = (NSString)"about:blank"
            };

            var container = new NSDictionary
            {
                ["WebMainResource"] = mainRsrc
            };

            var nsdata  = NSPropertyListSerialization.DataWithPropertyList(container, NSPropertyListFormat.Xml, out NSError error);
            var archive = NSString.FromData(nsdata, NSStringEncoding.UTF8);

            item.SetDataForType(nsdata, Pasteboard.NSPasteboardTypeWebArchive);
        }
Beispiel #8
0
 internal void ProvideDataForType(NSPasteboard pasteboard, NSPasteboardItem item, string type)
 {
     if (type == PasteboardTypeFileURLPromise && DraggedData is IDataObject idata)
     {
         var location = pasteboard.GetStringForType("com.apple.pastelocation");
         if (!String.IsNullOrEmpty(location))
         {
             var folder = new NSUrl(location).Path;
             if (DraggedData is Runtime.InteropServices.ComTypes.IDataObject cdata)
             {
                 var filename = dndFilenames[dndCurrentFileIndex];
                 var unique   = GenerateUniqueFilename(folder, filename);
                 var path     = Path.Combine(folder, unique);
                 try
                 {
                     var stream = GetStream(cdata, dndCurrentFileIndex);
                     using (var outputStream = File.Create(path))
                         stream.CopyTo(outputStream);
                 }
                 catch (IOException) { }                         // TODO: Handle this
                 catch (UnauthorizedAccessException) { }
             }
         }
     }
 }
            public override bool WriteRows(NSTableView tableView, NSIndexSet rowIndexes, NSPasteboard pboard)
            {
                var item = new NSPasteboardItem();

                item.SetDataForType(NSKeyedArchiver.ArchivedDataWithRootObject(rowIndexes), DataTypeName);
                pboard.WriteObjects(new[] { item });
                return(true);
            }
Beispiel #10
0
        protected void ProvideHtml(NSPasteboard pboard, NSPasteboardItem item, string type)
        {
            var s = GetHtmlWithoutMetadata();

            if (s != null)
            {
                item.SetStringForType(s, Pasteboard.NSPasteboardTypeHTML);
            }
        }
Beispiel #11
0
        protected void ProvideText(NSPasteboard pboard, NSPasteboardItem item, string type)
        {
            var text = (data.GetData(DataFormats.Text) ?? data.GetData(DataFormats.UnicodeText)) as string;

            if (text != null)
            {
                item.SetStringForType(text, type);
            }
        }
		public override void ProvideDataForType (NSPasteboard pasteboard, NSPasteboardItem item, string type)
		{
			// Take action based on the type
			switch (type) {
			case "public.text":
				// Encode the data to string 
				item.SetStringForType(string.Format("{0}.{1}", Name, ImageType),type);
				break;
			}
		}
Beispiel #13
0
 public override void ProvideDataForType(NSPasteboard pasteboard, NSPasteboardItem item, string type)
 {
     // Take action based on the type
     switch (type)
     {
     case "public.text":
         // Encode the data to string
         item.SetStringForType(string.Format("{0}.{1}", Name, ImageType), type);
         break;
     }
 }
Beispiel #14
0
        protected void ProvideRtf(NSPasteboard pboard, NSPasteboardItem item, string type)
        {
            var s = GetHtmlWithoutMetadata();

            if (s == null)
            {
                return;
            }

            var nsdata  = NSData.FromString(s, NSStringEncoding.UTF8);
            var options = new NSMutableDictionary
            {
                [Pasteboard.NSDocumentTypeDocumentAttribute]      = (NSString)Pasteboard.NSHTMLTextDocumentType,
                [Pasteboard.NSCharacterEncodingDocumentAttribute] = new NSNumber((ulong)NSStringEncoding.UTF8)
            };
            var rtf = new NSAttributedString(nsdata, options, out NSDictionary attributes, out NSError error);

            options[Pasteboard.NSDocumentTypeDocumentAttribute] = (NSString)Pasteboard.NSRTFTextDocumentType;
            nsdata = rtf.GetData(new NSRange(0, rtf.Length), options, out error);
            item.SetDataForType(nsdata, Pasteboard.NSPasteboardTypeRTF);
        }
Beispiel #15
0
        private NSDraggingItem CreateDraggingItem(string format, object data)
        {
            var    pasteboardItem = new NSPasteboardItem();
            NSData nsData;

            if (data is string s)
            {
                if (format == DataFormats.FileNames)
                {
                    s = new Uri(s).AbsoluteUri; // Ensure file uris...
                }
                nsData = NSData.FromString(s);
            }
            else if (data is Stream strm)
            {
                nsData = NSData.FromStream(strm);
            }
            else if (data is byte[] bytes)
            {
                nsData = NSData.FromArray(bytes);
            }
            else
            {
                BinaryFormatter bf = new BinaryFormatter();
                using (var ms = new MemoryStream())
                {
                    bf.Serialize(ms, data);
                    ms.Position = 0;
                    nsData      = NSData.FromStream(ms);
                }
            }
            pasteboardItem.SetDataForType(nsData, DataFormatToUTI(format));

            NSPasteboardWriting writing = new NSPasteboardWriting(pasteboardItem.Handle);

            return(new NSDraggingItem(writing));
        }
Beispiel #16
0
        public override void ProvideDataForType(NSPasteboard pboard, NSPasteboardItem item, string type)
        {
            switch (type)
            {
            case Pasteboard.NSPasteboardTypeText:
                ProvideText(pboard, item, type);
                break;

            case Pasteboard.NSPasteboardTypeHTML:
                ProvideHtml(pboard, item, type);
                break;

            case Pasteboard.NSPasteboardTypeWebArchive:
                ProvideWebArchive(pboard, item, type);
                break;

            case Pasteboard.NSPasteboardTypeRTF:
                ProvideRtf(pboard, item, type);
                break;

            case Pasteboard.NSPasteboardTypeTIFF:
                ProvideTiff(pboard, item, type);
                break;

            case Pasteboard.NSPasteboardTypePNG:
                ProvidePng(pboard, item, type);
                break;

            default:
                if (WebArchiveDynamicType == type)
                {
                    ProvideWebArchive(pboard, item, type);
                }
                break;
            }
        }
Beispiel #17
0
 public override void Apply(NSPasteboardItem item, string type)
 {
 }
Beispiel #18
0
        internal static async Task <NSDraggingItem[]> CreateNativeDragDropData(
            DataPackageView data,
            Point startPoint)
        {
            NSDraggingItem draggingItem;
            var            items             = new List <NSDraggingItem>();
            double         maxFrameDimension = 300.0;     // May be adjusted
            var            defaultFrameRect  = new CoreGraphics.CGRect(startPoint.X, startPoint.Y, 100, 30);

            /* Note that NSDraggingItems are required by the BeginDraggingSession methods.
             * Therefore, that is what is constructed here instead of pasteboard items.
             *
             * For several types such as NSString or NSImage, they implement the INSPasteboardWriting interface and
             * can therefore be used to directly construct an NSDraggingItem.
             * However, for other types (such as HTML) the full pasteboard item must be constructed first defining
             * both its type and string content.
             *
             * The dragging frame is used to represent the visual of the item being dragged. This could be a
             * preview of the image or sample text. At minimum, macOS requires the DraggingFrame property of the
             * NSDraggingItem to be set with a CGRect or the app will crash. It is however better to set both
             * the frame bounds and content at the same time with .SetDraggingFrame(). For caveats see:
             * https://developer.apple.com/documentation/appkit/nsdraggingitem/1528746-setdraggingframe
             *
             * Because Uno does not currently support the DragUI, this code only generates a real drag visual
             * for images where a visual is already defined. For other types such as text, no visual will be
             * generated. In the future, when DragUI and its corresponding image is supported, this can change.
             *
             */

            if (data?.Contains(StandardDataFormats.Bitmap) ?? false)
            {
                NSImage?image = null;

                using (var stream = (await(await data.GetBitmapAsync()).OpenReadAsync()).AsStream())
                {
                    if (stream != null)
                    {
                        using (var ms = new MemoryStream())
                        {
                            await stream.CopyToAsync(ms);

                            ms.Flush();
                            ms.Position = 0;

                            image = NSImage.FromStream(ms);
                        }
                    }
                }

                if (image != null)
                {
                    draggingItem = new NSDraggingItem(image);

                    // For an NSImage, we will use the image itself as the dragging visual.
                    // The visual should be no larger than the max dimension setting and is therefore scaled.
                    NSBitmapImageRep rep = new NSBitmapImageRep(image.CGImage);
                    int    width         = (int)rep.PixelsWide;
                    int    height        = (int)rep.PixelsHigh;
                    double scale         = maxFrameDimension / Math.Max(width, height);

                    // Dragging frame must be set
                    draggingItem.SetDraggingFrame(
                        new CoreGraphics.CGRect(startPoint.X, startPoint.Y, width * scale, height * scale),
                        image);
                    items.Add(draggingItem);
                }
            }

            if (data?.Contains(StandardDataFormats.Html) ?? false)
            {
                var html = await data.GetHtmlFormatAsync();

                if (!string.IsNullOrEmpty(html))
                {
                    var pasteboardItem = new NSPasteboardItem();
                    pasteboardItem.SetStringForType(html, NSPasteboard.NSPasteboardTypeHTML);

                    draggingItem = new NSDraggingItem(pasteboardItem);
                    draggingItem.DraggingFrame = defaultFrameRect;                     // Must be set
                    items.Add(draggingItem);
                }
            }

            if (data?.Contains(StandardDataFormats.Rtf) ?? false)
            {
                var rtf = await data.GetRtfAsync();

                if (!string.IsNullOrEmpty(rtf))
                {
                    // Use `NSPasteboardTypeRTF` instead of `NSPasteboardTypeRTFD` for max compatibility
                    var pasteboardItem = new NSPasteboardItem();
                    pasteboardItem.SetStringForType(rtf, NSPasteboard.NSPasteboardTypeRTF);

                    draggingItem = new NSDraggingItem(pasteboardItem);
                    draggingItem.DraggingFrame = defaultFrameRect;                     // Must be set
                    items.Add(draggingItem);
                }
            }

            if (data?.Contains(StandardDataFormats.StorageItems) ?? false)
            {
                var storageItems = await data.GetStorageItemsAsync();

                if (storageItems.Count > 0)
                {
                    // Not currently supported
                }
            }

            if (data?.Contains(StandardDataFormats.Text) ?? false)
            {
                var text = await data.GetTextAsync();

                if (!string.IsNullOrEmpty(text))
                {
                    draggingItem = new NSDraggingItem((NSString)text);
                    draggingItem.DraggingFrame = defaultFrameRect;                     // Must be set
                    items.Add(draggingItem);
                }
            }

            if (data != null)
            {
                var uri = DataPackage.CombineUri(
                    data.Contains(StandardDataFormats.WebLink) ? (await data.GetWebLinkAsync()).ToString() : null,
                    data.Contains(StandardDataFormats.ApplicationLink) ? (await data.GetApplicationLinkAsync()).ToString() : null,
                    data.Contains(StandardDataFormats.Uri) ? (await data.GetUriAsync()).ToString() : null);

                if (string.IsNullOrEmpty(uri) == false)
                {
                    draggingItem = new NSDraggingItem(new NSUrl(uri));
                    draggingItem.DraggingFrame = defaultFrameRect;                     // Must be set
                    items.Add(draggingItem);
                }
            }

            return(items.ToArray());
        }
Beispiel #19
0
 public override void ProvideDataForType(NSPasteboard pasteboard, NSPasteboardItem item, string type)
 {
     driver.ProvideDataForType(pasteboard, item, type);
     driver.dndCurrentFileIndex += 1;
 }
 public override void Apply(NSPasteboardItem item, string type) => item.SetDataForType(Value, type);
 public override void Apply(NSPasteboardItem item, string type) => item.SetPropertyListForType(Value, type);
Beispiel #22
0
 // provider must implement NSPasteboardItemDataProvider
 public static void SetDataProviderForTypes(this NSPasteboardItem item, NSObject provider, string[] types)
 {
     var sel   = new ObjCRuntime.Selector("setDataProvider:forTypes:");
     var array = NSArray.FromStrings(types);
     var ok    = LibObjc.bool_objc_msgSend_IntPtr_IntPtr(item.Handle, sel.Handle, provider.Handle, array.Handle);
 }
 public abstract void Apply(NSPasteboardItem item, string type);