Replacing Handle based code with CFData

This commit is contained in:
Stefan Csomor
2020-06-30 08:19:08 +02:00
parent 505a6f0807
commit 1d67ef6cef
2 changed files with 12 additions and 31 deletions

View File

@@ -52,7 +52,7 @@ protected :
void Init() ; void Init() ;
void Clear() ; void Clear() ;
void* m_pictHandle ; CFDataRef m_pictData ;
bool m_pictCreated ; bool m_pictCreated ;
}; };

View File

@@ -650,12 +650,7 @@ void wxBitmapDataObject::SetBitmap( const wxBitmap& rBitmap )
CGImageDestinationFinalize( destination ); CGImageDestinationFinalize( destination );
CFRelease( destination ); CFRelease( destination );
} }
m_pictHandle = NewHandle(CFDataGetLength(data)); m_pictData = data;
if ( m_pictHandle )
{
memcpy( *(Handle)m_pictHandle, (const char *)CFDataGetBytePtr(data), CFDataGetLength(data) );
}
CFRelease( data );
CGImageRelease(cgImageRef); CGImageRelease(cgImageRef);
} }
@@ -663,23 +658,23 @@ void wxBitmapDataObject::SetBitmap( const wxBitmap& rBitmap )
void wxBitmapDataObject::Init() void wxBitmapDataObject::Init()
{ {
m_pictHandle = NULL; m_pictData = NULL;
m_pictCreated = false; m_pictCreated = false;
} }
void wxBitmapDataObject::Clear() void wxBitmapDataObject::Clear()
{ {
if (m_pictHandle != NULL) if (m_pictData != NULL)
{ {
DisposeHandle( (Handle) m_pictHandle ); CFRelease( m_pictData );
m_pictHandle = NULL; m_pictData = NULL;
} }
m_pictCreated = false; m_pictCreated = false;
} }
bool wxBitmapDataObject::GetDataHere( void *pBuf ) const bool wxBitmapDataObject::GetDataHere( void *pBuf ) const
{ {
if (m_pictHandle == NULL) if (m_pictData == NULL)
{ {
wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") ); wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
return false; return false;
@@ -688,30 +683,19 @@ bool wxBitmapDataObject::GetDataHere( void *pBuf ) const
if (pBuf == NULL) if (pBuf == NULL)
return false; return false;
memcpy( pBuf, *(Handle)m_pictHandle, GetHandleSize( (Handle)m_pictHandle ) ); memcpy( pBuf, (const char *)CFDataGetBytePtr(m_pictData), CFDataGetLength(m_pictData) );
return true; return true;
} }
size_t wxBitmapDataObject::GetDataSize() const size_t wxBitmapDataObject::GetDataSize() const
{ {
if (m_pictHandle != NULL) if (m_pictData != NULL)
return GetHandleSize( (Handle)m_pictHandle ); return CFDataGetLength(m_pictData);
else else
return 0; return 0;
} }
Handle MacCreateDataReferenceHandle(Handle theDataHandle)
{
Handle dataRef = NULL;
OSErr err = noErr;
// Create a data reference handle for our data.
err = PtrToHand( &theDataHandle, &dataRef, sizeof(Handle));
return dataRef;
}
bool wxBitmapDataObject::SetData( size_t nSize, const void *pBuf ) bool wxBitmapDataObject::SetData( size_t nSize, const void *pBuf )
{ {
Clear(); Clear();
@@ -719,19 +703,16 @@ bool wxBitmapDataObject::SetData( size_t nSize, const void *pBuf )
if ((pBuf == NULL) || (nSize == 0)) if ((pBuf == NULL) || (nSize == 0))
return false; return false;
Handle picHandle = NewHandle( nSize );
memcpy( *picHandle, pBuf, nSize );
m_pictHandle = picHandle;
CGImageRef cgImageRef = 0; CGImageRef cgImageRef = 0;
CFDataRef data = CFDataCreateWithBytesNoCopy( kCFAllocatorDefault, (const UInt8*) pBuf, nSize, kCFAllocatorNull); CFDataRef data = CFDataCreate( kCFAllocatorDefault, (const UInt8*) pBuf, nSize);
CGImageSourceRef source = CGImageSourceCreateWithData( data, NULL ); CGImageSourceRef source = CGImageSourceCreateWithData( data, NULL );
if ( source ) if ( source )
{ {
cgImageRef = CGImageSourceCreateImageAtIndex(source, 0, NULL); cgImageRef = CGImageSourceCreateImageAtIndex(source, 0, NULL);
CFRelease( source ); CFRelease( source );
} }
CFRelease( data ); m_pictData = data;
if ( cgImageRef ) if ( cgImageRef )
{ {