adding data-transfer direction when getting native datatypes

we are publishing exactly what we have internally when data has to be get, and when setting, we might have more formats we can support
This commit is contained in:
Stefan Csomor
2020-07-12 17:53:10 +02:00
parent b7057c336f
commit b2d4a9dedc
5 changed files with 52 additions and 29 deletions

View File

@@ -61,9 +61,14 @@ public:
// returns true if the format is one of those defined in wxDataFormatId // returns true if the format is one of those defined in wxDataFormatId
bool IsStandard() const { return m_type > 0 && m_type < wxDF_PRIVATE; } bool IsStandard() const { return m_type > 0 && m_type < wxDF_PRIVATE; }
// adds all the native formats for this format to an array // adds all the native formats for this format when calling a GetData
void AddSupportedTypes(CFMutableArrayRef types) const; void AddSupportedTypesForGetting(CFMutableArrayRef types) const;
// adds all the native formats for this format when calling a SetData
void AddSupportedTypesForSetting(CFMutableArrayRef types) const;
private: private:
void DoAddSupportedTypes(CFMutableArrayRef types, bool forSetting) const;
void ClearNativeFormat(); void ClearNativeFormat();
wxDataFormatId m_type; wxDataFormatId m_type;

View File

@@ -33,7 +33,7 @@ public:
#if wxOSX_USE_COCOA #if wxOSX_USE_COCOA
// adds all the native formats (in descending order of preference) this data object supports // adds all the native formats (in descending order of preference) this data object supports
virtual void AddSupportedTypes( CFMutableArrayRef cfarray) const; virtual void AddSupportedTypes( CFMutableArrayRef cfarray, Direction dir ) const;
#endif #endif
}; };

View File

