adding implementation for GetMatchingPair on macOS, streamlining wxDataObject

When adding the GetMatchinPair implementation, some clean-ups were done, for further details please see #1821
This commit is contained in:
Stefan Csomor 2020-04-26 17:50:33 +02:00
parent a98410504b
commit 7c61841d27
3 changed files with 89 additions and 68 deletions

View File

@ -24,7 +24,12 @@ public:
void WriteToSink(wxOSXDataSink *sink) const;
bool ReadFromSource(wxOSXDataSource *source);
bool ReadFromSource(wxDataObject *source);
bool CanReadFromSource(wxOSXDataSource *source) const;
bool CanReadFromSource(wxDataObject *source) const;
wxDataFormat GetSupportedFormatInSource(wxOSXDataSource *source) const;
wxDataFormat GetSupportedFormatInSource(wxDataObject *source) const;
#if wxOSX_USE_COCOA
// adds all the native formats (in descending order of preference) this data object supports

View File

@ -329,6 +329,36 @@ void wxDataObject::WriteToSink(wxOSXDataSink * datatransfer) const
}
}
bool wxDataObject::ReadFromSource(wxDataObject * source)
{
bool transferred = false;
size_t formatcount = source->GetFormatCount();
wxScopedArray<wxDataFormat> array(formatcount);
source->GetAllFormats( array.get() );
for (size_t i = 0; !transferred && i < formatcount; i++)
{
wxDataFormat format = array[i];
if ( IsSupported( format, wxDataObject::Set ) )
{
int size = source->GetDataSize( format );
transferred = true;
if (size == 0)
{
SetData( format, 0, 0 );
}
else
{
wxCharBuffer d(size);
source->GetDataHere( format, d.data() );
SetData( format, size, d.data() );
}
}
}
return transferred;
}
bool wxDataObject::ReadFromSource(wxOSXDataSource * source)
{
bool transferred = false;
@ -439,30 +469,58 @@ bool wxDataObject::ReadFromSource(wxOSXDataSource * source)
return transferred;
}
bool wxDataObject::CanReadFromSource( wxOSXDataSource * source ) const
wxDataFormat wxDataObject::GetSupportedFormatInSource(wxDataObject *source) const
{
bool hasData = false;
wxDataFormat format;
size_t formatcount = source->GetFormatCount();
wxScopedArray<wxDataFormat> array(formatcount);
source->GetAllFormats( array.get() );
for (size_t i = 0; i < formatcount; i++)
{
wxDataFormat testFormat = array[i];
if ( IsSupported( testFormat, wxDataObject::Set ) )
{
format = testFormat;
break;
}
}
return format;
}
wxDataFormat wxDataObject::GetSupportedFormatInSource(wxOSXDataSource *source) const
{
wxDataFormat format;
size_t formatcount = GetFormatCount(wxDataObject::Set);
wxScopedArray<wxDataFormat> array(formatcount);
GetAllFormats(array.get(), wxDataObject::Set);
wxString filenamesPassed;
for (size_t i = 0; !hasData && i < formatcount; i++)
for (size_t i = 0; i < formatcount; i++)
{
// go through the data in our order of preference
wxDataFormat dataFormat = array[i];
if (source->IsSupported(dataFormat))
{
hasData = true;
format = dataFormat;
break;
}
}
return hasData;
return format;
}
bool wxDataObject::CanReadFromSource( wxOSXDataSource * source ) const
{
return GetSupportedFormatInSource(source) != wxDF_INVALID;
}
bool wxDataObject::CanReadFromSource( wxDataObject * source ) const
{
return GetSupportedFormatInSource(source) != wxDF_INVALID;
}
void wxDataObject::AddSupportedTypes( CFMutableArrayRef cfarray) const
{

View File

@ -36,8 +36,22 @@ wxDragResult wxDropTarget::OnDragOver(
wxDataFormat wxDropTarget::GetMatchingPair()
{
wxFAIL_MSG("wxDropTarget::GetMatchingPair() not implemented in src/osx/dnd_osx.cpp");
return wxDF_INVALID;
wxDataFormat supported;
if (m_dataObject != NULL)
{
if ( wxDropSource* currentSource = wxDropSource::GetCurrentDropSource() )
{
wxDataObject* data = currentSource->GetDataObject();
if ( data )
supported = m_dataObject->GetSupportedFormatInSource(data);
}
if ( supported == wxDF_INVALID )
supported = m_dataObject->GetSupportedFormatInSource( m_currentDragPasteboard );
}
return supported;
}
bool wxDropTarget::OnDrop( wxCoord WXUNUSED(x), wxCoord WXUNUSED(y) )
@ -63,37 +77,7 @@ wxDragResult wxDropTarget::OnData(
bool wxDropTarget::CurrentDragHasSupportedFormat()
{
bool supported = false;
if (m_dataObject == NULL)
return false;
if ( wxDropSource* currentSource = wxDropSource::GetCurrentDropSource() )
{
wxDataObject* data = currentSource->GetDataObject();
if ( data )
{
size_t formatcount = data->GetFormatCount();
wxScopedArray<wxDataFormat> array(formatcount);
data->GetAllFormats( array.get() );
for (size_t i = 0; !supported && i < formatcount; i++)
{
wxDataFormat format = array[i];
if ( m_dataObject->IsSupported( format, wxDataObject::Set ) )
{
supported = true;
break;
}
}
}
}
if ( !supported )
{
supported = m_dataObject->CanReadFromSource( m_currentDragPasteboard );
}
return supported;
return GetMatchingPair() != wxDF_INVALID;
}
bool wxDropTarget::GetData()
@ -110,37 +94,11 @@ bool wxDropTarget::GetData()
wxDataObject* data = currentSource->GetDataObject();
if (data != NULL)
{
size_t formatcount = data->GetFormatCount();
wxScopedArray<wxDataFormat> array(formatcount);
data->GetAllFormats( array.get() );
for (size_t i = 0; !transferred && i < formatcount; i++)
{
wxDataFormat format = array[i];
if ( m_dataObject->IsSupported( format, wxDataObject::Set ) )
{
int size = data->GetDataSize( format );
transferred = true;
if (size == 0)
{
m_dataObject->SetData( format, 0, 0 );
}
else
{
wxCharBuffer d(size);
data->GetDataHere( format, d.data() );
m_dataObject->SetData( format, size, d.data() );
}
}
}
}
transferred = m_dataObject->ReadFromSource(data);
}
if ( !transferred )
{
transferred = m_dataObject->ReadFromSource(m_currentDragPasteboard);
}
return transferred;
}