public override void SetData (TransferDataType type, Func<object> dataSource) { var pboard = NSPasteboard.GeneralPasteboard; pboard.ClearContents (); owner.DataSource = dataSource; pboard.AddTypes (new[] { type.ToUTI () }, owner); }
public override object GetData(TransferDataType type) { if (type == TransferDataType.Uri) { return((Uri)NSUrl.FromPasteboard(NSPasteboard.GeneralPasteboard)); } var data = NSPasteboard.GeneralPasteboard.GetDataForType(type.ToUTI()); if (data == null) { return(null); } if (type == TransferDataType.Text) { return(data.ToString()); } if (type == TransferDataType.Image) { return(new NSImage(data)); } unsafe { var bytes = new byte [data.Length]; using (var stream = new UnmanagedMemoryStream((byte *)data.Bytes, bytes.Length)) stream.Read(bytes, 0, bytes.Length); return(TransferDataSource.DeserializeValue(bytes)); } }
public override void SetData(TransferDataType type, Func <object> dataSource) { var pboard = NSPasteboard.GeneralPasteboard; pboard.ClearContents(); owner.DataSource = dataSource; pboard.AddTypes(new[] { type.ToUTI() }, owner); }
public override void SetData (TransferDataType type, Func<object> dataSource, bool cleanClipboardFirst = true) { var pboard = NSPasteboard.GeneralPasteboard; if(cleanClipboardFirst) { pboard.ClearContents(); } var owner = new PasteboardOwner(); owner.DataSource = dataSource; pboard.AddTypes (new[] { type.ToUTI () }, owner); }
public override object GetData (TransferDataType type) { if (type == TransferDataType.Uri) return (Uri)NSUrl.FromPasteboard (NSPasteboard.GeneralPasteboard); var data = NSPasteboard.GeneralPasteboard.GetDataForType (type.ToUTI ()); if (data == null) return null; if (type == TransferDataType.Text) return data.ToString (); if (type == TransferDataType.Image) return new NSImage (data); unsafe { var bytes = new byte [data.Length]; using (var stream = new UnmanagedMemoryStream ((byte*)data.Bytes, bytes.Length)) stream.Read (bytes, 0, bytes.Length); return TransferDataSource.DeserializeValue (bytes); } }
public override bool IsTypeAvailable(TransferDataType type) { return NSPasteboard.GeneralPasteboard.Types.Contains (type.ToUTI ()); }
public override bool IsTypeAvailable (TransferDataType type) { return NSPasteboard.GeneralPasteboard.CanReadItemWithDataConformingToTypes (new[] { type.ToUTI () }); }
public override bool IsTypeAvailable(TransferDataType type) { return(NSPasteboard.GeneralPasteboard.CanReadItemWithDataConformingToTypes(new[] { type.ToUTI() })); }
public override bool IsTypeAvailable(TransferDataType type) { return(NSPasteboard.GeneralPasteboard.Types.Contains(type.ToUTI())); }
public override bool IsTypeAvailable (TransferDataType type) { NSPasteboard pb = NSPasteboard.GeneralPasteboard; #if MONOMAC NSObject[] classes; #else Class[] classes; #endif NSDictionary options; bool isType; if (pb.PasteboardItems.Length == 0) { return false; } if (type == TransferDataType.Image) { //The below only works for images copied from web browsers, doesn't work for raw images. // NSObject urlClassObj = NSObject.FromObject(new MonoMac.ObjCRuntime.Class(typeof(NSUrl))); // NSObject imageClassObj = NSObject.FromObject(new MonoMac.ObjCRuntime.Class(typeof(NSImage))); // classes = new NSObject[]{ urlClassObj }; // NSObject a = new NSString(type.ToUTI()); // options = NSDictionary.FromObjectAndKey(imageClassObj, a); // isType = pb.CanReadObjectForClasses(classes, options); // return isType; var item = pb.PasteboardItems[0]; foreach (string itemType in item.Types) { if (itemType == "public.tiff" || itemType == "public.png") { return true; } } return false; } else if (type == TransferDataType.Text) { // text var item = pb.PasteboardItems[0]; foreach (string itemType in item.Types) { if (itemType == "public.file-url") { return true; } } #if MONOMAC classes = new NSObject[] { NSObject.FromObject(new MonoMac.ObjCRuntime.Class(typeof(NSAttributedString))), NSObject.FromObject(new MonoMac.ObjCRuntime.Class(typeof(NSString))), NSObject.FromObject(new MonoMac.ObjCRuntime.Class(typeof(NSUrl))), }; #else classes = new Class[] { new Class(typeof(NSAttributedString)), new Class(typeof(NSString)), new Class(typeof(NSUrl)), }; #endif options = new NSDictionary(); isType = pb.CanReadObjectForClasses(classes, options); return isType; } else if (type == TransferDataType.Uri) { //files #if MONOMAC classes = new NSObject[]{ NSObject.FromObject(new MonoMac.ObjCRuntime.Class(typeof(NSUrl))) }; #else classes = new Class[]{ new Class(typeof(NSUrl)) }; #endif options = NSDictionary.FromObjectAndKey(NSObject.FromObject(NSNumber.FromBoolean(true)), new NSString(type.ToUTI())); isType = pb.CanReadObjectForClasses(classes, options); return isType; } return NSPasteboard.GeneralPasteboard.Types.Contains (type.ToUTI ()); }
public override object GetData (TransferDataType type) { if (type == TransferDataType.Uri) { NSPasteboard pasteBoard = NSPasteboard.GeneralPasteboard; NSArray nsArray = (NSArray)pasteBoard.GetPropertyListForType(NSPasteboard.NSFilenamesType); NSString[] pathArray = NSArray.FromArray<NSString>(nsArray); if(pathArray != null) { string[] uriArray = new string[pathArray.Length]; for(int i = 0; i < pathArray.Length; i++) { Uri fileUrl = new Uri(pathArray[i].ToString()); if(fileUrl != null && fileUrl.IsFile) { uriArray[i] = pathArray[i].ToString(); } } return uriArray; } } if(type == TransferDataType.Image) { NSPasteboard pasteBoard = NSPasteboard.GeneralPasteboard; string[] imageTypes = NSImage.ImageUnfilteredPasteboardTypes(); for (int i = 0; i< imageTypes.Length; i++) { NSData imgData = pasteBoard.GetDataForType(imageTypes[i]); if(imgData != null) { NSImage nsImg = new NSImage(imgData); return ApplicationContext.Toolkit.WrapImage (nsImg); } } } // Url as text! if (type == TransferDataType.Text) { NSUrl url = NSUrl.FromPasteboard(NSPasteboard.GeneralPasteboard); if(url != null && url.IsFileUrl) { return "file://" + new Uri(url.Path).AbsolutePath; } } var data = NSPasteboard.GeneralPasteboard.GetDataForType (type.ToUTI ()); if (data == null) return null; if (type == TransferDataType.Text) return data.ToString (); if (type == TransferDataType.Image) return ApplicationContext.Toolkit.WrapImage (new NSImage (data)); unsafe { var bytes = new byte [data.Length]; using (var stream = new UnmanagedMemoryStream ((byte*)data.Bytes, bytes.Length)) stream.Read (bytes, 0, bytes.Length); try { return TransferDataSource.DeserializeValue(bytes); } catch(System.Runtime.Serialization.SerializationException) { // if data cannot be read, do not crash - return null return null; } } }