@@ -93,7 +93,13 @@ wxDataFormat::NativeFormat wxDataFormat::GetFormatForType(wxDataFormatId type)
break; break;
case wxDF_UNICODETEXT: case wxDF_UNICODETEXT:
#ifdef wxNEEDS_UTF8_FOR_TEXT_DATAOBJ
f = kUTTypeUTF8PlainText;
#elif defined(wxNEEDS_UTF16_FOR_TEXT_DATAOBJ)
f = kUTTypeUTF16PlainText; f = kUTTypeUTF16PlainText;
#else
#error "one of wxNEEDS_UTF{8,16}_FOR_TEXT_DATAOBJ must be defined"
#endif
break; break;
case wxDF_HTML: case wxDF_HTML:
@@ -125,7 +131,17 @@ void wxDataFormat::SetType( wxDataFormatId dataType )
m_format = GetFormatForType(dataType); m_format = GetFormatForType(dataType);
} }
void wxDataFormat::AddSupportedTypes(CFMutableArrayRef cfarray) const void wxDataFormat::AddSupportedTypesForSetting(CFMutableArrayRef types) const
{
DoAddSupportedTypes(types, true);
}
void wxDataFormat::AddSupportedTypesForGetting(CFMutableArrayRef types) const
{
DoAddSupportedTypes(types, false);
}
void wxDataFormat::DoAddSupportedTypes(CFMutableArrayRef cfarray, bool forSetting) const
{ {
if ( GetType() == wxDF_PRIVATE ) if ( GetType() == wxDF_PRIVATE )
{ {
@@ -134,20 +150,18 @@ void wxDataFormat::AddSupportedTypes(CFMutableArrayRef cfarray) const
else else
{ {
CFArrayAppendValue(cfarray, GetFormatForType(m_type)); CFArrayAppendValue(cfarray, GetFormatForType(m_type));
// add additional accepted types if ( forSetting )
switch (GetType())
{ {
case wxDF_UNICODETEXT: // add additional accepted types which we are ready to accept and can
CFArrayAppendValue(cfarray, kUTTypeUTF8PlainText); // convert to our internal formats
break; switch (GetType())
case wxDF_FILENAME: {
CFArrayAppendValue(cfarray, kPasteboardTypeFileURLPromise); case wxDF_FILENAME:
break; CFArrayAppendValue(cfarray, kPasteboardTypeFileURLPromise);
case wxDF_BITMAP: break;
CFArrayAppendValue(cfarray, kUTTypePICT); default:
break; break;
default: }
break;
} }
} }
} }
@@ -377,7 +391,7 @@ bool wxDataObject::ReadFromSource(wxOSXDataSource * source)
if (source->IsSupported(dataFormat)) if (source->IsSupported(dataFormat))
{ {
wxCFMutableArrayRef<CFStringRef> typesarray; wxCFMutableArrayRef<CFStringRef> typesarray;
dataFormat.AddSupportedTypes(typesarray); dataFormat.AddSupportedTypesForSetting(typesarray);
size_t itemCount = source->GetItemCount(); size_t itemCount = source->GetItemCount();
for ( size_t itemIndex = 0; itemIndex < itemCount && !transferred; ++itemIndex) for ( size_t itemIndex = 0; itemIndex < itemCount && !transferred; ++itemIndex)
@@ -522,15 +536,19 @@ bool wxDataObject::CanReadFromSource( wxDataObject * source ) const
return GetSupportedFormatInSource(source) != wxDF_INVALID; return GetSupportedFormatInSource(source) != wxDF_INVALID;
} }
void wxDataObject::AddSupportedTypes( CFMutableArrayRef cfarray) const void wxDataObject::AddSupportedTypes( CFMutableArrayRef cfarray, Direction dir) const
{ {
size_t nFormats = GetFormatCount(wxDataObject::Set); size_t nFormats = GetFormatCount(wxDataObject::Set);
wxScopedArray<wxDataFormat> array(GetFormatCount()); wxScopedArray<wxDataFormat> array(GetFormatCount());
GetAllFormats(array.get(), wxDataObject::Set); GetAllFormats(array.get(), wxDataObject::Set);
for (size_t i = 0; i < nFormats; i++) for (size_t i = 0; i < nFormats; i++)
array[i].AddSupportedTypes(cfarray); {
if ( dir == Direction::Get)
array[i].AddSupportedTypesForGetting(cfarray);
else
array[i].AddSupportedTypesForSetting(cfarray);
}
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@@ -50,14 +50,14 @@ wxOSXDataSourceItem::~wxOSXDataSourceItem()
bool wxOSXDataSource::IsSupported(const wxDataFormat &dataFormat) bool wxOSXDataSource::IsSupported(const wxDataFormat &dataFormat)
{ {
wxCFMutableArrayRef<CFStringRef> typesarray; wxCFMutableArrayRef<CFStringRef> typesarray;
dataFormat.AddSupportedTypes(typesarray); dataFormat.AddSupportedTypesForSetting(typesarray);
return HasData(typesarray); return HasData(typesarray);
} }
bool wxOSXDataSource::IsSupported(const wxDataObject &dataobj) bool wxOSXDataSource::IsSupported(const wxDataObject &dataobj)
{ {
wxCFMutableArrayRef<CFStringRef> typesarray; wxCFMutableArrayRef<CFStringRef> typesarray;
dataobj.AddSupportedTypes(typesarray); dataobj.AddSupportedTypes(typesarray, wxDataObjectBase::Direction::Get);
return HasData(typesarray); return HasData(typesarray);
} }
@@ -431,6 +431,10 @@ wxDropSource* wxDropSource::GetCurrentDropSource()
return gCurrentSource; return gCurrentSource;
} }
#if __MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_13
typedef NSString* NSPasteboardType;
#endif
@interface wxPasteBoardWriter : NSObject<NSPasteboardWriting> @interface wxPasteBoardWriter : NSObject<NSPasteboardWriting>
{ {
wxDataObject* m_data; wxDataObject* m_data;
@@ -447,10 +451,6 @@ wxDropSource* wxDropSource::GetCurrentDropSource()
return self; return self;
} }
#if __MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_13
typedef NSString* NSPasteboardType;
#endif
- (nullable id)pasteboardPropertyListForType:(nonnull NSPasteboardType)type - (nullable id)pasteboardPropertyListForType:(nonnull NSPasteboardType)type
{ {
wxDataFormat format((wxDataFormat::NativeFormat) type); wxDataFormat format((wxDataFormat::NativeFormat) type);
@@ -464,7 +464,7 @@ typedef NSString* NSPasteboardType;
- (nonnull NSArray<NSPasteboardType> *)writableTypesForPasteboard:(nonnull NSPasteboard *)pasteboard - (nonnull NSArray<NSPasteboardType> *)writableTypesForPasteboard:(nonnull NSPasteboard *)pasteboard
{ {
wxCFMutableArrayRef<CFStringRef> typesarray; wxCFMutableArrayRef<CFStringRef> typesarray;
m_data->AddSupportedTypes(typesarray); m_data->AddSupportedTypes(typesarray, wxDataObjectBase::Direction::Get);
return (NSArray<NSPasteboardType>*) typesarray.autorelease(); return (NSArray<NSPasteboardType>*) typesarray.autorelease();
} }

View File

@@ -3215,7 +3215,7 @@ void wxWidgetCocoaImpl::SetDropTarget(wxDropTarget* target)
if (dobj) if (dobj)
{ {
wxCFMutableArrayRef<CFStringRef> typesarray; wxCFMutableArrayRef<CFStringRef> typesarray;
dobj->AddSupportedTypes(typesarray); dobj->AddSupportedTypes(typesarray, wxDataObjectBase::Direction::Get);
NSView* targetView = m_osxView; NSView* targetView = m_osxView;
if ([m_osxView isKindOfClass:[NSScrollView class]]) if ([m_osxView isKindOfClass:[NSScrollView class]])
targetView = [(NSScrollView*)m_osxView documentView]; targetView = [(NSScrollView*)m_osxView documentView];