- CTabHandle newColors; /* Handle to the new color table */
-
- /* Allocate memory for the color table */
- newColors = (CTabHandle)NewHandleClear( sizeof (ColorTable) +
- sizeof (ColorSpec) * (numColors - 1) );
- if (newColors != nil)
- {
- /* Initialize the fields */
- (**newColors).ctSeed = GetCTSeed();
- (**newColors).ctFlags = 0;
- (**newColors).ctSize = numColors - 1;
- /* Initialize the table of colors */
- }
- return newColors ;
-}
-
-void wxMacDestroyColorTable( CTabHandle colors )
-{
- DisposeHandle( (Handle) colors ) ;
-}
-
-void wxMacSetColorTableEntry( CTabHandle newColors , int index , int red , int green , int blue )
-{
- (**newColors).ctTable[index].value = index;
- (**newColors).ctTable[index].rgb.red = 0 ;// someRedValue;
- (**newColors).ctTable[index].rgb.green = 0 ; // someGreenValue;
- (**newColors).ctTable[index].rgb.blue = 0 ; // someBlueValue;
-}
-
-GWorldPtr wxMacCreateGWorld( int width , int height , int depth )
-{
- OSErr err = noErr ;
- GWorldPtr port ;
- Rect rect = { 0 , 0 , height , width } ;
-
- if ( depth < 0 )
- {
- depth = wxDisplayDepth() ;
- }
-
- err = NewGWorld( &port , depth , &rect , NULL , NULL , 0 ) ;
- if ( err == noErr )
- {
- return port ;
- }
- return NULL ;
-}
-
-void wxMacDestroyGWorld( GWorldPtr gw )
-{
- if ( gw )
- DisposeGWorld( gw ) ;
-}
-
-PicHandle wxMacCreatePict(GWorldPtr wp, GWorldPtr mask)
-{
- CGrafPtr origPort ;
- GDHandle origDev ;
-
- PicHandle pict; // this is the Picture we give back
-
- RGBColor gray = { 0xCCCC ,0xCCCC , 0xCCCC } ;
- RGBColor white = { 0xffff ,0xffff , 0xffff } ;
- RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ;
-
- unsigned char *maskimage = NULL ;
- Rect portRect ;
- GetPortBounds( wp , &portRect ) ;
- int width = portRect.right - portRect.left ;
- int height = portRect.bottom - portRect.top ;
-
- LockPixels( GetGWorldPixMap( wp ) ) ;
- GetGWorld( &origPort , &origDev ) ;
-
- if ( mask )
- {
- maskimage = (unsigned char*) malloc( width * height ) ;
- SetGWorld( mask , NULL ) ;
- LockPixels( GetGWorldPixMap( mask ) ) ;
- for ( int y = 0 ; y < height ; y++ )
- {
- for( int x = 0 ; x < width ; x++ )
- {
- RGBColor col ;
-
- GetCPixel( x + portRect.left , y + portRect.top , &col ) ;
- maskimage[y*width + x] = ( col.red == 0 ) ; // for monochrome masks
- }
- }
- UnlockPixels( GetGWorldPixMap( mask ) ) ;
- }
-
- SetGWorld( wp , NULL ) ;
-
- pict = OpenPicture(&portRect); // open a picture, this disables drawing
- if(!pict)
- return NULL;
-
- if ( maskimage )
- {
- RGBForeColor( &black ) ;
- RGBBackColor( &white ) ;
- PenMode(transparent);
-
- for ( int y = 0 ; y < height ; ++y )
- {
- for( int x = 0 ; x < width ; ++x )
- {
- if ( maskimage[y*width + x] )
+ 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 * 4 + 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 , k32ARGBPixelFormat , &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 ;
+ // we must destroy an existing cached image, as
+ // the bitmap data may change now
+ wxASSERT_MSG( m_pictHandle == NULL && m_iconRef == NULL ,
+ wxT("Currently, modifing bitmaps that are used in controls already is not supported") ) ;
+#if wxMAC_USE_CORE_GRAPHICS
+ 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
+}
+
+bool wxBitmapRefData::HasNativeSize()
+{
+ int w = GetWidth() ;
+ int h = GetHeight() ;
+ int sz = wxMax( w , h ) ;
+
+ if ( sz == 128 || sz == 48 || sz == 32 || sz == 16 )
+ return true ;
+ return false ;
+}
+
+IconRef wxBitmapRefData::GetIconRef()
+{
+ if ( m_iconRef == NULL )
+ {
+ // Create Icon Family Handle
+
+ IconFamilyHandle iconFamily = NULL ;
+
+ iconFamily = (IconFamilyHandle) NewHandle(8) ;
+ (**iconFamily).resourceType = kIconFamilyType ;
+ (**iconFamily).resourceSize = sizeof(OSType) + sizeof(Size);
+
+ int w = GetWidth() ;
+ int h = GetHeight() ;
+ int sz = wxMax( w , h ) ;
+
+ OSType dataType = 0 ;
+ OSType maskType = 0 ;
+
+ 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 )
+ {
+ // setup the header properly
+
+ 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 = HasAlpha() ;
+ wxMask *mask = m_bitmapMask ;
+ unsigned char * source = (unsigned char*) GetRawAccess() ;
+ unsigned char * masksource = mask ? (unsigned char*) mask->GetRawAccess() : NULL ;
+ for ( int y = 0 ; y < h ; ++y )