]> git.saurik.com Git - wxWidgets.git/blame - src/mac/bitmap.cpp
compilation fix for Mac and reformatted the code
[wxWidgets.git] / src / mac / bitmap.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: bitmap.cpp
3// Purpose: wxBitmap
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
e9576ca5
SC
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "bitmap.h"
14#endif
15
b698c8e9
GD
16#include "wx/defs.h"
17
e9576ca5
SC
18#include "wx/bitmap.h"
19#include "wx/icon.h"
20#include "wx/log.h"
fec19ea9 21#include "wx/image.h"
973b0afb 22#include "wx/xpmdecod.h"
e9576ca5 23
e9576ca5
SC
24IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
25IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
bb356c9e 26IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject )
e9576ca5 27
f5c6eb5c 28#ifdef __DARWIN__
5fde6fcc 29 #include <ApplicationServices/ApplicationServices.h>
03e11df5
GD
30#else
31 #include <PictUtils.h>
32#endif
519cb848 33
31d30995
SC
34#include "wx/mac/uma.h"
35
519cb848
SC
36CTabHandle wxMacCreateColorTable( int numColors )
37{
4e9ed364
RR
38 CTabHandle newColors; /* Handle to the new color table */
39
40 /* Allocate memory for the color table */
41 newColors = (CTabHandle)NewHandleClear( sizeof (ColorTable) +
42 sizeof (ColorSpec) * (numColors - 1) );
43 if (newColors != nil)
44 {
45 /* Initialize the fields */
46 (**newColors).ctSeed = GetCTSeed();
47 (**newColors).ctFlags = 0;
48 (**newColors).ctSize = numColors - 1;
49 /* Initialize the table of colors */
50 }
51 return newColors ;
519cb848
SC
52}
53
54void wxMacDestroyColorTable( CTabHandle colors )
55{
4e9ed364 56 DisposeHandle( (Handle) colors ) ;
519cb848
SC
57}
58
59void wxMacSetColorTableEntry( CTabHandle newColors , int index , int red , int green , int blue )
60{
4e9ed364 61 (**newColors).ctTable[index].value = index;
be52b341 62 (**newColors).ctTable[index].rgb.red = red ; // someRedValue;
c5750ccb
SC
63 (**newColors).ctTable[index].rgb.green = green ; // someGreenValue;
64 (**newColors).ctTable[index].rgb.blue = blue ; // someBlueValue;
519cb848
SC
65}
66
03e11df5 67GWorldPtr wxMacCreateGWorld( int width , int height , int depth )
519cb848 68{
4e9ed364
RR
69 OSErr err = noErr ;
70 GWorldPtr port ;
71 Rect rect = { 0 , 0 , height , width } ;
72
73 if ( depth < 0 )
74 {
75 depth = wxDisplayDepth() ;
76 }
77
78 err = NewGWorld( &port , depth , &rect , NULL , NULL , 0 ) ;
79 if ( err == noErr )
80 {
81 return port ;
82 }
83 return NULL ;
03e11df5 84}
519cb848
SC
85
86void wxMacDestroyGWorld( GWorldPtr gw )
87{
4e9ed364
RR
88 if ( gw )
89 DisposeGWorld( gw ) ;
519cb848
SC
90}
91
72055702
SC
92#define kDefaultRes 0x00480000 /* Default resolution is 72 DPI; Fixed type */
93
94OSErr SetupCIconHandlePixMap( CIconHandle icon , short depth , Rect *bounds , CTabHandle colors )
5fde6fcc 95{
4e9ed364
RR
96 CTabHandle newColors; /* Color table used for the off-screen PixMap */
97 Ptr offBaseAddr; /* Pointer to the off-screen pixel image */
98 OSErr error; /* Returns error code */
99 short bytesPerRow; /* Number of bytes per row in the PixMap */
72055702
SC
100
101
102 error = noErr;
103 newColors = nil;
104 offBaseAddr = nil;
105
4e9ed364 106 bytesPerRow = ((depth * (bounds->right - bounds->left) + 31) / 32) * 4;
72055702
SC
107
108 /* Clone the clut if indexed color; allocate a dummy clut if direct color*/
109 if (depth <= 8)
110 {
111 newColors = colors;
112 error = HandToHand((Handle *) &newColors);
113 }
114 else
115 {
116 newColors = (CTabHandle) NewHandle(sizeof(ColorTable) -
117 sizeof(CSpecArray));
118 error = MemError();
119 }
120 if (error == noErr)
121 {
122 /* Allocate pixel image; long integer multiplication avoids overflow */
123 (**icon).iconData = NewHandle((unsigned long) bytesPerRow * (bounds->bottom -
124 bounds->top));
125 if ((**icon).iconData != nil)
5fde6fcc 126 {
72055702
SC
127 /* Initialize fields common to indexed and direct PixMaps */
128 (**icon).iconPMap.baseAddr = 0; /* Point to image */
129 (**icon).iconPMap.rowBytes = bytesPerRow | /* MSB set for PixMap */
130 0x8000;
131 (**icon).iconPMap.bounds = *bounds; /* Use given bounds */
132 (**icon).iconPMap.pmVersion = 0; /* No special stuff */
133 (**icon).iconPMap.packType = 0; /* Default PICT pack */
134 (**icon).iconPMap.packSize = 0; /* Always zero in mem */
135 (**icon).iconPMap.hRes = kDefaultRes; /* 72 DPI default res */
136 (**icon).iconPMap.vRes = kDefaultRes; /* 72 DPI default res */
137 (**icon).iconPMap.pixelSize = depth; /* Set # bits/pixel */
5fde6fcc 138
72055702
SC
139 /* Initialize fields specific to indexed and direct PixMaps */
140 if (depth <= 8)
141 {
142 /* PixMap is indexed */
143 (**icon).iconPMap.pixelType = 0; /* Indicates indexed */
144 (**icon).iconPMap.cmpCount = 1; /* Have 1 component */
145 (**icon).iconPMap.cmpSize = depth; /* Component size=depth */
146 (**icon).iconPMap.pmTable = newColors; /* Handle to CLUT */
147 }
148 else
149 {
150 /* PixMap is direct */
151 (**icon).iconPMap.pixelType = RGBDirect; /* Indicates direct */
152 (**icon).iconPMap.cmpCount = 3; /* Have 3 components */
153 if (depth == 16)
154 (**icon).iconPMap.cmpSize = 5; /* 5 bits/component */
155 else
156 (**icon).iconPMap.cmpSize = 8; /* 8 bits/component */
157 (**newColors).ctSeed = 3 * (**icon).iconPMap.cmpSize;
158 (**newColors).ctFlags = 0;
159 (**newColors).ctSize = 0;
160 (**icon).iconPMap.pmTable = newColors;
161 }
5fde6fcc 162 }
72055702
SC
163 else
164 error = MemError();
165 }
166 else
167 newColors = nil;
168
169 /* If no errors occured, return a handle to the new off-screen PixMap */
170 if (error != noErr)
171 {
172 if (newColors != nil)
173 DisposeCTable(newColors);
174 }
175
176 /* Return the error code */
177 return error;
178}
179
180CIconHandle wxMacCreateCIcon(GWorldPtr image , GWorldPtr mask , short dstDepth , short iconSize )
181{
4e9ed364
RR
182 GWorldPtr saveWorld;
183 GDHandle saveHandle;
72055702 184
4e9ed364
RR
185 GetGWorld(&saveWorld,&saveHandle); // save Graphics env state
186 SetGWorld(image,nil);
72055702
SC
187
188 Rect frame = { 0 , 0 , iconSize , iconSize } ;
189 Rect imageBounds = frame ;
4e9ed364 190 GetPortBounds( image , &imageBounds ) ;
72055702
SC
191
192 int bwSize = iconSize / 8 * iconSize ;
193 CIconHandle icon = (CIconHandle) NewHandleClear( sizeof ( CIcon ) + 2 * bwSize) ;
194 HLock((Handle)icon) ;
195 SetupCIconHandlePixMap( icon , dstDepth , &frame,GetCTable(dstDepth)) ;
196 HLock( (**icon).iconData ) ;
197 (**icon).iconPMap.baseAddr = *(**icon).iconData ;
198
4e9ed364
RR
199 LockPixels(GetGWorldPixMap(image));
200
201 CopyBits(GetPortBitMapForCopyBits(image),
202 (BitMapPtr)&((**icon).iconPMap),
203 &imageBounds,
204 &imageBounds,
205 srcCopy | ditherCopy, nil);
72055702
SC
206
207
4e9ed364 208 UnlockPixels(GetGWorldPixMap(image));
72055702
SC
209 HUnlock( (**icon).iconData ) ;
210
211 (**icon).iconMask.rowBytes = iconSize / 8 ;
212 (**icon).iconMask.bounds = frame ;
213
214 (**icon).iconBMap.rowBytes = iconSize / 8 ;
215 (**icon).iconBMap.bounds = frame ;
216 (**icon).iconMask.baseAddr = (char*) &(**icon).iconMaskData ;
217 (**icon).iconBMap.baseAddr = (char*) &(**icon).iconMaskData + bwSize ;
218
219 if ( mask )
220 {
e40298d5
JS
221 Rect r ;
222 GetPortBounds( image , &r ) ;
72055702
SC
223 LockPixels(GetGWorldPixMap(mask) ) ;
224 CopyBits(GetPortBitMapForCopyBits(mask) ,
93e5d899 225 &(**icon).iconBMap , &r , &r, srcCopy , nil ) ;
72055702 226 CopyBits(GetPortBitMapForCopyBits(mask) ,
93e5d899 227 &(**icon).iconMask , &r , &r, srcCopy , nil ) ;
72055702
SC
228 UnlockPixels(GetGWorldPixMap( mask ) ) ;
229 }
230 else
231 {
e40298d5
JS
232 Rect r ;
233 GetPortBounds( image , &r ) ;
4e9ed364 234 LockPixels(GetGWorldPixMap(image));
72055702 235 CopyBits(GetPortBitMapForCopyBits(image) ,
93e5d899 236 &(**icon).iconBMap , &r , &r, srcCopy , nil ) ;
72055702 237 CopyBits(GetPortBitMapForCopyBits(image) ,
93e5d899 238 &(**icon).iconMask , &r , &r, srcCopy , nil ) ;
4e9ed364 239 UnlockPixels(GetGWorldPixMap(image));
72055702
SC
240 }
241
242 (**icon).iconMask.baseAddr = NULL ;
243 (**icon).iconBMap.baseAddr = NULL ;
244 (**icon).iconPMap.baseAddr = NULL ;
245 HUnlock((Handle)icon) ;
4e9ed364
RR
246 SetGWorld(saveWorld,saveHandle);
247
248 return icon;
72055702
SC
249}
250
251PicHandle wxMacCreatePict(GWorldPtr wp, GWorldPtr mask)
252{
253 CGrafPtr origPort ;
254 GDHandle origDev ;
255
256 PicHandle pict;
257
258 RGBColor white = { 0xffff ,0xffff , 0xffff } ;
259 RGBColor black = { 0x0000 ,0x0000 , 0x0000 } ;
260
261 GetGWorld( &origPort , &origDev ) ;
262
263 RgnHandle clipRgn = NULL ;
264
265 if ( mask )
266 {
267 clipRgn = NewRgn() ;
268 LockPixels( GetGWorldPixMap( mask ) ) ;
269 BitMapToRegion( clipRgn , (BitMap*) *GetGWorldPixMap( mask ) ) ;
270 UnlockPixels( GetGWorldPixMap( mask ) ) ;
271 }
4e9ed364 272
72055702
SC
273 SetGWorld( wp , NULL ) ;
274 Rect portRect ;
2b5f62a0
VZ
275 if ( clipRgn )
276 GetRegionBounds( clipRgn , &portRect ) ;
277 else
e40298d5 278 GetPortBounds( wp , &portRect ) ;
72055702 279 pict = OpenPicture(&portRect);
4e9ed364 280 if(pict)
72055702
SC
281 {
282 RGBForeColor( &black ) ;
283 RGBBackColor( &white ) ;
2b5f62a0
VZ
284
285 if ( clipRgn )
e40298d5 286 SetClip( clipRgn ) ;
2b5f62a0 287
72055702 288 LockPixels( GetGWorldPixMap( wp ) ) ;
4e9ed364
RR
289 CopyBits(GetPortBitMapForCopyBits(wp),
290 GetPortBitMapForCopyBits(wp),
291 &portRect,
292 &portRect,
293 srcCopy,clipRgn);
72055702 294 UnlockPixels( GetGWorldPixMap( wp ) ) ;
4e9ed364 295 ClosePicture();
72055702
SC
296 }
297 SetGWorld( origPort , origDev ) ;
2b5f62a0 298 if ( clipRgn )
e40298d5 299 DisposeRgn( clipRgn ) ;
4e9ed364 300 return pict;
5fde6fcc
GD
301}
302
2b5f62a0 303void wxMacCreateBitmapButton( ControlButtonContentInfo*info , const wxBitmap& bitmap , int forceType )
be295828
SC
304{
305 memset( info , 0 , sizeof(ControlButtonContentInfo) ) ;
306 if ( bitmap.Ok() )
307 {
308 wxBitmapRefData * bmap = (wxBitmapRefData*) ( bitmap.GetRefData()) ;
309 if ( bmap == NULL )
310 return ;
311
312 if ( bmap->m_bitmapType == kMacBitmapTypePict )
313 {
314 info->contentType = kControlContentPictHandle ;
315 info->u.picture = MAC_WXHMETAFILE(bmap->m_hPict) ;
316 }
317 else if ( bmap->m_bitmapType == kMacBitmapTypeGrafWorld )
318 {
2b5f62a0 319 if ( (forceType == kControlContentCIconHandle || ( bmap->m_width == bmap->m_height && forceType != kControlContentPictHandle ) ) && ((bmap->m_width & 0x3) == 0) )
be295828
SC
320 {
321 info->contentType = kControlContentCIconHandle ;
322 if ( bitmap.GetMask() )
93e5d899 323 {
be295828
SC
324 info->u.cIconHandle = wxMacCreateCIcon( MAC_WXHBITMAP(bmap->m_hBitmap) , MAC_WXHBITMAP(bitmap.GetMask()->GetMaskBitmap()) ,
325 8 , bmap->m_width ) ;
326 }
327 else
328 {
329 info->u.cIconHandle = wxMacCreateCIcon( MAC_WXHBITMAP(bmap->m_hBitmap) , NULL ,
330 8 , bmap->m_width ) ;
331 }
332 }
333 else
334 {
335 info->contentType = kControlContentPictHandle ;
336 if ( bitmap.GetMask() )
337 {
e40298d5 338 info->u.picture = wxMacCreatePict( MAC_WXHBITMAP(bmap->m_hBitmap) , MAC_WXHBITMAP(bitmap.GetMask()->GetMaskBitmap() ) ) ;
be295828
SC
339 }
340 else
341 {
e40298d5 342 info->u.picture = wxMacCreatePict( MAC_WXHBITMAP(bmap->m_hBitmap) , NULL ) ;
be295828
SC
343 }
344 }
345 }
e40298d5
JS
346 else if ( bmap->m_bitmapType == kMacBitmapTypeIcon )
347 {
348 info->contentType = kControlContentCIconHandle ;
349 info->u.cIconHandle = MAC_WXHICON(bmap->m_hIcon) ;
350 }
be295828
SC
351 }
352}
353
e9576ca5 354wxBitmapRefData::wxBitmapRefData()
be52b341
GD
355 : m_width(0)
356 , m_height(0)
357 , m_depth(0)
358 , m_ok(FALSE)
359 , m_numColors(0)
360 , m_quality(0)
e9576ca5 361{
e9576ca5 362 m_bitmapMask = NULL;
973b0afb
GD
363 m_hBitmap = NULL ;
364 m_hPict = NULL ;
3dec57ad 365 m_hIcon = NULL ;
973b0afb 366 m_bitmapType = kMacBitmapTypeUnknownType ;
e9576ca5
SC
367}
368
be52b341 369// TODO move this to a public function of Bitmap Ref
85f296a3 370static void DisposeBitmapRefData(wxBitmapRefData *data)
e9576ca5 371{
e40298d5
JS
372 if ( !data )
373 return ;
374
375 switch (data->m_bitmapType)
376 {
377 case kMacBitmapTypePict :
378 {
379 if ( data->m_hPict )
380 {
381 KillPicture( MAC_WXHMETAFILE( data->m_hPict ) ) ;
382 data->m_hPict = NULL ;
383 }
384 }
385 break ;
386 case kMacBitmapTypeGrafWorld :
387 {
388 if ( data->m_hBitmap )
389 {
390 wxMacDestroyGWorld( MAC_WXHBITMAP(data->m_hBitmap) ) ;
391 data->m_hBitmap = NULL ;
392 }
393 }
394 break ;
395 case kMacBitmapTypeIcon :
396 if ( data->m_hIcon )
397 {
398 DisposeCIcon( MAC_WXHICON(data->m_hIcon) ) ;
399 data->m_hIcon = NULL ;
400 }
401
402 default :
403 // unkown type ?
404 break ;
405 }
4e9ed364 406
e40298d5
JS
407 if (data->m_bitmapMask)
408 {
409 delete data->m_bitmapMask;
410 data->m_bitmapMask = NULL;
411 }
e9576ca5
SC
412}
413
85f296a3
SC
414wxBitmapRefData::~wxBitmapRefData()
415{
416 DisposeBitmapRefData( this ) ;
417}
418
90b959ae
SC
419bool wxBitmap::CopyFromIcon(const wxIcon& icon)
420{
421 Ref(icon) ;
125c389e 422 return true;
90b959ae
SC
423}
424
e9576ca5
SC
425wxBitmap::wxBitmap()
426{
427 m_refData = NULL;
e9576ca5
SC
428}
429
430wxBitmap::~wxBitmap()
431{
e9576ca5
SC
432}
433
434wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
435{
436 m_refData = new wxBitmapRefData;
437
438 M_BITMAPDATA->m_width = the_width ;
439 M_BITMAPDATA->m_height = the_height ;
440 M_BITMAPDATA->m_depth = no_bits ;
441 M_BITMAPDATA->m_numColors = 0;
d2c6d549
GD
442 if ( no_bits == 1 )
443 {
973b0afb 444 M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ;
76a5e5d2
SC
445 MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) = wxMacCreateGWorld( the_width , the_height , no_bits ) ;
446 M_BITMAPDATA->m_ok = (MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) != NULL ) ;
4e9ed364
RR
447
448 CGrafPtr origPort ;
449 GDHandle origDevice ;
450
973b0afb 451 GetGWorld( &origPort , &origDevice ) ;
76a5e5d2
SC
452 SetGWorld( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) , NULL ) ;
453 LockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) ) ) ;
4e9ed364 454
973b0afb 455 // bits is a char array
4e9ed364 456
973b0afb
GD
457 unsigned char* linestart = (unsigned char*) bits ;
458 int linesize = ( the_width / (sizeof(unsigned char) * 8)) ;
459 if ( the_width % (sizeof(unsigned char) * 8) ) {
460 linesize += sizeof(unsigned char);
461 }
4e9ed364 462
973b0afb
GD
463 RGBColor colors[2] = {
464 { 0xFFFF , 0xFFFF , 0xFFFF } ,
465 { 0, 0 , 0 }
466 } ;
4e9ed364 467
973b0afb
GD
468 for ( int y = 0 ; y < the_height ; ++y , linestart += linesize )
469 {
470 for ( int x = 0 ; x < the_width ; ++x )
471 {
472 int index = x / 8 ;
473 int bit = x % 8 ;
474 int mask = 1 << bit ;
475 if ( linestart[index] & mask )
476 {
477 SetCPixel( x , y , &colors[1] ) ;
478 }
479 else
480 {
481 SetCPixel( x , y , &colors[0] ) ;
482 }
483 }
484 }
76a5e5d2 485 UnlockPixels( GetGWorldPixMap( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) ) ) ;
4e9ed364 486
973b0afb 487 SetGWorld( origPort , origDevice ) ;
d2c6d549
GD
488 }
489 else
490 {
973b0afb 491 wxFAIL_MSG(wxT("multicolor BITMAPs not yet implemented"));
d2c6d549 492 }
e9576ca5
SC
493}
494
495wxBitmap::wxBitmap(int w, int h, int d)
496{
497 (void)Create(w, h, d);
e9576ca5
SC
498}
499
a8562f55 500wxBitmap::wxBitmap(void *data, wxBitmapType type, int width, int height, int depth)
e9576ca5
SC
501{
502 (void) Create(data, type, width, height, depth);
e9576ca5
SC
503}
504
a8562f55 505wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
e9576ca5 506{
a8562f55 507 LoadFile(filename, type);
e9576ca5
SC
508}
509
973b0afb 510bool wxBitmap::CreateFromXpm(const char **bits)
e9576ca5 511{
973b0afb
GD
512 wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") )
513 wxXPMDecoder decoder;
514 wxImage img = decoder.ReadData(bits);
515 wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid bitmap data") )
516 *this = wxBitmap(img);
973b0afb
GD
517 return TRUE;
518}
519
520wxBitmap::wxBitmap(const char **bits)
521{
973b0afb 522 (void) CreateFromXpm(bits);
e9576ca5 523}
e9576ca5 524
973b0afb 525wxBitmap::wxBitmap(char **bits)
03e11df5 526{
973b0afb 527 (void) CreateFromXpm((const char **)bits);
03e11df5
GD
528}
529
5fde6fcc
GD
530wxBitmap wxBitmap::GetSubBitmap(const wxRect &rect) const
531{
532 wxCHECK_MSG( Ok() &&
533 (rect.x >= 0) && (rect.y >= 0) &&
534 (rect.x+rect.width <= GetWidth()) &&
535 (rect.y+rect.height <= GetHeight()),
536 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
537
538
539 wxBitmap ret( rect.width, rect.height, GetDepth() );
540 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
541
76a5e5d2 542 GWorldPtr origPort;
5fde6fcc
GD
543 GDHandle origDevice;
544
545 GetGWorld( &origPort, &origDevice );
546
547 // Update the subbitmaps reference data
548 wxBitmapRefData *ref = (wxBitmapRefData *)ret.GetRefData();
549
550 ref->m_numColors = M_BITMAPDATA->m_numColors;
551 ref->m_bitmapPalette = M_BITMAPDATA->m_bitmapPalette;
552 ref->m_bitmapType = M_BITMAPDATA->m_bitmapType;
553
554 // Copy sub region of this bitmap
555 if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypePict)
556 {
557 printf("GetSubBitmap: Copy a region of a Pict structure - TODO\n");
558 }
559 else if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypeGrafWorld)
560 {
561 // Copy mask
562 if(GetMask())
563 {
76a5e5d2 564 GWorldPtr submask, mask;
5fde6fcc
GD
565 RGBColor color;
566
76a5e5d2 567 mask = (GWorldPtr) GetMask()->GetMaskBitmap();
8dd336cb 568 submask = wxMacCreateGWorld(rect.width, rect.height, GetMask()->GetDepth() );
5fde6fcc
GD
569 LockPixels(GetGWorldPixMap(mask));
570 LockPixels(GetGWorldPixMap(submask));
571
572 for(int yy = 0; yy < rect.height; yy++)
573 {
574 for(int xx = 0; xx < rect.width; xx++)
575 {
576 SetGWorld(mask, NULL);
577 GetCPixel(rect.x + xx, rect.y + yy, &color);
578 SetGWorld(submask, NULL);
579 SetCPixel(xx,yy, &color);
580 }
581 }
582 UnlockPixels(GetGWorldPixMap(mask));
583 UnlockPixels(GetGWorldPixMap(submask));
584 ref->m_bitmapMask = new wxMask;
585 ref->m_bitmapMask->SetMaskBitmap(submask);
586 }
587
588 // Copy bitmap
589 if(GetHBITMAP())
590 {
76a5e5d2 591 GWorldPtr subbitmap, bitmap;
5fde6fcc
GD
592 RGBColor color;
593
76a5e5d2
SC
594 bitmap = (GWorldPtr) GetHBITMAP();
595 subbitmap = (GWorldPtr) ref->m_hBitmap ;
5fde6fcc
GD
596 LockPixels(GetGWorldPixMap(bitmap));
597 LockPixels(GetGWorldPixMap(subbitmap));
598
599 for(int yy = 0; yy < rect.height; yy++)
600 {
601 for(int xx = 0; xx < rect.width; xx++)
602 {
603 SetGWorld(bitmap, NULL);
604 GetCPixel(rect.x + xx, rect.y + yy, &color);
605 SetGWorld(subbitmap, NULL);
606 SetCPixel(xx, yy, &color);
607 }
608 }
609 UnlockPixels(GetGWorldPixMap(bitmap));
610 UnlockPixels(GetGWorldPixMap(subbitmap));
5fde6fcc
GD
611 }
612 }
613 SetGWorld( origPort, origDevice );
614
615 return ret;
616}
617
e9576ca5
SC
618bool wxBitmap::Create(int w, int h, int d)
619{
620 UnRef();
621
622 m_refData = new wxBitmapRefData;
623
624 M_BITMAPDATA->m_width = w;
625 M_BITMAPDATA->m_height = h;
626 M_BITMAPDATA->m_depth = d;
627
519cb848
SC
628 M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ;
629 M_BITMAPDATA->m_hBitmap = wxMacCreateGWorld( w , h , d ) ;
76a5e5d2 630 M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hBitmap != NULL ) ;
e9576ca5
SC
631 return M_BITMAPDATA->m_ok;
632}
633
5fde6fcc
GD
634int wxBitmap::GetBitmapType() const
635{
636 wxCHECK_MSG( Ok(), kMacBitmapTypeUnknownType, wxT("invalid bitmap") );
637
638 return M_BITMAPDATA->m_bitmapType;
639}
640
519cb848
SC
641void wxBitmap::SetHBITMAP(WXHBITMAP bmp)
642{
2a391f87
SC
643 if (!M_BITMAPDATA)
644 m_refData = new wxBitmapRefData;
e40298d5
JS
645 else
646 DisposeBitmapRefData( M_BITMAPDATA ) ;
85f296a3 647
519cb848
SC
648 M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ;
649 M_BITMAPDATA->m_hBitmap = bmp ;
2a391f87
SC
650 M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hBitmap != NULL ) ;
651}
652
653void wxBitmap::SetHICON(WXHICON ico)
654{
655 if (!M_BITMAPDATA)
656 m_refData = new wxBitmapRefData;
e40298d5
JS
657 else
658 DisposeBitmapRefData( M_BITMAPDATA ) ;
2a391f87
SC
659
660 M_BITMAPDATA->m_bitmapType = kMacBitmapTypeIcon ;
661 M_BITMAPDATA->m_hIcon = ico ;
662 M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hIcon != NULL ) ;
663}
664
665void wxBitmap::SetPict(WXHMETAFILE pict)
666{
667 if (!M_BITMAPDATA)
668 m_refData = new wxBitmapRefData;
e40298d5
JS
669 else
670 DisposeBitmapRefData( M_BITMAPDATA ) ;
2a391f87
SC
671
672 M_BITMAPDATA->m_bitmapType = kMacBitmapTypePict ;
673 M_BITMAPDATA->m_hPict = pict ;
674 M_BITMAPDATA->m_ok = ( M_BITMAPDATA->m_hPict != NULL ) ;
519cb848
SC
675}
676
a8562f55 677bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
e9576ca5
SC
678{
679 UnRef();
680
e9576ca5
SC
681 wxBitmapHandler *handler = FindHandler(type);
682
a8562f55
GD
683 if ( handler )
684 {
4e9ed364 685 m_refData = new wxBitmapRefData;
e9576ca5 686
a8562f55 687 return handler->LoadFile(this, filename, type, -1, -1);
e9576ca5 688 }
a8562f55
GD
689 else
690 {
691 wxImage loadimage(filename, type);
692 if (loadimage.Ok()) {
693 *this = loadimage;
694 return true;
695 }
696 }
427ff662 697 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
a8562f55 698 return false;
e9576ca5
SC
699}
700
a8562f55 701bool wxBitmap::Create(void *data, wxBitmapType type, int width, int height, int depth)
e9576ca5
SC
702{
703 UnRef();
704
705 m_refData = new wxBitmapRefData;
706
707 wxBitmapHandler *handler = FindHandler(type);
708
709 if ( handler == NULL ) {
427ff662 710 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
e9576ca5
SC
711
712 return FALSE;
713 }
714
715 return handler->Create(this, data, type, width, height, depth);
716}
717
fec19ea9
VS
718wxBitmap::wxBitmap(const wxImage& image, int depth)
719{
720 wxCHECK_RET( image.Ok(), wxT("invalid image") )
721 wxCHECK_RET( depth == -1, wxT("invalid bitmap depth") )
722
723 m_refData = new wxBitmapRefData();
724
fec19ea9
VS
725 // width and height of the device-dependent bitmap
726 int width = image.GetWidth();
727 int height = image.GetHeight();
728
729 // Create picture
730
71789654 731 Create( width , height , 32 ) ;
fec19ea9
VS
732
733 CGrafPtr origPort ;
734 GDHandle origDevice ;
735
76a5e5d2 736 PixMapHandle pixMap = GetGWorldPixMap((GWorldPtr)GetHBITMAP()) ;
71789654 737 LockPixels( pixMap );
fec19ea9
VS
738
739 GetGWorld( &origPort , &origDevice ) ;
76a5e5d2 740 SetGWorld( (GWorldPtr) GetHBITMAP() , NULL ) ;
fec19ea9
VS
741
742 // Render image
fec19ea9 743 register unsigned char* data = image.GetData();
71789654
SC
744 char* destinationBase = GetPixBaseAddr( pixMap );
745 register unsigned char* destination = (unsigned char*) destinationBase ;
fec19ea9
VS
746 for (int y = 0; y < height; y++)
747 {
748 for (int x = 0; x < width; x++)
749 {
71789654
SC
750 *destination++ = 0 ;
751 *destination++ = *data++ ;
752 *destination++ = *data++ ;
753 *destination++ = *data++ ;
fec19ea9 754 }
71789654
SC
755 destinationBase += ((**pixMap).rowBytes & 0x7fff);
756 destination = (unsigned char*) destinationBase ;
757 }
8dd336cb
SC
758 if ( image.HasAlpha() )
759 {
760 unsigned char *alpha = image.GetAlpha();
761
762 wxColour maskcolor(image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue());
763 RGBColor color ;
764 wxBitmap maskBitmap ;
765
766 maskBitmap.Create( width, height, 24);
767 LockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) );
768 SetGWorld( (GWorldPtr) maskBitmap.GetHBITMAP(), NULL);
769
770 for (int y = 0; y < height; y++)
771 {
772 for (int x = 0; x < width; x++)
773 {
774 memset( &color , 255 - *alpha , sizeof( color ) );
775 SetCPixel(x,y, &color);
776
777 alpha += 1 ;
778 }
779 } // for height
780 SetGWorld( (GWorldPtr) GetHBITMAP(), NULL);
781 SetMask(new wxMask( maskBitmap ));
782 UnlockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) );
783 }
784 else if ( image.HasMask() )
71789654
SC
785 {
786 data = image.GetData();
787
788 wxColour maskcolor(image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue());
789 RGBColor white = { 0xffff, 0xffff, 0xffff };
790 RGBColor black = { 0 , 0 , 0 };
791 wxBitmap maskBitmap ;
fec19ea9 792
71789654 793 maskBitmap.Create( width, height, 1);
76a5e5d2
SC
794 LockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) );
795 SetGWorld( (GWorldPtr) maskBitmap.GetHBITMAP(), NULL);
71789654
SC
796
797 for (int y = 0; y < height; y++)
798 {
799 for (int x = 0; x < width; x++)
800 {
2af9ef13 801 if ( data[0] == image.GetMaskRed() && data[1] == image.GetMaskGreen() && data[2] == image.GetMaskBlue() )
71789654
SC
802 {
803 SetCPixel(x,y, &white);
804 }
805 else {
8dd336cb 806 SetCPixel(x,y, &black);
71789654
SC
807 }
808 data += 3 ;
809 }
810 } // for height
76a5e5d2 811 SetGWorld( (GWorldPtr) GetHBITMAP(), NULL);
71789654 812 SetMask(new wxMask( maskBitmap ));
76a5e5d2 813 UnlockPixels( GetGWorldPixMap( (GWorldPtr) maskBitmap.GetHBITMAP()) );
fec19ea9
VS
814 }
815
76a5e5d2 816 UnlockPixels( GetGWorldPixMap( (GWorldPtr) GetHBITMAP()) );
fec19ea9
VS
817 SetGWorld( origPort, origDevice );
818}
819
820wxImage wxBitmap::ConvertToImage() const
821{
822 wxImage image;
823
824 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
825
826 // create an wxImage object
827 int width = GetWidth();
828 int height = GetHeight();
829 image.Create( width, height );
830
831 unsigned char *data = image.GetData();
832
833 wxCHECK_MSG( data, wxNullImage, wxT("Could not allocate data for image") );
834
76a5e5d2 835 GWorldPtr origPort;
fec19ea9 836 GDHandle origDevice;
2b5f62a0
VZ
837 RgnHandle maskRgn = NULL ;
838 GWorldPtr tempPort = NULL ;
fec19ea9
VS
839 int index;
840 RGBColor color;
841 // background color set to RGB(16,16,16) in consistent with wxGTK
842 unsigned char mask_r=16, mask_g=16, mask_b=16;
843 SInt16 r,g,b;
844 wxMask *mask = GetMask();
845
846 GetGWorld( &origPort, &origDevice );
2b5f62a0
VZ
847 if ( GetBitmapType() != kMacBitmapTypeGrafWorld )
848 {
e40298d5 849 tempPort = wxMacCreateGWorld( width , height , -1) ;
2b5f62a0
VZ
850 }
851 else
852 {
e40298d5
JS
853 tempPort = (GWorldPtr) GetHBITMAP() ;
854 }
855 LockPixels(GetGWorldPixMap(tempPort));
856 SetGWorld( tempPort, NULL);
857 if ( GetBitmapType() == kMacBitmapTypePict || GetBitmapType() == kMacBitmapTypeIcon )
858 {
2b5f62a0 859 Rect bitmaprect = { 0 , 0 , height, width };
e40298d5
JS
860 if ( GetBitmapType() == kMacBitmapTypeIcon )
861 {
862 ::PlotCIconHandle( &bitmaprect , atNone , ttNone , MAC_WXHICON(GetHICON()) ) ;
863 maskRgn = NewRgn() ;
864 BitMapToRegion( maskRgn , &(**(MAC_WXHICON(GetHICON()))).iconMask ) ;
2b5f62a0 865 }
e40298d5
JS
866 else
867 ::DrawPicture( (PicHandle) GetPict(), &bitmaprect ) ;
868 }
fec19ea9
VS
869 // Copy data into image
870 index = 0;
871 for (int yy = 0; yy < height; yy++)
872 {
873 for (int xx = 0; xx < width; xx++)
874 {
875 GetCPixel(xx,yy, &color);
876 r = ((color.red ) >> 8);
877 g = ((color.green ) >> 8);
878 b = ((color.blue ) >> 8);
879 data[index ] = r;
880 data[index + 1] = g;
881 data[index + 2] = b;
2b5f62a0 882 if ( maskRgn )
e40298d5
JS
883 {
884 Point pt ;
885 pt.h = xx ;
886 pt.v = yy ;
887 if ( !PtInRgn( pt , maskRgn ) )
888 {
fec19ea9
VS
889 data[index ] = mask_r;
890 data[index + 1] = mask_g;
891 data[index + 2] = mask_b;
e40298d5 892 }
2b5f62a0
VZ
893 }
894 else
895 {
e40298d5
JS
896 if (mask)
897 {
898 if (mask->PointMasked(xx,yy))
899 {
900 data[index ] = mask_r;
901 data[index + 1] = mask_g;
902 data[index + 2] = mask_b;
903 }
904 }
fec19ea9
VS
905 }
906 index += 3;
907 }
908 }
2b5f62a0 909 if (mask || maskRgn )
fec19ea9
VS
910 {
911 image.SetMaskColour( mask_r, mask_g, mask_b );
912 image.SetMask( true );
913 }
914
915 // Free resources
2b5f62a0 916 UnlockPixels(GetGWorldPixMap( tempPort ));
fec19ea9 917 SetGWorld(origPort, origDevice);
2b5f62a0
VZ
918 if ( GetBitmapType() != kMacBitmapTypeGrafWorld )
919 {
e40298d5 920 wxMacDestroyGWorld( tempPort ) ;
2b5f62a0
VZ
921 }
922 if ( maskRgn )
923 {
e40298d5 924 DisposeRgn( maskRgn ) ;
2b5f62a0 925 }
fec19ea9
VS
926
927 return image;
928}
929
930
a8562f55
GD
931bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type,
932 const wxPalette *palette) const
e9576ca5
SC
933{
934 wxBitmapHandler *handler = FindHandler(type);
935
a8562f55
GD
936 if ( handler )
937 {
938 return handler->SaveFile(this, filename, type, palette);
939 }
940 else
941 {
942 wxImage image = ConvertToImage();
e9576ca5 943
a8562f55
GD
944 return image.SaveFile(filename, type);
945 }
946
427ff662 947 wxLogWarning(wxT("no bitmap handler for type %d defined."), type);
a8562f55 948 return false;
e9576ca5
SC
949}
950
5fde6fcc
GD
951bool wxBitmap::Ok() const
952{
953 return (M_BITMAPDATA && M_BITMAPDATA->m_ok);
954}
955
956int wxBitmap::GetHeight() const
957{
958 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
959
960 return M_BITMAPDATA->m_height;
961}
962
963int wxBitmap::GetWidth() const
964{
965 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
966
967 return M_BITMAPDATA->m_width;
968}
969
970int wxBitmap::GetDepth() const
971{
972 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
973
974 return M_BITMAPDATA->m_depth;
975}
976
977int wxBitmap::GetQuality() const
978{
979 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
980
981 return M_BITMAPDATA->m_quality;
982}
983
984wxMask *wxBitmap::GetMask() const
985{
986 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
987
988 return M_BITMAPDATA->m_bitmapMask;
989}
990
e9576ca5
SC
991void wxBitmap::SetWidth(int w)
992{
993 if (!M_BITMAPDATA)
994 m_refData = new wxBitmapRefData;
995
996 M_BITMAPDATA->m_width = w;
997}
998
999void wxBitmap::SetHeight(int h)
1000{
1001 if (!M_BITMAPDATA)
1002 m_refData = new wxBitmapRefData;
1003
1004 M_BITMAPDATA->m_height = h;
1005}
1006
1007void wxBitmap::SetDepth(int d)
1008{
1009 if (!M_BITMAPDATA)
1010 m_refData = new wxBitmapRefData;
1011
1012 M_BITMAPDATA->m_depth = d;
1013}
1014
1015void wxBitmap::SetQuality(int q)
1016{
1017 if (!M_BITMAPDATA)
1018 m_refData = new wxBitmapRefData;
1019
1020 M_BITMAPDATA->m_quality = q;
1021}
1022
1023void wxBitmap::SetOk(bool isOk)
1024{
1025 if (!M_BITMAPDATA)
1026 m_refData = new wxBitmapRefData;
1027
1028 M_BITMAPDATA->m_ok = isOk;
1029}
1030
5fde6fcc
GD
1031wxPalette *wxBitmap::GetPalette() const
1032{
1033 wxCHECK_MSG( Ok(), NULL, wxT("Invalid bitmap GetPalette()") );
1034
1035 return &M_BITMAPDATA->m_bitmapPalette;
1036}
1037
e9576ca5
SC
1038void wxBitmap::SetPalette(const wxPalette& palette)
1039{
1040 if (!M_BITMAPDATA)
1041 m_refData = new wxBitmapRefData;
1042
1043 M_BITMAPDATA->m_bitmapPalette = palette ;
1044}
1045
1046void wxBitmap::SetMask(wxMask *mask)
1047{
1048 if (!M_BITMAPDATA)
1049 m_refData = new wxBitmapRefData;
1050
a8562f55
GD
1051 // Remove existing mask if there is one.
1052 if (M_BITMAPDATA->m_bitmapMask)
1053 delete M_BITMAPDATA->m_bitmapMask;
1054
e9576ca5
SC
1055 M_BITMAPDATA->m_bitmapMask = mask ;
1056}
1057
5fde6fcc
GD
1058WXHBITMAP wxBitmap::GetHBITMAP() const
1059{
1060 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
1061
76a5e5d2 1062 return MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap);
5fde6fcc
GD
1063}
1064
2a391f87 1065WXHMETAFILE wxBitmap::GetPict( bool *created ) const
5fde6fcc 1066{
e40298d5 1067 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
5fde6fcc 1068
e40298d5
JS
1069 PicHandle picture = NULL ; // This is the returned picture
1070 if ( created )
1071 (*created) = false ;
1072 // If bitmap already in Pict format return pointer
1073 if(M_BITMAPDATA->m_bitmapType == kMacBitmapTypePict) {
1074 return M_BITMAPDATA->m_hPict;
1075 }
1076 else if(M_BITMAPDATA->m_bitmapType != kMacBitmapTypeGrafWorld) {
1077 // Invalid bitmap
1078 return NULL;
1079 }
1080 else
1081 {
1082 if ( GetMask() )
1083 {
1084 picture = wxMacCreatePict( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) , MAC_WXHBITMAP(GetMask()->GetMaskBitmap() ) ) ;
1085 }
1086 else
1087 {
1088 picture = wxMacCreatePict( MAC_WXHBITMAP(M_BITMAPDATA->m_hBitmap) , NULL ) ;
1089 }
1090 if ( created && picture )
1091 (*created) = true ;
2a391f87 1092 }
e40298d5 1093 return picture ;
5fde6fcc
GD
1094}
1095
e9576ca5
SC
1096/*
1097 * wxMask
1098 */
1099
1100wxMask::wxMask()
be52b341 1101 : m_maskBitmap(NULL)
e9576ca5 1102{
e9576ca5
SC
1103}
1104
1105// Construct a mask from a bitmap and a colour indicating
1106// the transparent area
1107wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
be52b341 1108 : m_maskBitmap(NULL)
e9576ca5 1109{
e9576ca5
SC
1110 Create(bitmap, colour);
1111}
1112
1113// Construct a mask from a bitmap and a palette index indicating
1114// the transparent area
1115wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
be52b341 1116 : m_maskBitmap(NULL)
e9576ca5 1117{
e9576ca5
SC
1118 Create(bitmap, paletteIndex);
1119}
1120
1121// Construct a mask from a mono bitmap (copies the bitmap).
1122wxMask::wxMask(const wxBitmap& bitmap)
be52b341 1123 : m_maskBitmap(NULL)
e9576ca5 1124{
e9576ca5
SC
1125 Create(bitmap);
1126}
1127
1128wxMask::~wxMask()
1129{
4e9ed364
RR
1130 if ( m_maskBitmap )
1131 {
76a5e5d2 1132 wxMacDestroyGWorld( (GWorldPtr) m_maskBitmap ) ;
4e9ed364
RR
1133 m_maskBitmap = NULL ;
1134 }
e9576ca5
SC
1135}
1136
1137// Create a mask from a mono bitmap (copies the bitmap).
1138bool wxMask::Create(const wxBitmap& bitmap)
1139{
5fde6fcc
GD
1140 if ( m_maskBitmap )
1141 {
76a5e5d2 1142 wxMacDestroyGWorld( (GWorldPtr) m_maskBitmap ) ;
5fde6fcc
GD
1143 m_maskBitmap = NULL ;
1144 }
1145 wxCHECK_MSG( bitmap.GetBitmapType() == kMacBitmapTypeGrafWorld, false,
1146 wxT("Cannot create mask from this bitmap type (TODO)"));
1147 // other types would require a temporary bitmap. not yet implemented
1148
1149 wxCHECK_MSG( bitmap.Ok(), false, wxT("Invalid bitmap"));
1150
8dd336cb
SC
1151 m_depth = bitmap.GetDepth() ;
1152 m_maskBitmap = wxMacCreateGWorld(bitmap.GetWidth(), bitmap.GetHeight(), bitmap.GetDepth() );
5fde6fcc
GD
1153 Rect rect = { 0,0, bitmap.GetHeight(), bitmap.GetWidth() };
1154
76a5e5d2
SC
1155 LockPixels( GetGWorldPixMap( (GWorldPtr) m_maskBitmap) );
1156 LockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP()) );
1157 CopyBits(GetPortBitMapForCopyBits( (GWorldPtr) bitmap.GetHBITMAP()),
1158 GetPortBitMapForCopyBits( (GWorldPtr) m_maskBitmap),
5fde6fcc 1159 &rect, &rect, srcCopy, 0);
76a5e5d2
SC
1160 UnlockPixels( GetGWorldPixMap( (GWorldPtr) m_maskBitmap) );
1161 UnlockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP()) );
5fde6fcc
GD
1162
1163 return FALSE;
e9576ca5
SC
1164}
1165
1166// Create a mask from a bitmap and a palette index indicating
1167// the transparent area
1168bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
1169{
be52b341
GD
1170 // TODO
1171 wxCHECK_MSG( 0, false, wxT("wxMask::Create not yet implemented"));
e9576ca5
SC
1172 return FALSE;
1173}
1174
1175// Create a mask from a bitmap and a colour indicating
1176// the transparent area
1177bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1178{
4e9ed364
RR
1179 if ( m_maskBitmap )
1180 {
76a5e5d2 1181 wxMacDestroyGWorld( (GWorldPtr) m_maskBitmap ) ;
4e9ed364
RR
1182 m_maskBitmap = NULL ;
1183 }
1184 wxCHECK_MSG( bitmap.GetBitmapType() == kMacBitmapTypeGrafWorld, false,
5fde6fcc 1185 wxT("Cannot create mask from this bitmap type (TODO)"));
4e9ed364
RR
1186 // other types would require a temporary bitmap. not yet implemented
1187
5fde6fcc 1188 wxCHECK_MSG( bitmap.Ok(), false, wxT("Illigal bitmap"));
8208e181 1189
8dd336cb
SC
1190 m_maskBitmap = wxMacCreateGWorld( bitmap.GetWidth() , bitmap.GetHeight() , 1 );
1191 m_depth = 1 ;
76a5e5d2
SC
1192 LockPixels( GetGWorldPixMap( (GWorldPtr) m_maskBitmap ) );
1193 LockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP() ) );
1194 RGBColor maskColor = MAC_WXCOLORREF(colour.GetPixel());
8208e181
SC
1195
1196 // this is not very efficient, but I can't think
1197 // of a better way of doing it
4e9ed364
RR
1198 CGrafPtr origPort ;
1199 GDHandle origDevice ;
5fde6fcc
GD
1200 RGBColor col;
1201 RGBColor colors[2] = {
1202 { 0xFFFF, 0xFFFF, 0xFFFF },
1203 { 0, 0, 0 }};
4e9ed364
RR
1204
1205 GetGWorld( &origPort , &origDevice ) ;
1206 for (int w = 0; w < bitmap.GetWidth(); w++)
8208e181
SC
1207 {
1208 for (int h = 0; h < bitmap.GetHeight(); h++)
4e9ed364 1209 {
76a5e5d2 1210 SetGWorld( (GWorldPtr) bitmap.GetHBITMAP(), NULL ) ;
4e9ed364 1211 GetCPixel( w , h , &col ) ;
76a5e5d2 1212 SetGWorld( (GWorldPtr) m_maskBitmap , NULL ) ;
5fde6fcc 1213 if (col.red == maskColor.red && col.green == maskColor.green && col.blue == maskColor.blue)
8208e181 1214 {
4e9ed364 1215 SetCPixel( w , h , &colors[0] ) ;
8208e181
SC
1216 }
1217 else
1218 {
4e9ed364 1219 SetCPixel( w , h , &colors[1] ) ;
8208e181
SC
1220 }
1221 }
1222 }
4e9ed364 1223 UnlockPixels( GetGWorldPixMap( (CGrafPtr) m_maskBitmap ) ) ;
76a5e5d2 1224 UnlockPixels( GetGWorldPixMap( (GWorldPtr) bitmap.GetHBITMAP() ) ) ;
4e9ed364 1225 SetGWorld( origPort , origDevice ) ;
8208e181
SC
1226
1227 return TRUE;
e9576ca5
SC
1228}
1229
5fde6fcc
GD
1230bool wxMask::PointMasked(int x, int y)
1231{
76a5e5d2 1232 GWorldPtr origPort;
5fde6fcc
GD
1233 GDHandle origDevice;
1234 RGBColor color;
1235 bool masked = true;
1236
1237 GetGWorld( &origPort, &origDevice);
1238
1239 //Set port to mask and see if it masked (1) or not ( 0 )
76a5e5d2
SC
1240 SetGWorld( (GWorldPtr) m_maskBitmap, NULL);
1241 LockPixels(GetGWorldPixMap( (GWorldPtr) m_maskBitmap));
5fde6fcc
GD
1242 GetCPixel(x,y, &color);
1243 masked = !(color.red == 0 && color.green == 0 && color.blue == 0);
76a5e5d2 1244 UnlockPixels(GetGWorldPixMap( (GWorldPtr) m_maskBitmap));
5fde6fcc
GD
1245
1246 SetGWorld( origPort, origDevice);
1247
1248 return masked;
1249}
1250
e9576ca5
SC
1251/*
1252 * wxBitmapHandler
1253 */
1254
be52b341
GD
1255wxBitmapHandler::~wxBitmapHandler()
1256{
1257}
1258
e9576ca5
SC
1259bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth)
1260{
1261 return FALSE;
1262}
1263
a8562f55 1264bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
e9576ca5
SC
1265 int desiredWidth, int desiredHeight)
1266{
1267 return FALSE;
1268}
1269
a8562f55 1270bool wxBitmapHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette)
e9576ca5
SC
1271{
1272 return FALSE;
1273}
1274
1275/*
1276 * Standard handlers
1277 */
1278
519cb848
SC
1279class WXDLLEXPORT wxPICTResourceHandler: public wxBitmapHandler
1280{
1281 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler)
1282public:
1283 inline wxPICTResourceHandler()
1284 {
427ff662
SC
1285 m_name = wxT("Macintosh Pict resource");
1286 m_extension = wxEmptyString;
519cb848
SC
1287 m_type = wxBITMAP_TYPE_PICT_RESOURCE;
1288 };
1289
1290 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1291 int desiredWidth, int desiredHeight);
1292};
1293IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler, wxBitmapHandler)
1294
1295bool wxPICTResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
1296 int desiredWidth, int desiredHeight)
1297{
4e9ed364 1298 Str255 theName ;
427ff662
SC
1299 wxMacStringToPascal( name , theName ) ;
1300
4e9ed364
RR
1301 PicHandle thePict = (PicHandle ) GetNamedResource( 'PICT' , theName ) ;
1302 if ( thePict )
1303 {
1304 PictInfo theInfo ;
1305
1306 GetPictInfo( thePict , &theInfo , 0 , 0 , systemMethod , 0 ) ;
1307 DetachResource( (Handle) thePict ) ;
1308 M_BITMAPHANDLERDATA->m_bitmapType = kMacBitmapTypePict ;
1309 M_BITMAPHANDLERDATA->m_hPict = thePict ;
1310 M_BITMAPHANDLERDATA->m_width = theInfo.sourceRect.right - theInfo.sourceRect.left ;
1311 M_BITMAPHANDLERDATA->m_height = theInfo.sourceRect.bottom - theInfo.sourceRect.top ;
1312
1313 M_BITMAPHANDLERDATA->m_depth = theInfo.depth ;
1314 M_BITMAPHANDLERDATA->m_ok = true ;
1315 M_BITMAPHANDLERDATA->m_numColors = theInfo.uniqueColors ;
1316// M_BITMAPHANDLERDATA->m_bitmapPalette;
1317// M_BITMAPHANDLERDATA->m_quality;
1318 return TRUE ;
1319 }
1320 return FALSE ;
519cb848
SC
1321}
1322
e9576ca5
SC
1323void wxBitmap::InitStandardHandlers()
1324{
973b0afb
GD
1325 AddHandler(new wxPICTResourceHandler) ;
1326 AddHandler(new wxICONResourceHandler) ;
e9576ca5 1327}