Exemple #1
0
        public override bool PerformDragOperation(NSDraggingInfo sender)
        {
            NSPasteboard pb = sender.DraggingPasteboard;

            foreach (var x in pb.PasteboardItems)
            {
                Console.WriteLine("x => {0}", x.Types);
                foreach (var y in x.Types)
                {
                    Console.WriteLine("{0} => {1}", y, pb.GetStringForType(y));
                }
            }

            var file = pb.GetStringForType("public.file-url");

            if (!string.IsNullOrEmpty(file))
            {
                if (dropped != null)
                {
                    file = HttpUtility.UrlDecode(file);
                    dropped.Invoke(file);
                }

                return(true);
            }

            Console.WriteLine("Got no string");
            return(false);
        }
Exemple #2
0
        private static DataPackageView GetFromNative(NSPasteboard pasteboard)
        {
            if (pasteboard is null)
            {
                throw new ArgumentException(nameof(pasteboard));
            }

            var dataPackage = new DataPackage();

            var clipHtml = pasteboard.GetStringForType(NSPasteboard.NSPasteboardTypeHTML);

            if (clipHtml != null)
            {
                dataPackage.SetHtmlFormat(clipHtml);
            }

            var clipRtf = pasteboard.GetStringForType(NSPasteboard.NSPasteboardTypeRTF);

            if (clipRtf != null)
            {
                dataPackage.SetRtf(clipRtf);
            }

            var clipText = pasteboard.GetStringForType(NSPasteboard.NSPasteboardTypeString);

            if (clipText != null)
            {
                dataPackage.SetText(clipText);
            }

            return(dataPackage.GetView());
        }
Exemple #3
0
        // Retrieves array of identifiers of available formats.
        internal static int[] GetAvailableFormats(NSPasteboard pboard)
        {
            var ids = new List <int>();

            var native = null == pboard.GetStringForType(Clipboard.IDataObjectFormat);

            if (native)
            {
                // Data in the pasteboard comes from another application
                var names = DataObjectPasteboard.GetFormats(pboard);
                foreach (var name in names)
                {
                    ids.Add(DataFormats.Format.Add(name).Id);
                }
            }
            else
            {
                // We provided the data for the pasteboard
                if (managed.TryGetValue(Clipboard.IDataObjectFormat, out object obj) && obj is IDataObject idata)
                {
                    foreach (var fmt in idata.GetFormats())
                    {
                        ids.Add(DataFormats.Format.Add(fmt).Id);
                    }
                }

                foreach (var key in managed.Keys)
                {
                    ids.Add(DataFormats.Format.Add(key).Id);
                }
            }

            return(ids.ToArray());
        }
        internal string[] GetFileDrop(NSPasteboard pboard)
        {
            var s = pboard.GetStringForType(Pasteboard.NSPasteboardTypeFileURL);

            if (s != null)
            {
                var paths = new List <string>();

                if (TryGetFilePath(s, out var path))
                {
                    paths.Add(path);
                }

                foreach (var item in pboard.PasteboardItems)
                {
                    if (TryGetFilePath(item.GetStringForType(Pasteboard.NSPasteboardTypeFileURL), out var itemPath))
                    {
                        paths.Add(itemPath);
                    }
                }

                return(paths.Distinct().ToArray());
            }

            return(null);
        }
        internal object GetData(string format, NSPasteboard pboard, bool autoConvert)
        {
            switch (format)
            {
            case DataFormats.Text:
            case DataFormats.UnicodeText:
                return(pboard.GetStringForType(Pasteboard.NSPasteboardTypeText));

            case DataFormats.Rtf:
                return(GetRtf(pboard));

            case DataFormats.Html:
                return(GetHtml(pboard));

            case DataFormats.HtmlStream:
                return(GetHtml(pboard)?.ToStream(Encoding.UTF8));

            case Pasteboard.UniformResourceLocatorW:
                return(GetUri(pboard));

            case DataFormats.Bitmap:
                return(GetBitmap(pboard));

            case DataFormats.FileDrop:
                return(GetFileDrop(pboard));
            }

            return(null);
        }
Exemple #6
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) { }
             }
         }
     }
 }
