+ size = sz * sz * 4 ;
+ data = NewHandle( size) ;
+ HLock( data ) ;
+ ptr = (unsigned char*) *data ;
+ memset( ptr , 0, size ) ;
+
+ masksize = sz * sz ;
+ maskdata = NewHandle( masksize ) ;
+ HLock( maskdata ) ;
+ maskptr = (unsigned char*) *maskdata ;
+ memset( maskptr , 0 , masksize ) ;
+
+ unsigned char * source = (unsigned char*) bmp.GetRawAccess() ;
+ unsigned char * masksource = mask ? (unsigned char*) mask->GetRawAccess() : NULL ;
+ for ( int y = 0 ; y < h ; ++y )
+ {
+ unsigned char * dest = ptr + y * sz * 4 ;
+ unsigned char * maskdest = maskptr + y * sz ;
+ for ( int x = 0 ; x < w ; ++x )
+ {
+ unsigned char a = *source ++ ;
+ unsigned char r = *source ++ ;
+ unsigned char g = *source ++ ;
+ unsigned char b = *source ++ ;
+
+ *dest++ = 0 ;
+ *dest++ = r ;
+ *dest++ = g ;
+ *dest++ = b ;
+
+ if ( mask )
+ *maskdest++ = *masksource++ ;
+ else if ( hasAlpha )
+ *maskdest++ = a ;
+ else
+ *maskdest++ = 0xFF ;
+ }
+ }
+
+ OSStatus err = SetIconFamilyData( iconFamily, dataType , data ) ;
+ wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ;
+
+ err = SetIconFamilyData( iconFamily, maskType , maskdata ) ;
+ wxASSERT_MSG( err == noErr , wxT("Error when adding mask") ) ;
+ HUnlock( data ) ;
+ HUnlock( maskdata ) ;
+ DisposeHandle( data ) ;
+ DisposeHandle( maskdata ) ;
+ return iconFamily ;
+}
+
+IconRef wxMacCreateIconRef(const wxBitmap& bmp)
+{
+ IconFamilyHandle iconFamily = wxMacCreateIconFamily( bmp ) ;
+ IconRef iconRef ;
+ static int iconCounter = 2 ;
+
+ OSStatus err = RegisterIconRefFromIconFamily( 'WXNG' , (OSType) iconCounter, iconFamily, &iconRef ) ;
+ UInt16 owners ;
+ err = GetIconRefOwners(iconRef , &owners ) ;
+
+ wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") ) ;
+ // we have to retain a reference, as Unregister will decrement it
+ AcquireIconRef( iconRef ) ;
+ UnregisterIconRef( 'WXNG' , (OSType) iconCounter ) ;
+ DisposeHandle( (Handle) iconFamily ) ;
+ ++iconCounter ;
+
+ return iconRef ;
+}
+
+PicHandle wxMacCreatePicHandle( const wxBitmap &bmp )
+{
+ CGrafPtr origPort = NULL ;
+ GDHandle origDev = NULL ;
+ PicHandle pict = NULL ;
+ GWorldPtr wp = NULL ;
+ GWorldPtr mask = NULL ;
+
+ GetGWorld( &origPort , &origDev ) ;
+
+ wp = (GWorldPtr) bmp.GetHBITMAP( (WXHBITMAP*) &mask ) ;
+
+ SetGWorld( wp , NULL ) ;
+ Rect portRect ;
+ GetPortBounds( wp , &portRect ) ;
+ pict = OpenPicture(&portRect);
+
+ if(pict)
+ {
+ RGBColor white = { 0xffff ,0xffff , 0xffff } ;
+ RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ;
+ RGBForeColor( &black ) ;
+ RGBBackColor( &white ) ;
+
+ LockPixels( GetGWorldPixMap( wp ) ) ;
+ CopyBits(GetPortBitMapForCopyBits(wp),
+ GetPortBitMapForCopyBits(wp),
+ &portRect,
+ &portRect,
+ srcCopy,NULL);
+ UnlockPixels( GetGWorldPixMap( wp ) ) ;
+ ClosePicture();
+ }
+ SetGWorld( origPort , origDev ) ;
+
+ return pict;
+}
+
+void wxMacCreateBitmapButton( ControlButtonContentInfo*info , const wxBitmap& bitmap , int forceType )
+{
+ memset( info , 0 , sizeof(ControlButtonContentInfo) ) ;
+ if ( bitmap.Ok() )
+ {
+ wxBitmapRefData * bmap = (wxBitmapRefData*) ( bitmap.GetRefData()) ;
+ if ( bmap == NULL )
+ return ;
+ info->contentType = kControlContentIconRef ;
+ info->u.iconRef = wxMacCreateIconRef( bitmap ) ;
+
+#if wxMAC_USE_CORE_GRAPHICS
+ /*
+ // only on 10.4 more controls will accept a CGImage
+
+ info->contentType = kControlContentCGImageRef ;
+ info->u.imageRef = (CGImageRef) bmap->CGImageCreate() ;
+ */
+#endif
+ }
+}
+
+void wxMacReleaseBitmapButton( ControlButtonContentInfo*info )
+{
+ if ( info->contentType == kControlContentIconRef )
+ {
+ ReleaseIconRef(info->u.iconRef) ;
+ }
+#if wxMAC_USE_CORE_GRAPHICS && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
+ else if ( info->contentType == kControlContentCGImageRef )
+ {
+ CGImageRelease( info->u.imageRef ) ;
+ }
+#endif
+ else
+ {
+ wxFAIL_MSG(wxT("Unexpected bitmap type") ) ;
+ }
+}
+
+
+void wxBitmapRefData::Init()
+{
+ m_width = 0 ;
+ m_height = 0 ;
+ m_depth = 0 ;
+ m_ok = false ;
+ m_bitmapMask = NULL ;
+#if wxMAC_USE_CORE_GRAPHICS
+ m_cgImageRef = NULL ;
+#endif
+ m_hBitmap = NULL ;
+ m_hMaskBitmap = NULL;
+ m_maskBytesPerRow = NULL ;
+
+ m_rawAccessCount = 0 ;
+ m_hasAlpha = false;
+}
+
+wxBitmapRefData::wxBitmapRefData()
+{
+ Init() ;
+}
+
+wxBitmapRefData::wxBitmapRefData( int w , int h , int d )
+{
+ Init() ;
+ Create( w , h , d ) ;
+}
+
+bool wxBitmapRefData::Create( int w , int h , int d )
+{
+ m_width = w ;
+ m_height = h ;
+ m_depth = d ;
+
+ m_bytesPerRow = w * 4 ;
+ size_t size = m_bytesPerRow * h ;
+ void* data = m_memBuf.GetWriteBuf(size) ;
+ memset( data , 0 , size) ;
+ m_memBuf.UngetWriteBuf(size) ;
+
+ m_hBitmap = NULL ;
+ Rect rect = { 0 , 0 , m_height , m_width } ;
+ verify_noerr( NewGWorldFromPtr( (GWorldPtr*) &m_hBitmap , k32ARGBPixelFormat , &rect , NULL , NULL , 0 ,
+ (char*) data , m_bytesPerRow ) ) ;
+ wxASSERT_MSG( m_hBitmap , wxT("Unable to create GWorld context") ) ;
+ m_ok = ( m_hBitmap != NULL ) ;
+
+ return m_ok ;
+}
+
+void wxBitmapRefData::UseAlpha( bool use )
+{
+ if ( m_hasAlpha == use )
+ return ;
+
+ m_hasAlpha = use ;
+ if ( m_hasAlpha )
+ {
+ int width = GetWidth() ;
+ int height = GetHeight() ;
+ m_maskBytesPerRow = ( width + 3 ) & 0xFFFFFFC ;
+ size_t size = height * m_maskBytesPerRow ;
+ unsigned char * data = (unsigned char * ) m_maskMemBuf.GetWriteBuf( size ) ;
+ memset( data , 0 , size ) ;
+ wxASSERT( m_hMaskBitmap == NULL ) ;
+ Rect rect = { 0 , 0 , height , width } ;
+ verify_noerr( NewGWorldFromPtr( (GWorldPtr*) &m_hMaskBitmap , k8IndexedGrayPixelFormat , &rect , NULL , NULL , 0 ,
+ (char*) data , m_maskBytesPerRow ) ) ;
+ wxASSERT_MSG( m_hMaskBitmap , wxT("Unable to create GWorld context for alpha mask") ) ;
+ m_maskMemBuf.UngetWriteBuf(size) ;
+#if !wxMAC_USE_CORE_GRAPHICS
+ UpdateAlphaMask() ;
+#endif
+ }
+ else
+ {
+ DisposeGWorld( m_hMaskBitmap ) ;
+ m_hMaskBitmap = NULL ;
+ m_maskBytesPerRow = 0 ;
+ }
+}
+
+void *wxBitmapRefData::GetRawAccess() const
+{
+ wxCHECK_MSG( Ok(), NULL , wxT("invalid bitmap") ) ;
+ return m_memBuf.GetData() ;
+}
+
+void *wxBitmapRefData::BeginRawAccess()
+{
+ wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ) ;
+ wxASSERT( m_rawAccessCount == 0 ) ;
+ ++m_rawAccessCount ;
+#if wxMAC_USE_CORE_GRAPHICS
+ // we must destroy an existing cached image, as
+ // the bitmap data may change now
+ if ( m_cgImageRef )
+ {
+ CGImageRelease( m_cgImageRef ) ;
+ m_cgImageRef = NULL ;
+ }
+#endif
+ return m_memBuf.GetData() ;
+}
+
+void wxBitmapRefData::EndRawAccess()
+{
+ wxCHECK_RET( Ok() , wxT("invalid bitmap") ) ;
+ wxASSERT( m_rawAccessCount == 1 ) ;
+ --m_rawAccessCount ;
+#if !wxMAC_USE_CORE_GRAPHICS
+ UpdateAlphaMask() ;
+#endif
+}
+
+
+#if wxMAC_USE_CORE_GRAPHICS
+void wxMacMemoryBufferReleaseProc(void *info, const void *data, size_t size)
+{
+ wxMemoryBuffer* membuf = (wxMemoryBuffer*) info ;
+ wxASSERT( data == membuf->GetData() ) ;
+ delete membuf ;
+}
+
+CGImageRef wxBitmapRefData::CGImageCreate() const
+{
+ wxASSERT( m_ok ) ;
+ wxASSERT( m_rawAccessCount >= 0 ) ;
+ CGImageRef image ;
+ if ( m_rawAccessCount > 0 || m_cgImageRef == NULL )
+ {
+ size_t imageSize = m_width * m_height * 4 ;
+ void * dataBuffer = m_memBuf.GetData() ;
+ int w = m_width ;
+ int h = m_height ;
+ CGImageAlphaInfo alphaInfo = kCGImageAlphaNoneSkipFirst ;
+ wxMemoryBuffer* membuf = NULL ;
+
+ if ( m_bitmapMask )
+ {
+ membuf = new wxMemoryBuffer( imageSize ) ;
+ memcpy( membuf->GetData() , dataBuffer , imageSize ) ;
+ unsigned char *sourcemaskstart = (unsigned char *) m_bitmapMask->GetRawAccess() ;
+ int maskrowbytes = m_bitmapMask->GetBytesPerRow() ;
+ unsigned char *destalpha = (unsigned char *) membuf->GetData() ;
+ alphaInfo = kCGImageAlphaFirst ;
+ for ( int y = 0 ; y < h ; ++y , sourcemaskstart += maskrowbytes)
+ {
+ unsigned char *sourcemask = sourcemaskstart ;
+ for( int x = 0 ; x < w ; ++x , sourcemask++ , destalpha += 4 )
+ {
+ *destalpha = *sourcemask ;
+ }