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