Exemple #7
0
        public string GetClipboardContent()
        {
            string clipboardContents = "";

            if (clipboard.GetStringForType("public.html") != null)
            {
                clipboardContents = clipboard.GetStringForType("public.html");
                CurrentType       = "HTML";
            }
            else if (clipboard.GetStringForType("NSStringPboardType") != null)
            {
                clipboardContents = clipboard.GetStringForType("NSStringPboardType");
                CurrentType       = "Text";
            }
            return(clipboardContents);
        }
        public static object GetData(this NSPasteboard pboard, string format, bool autoConvert)
        {
            switch (format)
            {
            case DataFormats.Text:
            case DataFormats.UnicodeText:
                return(pboard.GetStringForType(Pasteboard.NSPasteboardTypeText));

            case DataFormats.Rtf:
                return(pboard.GetRtf());

            case DataFormats.Html:
                return(pboard.GetHtml());

            case DataFormats.HtmlStream:
                return(pboard.GetHtml()?.ToStream(Encoding.UTF8));

            case Pasteboard.UniformResourceLocatorW:
                return(pboard.GetUri());

            case DataFormats.Bitmap:
                return(pboard.GetBitmap());

            case DataFormats.FileDrop:
                return(pboard.GetFileDrop());

            case "FilePromise":
                return(pboard.GetFilePromise());
            }

            return(null);
        }
        internal Uri GetUri(NSPasteboard pboard)
        {
            if (Uri.TryCreate(pboard.GetStringForType(Pasteboard.NSPasteboardTypeText), UriKind.Absolute, out Uri uri))
            {
                return(uri);
            }

            return(null);
        }
        internal string GetHtml(NSPasteboard pboard)
        {
            string html = pboard.GetStringForType(Pasteboard.NSPasteboardTypeHTML);

            if (html != null)
            {
                return(HtmlClip.AddMetadata(html));
            }

            return(null);
        }
Exemple #11
0
        //NSClassFromString
        //NSSelectorFromString
        //NSProtocolFromString

        #region NSPasteboard Extensions

        public static string[] GetFormats(this NSPasteboard pboard)
        {
            var types = new List <string>();

            foreach (var type in pboard.Types)
            {
                switch (type)
                {
                case Pasteboard.NSPasteboardTypeText:
                    types.Add(DataFormats.Text);
                    types.Add(DataFormats.UnicodeText);
                    break;

                case Pasteboard.NSPasteboardTypeURL:
                    if (Uri.TryCreate(pboard.GetStringForType(type), UriKind.Absolute, out Uri uri))
                    {
                        types.Add(Pasteboard.UniformResourceLocatorW);
                    }
                    break;

                case Pasteboard.NSPasteboardTypeHTML:
                    types.Add(DataFormats.Html);
                    break;

                case Pasteboard.NSPasteboardTypeRTF:
                    types.Add(DataFormats.Rtf);
                    break;

                case Pasteboard.NSPasteboardTypeImage:
                case Pasteboard.NSPasteboardTypePNG:
                case Pasteboard.NSPasteboardTypeTIFF:
                case Pasteboard.NSPasteboardTypeJPEG:
                    types.Add(DataFormats.Bitmap);
                    break;

                case Pasteboard.NSPasteboardTypeFileURL:
                    types.Add(DataFormats.FileDrop);
                    break;

                case Pasteboard.NSPasteboardTypeFileURLPromise:
                    types.Add("FilePromise");
                    break;
                }
            }

            // Special rules that decrease chance for misinterpretation of data in SWF apps
            if (types.Contains(DataFormats.FileDrop))
            {
                types.Remove(DataFormats.Bitmap);
            }

            return(types.ToArray());
        }
Exemple #12
0
        private IDataObject ToIDataObject(NSPasteboard pboard)
        {
            var types = pboard.Types;

            if (Array.IndexOf(types, XplatUICocoa.IDataObjectPboardType) != -1)
            {
                if (XplatUICocoa.DraggedData is IDataObject idata)
                {
                    return(idata);
                }
            }

            var s = pboard.GetStringForType(XplatUICocoa.NSStringPboardType);

            if (s != null)
            {
                return(new DataObject(s));
            }

            s = pboard.GetStringForType(XplatUICocoa.UTTypeFileUrl);
            if (s != null)
            {
                var paths = new List <string>();
                foreach (var item in pboard.PasteboardItems)
                {
                    var url = item.GetStringForType(XplatUICocoa.UTTypeFileUrl);
                    paths.Add(NSUrl.FromString(url).Path);
                }

                if (paths.Count != 0)
                {
                    return(new DataObject(DataFormats.FileDrop, paths.ToArray()));
                }
            }

            // TODO: Add more conversions/wrappers - for files, images etc.
            // See DataObjectPasteboard - merge somehow?

            return(null);
        }
