+IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject )
+
+#ifdef __DARWIN__
+ #include <ApplicationServices/ApplicationServices.h>
+#else
+ #include <PictUtils.h>
+#endif
+
+#include "wx/mac/uma.h"
+
+#include "wx/dcmemory.h"
+
+// Implementation Notes
+// --------------------
+//
+// we are always working with a 32 bit deep pixel buffer
+// under QuickDraw its alpha parts are going to be ignored in the GWorld,
+// therefore we have a separate GWorld there for blitting the mask in
+
+// under Quartz then content is transformed into a CGImageRef representing the same data
+// which can be transferred to the GPU by the OS for fast rendering
+
+// we don't dare premultiplied alpha yet
+#define wxMAC_USE_PREMULTIPLIED_ALPHA 0
+
+IconFamilyHandle wxMacCreateIconFamily(const wxBitmap& bitmap)
+{
+ wxBitmap bmp = bitmap ;
+ // setup the header properly
+
+ IconFamilyHandle iconFamily = (IconFamilyHandle) NewHandle(8) ;
+ (**iconFamily).resourceType = kIconFamilyType ;
+ (**iconFamily).resourceSize = sizeof(OSType) + sizeof(Size);
+
+ int w = bmp.GetWidth() ;
+ int h = bmp.GetHeight() ;
+ int sz = wxMax( w , h ) ;
+
+ OSType dataType = 0 ;
+ OSType maskType = 0 ;
+
+ if ( sz == 64 )
+ {
+ bmp = wxBitmap( bitmap.ConvertToImage().Scale( 128 , 128 ) ) ;
+ sz = 128 ;
+ h = w = 128 ;
+ }
+ else if ( sz == 24 )
+ {
+ bmp = wxBitmap( bitmap.ConvertToImage().Scale( 48 , 48 ) ) ;
+ sz = 48 ;
+ h = w = 48 ;
+ }
+
+ if ( sz == 128 )
+ {
+ dataType = kThumbnail32BitData ;
+ maskType = kThumbnail8BitMask ;
+ }
+ else if ( sz == 48 )
+ {
+ dataType = kHuge32BitData ;
+ maskType = kHuge8BitMask ;
+ }
+ else if ( sz == 32 )
+ {
+ dataType = kLarge32BitData ;
+ maskType = kLarge8BitMask ;
+ }
+ else if ( sz == 16 )
+ {
+ dataType = kSmall32BitData ;
+ maskType = kSmall8BitMask ;
+ }
+
+ if ( dataType != 0 )
+ {
+ Handle data = NULL ;
+ Handle maskdata = NULL ;
+ unsigned char * maskptr = NULL ;
+ unsigned char * ptr = NULL ;
+ size_t size ;
+ size_t masksize ;
+
+ 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 ) ;
+
+ bool hasAlpha = bmp.HasAlpha() ;
+ wxMask *mask = bmp.GetMask() ;
+ 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 ) ;
+ }
+ else
+ {
+ PicHandle pic = wxMacCreatePicHandle( bitmap ) ;
+ SetIconFamilyData( iconFamily, 'PICT' , (Handle) pic ) ;
+ KillPicture( pic ) ;
+ }
+ return iconFamily ;
+}
+
+IconRef wxMacCreateIconRef(const wxBitmap& bmp)
+{
+ IconFamilyHandle iconFamily = wxMacCreateIconFamily( bmp ) ;
+ if ( iconFamily == NULL )
+ return NULL ;
+
+ 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 ;
+ int height = bmp.GetHeight() ;
+ int width = bmp.GetWidth() ;
+
+ Rect rect = { 0 , 0 , height , width } ;
+
+ GetGWorld( &origPort , &origDev ) ;
+
+ wp = (GWorldPtr) bmp.GetHBITMAP( (WXHBITMAP*) &mask ) ;
+
+ RgnHandle clipRgn = NULL ;
+
+ if ( mask )
+ {
+ GWorldPtr monoworld ;
+ clipRgn = NewRgn() ;
+ OSStatus err = NewGWorld( &monoworld , 1 , &rect , NULL , NULL , 0 ) ;
+ LockPixels( GetGWorldPixMap( monoworld ) ) ;
+ LockPixels( GetGWorldPixMap( mask ) ) ;
+ SetGWorld( monoworld , NULL ) ;
+ RGBColor white = { 0xffff ,0xffff , 0xffff } ;
+ RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ;
+ RGBForeColor( &black ) ;
+ RGBBackColor( &white ) ;
+ CopyBits(GetPortBitMapForCopyBits(mask),
+ GetPortBitMapForCopyBits(monoworld),
+ &rect,
+ &rect,
+ srcCopy,NULL);
+ BitMapToRegion( clipRgn , (BitMap*) *GetGWorldPixMap( monoworld ) ) ;
+ UnlockPixels( GetGWorldPixMap( monoworld ) ) ;
+ UnlockPixels( GetGWorldPixMap( mask ) ) ;
+ DisposeGWorld( monoworld ) ;
+ }
+
+ 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 ) ;
+
+ if ( clipRgn )
+ SetClip( clipRgn ) ;
+
+ LockPixels( GetGWorldPixMap( wp ) ) ;
+ CopyBits(GetPortBitMapForCopyBits(wp),
+ GetPortBitMapForCopyBits(wp),
+ &portRect,
+ &portRect,
+ srcCopy,clipRgn);
+ UnlockPixels( GetGWorldPixMap( wp ) ) ;
+ ClosePicture();
+ }
+ SetGWorld( origPort , origDev ) ;
+ if ( clipRgn )
+ DisposeRgn( clipRgn ) ;
+
+ 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 ) ;
+ wxASSERT_MSG( info->u.iconRef , wxT("Converting to IconRef not possible") ) ;
+#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
+ }
+}