From 2cdd43432ec04e1662945c4c4be8c56a1cc45764 Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Sun, 2 Feb 2003 13:22:00 +0000 Subject: [PATCH] native picture clipboard support improved git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_4_BRANCH@19064 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- src/mac/bitmap.cpp | 142 ++++++++++++++----------------------- src/mac/carbon/bitmap.cpp | 142 ++++++++++++++----------------------- src/mac/carbon/clipbrd.cpp | 64 ++++++++--------- src/mac/carbon/dataobj.cpp | 76 +++++++++----------- src/mac/clipbrd.cpp | 64 ++++++++--------- src/mac/dataobj.cpp | 76 +++++++++----------- 6 files changed, 238 insertions(+), 326 deletions(-) diff --git a/src/mac/bitmap.cpp b/src/mac/bitmap.cpp index cd1c398c99..78a8bd0b68 100644 --- a/src/mac/bitmap.cpp +++ b/src/mac/bitmap.cpp @@ -369,6 +369,9 @@ wxBitmapRefData::wxBitmapRefData() // TODO move this to a public function of Bitmap Ref static void DisposeBitmapRefData(wxBitmapRefData *data) { + if ( !data ) + return ; + switch (data->m_bitmapType) { case kMacBitmapTypePict : @@ -637,11 +640,38 @@ int wxBitmap::GetBitmapType() const void wxBitmap::SetHBITMAP(WXHBITMAP bmp) { - DisposeBitmapRefData( M_BITMAPDATA ) ; + if (!M_BITMAPDATA) + m_refData = new wxBitmapRefData; + else + DisposeBitmapRefData( M_BITMAPDATA ) ; M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ; M_BITMAPDATA->m_hBitmap = bmp ; - M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hBitmap != NULL ) ; + M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hBitmap != NULL ) ; +} + +void wxBitmap::SetHICON(WXHICON ico) +{ + if (!M_BITMAPDATA) + m_refData = new wxBitmapRefData; + else + DisposeBitmapRefData( M_BITMAPDATA ) ; + + M_BITMAPDATA->m_bitmapType = kMacBitmapTypeIcon ; + M_BITMAPDATA->m_hIcon = ico ; + M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hIcon != NULL ) ; +} + +void wxBitmap::SetPict(WXHMETAFILE pict) +{ + if (!M_BITMAPDATA) + m_refData = new wxBitmapRefData; + else + DisposeBitmapRefData( M_BITMAPDATA ) ; + + M_BITMAPDATA->m_bitmapType = kMacBitmapTypePict ; + M_BITMAPDATA->m_hPict = pict ; + M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hPict != NULL ) ; } bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type) @@ -1008,94 +1038,30 @@ WXHBITMAP wxBitmap::GetHBITMAP() const WXHMETAFILE wxBitmap::GetPict() const { - wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); + wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); - PicHandle picture; // This is the returned picture + PicHandle picture = NULL ; // This is the returned picture - // If bitmap already in Pict format return pointer - if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypePict) { - return M_BITMAPDATA->m_hPict; - } - else if(M_BITMAPDATA->m_bitmapType != kMacBitmapTypeGrafWorld) { - // Invalid bitmap - return NULL; - } - - RGBColor gray = { 0xCCCC ,0xCCCC , 0xCCCC } ; - RGBColor white = { 0xffff ,0xffff , 0xffff } ; - RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ; - CGrafPtr origPort; - GDHandle origDev ; - wxMask *mask; - Rect portRect ; - - GetPortBounds( (GWorldPtr) GetHBITMAP() , &portRect ) ; - int width = portRect.right - portRect.left ; - int height = portRect.bottom - portRect.top ; - - LockPixels( GetGWorldPixMap( (GWorldPtr) GetHBITMAP() ) ) ; - GetGWorld( &origPort , &origDev ) ; - - mask = GetMask(); - - SetGWorld( (GWorldPtr) GetHBITMAP() , NULL ) ; - - picture = OpenPicture(&portRect); // open a picture, this disables drawing - if(!picture) { - return NULL; - } - - if( mask ) - { -#ifdef __DARWIN__ - RGBColor trans = white; -#else - RGBBackColor( &gray ); - EraseRect( &portRect ); - RGBColor trans = gray; -#endif - RGBForeColor( &black ) ; - RGBBackColor( &white ) ; - PenMode(transparent); - - for ( int y = 0 ; y < height ; ++y ) - { - for( int x = 0 ; x < width ; ++x ) - { - if ( !mask->PointMasked(x,y) ) - { - RGBColor col ; - - GetCPixel( x + portRect.left , y + portRect.top , &col ) ; - SetCPixel( x + portRect.left , y + portRect.top , &col ) ; - } - else { - // With transparency this sets a blank pixel - SetCPixel( x + portRect.left , y + portRect.top , &trans); - } - } - } - } - else - { - RGBBackColor( &gray ) ; - EraseRect(&portRect); - RGBForeColor( &black ) ; - RGBBackColor( &white ) ; - - CopyBits(GetPortBitMapForCopyBits( (GWorldPtr) GetHBITMAP()), - // src PixMap - we copy image over itself - - GetPortBitMapForCopyBits( (GWorldPtr) GetHBITMAP()), - // dst PixMap - no drawing occurs - &portRect, // srcRect - it will be recorded and compressed - - &portRect, // dstRect - into the picture that is open - - srcCopy,NULL); // copyMode and no clip region - } - ClosePicture(); // We are done recording the picture - UnlockPixels( GetGWorldPixMap( (GWorldPtr) GetHBITMAP() ) ) ; - SetGWorld( origPort , origDev ) ; - - return picture; // return our groovy pict handle + // If bitmap already in Pict format return pointer + if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypePict) { + return M_BITMAPDATA->m_hPict; + } + else if(M_BITMAPDATA->m_bitmapType != kMacBitmapTypeGrafWorld) { + // Invalid bitmap + return NULL; + } + else + { + if ( GetMask() ) + { + picture = wxMacCreatePict( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) , MAC_WXHBITMAP(GetMask()->GetMaskBitmap() ) ) ; + } + else + { + picture = wxMacCreatePict( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) , NULL ) ; + } + } + return picture ; } /* diff --git a/src/mac/carbon/bitmap.cpp b/src/mac/carbon/bitmap.cpp index cd1c398c99..78a8bd0b68 100644 --- a/src/mac/carbon/bitmap.cpp +++ b/src/mac/carbon/bitmap.cpp @@ -369,6 +369,9 @@ wxBitmapRefData::wxBitmapRefData() // TODO move this to a public function of Bitmap Ref static void DisposeBitmapRefData(wxBitmapRefData *data) { + if ( !data ) + return ; + switch (data->m_bitmapType) { case kMacBitmapTypePict : @@ -637,11 +640,38 @@ int wxBitmap::GetBitmapType() const void wxBitmap::SetHBITMAP(WXHBITMAP bmp) { - DisposeBitmapRefData( M_BITMAPDATA ) ; + if (!M_BITMAPDATA) + m_refData = new wxBitmapRefData; + else + DisposeBitmapRefData( M_BITMAPDATA ) ; M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ; M_BITMAPDATA->m_hBitmap = bmp ; - M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hBitmap != NULL ) ; + M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hBitmap != NULL ) ; +} + +void wxBitmap::SetHICON(WXHICON ico) +{ + if (!M_BITMAPDATA) + m_refData = new wxBitmapRefData; + else + DisposeBitmapRefData( M_BITMAPDATA ) ; + + M_BITMAPDATA->m_bitmapType = kMacBitmapTypeIcon ; + M_BITMAPDATA->m_hIcon = ico ; + M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hIcon != NULL ) ; +} + +void wxBitmap::SetPict(WXHMETAFILE pict) +{ + if (!M_BITMAPDATA) + m_refData = new wxBitmapRefData; + else + DisposeBitmapRefData( M_BITMAPDATA ) ; + + M_BITMAPDATA->m_bitmapType = kMacBitmapTypePict ; + M_BITMAPDATA->m_hPict = pict ; + M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hPict != NULL ) ; } bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type) @@ -1008,94 +1038,30 @@ WXHBITMAP wxBitmap::GetHBITMAP() const WXHMETAFILE wxBitmap::GetPict() const { - wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); + wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); - PicHandle picture; // This is the returned picture + PicHandle picture = NULL ; // This is the returned picture - // If bitmap already in Pict format return pointer - if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypePict) { - return M_BITMAPDATA->m_hPict; - } - else if(M_BITMAPDATA->m_bitmapType != kMacBitmapTypeGrafWorld) { - // Invalid bitmap - return NULL; - } - - RGBColor gray = { 0xCCCC ,0xCCCC , 0xCCCC } ; - RGBColor white = { 0xffff ,0xffff , 0xffff } ; - RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ; - CGrafPtr origPort; - GDHandle origDev ; - wxMask *mask; - Rect portRect ; - - GetPortBounds( (GWorldPtr) GetHBITMAP() , &portRect ) ; - int width = portRect.right - portRect.left ; - int height = portRect.bottom - portRect.top ; - - LockPixels( GetGWorldPixMap( (GWorldPtr) GetHBITMAP() ) ) ; - GetGWorld( &origPort , &origDev ) ; - - mask = GetMask(); - - SetGWorld( (GWorldPtr) GetHBITMAP() , NULL ) ; - - picture = OpenPicture(&portRect); // open a picture, this disables drawing - if(!picture) { - return NULL; - } - - if( mask ) - { -#ifdef __DARWIN__ - RGBColor trans = white; -#else - RGBBackColor( &gray ); - EraseRect( &portRect ); - RGBColor trans = gray; -#endif - RGBForeColor( &black ) ; - RGBBackColor( &white ) ; - PenMode(transparent); - - for ( int y = 0 ; y < height ; ++y ) - { - for( int x = 0 ; x < width ; ++x ) - { - if ( !mask->PointMasked(x,y) ) - { - RGBColor col ; - - GetCPixel( x + portRect.left , y + portRect.top , &col ) ; - SetCPixel( x + portRect.left , y + portRect.top , &col ) ; - } - else { - // With transparency this sets a blank pixel - SetCPixel( x + portRect.left , y + portRect.top , &trans); - } - } - } - } - else - { - RGBBackColor( &gray ) ; - EraseRect(&portRect); - RGBForeColor( &black ) ; - RGBBackColor( &white ) ; - - CopyBits(GetPortBitMapForCopyBits( (GWorldPtr) GetHBITMAP()), - // src PixMap - we copy image over itself - - GetPortBitMapForCopyBits( (GWorldPtr) GetHBITMAP()), - // dst PixMap - no drawing occurs - &portRect, // srcRect - it will be recorded and compressed - - &portRect, // dstRect - into the picture that is open - - srcCopy,NULL); // copyMode and no clip region - } - ClosePicture(); // We are done recording the picture - UnlockPixels( GetGWorldPixMap( (GWorldPtr) GetHBITMAP() ) ) ; - SetGWorld( origPort , origDev ) ; - - return picture; // return our groovy pict handle + // If bitmap already in Pict format return pointer + if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypePict) { + return M_BITMAPDATA->m_hPict; + } + else if(M_BITMAPDATA->m_bitmapType != kMacBitmapTypeGrafWorld) { + // Invalid bitmap + return NULL; + } + else + { + if ( GetMask() ) + { + picture = wxMacCreatePict( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) , MAC_WXHBITMAP(GetMask()->GetMaskBitmap() ) ) ; + } + else + { + picture = wxMacCreatePict( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) , NULL ) ; + } + } + return picture ; } /* diff --git a/src/mac/carbon/clipbrd.cpp b/src/mac/carbon/clipbrd.cpp index 76952a442e..2c0eafd242 100644 --- a/src/mac/carbon/clipbrd.cpp +++ b/src/mac/carbon/clipbrd.cpp @@ -23,7 +23,7 @@ #include "wx/intl.h" #include "wx/log.h" -#include "wx/mac/private.h" +#include "wx/mac/uma.h" #ifndef __DARWIN__ #include #endif @@ -44,7 +44,8 @@ void *wxGetClipboardData(wxDataFormat dataFormat, long *len) #else OSStatus err = noErr ; #endif - void * data = NULL ; + void * data = NULL ; + Size byteCount; switch (dataFormat.GetType()) { @@ -54,6 +55,9 @@ void *wxGetClipboardData(wxDataFormat dataFormat, long *len) case wxDF_TEXT: break; + case wxDF_BITMAP : + case wxDF_METAFILE : + break ; default: { wxLogError(_("Unsupported clipboard format.")); @@ -68,7 +72,6 @@ void *wxGetClipboardData(wxDataFormat dataFormat, long *len) if ( err != noTypeErr && err != memFullErr ) { ScrapFlavorFlags flavorFlags; - Size byteCount; if (( err = GetScrapFlavorFlags( scrapRef, dataFormat.GetFormatId(), &flavorFlags )) == noErr) { @@ -101,7 +104,7 @@ void *wxGetClipboardData(wxDataFormat dataFormat, long *len) HUnlock( datahandle ) ; if ( GetHandleSize( datahandle ) > 0 ) { - long byteCount = GetHandleSize( datahandle ) ; + byteCount = GetHandleSize( datahandle ) ; if ( dataFormat.GetType() == wxDF_TEXT ) data = new char[ byteCount + 1] ; else @@ -122,7 +125,7 @@ void *wxGetClipboardData(wxDataFormat dataFormat, long *len) } if ( dataFormat.GetType() == wxDF_TEXT && wxApp::s_macDefaultEncodingIsPC ) { - wxMacConvertToPC((char*)data) ; + wxMacConvertToPC((char*)data,(char*)data,byteCount) ; } return data; } @@ -240,43 +243,38 @@ bool wxClipboard::AddData( wxDataObject *data ) { mac = textDataObject->GetText() ; } - #if !TARGET_CARBON - err = PutScrap( mac.Length() , 'TEXT' , mac.c_str() ) ; - #else - ScrapRef scrap; - err = GetCurrentScrap (&scrap); - if ( !err ) - { - err = PutScrapFlavor (scrap, 'TEXT', 0, mac.Length(), mac.c_str()); - } - #endif + err = UMAPutScrap( mac.Length() , 'TEXT' , (void*) mac.c_str() ) ; } break ; #if wxUSE_DRAG_AND_DROP case wxDF_METAFILE: { - wxMetafileDataObject* metaFileDataObject = + wxMetafileDataObject* metaFileDataObject = (wxMetafileDataObject*) data; - wxMetafile metaFile = metaFileDataObject->GetMetafile(); - PicHandle pict = (PicHandle) metaFile.GetHMETAFILE() ; - HLock( (Handle) pict ) ; - #if !TARGET_CARBON - err = PutScrap( GetHandleSize( (Handle) pict ) , 'PICT' , *pict ) ; - #else - ScrapRef scrap; - err = GetCurrentScrap (&scrap); - if ( !err ) - { - err = PutScrapFlavor (scrap, 'PICT', 0, GetHandleSize((Handle) pict), *pict); - } - #endif - HUnlock( (Handle) pict ) ; + wxMetafile metaFile = metaFileDataObject->GetMetafile(); + PicHandle pict = (PicHandle) metaFile.GetHMETAFILE() ; + HLock( (Handle) pict ) ; + err = UMAPutScrap( GetHandleSize( (Handle) pict ) , 'PICT' , *pict ) ; + HUnlock( (Handle) pict ) ; } break ; #endif case wxDF_BITMAP: case wxDF_DIB: + { + PicHandle pict = NULL ; + + wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data ; + bool created = bitmapDataObject->GetBitmap().GetBitmapType() != kMacBitmapTypePict ; + pict = (PicHandle) bitmapDataObject->GetBitmap().GetPict() ; + + HLock( (Handle) pict ) ; + err = UMAPutScrap( GetHandleSize( (Handle) pict ) , 'PICT' , *pict ) ; + HUnlock( (Handle) pict ) ; + if ( created && pict) + KillPicture( pict ) ; + } default: break ; } @@ -375,8 +373,10 @@ bool wxClipboard::GetData( wxDataObject& data ) switch ( format.GetType() ) { - case wxDF_TEXT: - case wxDF_OEMTEXT: + case wxDF_TEXT : + case wxDF_OEMTEXT : + case wxDF_BITMAP : + case wxDF_METAFILE : { long len ; char* s = (char*)wxGetClipboardData(format, &len ); diff --git a/src/mac/carbon/dataobj.cpp b/src/mac/carbon/dataobj.cpp index c2f75326c0..049622ffb3 100644 --- a/src/mac/carbon/dataobj.cpp +++ b/src/mac/carbon/dataobj.cpp @@ -230,34 +230,51 @@ wxBitmapDataObject::wxBitmapDataObject( : wxBitmapDataObjectBase(rBitmap) { Init(); - - DoConvertToPng(); + if ( m_bitmap.Ok() ) + { + m_pngData = m_bitmap.GetPict() ; + m_pngSize = GetHandleSize( (Handle) m_pngData ) ; + } } wxBitmapDataObject::~wxBitmapDataObject() { - Clear(); + if ( m_pngData && m_bitmap.GetBitmapType() != kMacBitmapTypePict ) + { + KillPicture( (PicHandle) m_pngData ) ; + } + m_pngData = NULL ; + m_pngSize = 0 ; } void wxBitmapDataObject::SetBitmap( const wxBitmap& rBitmap ) { - ClearAll(); + if ( m_pngData && m_bitmap.GetBitmapType() != kMacBitmapTypePict ) + { + KillPicture( (PicHandle) m_pngData ) ; + } + m_pngData = NULL ; + m_pngSize = 0 ; wxBitmapDataObjectBase::SetBitmap(rBitmap); - DoConvertToPng(); + if ( m_bitmap.Ok() ) + { + m_pngData = m_bitmap.GetPict() ; + m_pngSize = GetHandleSize( (Handle) m_pngData ) ; + } } bool wxBitmapDataObject::GetDataHere( void* pBuf ) const { - if (!m_pngSize) + if (!m_pngData) { wxFAIL_MSG(wxT("attempt to copy empty bitmap failed")); return FALSE; } - memcpy(pBuf, m_pngData, m_pngSize); + memcpy(pBuf, *(Handle)m_pngData, GetHandleSize((Handle)m_pngData)); return TRUE; } @@ -267,41 +284,14 @@ bool wxBitmapDataObject::SetData( ) { Clear(); - m_pngSize = nSize; - m_pngData = malloc(m_pngSize); - - memcpy(m_pngData, pBuf, m_pngSize); - - wxMemoryInputStream vMstream((char*)m_pngData, m_pngSize); - wxImage vImage; - wxPNGHandler vHandler; - - if (!vHandler.LoadFile(&vImage, vMstream)) - { - return FALSE; - } - - m_bitmap = wxBitmap( vImage ) ; + PicHandle picHandle = (PicHandle) NewHandle( nSize ) ; + memcpy( *picHandle , pBuf , nSize ) ; + m_pngData = picHandle ; + m_pngSize = GetHandleSize( (Handle) picHandle ) ; + Rect frame = (**picHandle).picFrame ; + + m_bitmap.SetPict( m_pngData ) ; + m_bitmap.SetWidth( frame.right - frame.left ) ; + m_bitmap.SetHeight( frame.bottom - frame.top ) ; return m_bitmap.Ok(); } - -void wxBitmapDataObject::DoConvertToPng() -{ - if (!m_bitmap.Ok()) - return; - - wxCHECK_RET( wxImage::FindHandler(wxBITMAP_TYPE_PNG) != NULL, - wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") ); - - wxImage image = m_bitmap.ConvertToImage(); - - wxCountingOutputStream count; - image.SaveFile(count, wxBITMAP_TYPE_PNG); - - m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ??? - m_pngData = malloc(m_pngSize); - - wxMemoryOutputStream mstream((char*) m_pngData, m_pngSize); - image.SaveFile(mstream, wxBITMAP_TYPE_PNG); -} - diff --git a/src/mac/clipbrd.cpp b/src/mac/clipbrd.cpp index 76952a442e..2c0eafd242 100644 --- a/src/mac/clipbrd.cpp +++ b/src/mac/clipbrd.cpp @@ -23,7 +23,7 @@ #include "wx/intl.h" #include "wx/log.h" -#include "wx/mac/private.h" +#include "wx/mac/uma.h" #ifndef __DARWIN__ #include #endif @@ -44,7 +44,8 @@ void *wxGetClipboardData(wxDataFormat dataFormat, long *len) #else OSStatus err = noErr ; #endif - void * data = NULL ; + void * data = NULL ; + Size byteCount; switch (dataFormat.GetType()) { @@ -54,6 +55,9 @@ void *wxGetClipboardData(wxDataFormat dataFormat, long *len) case wxDF_TEXT: break; + case wxDF_BITMAP : + case wxDF_METAFILE : + break ; default: { wxLogError(_("Unsupported clipboard format.")); @@ -68,7 +72,6 @@ void *wxGetClipboardData(wxDataFormat dataFormat, long *len) if ( err != noTypeErr && err != memFullErr ) { ScrapFlavorFlags flavorFlags; - Size byteCount; if (( err = GetScrapFlavorFlags( scrapRef, dataFormat.GetFormatId(), &flavorFlags )) == noErr) { @@ -101,7 +104,7 @@ void *wxGetClipboardData(wxDataFormat dataFormat, long *len) HUnlock( datahandle ) ; if ( GetHandleSize( datahandle ) > 0 ) { - long byteCount = GetHandleSize( datahandle ) ; + byteCount = GetHandleSize( datahandle ) ; if ( dataFormat.GetType() == wxDF_TEXT ) data = new char[ byteCount + 1] ; else @@ -122,7 +125,7 @@ void *wxGetClipboardData(wxDataFormat dataFormat, long *len) } if ( dataFormat.GetType() == wxDF_TEXT && wxApp::s_macDefaultEncodingIsPC ) { - wxMacConvertToPC((char*)data) ; + wxMacConvertToPC((char*)data,(char*)data,byteCount) ; } return data; } @@ -240,43 +243,38 @@ bool wxClipboard::AddData( wxDataObject *data ) { mac = textDataObject->GetText() ; } - #if !TARGET_CARBON - err = PutScrap( mac.Length() , 'TEXT' , mac.c_str() ) ; - #else - ScrapRef scrap; - err = GetCurrentScrap (&scrap); - if ( !err ) - { - err = PutScrapFlavor (scrap, 'TEXT', 0, mac.Length(), mac.c_str()); - } - #endif + err = UMAPutScrap( mac.Length() , 'TEXT' , (void*) mac.c_str() ) ; } break ; #if wxUSE_DRAG_AND_DROP case wxDF_METAFILE: { - wxMetafileDataObject* metaFileDataObject = + wxMetafileDataObject* metaFileDataObject = (wxMetafileDataObject*) data; - wxMetafile metaFile = metaFileDataObject->GetMetafile(); - PicHandle pict = (PicHandle) metaFile.GetHMETAFILE() ; - HLock( (Handle) pict ) ; - #if !TARGET_CARBON - err = PutScrap( GetHandleSize( (Handle) pict ) , 'PICT' , *pict ) ; - #else - ScrapRef scrap; - err = GetCurrentScrap (&scrap); - if ( !err ) - { - err = PutScrapFlavor (scrap, 'PICT', 0, GetHandleSize((Handle) pict), *pict); - } - #endif - HUnlock( (Handle) pict ) ; + wxMetafile metaFile = metaFileDataObject->GetMetafile(); + PicHandle pict = (PicHandle) metaFile.GetHMETAFILE() ; + HLock( (Handle) pict ) ; + err = UMAPutScrap( GetHandleSize( (Handle) pict ) , 'PICT' , *pict ) ; + HUnlock( (Handle) pict ) ; } break ; #endif case wxDF_BITMAP: case wxDF_DIB: + { + PicHandle pict = NULL ; + + wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data ; + bool created = bitmapDataObject->GetBitmap().GetBitmapType() != kMacBitmapTypePict ; + pict = (PicHandle) bitmapDataObject->GetBitmap().GetPict() ; + + HLock( (Handle) pict ) ; + err = UMAPutScrap( GetHandleSize( (Handle) pict ) , 'PICT' , *pict ) ; + HUnlock( (Handle) pict ) ; + if ( created && pict) + KillPicture( pict ) ; + } default: break ; } @@ -375,8 +373,10 @@ bool wxClipboard::GetData( wxDataObject& data ) switch ( format.GetType() ) { - case wxDF_TEXT: - case wxDF_OEMTEXT: + case wxDF_TEXT : + case wxDF_OEMTEXT : + case wxDF_BITMAP : + case wxDF_METAFILE : { long len ; char* s = (char*)wxGetClipboardData(format, &len ); diff --git a/src/mac/dataobj.cpp b/src/mac/dataobj.cpp index c2f75326c0..049622ffb3 100644 --- a/src/mac/dataobj.cpp +++ b/src/mac/dataobj.cpp @@ -230,34 +230,51 @@ wxBitmapDataObject::wxBitmapDataObject( : wxBitmapDataObjectBase(rBitmap) { Init(); - - DoConvertToPng(); + if ( m_bitmap.Ok() ) + { + m_pngData = m_bitmap.GetPict() ; + m_pngSize = GetHandleSize( (Handle) m_pngData ) ; + } } wxBitmapDataObject::~wxBitmapDataObject() { - Clear(); + if ( m_pngData && m_bitmap.GetBitmapType() != kMacBitmapTypePict ) + { + KillPicture( (PicHandle) m_pngData ) ; + } + m_pngData = NULL ; + m_pngSize = 0 ; } void wxBitmapDataObject::SetBitmap( const wxBitmap& rBitmap ) { - ClearAll(); + if ( m_pngData && m_bitmap.GetBitmapType() != kMacBitmapTypePict ) + { + KillPicture( (PicHandle) m_pngData ) ; + } + m_pngData = NULL ; + m_pngSize = 0 ; wxBitmapDataObjectBase::SetBitmap(rBitmap); - DoConvertToPng(); + if ( m_bitmap.Ok() ) + { + m_pngData = m_bitmap.GetPict() ; + m_pngSize = GetHandleSize( (Handle) m_pngData ) ; + } } bool wxBitmapDataObject::GetDataHere( void* pBuf ) const { - if (!m_pngSize) + if (!m_pngData) { wxFAIL_MSG(wxT("attempt to copy empty bitmap failed")); return FALSE; } - memcpy(pBuf, m_pngData, m_pngSize); + memcpy(pBuf, *(Handle)m_pngData, GetHandleSize((Handle)m_pngData)); return TRUE; } @@ -267,41 +284,14 @@ bool wxBitmapDataObject::SetData( ) { Clear(); - m_pngSize = nSize; - m_pngData = malloc(m_pngSize); - - memcpy(m_pngData, pBuf, m_pngSize); - - wxMemoryInputStream vMstream((char*)m_pngData, m_pngSize); - wxImage vImage; - wxPNGHandler vHandler; - - if (!vHandler.LoadFile(&vImage, vMstream)) - { - return FALSE; - } - - m_bitmap = wxBitmap( vImage ) ; + PicHandle picHandle = (PicHandle) NewHandle( nSize ) ; + memcpy( *picHandle , pBuf , nSize ) ; + m_pngData = picHandle ; + m_pngSize = GetHandleSize( (Handle) picHandle ) ; + Rect frame = (**picHandle).picFrame ; + + m_bitmap.SetPict( m_pngData ) ; + m_bitmap.SetWidth( frame.right - frame.left ) ; + m_bitmap.SetHeight( frame.bottom - frame.top ) ; return m_bitmap.Ok(); } - -void wxBitmapDataObject::DoConvertToPng() -{ - if (!m_bitmap.Ok()) - return; - - wxCHECK_RET( wxImage::FindHandler(wxBITMAP_TYPE_PNG) != NULL, - wxT("You must call wxImage::AddHandler(new wxPNGHandler); to be able to use clipboard with bitmaps!") ); - - wxImage image = m_bitmap.ConvertToImage(); - - wxCountingOutputStream count; - image.SaveFile(count, wxBITMAP_TYPE_PNG); - - m_pngSize = count.GetSize() + 100; // sometimes the size seems to vary ??? - m_pngData = malloc(m_pngSize); - - wxMemoryOutputStream mstream((char*) m_pngData, m_pngSize); - image.SaveFile(mstream, wxBITMAP_TYPE_PNG); -} -