+ if ( m_hasAlpha == use )
+ return ;
+
+ m_hasAlpha = use ;
+
+ CGContextRelease( m_hBitmap );
+ m_hBitmap = CGBitmapContextCreate((char*) m_memBuf.GetData(), m_width, m_height, 8, m_bytesPerRow, wxMacGetGenericRGBColorSpace(), m_hasAlpha ? kCGImageAlphaPremultipliedFirst : kCGImageAlphaNoneSkipFirst );
+ wxASSERT_MSG( m_hBitmap , wxT("Unable to create CGBitmapContext context") ) ;
+ CGContextTranslateCTM( m_hBitmap, 0, m_height );
+ CGContextScaleCTM( m_hBitmap, 1, -1 );
+}
+
+void *wxBitmapRefData::GetRawAccess() const
+{
+ wxCHECK_MSG( IsOk(), NULL , wxT("invalid bitmap") ) ;
+ return m_memBuf.GetData() ;
+}
+
+void *wxBitmapRefData::BeginRawAccess()
+{
+ wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") ) ;
+ wxASSERT( m_rawAccessCount == 0 ) ;
+ wxASSERT_MSG( m_pictHandle == NULL && m_iconRef == NULL ,
+ wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
+
+ ++m_rawAccessCount ;
+
+ // we must destroy an existing cached image, as
+ // the bitmap data may change now
+ if ( m_cgImageRef )
+ {
+ CGImageRelease( m_cgImageRef ) ;
+ m_cgImageRef = NULL ;
+ }
+
+ return m_memBuf.GetData() ;
+}
+
+void wxBitmapRefData::EndRawAccess()
+{
+ wxCHECK_RET( IsOk() , wxT("invalid bitmap") ) ;
+ wxASSERT( m_rawAccessCount == 1 ) ;
+
+ --m_rawAccessCount ;
+}
+
+bool wxBitmapRefData::HasNativeSize()
+{
+ int w = GetWidth() ;
+ int h = GetHeight() ;
+ int sz = wxMax( w , h ) ;
+
+ return ( sz == 128 || sz == 48 || sz == 32 || sz == 16 );
+}
+
+IconRef wxBitmapRefData::GetIconRef()
+{
+ if ( m_iconRef == NULL )
+ {
+ // Create Icon Family Handle
+
+ IconFamilyHandle iconFamily = (IconFamilyHandle) NewHandle( 0 );
+
+ int w = GetWidth() ;
+ int h = GetHeight() ;
+ int sz = wxMax( w , h ) ;
+
+ OSType dataType = 0 ;
+ OSType maskType = 0 ;
+
+ switch (sz)
+ {
+ case 128:
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
+ if ( UMAGetSystemVersion() >= 0x1050 )
+ {
+ dataType = kIconServices128PixelDataARGB ;
+ }
+ else
+#endif
+ {
+ dataType = kThumbnail32BitData ;
+ maskType = kThumbnail8BitMask ;
+ }
+ break;
+
+ case 48:
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
+ if ( UMAGetSystemVersion() >= 0x1050 )
+ {
+ dataType = kIconServices48PixelDataARGB ;
+ }
+ else
+#endif
+ {
+ dataType = kHuge32BitData ;
+ maskType = kHuge8BitMask ;
+ }
+ break;
+
+ case 32:
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
+ if ( UMAGetSystemVersion() >= 0x1050 )
+ {
+ dataType = kIconServices32PixelDataARGB ;
+ }
+ else
+#endif
+ {
+ dataType = kLarge32BitData ;
+ maskType = kLarge8BitMask ;
+ }
+ break;
+
+ case 16:
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
+ if ( UMAGetSystemVersion() >= 0x1050 )
+ {
+ dataType = kIconServices16PixelDataARGB ;
+ }
+ else
+#endif
+ {
+ dataType = kSmall32BitData ;
+ maskType = kSmall8BitMask ;
+ }
+ break;
+
+ default:
+ break;
+ }
+
+ if ( dataType != 0 )
+ {
+#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
+ if ( maskType == 0 && UMAGetSystemVersion() >= 0x1050 )
+ {
+ size_t datasize = sz * sz * 4 ;
+ Handle data = NewHandle( datasize ) ;
+ HLock( data ) ;
+ unsigned char* ptr = (unsigned char*) *data ;
+ memset( ptr, 0, datasize );
+ bool hasAlpha = HasAlpha() ;
+ wxMask *mask = m_bitmapMask ;
+ unsigned char * sourcePtr = (unsigned char*) GetRawAccess() ;
+ unsigned char * masksourcePtr = mask ? (unsigned char*) mask->GetRawAccess() : NULL ;
+
+ for ( int y = 0 ; y < h ; ++y, sourcePtr += m_bytesPerRow , masksourcePtr += mask ? mask->GetBytesPerRow() : 0 )
+ {
+ unsigned char * source = sourcePtr;
+ unsigned char * masksource = masksourcePtr;
+ unsigned char * dest = ptr + y * sz * 4 ;
+ unsigned char a, r, g, b;
+
+ for ( int x = 0 ; x < w ; ++x )
+ {
+ a = *source ++ ;
+ r = *source ++ ;
+ g = *source ++ ;
+ b = *source ++ ;
+
+ if ( mask )
+ {
+ a = 0xFF - *masksource++ ;
+ }
+ else if ( !hasAlpha )
+ a = 0xFF ;
+ else
+ {
+#if wxMAC_USE_PREMULTIPLIED_ALPHA
+ // this must be non-premultiplied data
+ if ( a != 0xFF && a!= 0 )
+ {
+ r = r * 255 / a;
+ g = g * 255 / a;
+ b = b * 255 / a;
+ }
+#endif
+ }
+ *dest++ = a ;
+ *dest++ = r ;
+ *dest++ = g ;
+ *dest++ = b ;
+
+ }
+ }
+ HUnlock( data );
+ OSStatus err = SetIconFamilyData( iconFamily, dataType , data );
+ wxASSERT_MSG( err == noErr , wxT("Error when adding bitmap") );
+ DisposeHandle( data );
+ }
+ else
+#endif
+ {
+ // setup the header properly
+
+ Handle data = NULL ;
+ Handle maskdata = NULL ;
+ unsigned char * maskptr = NULL ;
+ unsigned char * ptr = NULL ;
+ size_t datasize, masksize ;
+
+ datasize = sz * sz * 4 ;
+ data = NewHandle( datasize ) ;
+ HLock( data ) ;
+ ptr = (unsigned char*) *data ;
+ memset( ptr , 0, datasize ) ;
+
+ masksize = sz * sz ;
+ maskdata = NewHandle( masksize ) ;
+ HLock( maskdata ) ;
+ maskptr = (unsigned char*) *maskdata ;
+ memset( maskptr , 0 , masksize ) ;
+
+ bool hasAlpha = HasAlpha() ;
+ wxMask *mask = m_bitmapMask ;
+ unsigned char * sourcePtr = (unsigned char*) GetRawAccess() ;
+ unsigned char * masksourcePtr = mask ? (unsigned char*) mask->GetRawAccess() : NULL ;
+
+ for ( int y = 0 ; y < h ; ++y, sourcePtr += m_bytesPerRow , masksourcePtr += mask ? mask->GetBytesPerRow() : 0 )
+ {
+ unsigned char * source = sourcePtr;
+ unsigned char * masksource = masksourcePtr;
+ unsigned char * dest = ptr + y * sz * 4 ;
+ unsigned char * maskdest = maskptr + y * sz ;
+ unsigned char a, r, g, b;
+
+ for ( int x = 0 ; x < w ; ++x )
+ {
+ a = *source ++ ;
+ r = *source ++ ;
+ g = *source ++ ;
+ b = *source ++ ;
+
+ *dest++ = 0 ;
+ *dest++ = r ;
+ *dest++ = g ;
+ *dest++ = b ;
+
+ if ( mask )
+ *maskdest++ = 0xFF - *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 ) ;
+ }
+ }
+ else
+ {
+ PicHandle pic = GetPictHandle() ;
+ SetIconFamilyData( iconFamily, 'PICT' , (Handle) pic ) ;
+ }
+ // transform into IconRef
+
+ // cleaner version existing from 10.3 upwards
+ HLock((Handle) iconFamily);
+ OSStatus err = GetIconRefFromIconFamilyPtr( *iconFamily, GetHandleSize((Handle) iconFamily), &m_iconRef );
+ HUnlock((Handle) iconFamily);
+ wxASSERT_MSG( err == noErr , wxT("Error when constructing icon ref") );
+ DisposeHandle( (Handle) iconFamily ) ;
+ }
+
+ return m_iconRef ;
+}
+
+PicHandle wxBitmapRefData::GetPictHandle()
+{
+ if ( m_pictHandle == NULL )
+ {
+#ifndef __LP64__
+ GraphicsExportComponent exporter = 0;
+ OSStatus err = OpenADefaultComponent(GraphicsExporterComponentType, kQTFileTypePicture, &exporter);
+ if (noErr == err)
+ {
+ m_pictHandle = (PicHandle) NewHandle(0);
+ if ( m_pictHandle )
+ {
+ // QT does not correctly export the mask
+ // TODO if we get around to it create a synthetic PICT with the CopyBits and Mask commands
+ CGImageRef imageRef = CreateCGImage();
+ err = GraphicsExportSetInputCGImage( exporter, imageRef );
+ err = GraphicsExportSetOutputHandle(exporter, (Handle)m_pictHandle);
+ err = GraphicsExportDoExport(exporter, NULL);
+ CGImageRelease( imageRef );
+
+ size_t handleSize = GetHandleSize( (Handle) m_pictHandle );
+ // the 512 bytes header is only needed for pict files, but not in memory
+ if ( handleSize >= 512 )