+#include "wx/mac/uma.h"
+
+CTabHandle wxMacCreateColorTable( int numColors )
+{
+ 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 = red ; // someRedValue;
+ (**newColors).ctTable[index].rgb.green = green ; // someGreenValue;
+ (**newColors).ctTable[index].rgb.blue = blue ; // 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 ) ;
+}
+
+#define kDefaultRes 0x00480000 /* Default resolution is 72 DPI; Fixed type */
+
+OSErr SetupCIconHandlePixMap( CIconHandle icon , short depth , Rect *bounds , CTabHandle colors )
+{
+ CTabHandle newColors; /* Color table used for the off-screen PixMap */
+ Ptr offBaseAddr; /* Pointer to the off-screen pixel image */
+ OSErr error; /* Returns error code */
+ short bytesPerRow; /* Number of bytes per row in the PixMap */
+
+
+ error = noErr;
+ newColors = nil;
+ offBaseAddr = nil;
+
+ bytesPerRow = ((depth * (bounds->right - bounds->left) + 31) / 32) * 4;
+
+ /* Clone the clut if indexed color; allocate a dummy clut if direct color*/
+ if (depth <= 8)
+ {
+ newColors = colors;
+ error = HandToHand((Handle *) &newColors);
+ }
+ else
+ {
+ newColors = (CTabHandle) NewHandle(sizeof(ColorTable) -
+ sizeof(CSpecArray));
+ error = MemError();
+ }
+ if (error == noErr)
+ {
+ /* Allocate pixel image; long integer multiplication avoids overflow */
+ (**icon).iconData = NewHandle((unsigned long) bytesPerRow * (bounds->bottom -
+ bounds->top));
+ if ((**icon).iconData != nil)
+ {
+ /* Initialize fields common to indexed and direct PixMaps */
+ (**icon).iconPMap.baseAddr = 0; /* Point to image */
+ (**icon).iconPMap.rowBytes = bytesPerRow | /* MSB set for PixMap */
+ 0x8000;
+ (**icon).iconPMap.bounds = *bounds; /* Use given bounds */
+ (**icon).iconPMap.pmVersion = 0; /* No special stuff */
+ (**icon).iconPMap.packType = 0; /* Default PICT pack */
+ (**icon).iconPMap.packSize = 0; /* Always zero in mem */
+ (**icon).iconPMap.hRes = kDefaultRes; /* 72 DPI default res */
+ (**icon).iconPMap.vRes = kDefaultRes; /* 72 DPI default res */
+ (**icon).iconPMap.pixelSize = depth; /* Set # bits/pixel */
+
+ /* Initialize fields specific to indexed and direct PixMaps */
+ if (depth <= 8)
+ {
+ /* PixMap is indexed */
+ (**icon).iconPMap.pixelType = 0; /* Indicates indexed */
+ (**icon).iconPMap.cmpCount = 1; /* Have 1 component */
+ (**icon).iconPMap.cmpSize = depth; /* Component size=depth */
+ (**icon).iconPMap.pmTable = newColors; /* Handle to CLUT */
+ }
+ else
+ {
+ /* PixMap is direct */
+ (**icon).iconPMap.pixelType = RGBDirect; /* Indicates direct */
+ (**icon).iconPMap.cmpCount = 3; /* Have 3 components */
+ if (depth == 16)
+ (**icon).iconPMap.cmpSize = 5; /* 5 bits/component */
+ else
+ (**icon).iconPMap.cmpSize = 8; /* 8 bits/component */
+ (**newColors).ctSeed = 3 * (**icon).iconPMap.cmpSize;
+ (**newColors).ctFlags = 0;
+ (**newColors).ctSize = 0;
+ (**icon).iconPMap.pmTable = newColors;
+ }
+ }
+ else
+ error = MemError();
+ }
+ else
+ newColors = nil;
+
+ /* If no errors occured, return a handle to the new off-screen PixMap */
+ if (error != noErr)
+ {
+ if (newColors != nil)
+ DisposeCTable(newColors);
+ }
+
+ /* Return the error code */
+ return error;
+}
+
+CIconHandle wxMacCreateCIcon(GWorldPtr image , GWorldPtr mask , short dstDepth , short iconSize )
+{
+ GWorldPtr saveWorld;
+ GDHandle saveHandle;
+
+ GetGWorld(&saveWorld,&saveHandle); // save Graphics env state
+ SetGWorld(image,nil);
+
+ Rect frame = { 0 , 0 , iconSize , iconSize } ;
+ Rect imageBounds = frame ;
+ GetPortBounds( image , &imageBounds ) ;
+
+ int bwSize = iconSize / 8 * iconSize ;
+ CIconHandle icon = (CIconHandle) NewHandleClear( sizeof ( CIcon ) + 2 * bwSize) ;
+ HLock((Handle)icon) ;
+ SetupCIconHandlePixMap( icon , dstDepth , &frame,GetCTable(dstDepth)) ;
+ HLock( (**icon).iconData ) ;
+ (**icon).iconPMap.baseAddr = *(**icon).iconData ;
+
+ LockPixels(GetGWorldPixMap(image));
+
+ CopyBits(GetPortBitMapForCopyBits(image),
+ (BitMapPtr)&((**icon).iconPMap),
+ &imageBounds,
+ &imageBounds,
+ srcCopy | ditherCopy, nil);
+
+
+ UnlockPixels(GetGWorldPixMap(image));
+ HUnlock( (**icon).iconData ) ;
+
+ (**icon).iconMask.rowBytes = iconSize / 8 ;
+ (**icon).iconMask.bounds = frame ;
+
+ (**icon).iconBMap.rowBytes = iconSize / 8 ;
+ (**icon).iconBMap.bounds = frame ;
+ (**icon).iconMask.baseAddr = (char*) &(**icon).iconMaskData ;
+ (**icon).iconBMap.baseAddr = (char*) &(**icon).iconMaskData + bwSize ;
+
+ if ( mask )
+ {
+ Rect r ;
+ GetPortBounds( image , &r ) ;
+ LockPixels(GetGWorldPixMap(mask) ) ;
+ CopyBits(GetPortBitMapForCopyBits(mask) ,
+ &(**icon).iconBMap , &r , &r, srcCopy , nil ) ;
+ CopyBits(GetPortBitMapForCopyBits(mask) ,
+ &(**icon).iconMask , &r , &r, srcCopy , nil ) ;
+ UnlockPixels(GetGWorldPixMap( mask ) ) ;
+ }
+ else
+ {
+ Rect r ;
+ GetPortBounds( image , &r ) ;
+ LockPixels(GetGWorldPixMap(image));
+ CopyBits(GetPortBitMapForCopyBits(image) ,
+ &(**icon).iconBMap , &r , &r, srcCopy , nil ) ;
+ CopyBits(GetPortBitMapForCopyBits(image) ,
+ &(**icon).iconMask , &r , &r, srcCopy , nil ) ;
+ UnlockPixels(GetGWorldPixMap(image));
+ }
+
+ (**icon).iconMask.baseAddr = NULL ;
+ (**icon).iconBMap.baseAddr = NULL ;
+ (**icon).iconPMap.baseAddr = NULL ;
+ HUnlock((Handle)icon) ;
+ SetGWorld(saveWorld,saveHandle);
+
+ return icon;
+}
+
+PicHandle wxMacCreatePict(GWorldPtr wp, GWorldPtr mask)
+{
+ CGrafPtr origPort ;
+ GDHandle origDev ;
+
+ PicHandle pict;
+
+ RGBColor white = { 0xffff ,0xffff , 0xffff } ;
+ RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ;
+
+ GetGWorld( &origPort , &origDev ) ;
+
+ RgnHandle clipRgn = NULL ;
+
+ if ( mask )
+ {
+ clipRgn = NewRgn() ;
+ LockPixels( GetGWorldPixMap( mask ) ) ;
+ BitMapToRegion( clipRgn , (BitMap*) *GetGWorldPixMap( mask ) ) ;
+ UnlockPixels( GetGWorldPixMap( mask ) ) ;
+ }
+
+ SetGWorld( wp , NULL ) ;
+ Rect portRect ;
+ if ( clipRgn )
+ GetRegionBounds( clipRgn , &portRect ) ;
+ else
+ GetPortBounds( wp , &portRect ) ;
+ pict = OpenPicture(&portRect);
+ if(pict)
+ {
+ 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 ;
+
+ if ( bmap->m_bitmapType == kMacBitmapTypePict )
+ {
+ info->contentType = kControlContentPictHandle ;
+ info->u.picture = MAC_WXHMETAFILE(bmap->m_hPict) ;
+ }
+ else if ( bmap->m_bitmapType == kMacBitmapTypeGrafWorld )
+ {
+ if ( (forceType == kControlContentCIconHandle || ( bmap->m_width == bmap->m_height && forceType != kControlContentPictHandle ) ) && ((bmap->m_width & 0x3) == 0) )
+ {
+ info->contentType = kControlContentCIconHandle ;
+ if ( bitmap.GetMask() )
+ {
+ info->u.cIconHandle = wxMacCreateCIcon( MAC_WXHBITMAP(bmap->m_hBitmap) , MAC_WXHBITMAP(bitmap.GetMask()->GetMaskBitmap()) ,
+ 8 , bmap->m_width ) ;
+ }
+ else
+ {
+ info->u.cIconHandle = wxMacCreateCIcon( MAC_WXHBITMAP(bmap->m_hBitmap) , NULL ,
+ 8 , bmap->m_width ) ;
+ }
+ }
+ else
+ {
+ info->contentType = kControlContentPictHandle ;
+ if ( bitmap.GetMask() )
+ {
+ info->u.picture = wxMacCreatePict( MAC_WXHBITMAP(bmap->m_hBitmap) , MAC_WXHBITMAP(bitmap.GetMask()->GetMaskBitmap() ) ) ;
+ }
+ else
+ {
+ info->u.picture = wxMacCreatePict( MAC_WXHBITMAP(bmap->m_hBitmap) , NULL ) ;
+ }
+ }
+ }
+ else if ( bmap->m_bitmapType == kMacBitmapTypeIcon )
+ {
+ info->contentType = kControlContentCIconHandle ;
+ info->u.cIconHandle = MAC_WXHICON(bmap->m_hIcon) ;
+ }
+ }
+}
+