Exemple #13
0
        public bool ReadFromPasteboard(NSPasteboard pb)
        {
            string pbString = pb.GetStringForType(pboardTypes[0]);

            if (pbString != null)
            {
                // Read the string from the pasteboard
                // Our view can only handle one letter
                Letter = pbString.GetFirstLetter();
                return(true);
            }
            return(false);
        }
Exemple #14
0
 static void FillDataStore(TransferDataStore store, NSPasteboard pb, string[] types)
 {
     foreach (var t in types)
     {
         if (!pb.Types.Contains(t))
         {
             continue;
         }
         if (t == NSPasteboard.NSStringType)
         {
             store.AddText(pb.GetStringForType(t));
         }
         else if (t == NSPasteboard.NSFilenamesType)
         {
             string      data = pb.GetStringForType(t);
             XmlDocument doc  = new XmlDocument();
             doc.XmlResolver = null;                     // Avoid DTD validation
             doc.LoadXml(data);
             store.AddUris(doc.SelectNodes("/plist/array/string").Cast <XmlElement> ().Select(e => new Uri(e.InnerText)).ToArray());
         }
     }
 }
Exemple #15
0
        internal string[] GetFileDrop(NSPasteboard pboard)
        {
            var s = pboard.GetStringForType(Pasteboard.NSPasteboardTypeFileURL);

            if (s != null)
            {
                var paths = new List <string>();
                foreach (var item in pboard.PasteboardItems)
                {
                    var url = item.GetStringForType(Pasteboard.NSPasteboardTypeFileURL);
                    paths.Add(NSUrl.FromString(url).Path);
                }

                return(paths.ToArray());
            }

            return(null);
        }
Exemple #16
0
        internal static object Retrieve(NSPasteboard pboard, int id)
        {
            var name = DataFormats.GetFormat(id)?.Name ?? String.Empty;

            if (name == Clipboard.IDataObjectFormat)
            {
                var native = null == pboard.GetStringForType(Clipboard.IDataObjectFormat);
                if (native)
                {
                    return(new DataObjectPasteboard(pboard));
                }

                if (managed.TryGetValue(name, out object value))
                {
                    return(value is IDataObject idata ? new DataObjectWrapper(idata) : value);
                }
            }

            return(null);
        }
Exemple #17
0
        internal static string[] GetFormats(NSPasteboard pboard)
        {
            var types = new List <string>();

            foreach (var type in pboard.Types)
            {
                switch (type)
                {
                case Pasteboard.NSPasteboardTypeText:
                    types.Add(DataFormats.Text);
                    types.Add(DataFormats.UnicodeText);
                    break;

                case Pasteboard.NSPasteboardTypeURL:
                    if (Uri.TryCreate(pboard.GetStringForType(type), UriKind.Absolute, out Uri uri))
                    {
                        types.Add(Pasteboard.UniformResourceLocatorW);
                    }
                    break;

                case Pasteboard.NSPasteboardTypeHTML:
                    types.Add(DataFormats.Html);
                    break;

                case Pasteboard.NSPasteboardTypeRTF:
                    types.Add(DataFormats.Rtf);
                    break;

                case Pasteboard.NSPasteboardTypePNG:
                case Pasteboard.NSPasteboardTypeTIFF:
                    types.Add(DataFormats.Bitmap);
                    break;
                }
            }

            return(types.ToArray());
        }
        public static IDataObject GetDataObject()
        {
            var dataObject = new DataObject();

            // Beside copying and pasting UnicodeText to/from pasteboard
            // editor inserts booleans like "VisualStudioEditorOperationsLineCutCopyClipboardTag"
            // which allows editor to know whole line was copied into pasteboard so on paste
            // it inserts line into new line, so we enumerate over all types and if length == 1
            // we just assume it's boolean we set in method above
            foreach (var type in pasteboard.Types)
            {
                if (type == DataFormats.UnicodeText)
                {
                    dataObject.SetText(pasteboard.GetStringForType(type));
                    continue;
                }
                var data = pasteboard.GetDataForType(type);
                if (data != null && data.Length == 1)
                {
                    dataObject.SetData(type, data: data [0] != 0);
                }
            }
            return(dataObject);
        }
Exemple #19
0
        public bool ReadFromPasteboard(NSPasteboard pb)
        {
            string pbString = pb.GetStringForType(pboardTypes[0]);

            if (pbString != null) {
                // Read the string from the pasteboard
                // Our view can only handle one letter
                Letter = pbString.GetFirstLetter();
                return true;
            }
            return false;
        }