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
bool IsStandard() const { return m_type > 0 && m_type < wxDF_PRIVATE; }
// adds all the native formats for this format to an array
void AddSupportedTypes(CFMutableArrayRef types) const;
// adds all the native formats for this format when calling a GetData
void AddSupportedTypesForGetting(CFMutableArrayRef types) const;
// adds all the native formats for this format when calling a SetData
void AddSupportedTypesForSetting(CFMutableArrayRef types) const;
private:
void DoAddSupportedTypes(CFMutableArrayRef types, bool forSetting) const;
void ClearNativeFormat();
wxDataFormatId m_type;

View File

@ -33,7 +33,7 @@ public:
#if wxOSX_USE_COCOA
// 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
};

View File

@ -93,7 +93,13 @@ wxDataFormat::NativeFormat wxDataFormat::GetFormatForType(wxDataFormatId type)
break;
case wxDF_UNICODETEXT:
#ifdef wxNEEDS_UTF8_FOR_TEXT_DATAOBJ
f = kUTTypeUTF8PlainText;
#elif defined(wxNEEDS_UTF16_FOR_TEXT_DATAOBJ)
f = kUTTypeUTF16PlainText;
#else
#error "one of wxNEEDS_UTF{8,16}_FOR_TEXT_DATAOBJ must be defined"
#endif
break;
case wxDF_HTML:
@ -125,7 +131,17 @@ void wxDataFormat::SetType( wxDataFormatId 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 )
{
@ -134,20 +150,18 @@ void wxDataFormat::AddSupportedTypes(CFMutableArrayRef cfarray) const
else
{
CFArrayAppendValue(cfarray, GetFormatForType(m_type));
// add additional accepted types
switch (GetType())
if ( forSetting )
{
case wxDF_UNICODETEXT:
CFArrayAppendValue(cfarray, kUTTypeUTF8PlainText);
break;
case wxDF_FILENAME:
CFArrayAppendValue(cfarray, kPasteboardTypeFileURLPromise);
break;
case wxDF_BITMAP:
CFArrayAppendValue(cfarray, kUTTypePICT);
break;
default:
break;
// add additional accepted types which we are ready to accept and can
// convert to our internal formats
switch (GetType())
{
case wxDF_FILENAME:
CFArrayAppendValue(cfarray, kPasteboardTypeFileURLPromise);
break;
default:
break;
}
}
}
}
@ -377,7 +391,7 @@ bool wxDataObject::ReadFromSource(wxOSXDataSource * source)
if (source->IsSupported(dataFormat))
{
wxCFMutableArrayRef<CFStringRef> typesarray;
dataFormat.AddSupportedTypes(typesarray);
dataFormat.AddSupportedTypesForSetting(typesarray);
size_t itemCount = source->GetItemCount();
for ( size_t itemIndex = 0; itemIndex < itemCount && !transferred; ++itemIndex)
@ -522,15 +536,19 @@ bool wxDataObject::CanReadFromSource( wxDataObject * source ) const
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);
wxScopedArray<wxDataFormat> array(GetFormatCount());
GetAllFormats(array.get(), wxDataObject::Set);
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)
{
wxCFMutableArrayRef<CFStringRef> typesarray;
dataFormat.AddSupportedTypes(typesarray);
dataFormat.AddSupportedTypesForSetting(typesarray);
return HasData(typesarray);
}
bool wxOSXDataSource::IsSupported(const wxDataObject &dataobj)
{
wxCFMutableArrayRef<CFStringRef> typesarray;
dataobj.AddSupportedTypes(typesarray);
dataobj.AddSupportedTypes(typesarray, wxDataObjectBase::Direction::Get);
return HasData(typesarray);
}
@ -431,6 +431,10 @@ wxDropSource* wxDropSource::GetCurrentDropSource()
return gCurrentSource;
}
#if __MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_13
typedef NSString* NSPasteboardType;
#endif
@interface wxPasteBoardWriter : NSObject<NSPasteboardWriting>
{
wxDataObject* m_data;
@ -447,10 +451,6 @@ wxDropSource* wxDropSource::GetCurrentDropSource()
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
{
wxDataFormat format((wxDataFormat::NativeFormat) type);
@ -464,7 +464,7 @@ typedef NSString* NSPasteboardType;
- (nonnull NSArray<NSPasteboardType> *)writableTypesForPasteboard:(nonnull NSPasteboard *)pasteboard
{
wxCFMutableArrayRef<CFStringRef> typesarray;
m_data->AddSupportedTypes(typesarray);
m_data->AddSupportedTypes(typesarray, wxDataObjectBase::Direction::Get);
return (NSArray<NSPasteboardType>*) typesarray.autorelease();
}

View File

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