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