Skip to content

Commit

Permalink
DataObject/Clipboard enhancements
Browse files Browse the repository at this point in the history
- Switches out BinarySerializer to DataContractSerializer for GetObject/SetObject
- Support [DataContract] types for GetObject/SetObject (in addition to Serializable)
- Add GetObject() override that takes desired type
- GetObject() returns object for known format types for String, Bitmap, Color (mac)
- Wpf/WinForms: GetData() returns byte data for more types
- Wpf: Prevent crash getting data for FileContents, and return MemoryStream[] with GetObject()
  • Loading branch information
cwensley committed Jul 31, 2023
1 parent a7b303d commit e425a21
Show file tree
Hide file tree
Showing 14 changed files with 669 additions and 110 deletions.
2 changes: 1 addition & 1 deletion lib/monomac
Submodule monomac updated 1 files
+11 −0 src/appkit.cs
23 changes: 21 additions & 2 deletions src/Eto.Gtk/Forms/ClipboardHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,34 @@ public bool Contains(string type)

public bool TrySetObject(object value, string type) => false;

public bool TryGetObject(string type, out object value)
public bool TryGetObject(string type, Type objectType, out object value)
{
if (objectType == null || objectType == typeof(string))
{
if (DataObjectHandler.string_types.Contains(type, StringComparer.OrdinalIgnoreCase))
{
value = GetString(type);
if (value != null)
return true;
}
}
if (objectType == null || objectType == typeof(Bitmap))
{
if (DataObjectHandler.image_types.Contains(type, StringComparer.OrdinalIgnoreCase))
{
value = new Bitmap(GetData(type));
return true;
}
}

value = null;
return false;
}

public void SetObject(object value, string type) => Widget.SetObject(value, type);

public T GetObject<T>(string type) => Widget.GetObject<T>(type);
public object GetObject(string type, Type objectType) => Widget.GetObject(type, objectType);
public object GetObject(string type) => Widget.GetObject(type);

public string[] Types
{
Expand Down
24 changes: 23 additions & 1 deletion src/Eto.Gtk/Forms/DataObjectHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,37 @@ public bool Contains(params string[] types)

public bool TrySetObject(object value, string type) => false;

public bool TryGetObject(string type, out object value)
internal static string[] string_types = { "UTF8_STRING", "TEXT", "STRING", "text/html", "text/plain" };
internal static string[] image_types = { "image/pixbuf", "image/png", "image/tiff", "image/bmp", "image/jpeg" };

public bool TryGetObject(string type, Type objectType, out object value)
{
if (objectType == null || objectType == typeof(string))
{
if (string_types.Contains(type, StringComparer.OrdinalIgnoreCase))
{
value = GetString(type);
if (value != null)
return true;
}
}
if (objectType == null || objectType == typeof(Bitmap))
{
if (image_types.Contains(type, StringComparer.OrdinalIgnoreCase))
{
value = new Bitmap(GetData(type));
return true;
}
}
value = null;
return false;
}

public void SetObject(object value, string type) => Widget.SetObject(value, type);

public T GetObject<T>(string type) => Widget.GetObject<T>(type);
public object GetObject(string type, Type objectType) => Widget.GetObject(type, objectType);
public object GetObject(string type) => Widget.GetObject(type);

public string[] Types
{
Expand Down
35 changes: 31 additions & 4 deletions src/Eto.Mac/Forms/DataObjectHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,37 @@ public bool Contains(string type)
return Control.GetAvailableTypeFromArray(new[] { type }) != null;
}

public bool TryGetObject(string type, out object value)
public bool TryGetObject(string type, Type objectType, out object value)
{
if (type == NSPasteboard.NSPasteboardTypeColor)
if (objectType == null || objectType == typeof(Color))
{
value = NSColor.FromPasteboard(Control)?.ToEto();
return true;
if (type == NSPasteboard.NSPasteboardTypeColor)
{
value = NSColor.FromPasteboard(Control)?.ToEto();
return true;
}
}
if (objectType == null || objectType == typeof(string))
{
if (type == NSPasteboard.NSPasteboardTypeString
|| type == NSPasteboard.NSPasteboardTypeTabularText
|| type == NSPasteboard.NSPasteboardTypeUrl
|| type == NSPasteboard.NSPasteboardTypeFileUrl
|| type == NSPasteboard.NSPasteboardTypeRTF
|| type == NSPasteboard.NSPasteboardTypeHTML)
{
value = GetString(type);
return true;
}
}
if (objectType == null || objectType == typeof(Bitmap))
{
if (type == NSPasteboard.NSPasteboardTypeTIFF
|| type == NSPasteboard.NSPasteboardTypePNG)
{
value = new Bitmap(new MemoryStream(GetData(type)));
return true;
}
}
value = null;
return false;
Expand All @@ -219,5 +244,7 @@ public bool TrySetObject(object value, string type)
public void SetObject(object value, string type) => Widget.SetObject(value, type);

public T GetObject<T>(string type) => Widget.GetObject<T>(type);
public object GetObject(string type, Type objectType) => Widget.GetObject(type, objectType);
public object GetObject(string type) => Widget.GetObject(type);
}
}
38 changes: 33 additions & 5 deletions src/Eto.Mac/Forms/MemoryDataObjectHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,20 +193,48 @@ public bool TrySetObject(object value, string type)
return false;
}

public bool TryGetObject(string type, out object value)
public bool TryGetObject(string type, Type objectType, out object value)
{
var colorItem = GetDataItem<ColorItem>(type);
if (colorItem != null)
if (objectType == null || objectType == typeof(Color))
{
value = colorItem.Value.ToEto();
return true;
var colorItem = GetDataItem<ColorItem>(type);
if (colorItem != null)
{
value = colorItem.Value.ToEto();
return true;
}
}
if (objectType == null || objectType == typeof(string))
{
if (type == NSPasteboard.NSPasteboardTypeString
|| type == NSPasteboard.NSPasteboardTypeTabularText
|| type == NSPasteboard.NSPasteboardTypeUrl
|| type == NSPasteboard.NSPasteboardTypeFileUrl
|| type == NSPasteboard.NSPasteboardTypeRTF
|| type == NSPasteboard.NSPasteboardTypeHTML)
{
value = GetString(type);
return true;
}
}
if (objectType == null || objectType == typeof(Bitmap))
{
if (type == NSPasteboard.NSPasteboardTypeTIFF
|| type == NSPasteboard.NSPasteboardTypePNG)
{
value = new Bitmap(new MemoryStream(GetData(type)));
return true;
}
}

value = null;
return false;
}

public void SetObject(object value, string type) => Widget.SetObject(value, type);

public T GetObject<T>(string type) => Widget.GetObject<T>(type);
public object GetObject(string type, Type objectType) => Widget.GetObject(type, objectType);
public object GetObject(string type) => Widget.GetObject(type);
}
}
9 changes: 9 additions & 0 deletions src/Eto.WinForms/Win32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -554,5 +554,14 @@ public enum SC : uint
{
CLOSE = 0xF060
}

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GlobalLock(IntPtr handle);

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool GlobalUnlock(IntPtr handle);

[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern int GlobalSize(IntPtr handle);
}
}
2 changes: 2 additions & 0 deletions src/Eto.Wpf/Forms/ClipboardHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public ClipboardHandler()
Control = new sw.DataObject();
}

public override sw.IDataObject ReadingDataObject => sw.Clipboard.GetDataObject();

public override string[] Types => sw.Clipboard.GetDataObject()?.GetFormats();

protected override void Update()
Expand Down
Loading

0 comments on commit e425a21

Please sign in to comment.