]> git.saurik.com Git - wxWidgets.git/blob - src/mac/bitmap.cpp
VTK wrapper of vtkRenderWindow for wxPython. Tested on MSW so far.
[wxWidgets.git] / src / mac / 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 "bitmap.h"
14 #endif
15
16 #include "wx/setup.h"
17 #include "wx/utils.h"
18 #include "wx/palette.h"
19 #include "wx/bitmap.h"
20 #include "wx/icon.h"
21 #include "wx/log.h"
22
23 extern "C"
24 {
25 #include "xpm.h"
26 } ;
27
28 #if !USE_SHARED_LIBRARIES
29 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
30 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
31 #endif
32
33 #include <PictUtils.h>
34
35 CTabHandle wxMacCreateColorTable( int numColors )
36 {
37 CTabHandle newColors; /* Handle to the new color table */
38 short index; /* Index into the table of colors */
39 /* Allocate memory for the color table */
40 newColors = (CTabHandle)NewHandleClear( sizeof (ColorTable) +
41 sizeof (ColorSpec) * (numColors - 1) );
42 if (newColors != nil)
43 {
44 /* Initialize the fields */
45 (**newColors).ctSeed = GetCTSeed();
46 (**newColors).ctFlags = 0;
47 (**newColors).ctSize = numColors - 1;
48 /* Initialize the table of colors */
49 }
50 return newColors ;
51 }
52
53 void wxMacDestroyColorTable( CTabHandle colors )
54 {
55 DisposeHandle( (Handle) colors ) ;
56 }
57
58 void wxMacSetColorTableEntry( CTabHandle newColors , int index , int red , int green , int blue )
59 {
60 (**newColors).ctTable[index].value = index;
61 (**newColors).ctTable[index].rgb.red = 0 ;// someRedValue;
62 (**newColors).ctTable[index].rgb.green = 0 ; // someGreenValue;
63 (**newColors).ctTable[index].rgb.blue = 0 ; // someBlueValue;
64 }
65
66 GWorldPtr wxMacCreateGWorld( int height , int width , int depth )
67 {
68 OSErr err = noErr ;
69 GWorldPtr port ;
70 Rect rect = { 0 , 0 , width , height } ;
71
72 if ( depth < 0 )
73 {
74 // get max pixel depth
75 CGrafPtr port ;
76 GetCWMgrPort( &port ) ;
77 GDHandle maxDevice ;
78
79 maxDevice = GetMaxDevice( &port->portRect ) ;
80 if ( maxDevice )
81 depth = (**((**maxDevice).gdPMap)).pixelSize ;
82 else
83 depth = 8 ;
84 }
85
86 err = NewGWorld( &port , depth , &rect , NULL , NULL , 0 ) ;
87 if ( err == noErr )
88 {
89 return port ;
90 }
91 return NULL ;
92 }
93
94 void wxMacDestroyGWorld( GWorldPtr gw )
95 {
96 if ( gw )
97 DisposeGWorld( gw ) ;
98 }
99
100 wxBitmapRefData::wxBitmapRefData()
101 {
102 m_ok = FALSE;
103 m_width = 0;
104 m_height = 0;
105 m_depth = 0;
106 m_quality = 0;
107 m_numColors = 0;
108 m_bitmapMask = NULL;
109 m_hBitmap = NULL ;
110 m_hPict = NULL ;
111 m_bitmapType = kMacBitmapTypeUnknownType ;
112 }
113
114 wxBitmapRefData::~wxBitmapRefData()
115 {
116 switch (m_bitmapType)
117 {
118 case kMacBitmapTypePict :
119 {
120 if ( m_hPict )
121 {
122 KillPicture( m_hPict ) ;
123 m_hPict = NULL ;
124 }
125 }
126 break ;
127 case kMacBitmapTypeGrafWorld :
128 {
129 if ( m_hBitmap )
130 {
131 wxMacDestroyGWorld( m_hBitmap ) ;
132 m_hBitmap = NULL ;
133 }
134 }
135 break ;
136 default :
137 // unkown type ?
138 break ;
139 } ;
140
141 if (m_bitmapMask)
142 {
143 delete m_bitmapMask;
144 m_bitmapMask = NULL;
145 }
146 }
147
148 wxList wxBitmap::sm_handlers;
149
150 wxBitmap::wxBitmap()
151 {
152 m_refData = NULL;
153
154 if ( wxTheBitmapList )
155 wxTheBitmapList->AddBitmap(this);
156 }
157
158 wxBitmap::~wxBitmap()
159 {
160 if (wxTheBitmapList)
161 wxTheBitmapList->DeleteObject(this);
162 }
163
164 wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
165 {
166 m_refData = new wxBitmapRefData;
167
168 M_BITMAPDATA->m_width = the_width ;
169 M_BITMAPDATA->m_height = the_height ;
170 M_BITMAPDATA->m_depth = no_bits ;
171 M_BITMAPDATA->m_numColors = 0;
172 if ( no_bits == 1 )
173 {
174 M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ;
175 M_BITMAPDATA->m_hBitmap = wxMacCreateGWorld( the_width , the_height , no_bits ) ;
176 M_BITMAPDATA->m_ok = (M_BITMAPDATA->m_hBitmap != NULL ) ;
177
178 CGrafPtr origPort ;
179 GDHandle origDevice ;
180
181 GetGWorld( &origPort , &origDevice ) ;
182 SetGWorld( M_BITMAPDATA->m_hBitmap , NULL ) ;
183
184 // bits is a word aligned array
185
186 unsigned char* linestart = (unsigned char*) bits ;
187 int linesize = ( the_width / 16 ) * 2 ;
188 if ( the_width % 16 )
189 {
190 linesize += 2 ;
191 } ;
192
193 RGBColor colors[2] = {
194 { 0xFFFF , 0xFFFF , 0xFFFF } ,
195 { 0, 0 , 0 }
196 } ;
197
198 for( int y = 0 ; y < the_height ; ++y , linestart += linesize )
199 {
200 for( int x = 0 ; x < the_width ; ++x )
201 {
202 int index = x / 8 ;
203 int bit = x % 8 ;
204 int mask = 1 << bit ;
205 if ( linestart[index] & mask )
206 {
207 SetCPixel( x , y , &colors[1] ) ;
208 }
209 else
210 {
211 SetCPixel( x , y , &colors[0] ) ;
212 }
213 }
214
215 }
216
217 SetGWorld( origPort , origDevice ) ;
218 }
219 else
220 {
221 //multicolor BITMAPs not yet implemented
222 }
223
224 if ( wxTheBitmapList )
225 wxTheBitmapList->AddBitmap(this);
226 }
227
228 wxBitmap::wxBitmap(int w, int h, int d)
229 {
230 (void)Create(w, h, d);
231
232 if ( wxTheBitmapList )
233 wxTheBitmapList->AddBitmap(this);
234 }
235
236 wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth)
237 {
238 (void) Create(data, type, width, height, depth);
239
240 if ( wxTheBitmapList )
241 wxTheBitmapList->AddBitmap(this);
242 }
243
244 wxBitmap::wxBitmap(const wxString& filename, long type)
245 {
246 LoadFile(filename, (int)type);
247
248 if ( wxTheBitmapList )
249 wxTheBitmapList->AddBitmap(this);
250 }
251
252 wxBitmap::wxBitmap(const char **data)
253 {
254 (void) Create((void *)data, wxBITMAP_TYPE_XPM_DATA, 0, 0, 0);
255 }
256
257 bool wxBitmap::Create(int w, int h, int d)
258 {
259 UnRef();
260
261 m_refData = new wxBitmapRefData;
262
263 M_BITMAPDATA->m_width = w;
264 M_BITMAPDATA->m_height = h;
265 M_BITMAPDATA->m_depth = d;
266
267 M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ;
268 M_BITMAPDATA->m_hBitmap = wxMacCreateGWorld( w , h , d ) ;
269 M_BITMAPDATA->m_ok = (M_BITMAPDATA->m_hBitmap != NULL ) ;
270 return M_BITMAPDATA->m_ok;
271 }
272
273 void wxBitmap::SetHBITMAP(WXHBITMAP bmp)
274 {
275 M_BITMAPDATA->m_bitmapType = kMacBitmapTypeGrafWorld ;
276 M_BITMAPDATA->m_hBitmap = bmp ;
277 M_BITMAPDATA->m_ok = (M_BITMAPDATA->m_hBitmap != NULL ) ;
278 }
279
280 bool wxBitmap::LoadFile(const wxString& filename, long type)
281 {
282 UnRef();
283
284 m_refData = new wxBitmapRefData;
285
286 wxBitmapHandler *handler = FindHandler(type);
287
288 if ( handler == NULL ) {
289 wxLogWarning("no bitmap handler for type %d defined.", type);
290
291 return FALSE;
292 }
293
294 return handler->LoadFile(this, filename, type, -1, -1);
295 }
296
297 bool wxBitmap::Create(void *data, long type, int width, int height, int depth)
298 {
299 UnRef();
300
301 m_refData = new wxBitmapRefData;
302
303 wxBitmapHandler *handler = FindHandler(type);
304
305 if ( handler == NULL ) {
306 wxLogWarning("no bitmap handler for type %d defined.", type);
307
308 return FALSE;
309 }
310
311 return handler->Create(this, data, type, width, height, depth);
312 }
313
314 bool wxBitmap::SaveFile(const wxString& filename, int type, const wxPalette *palette)
315 {
316 wxBitmapHandler *handler = FindHandler(type);
317
318 if ( handler == NULL ) {
319 wxLogWarning("no bitmap handler for type %d defined.", type);
320
321 return FALSE;
322 }
323
324 return handler->SaveFile(this, filename, type, palette);
325 }
326
327 void wxBitmap::SetWidth(int w)
328 {
329 if (!M_BITMAPDATA)
330 m_refData = new wxBitmapRefData;
331
332 M_BITMAPDATA->m_width = w;
333 }
334
335 void wxBitmap::SetHeight(int h)
336 {
337 if (!M_BITMAPDATA)
338 m_refData = new wxBitmapRefData;
339
340 M_BITMAPDATA->m_height = h;
341 }
342
343 void wxBitmap::SetDepth(int d)
344 {
345 if (!M_BITMAPDATA)
346 m_refData = new wxBitmapRefData;
347
348 M_BITMAPDATA->m_depth = d;
349 }
350
351 void wxBitmap::SetQuality(int q)
352 {
353 if (!M_BITMAPDATA)
354 m_refData = new wxBitmapRefData;
355
356 M_BITMAPDATA->m_quality = q;
357 }
358
359 void wxBitmap::SetOk(bool isOk)
360 {
361 if (!M_BITMAPDATA)
362 m_refData = new wxBitmapRefData;
363
364 M_BITMAPDATA->m_ok = isOk;
365 }
366
367 void wxBitmap::SetPalette(const wxPalette& palette)
368 {
369 if (!M_BITMAPDATA)
370 m_refData = new wxBitmapRefData;
371
372 M_BITMAPDATA->m_bitmapPalette = palette ;
373 }
374
375 void wxBitmap::SetMask(wxMask *mask)
376 {
377 if (!M_BITMAPDATA)
378 m_refData = new wxBitmapRefData;
379
380 M_BITMAPDATA->m_bitmapMask = mask ;
381 }
382
383 void wxBitmap::AddHandler(wxBitmapHandler *handler)
384 {
385 sm_handlers.Append(handler);
386 }
387
388 void wxBitmap::InsertHandler(wxBitmapHandler *handler)
389 {
390 sm_handlers.Insert(handler);
391 }
392
393 bool wxBitmap::RemoveHandler(const wxString& name)
394 {
395 wxBitmapHandler *handler = FindHandler(name);
396 if ( handler )
397 {
398 sm_handlers.DeleteObject(handler);
399 return TRUE;
400 }
401 else
402 return FALSE;
403 }
404
405 wxBitmapHandler *wxBitmap::FindHandler(const wxString& name)
406 {
407 wxNode *node = sm_handlers.First();
408 while ( node )
409 {
410 wxBitmapHandler *handler = (wxBitmapHandler *)node->Data();
411 if ( handler->GetName() == name )
412 return handler;
413 node = node->Next();
414 }
415 return NULL;
416 }
417
418 wxBitmapHandler *wxBitmap::FindHandler(const wxString& extension, long bitmapType)
419 {
420 wxNode *node = sm_handlers.First();
421 while ( node )
422 {
423 wxBitmapHandler *handler = (wxBitmapHandler *)node->Data();
424 if ( handler->GetExtension() == extension &&
425 (bitmapType == -1 || handler->GetType() == bitmapType) )
426 return handler;
427 node = node->Next();
428 }
429 return NULL;
430 }
431
432 wxBitmapHandler *wxBitmap::FindHandler(long bitmapType)
433 {
434 wxNode *node = sm_handlers.First();
435 while ( node )
436 {
437 wxBitmapHandler *handler = (wxBitmapHandler *)node->Data();
438 if (handler->GetType() == bitmapType)
439 return handler;
440 node = node->Next();
441 }
442 return NULL;
443 }
444
445 /*
446 * wxMask
447 */
448
449 wxMask::wxMask()
450 {
451 /* TODO
452 m_maskBitmap = 0;
453 */
454 }
455
456 // Construct a mask from a bitmap and a colour indicating
457 // the transparent area
458 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
459 {
460 /* TODO
461 m_maskBitmap = 0;
462 */
463 Create(bitmap, colour);
464 }
465
466 // Construct a mask from a bitmap and a palette index indicating
467 // the transparent area
468 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
469 {
470 /* TODO
471 m_maskBitmap = 0;
472 */
473
474 Create(bitmap, paletteIndex);
475 }
476
477 // Construct a mask from a mono bitmap (copies the bitmap).
478 wxMask::wxMask(const wxBitmap& bitmap)
479 {
480 /* TODO
481 m_maskBitmap = 0;
482 */
483
484 Create(bitmap);
485 }
486
487 wxMask::~wxMask()
488 {
489 // TODO: delete mask bitmap
490 }
491
492 // Create a mask from a mono bitmap (copies the bitmap).
493 bool wxMask::Create(const wxBitmap& bitmap)
494 {
495 // TODO
496 return FALSE;
497 }
498
499 // Create a mask from a bitmap and a palette index indicating
500 // the transparent area
501 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
502 {
503 // TODO
504 return FALSE;
505 }
506
507 // Create a mask from a bitmap and a colour indicating
508 // the transparent area
509 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
510 {
511 // TODO
512 return FALSE;
513 }
514
515 /*
516 * wxBitmapHandler
517 */
518
519 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject)
520
521 bool wxBitmapHandler::Create(wxBitmap *bitmap, void *data, long type, int width, int height, int depth)
522 {
523 return FALSE;
524 }
525
526 bool wxBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long type,
527 int desiredWidth, int desiredHeight)
528 {
529 return FALSE;
530 }
531
532 bool wxBitmapHandler::SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette)
533 {
534 return FALSE;
535 }
536
537 /*
538 * Standard handlers
539 */
540
541 class WXDLLEXPORT wxPICTResourceHandler: public wxBitmapHandler
542 {
543 DECLARE_DYNAMIC_CLASS(wxPICTResourceHandler)
544 public:
545 inline wxPICTResourceHandler()
546 {
547 m_name = "Macintosh Pict resource";
548 m_extension = "";
549 m_type = wxBITMAP_TYPE_PICT_RESOURCE;
550 };
551
552 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
553 int desiredWidth, int desiredHeight);
554 };
555 IMPLEMENT_DYNAMIC_CLASS(wxPICTResourceHandler, wxBitmapHandler)
556
557 bool wxPICTResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
558 int desiredWidth, int desiredHeight)
559 {
560 Str255 theName ;
561
562 strcpy( (char*) theName , name ) ;
563 c2pstr( (char*) theName ) ;
564
565 PicHandle thePict = (PicHandle ) GetNamedResource( 'PICT' , theName ) ;
566 if ( thePict )
567 {
568 PictInfo theInfo ;
569
570 GetPictInfo( thePict , &theInfo , 0 , 0 , systemMethod , 0 ) ;
571 DetachResource( (Handle) thePict ) ;
572 M_BITMAPHANDLERDATA->m_bitmapType = kMacBitmapTypePict ;
573 M_BITMAPHANDLERDATA->m_hPict = thePict ;
574 M_BITMAPHANDLERDATA->m_width = theInfo.sourceRect.right - theInfo.sourceRect.left ;
575 M_BITMAPHANDLERDATA->m_height = theInfo.sourceRect.bottom - theInfo.sourceRect.top ;
576
577 M_BITMAPHANDLERDATA->m_depth = theInfo.depth ;
578 M_BITMAPHANDLERDATA->m_ok = true ;
579 M_BITMAPHANDLERDATA->m_numColors = theInfo.uniqueColors ;
580 // M_BITMAPHANDLERDATA->m_bitmapPalette;
581 // M_BITMAPHANDLERDATA->m_quality;
582 return TRUE ;
583 }
584 return FALSE ;
585 }
586
587 /* TODO: bitmap handlers, a bit like this:
588 class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler
589 {
590 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler)
591 public:
592 inline wxBMPResourceHandler()
593 {
594 m_name = "Windows bitmap resource";
595 m_extension = "";
596 m_type = wxBITMAP_TYPE_BMP_RESOURCE;
597 };
598
599 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
600 int desiredWidth, int desiredHeight);
601 };
602 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler)
603 */
604
605 class WXDLLEXPORT wxXPMFileHandler: public wxBitmapHandler
606 {
607 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler)
608 public:
609 inline wxXPMFileHandler(void)
610 {
611 m_name = "XPM bitmap file";
612 m_extension = "xpm";
613 m_type = wxBITMAP_TYPE_XPM;
614 };
615
616 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
617 int desiredWidth = -1, int desiredHeight = -1);
618 virtual bool SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette = NULL);
619 };
620 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler, wxBitmapHandler)
621
622 bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
623 int desiredWidth, int desiredHeight)
624 {
625 #if USE_XPM_IN_MSW
626 XImage *ximage;
627 XpmAttributes xpmAttr;
628 HDC dc;
629
630 M_BITMAPHANDLERDATA->m_ok = FALSE;
631 dc = CreateCompatibleDC(NULL);
632 if (dc)
633 {
634 xpmAttr.valuemask = XpmReturnPixels;
635 int errorStatus = XpmReadFileToImage(&dc, WXSTRINGCAST name, &ximage, (XImage **) NULL, &xpmAttr);
636 DeleteDC(dc);
637 if (errorStatus == XpmSuccess)
638 {
639 M_BITMAPHANDLERDATA->m_hBitmap = (WXHBITMAP) ximage->bitmap;
640
641 BITMAP bm;
642 GetObject((HBITMAP)M_BITMAPHANDLERDATA->m_hBitmap, sizeof(bm), (LPSTR) & bm);
643
644 M_BITMAPHANDLERDATA->m_width = (bm.bmWidth);
645 M_BITMAPHANDLERDATA->m_height = (bm.bmHeight);
646 M_BITMAPHANDLERDATA->m_depth = (bm.bmPlanes * bm.bmBitsPixel);
647 M_BITMAPHANDLERDATA->m_numColors = xpmAttr.npixels;
648 XpmFreeAttributes(&xpmAttr);
649 XImageFree(ximage);
650
651 M_BITMAPHANDLERDATA->m_ok = TRUE;
652 return TRUE;
653 }
654 else
655 {
656 M_BITMAPHANDLERDATA->m_ok = FALSE;
657 return FALSE;
658 }
659 }
660 #endif
661
662 return FALSE;
663 }
664
665 bool wxXPMFileHandler::SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette)
666 {
667 #if USE_XPM_IN_MSW
668 HDC dc = NULL;
669
670 Visual *visual = NULL;
671 XImage ximage;
672
673 dc = CreateCompatibleDC(NULL);
674 if (dc)
675 {
676 if (SelectObject(dc, (HBITMAP) M_BITMAPHANDLERDATA->m_hBitmap))
677 { /* for following SetPixel */
678 /* fill the XImage struct 'by hand' */
679 ximage.width = M_BITMAPHANDLERDATA->m_width;
680 ximage.height = M_BITMAPHANDLERDATA->m_height;
681 ximage.depth = M_BITMAPHANDLERDATA->m_depth;
682 ximage.bitmap = (void *)M_BITMAPHANDLERDATA->m_hBitmap;
683 int errorStatus = XpmWriteFileFromImage(&dc, WXSTRINGCAST name,
684 &ximage, (XImage *) NULL, (XpmAttributes *) NULL);
685
686 if (dc)
687 DeleteDC(dc);
688
689 if (errorStatus == XpmSuccess)
690 return TRUE; /* no error */
691 else
692 return FALSE;
693 } else return FALSE;
694 } else return FALSE;
695 #else
696 return FALSE;
697 #endif
698 }
699
700
701 class WXDLLEXPORT wxXPMDataHandler: public wxBitmapHandler
702 {
703 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler)
704 public:
705 inline wxXPMDataHandler(void)
706 {
707 m_name = "XPM bitmap data";
708 m_extension = "xpm";
709 m_type = wxBITMAP_TYPE_XPM_DATA;
710 };
711
712 virtual bool Create(wxBitmap *bitmap, void *data, long flags, int width, int height, int depth = 1);
713 };
714 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler)
715
716 bool wxXPMDataHandler::Create(wxBitmap *bitmap, void *data, long flags, int width, int height, int depth)
717 {
718 XImage * ximage;
719 int ErrorStatus;
720 XpmAttributes xpmAttr;
721
722 xpmAttr.valuemask = XpmReturnInfos; // get infos back
723 ErrorStatus = XpmCreateImageFromData( GetMainDevice() , (char **)data,
724 &ximage, (XImage **) NULL, &xpmAttr);
725
726 if (ErrorStatus == XpmSuccess)
727 {
728 M_BITMAPHANDLERDATA->m_ok = FALSE;
729 M_BITMAPHANDLERDATA->m_numColors = 0;
730 M_BITMAPHANDLERDATA->m_hBitmap = ximage->gworldptr ;
731
732 M_BITMAPHANDLERDATA->m_width = ximage->width;
733 M_BITMAPHANDLERDATA->m_height = ximage->height;
734 M_BITMAPHANDLERDATA->m_depth = ximage->depth;
735 M_BITMAPHANDLERDATA->m_numColors = xpmAttr.npixels;
736 XpmFreeAttributes(&xpmAttr);
737 M_BITMAPHANDLERDATA->m_ok = TRUE;
738 ximage->gworldptr = NULL ;
739 XImageFree(ximage); // releases the malloc, but does not detroy
740 // the bitmap
741 M_BITMAPHANDLERDATA->m_bitmapType = kMacBitmapTypeGrafWorld ;
742
743 return TRUE;
744 }
745 else
746 {
747 M_BITMAPHANDLERDATA->m_ok = FALSE;
748 return FALSE;
749 }
750 return FALSE;
751 }
752
753 class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler
754 {
755 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler)
756 public:
757 inline wxBMPResourceHandler()
758 {
759 m_name = "Windows bitmap resource";
760 m_extension = "";
761 m_type = wxBITMAP_TYPE_BMP_RESOURCE;
762 };
763
764 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
765 int desiredWidth, int desiredHeight);
766 };
767
768 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler)
769
770 bool wxBMPResourceHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
771 int desiredWidth, int desiredHeight)
772 {
773 // TODO: load colourmap.
774 /*
775 M_BITMAPHANDLERDATA->m_hBitmap = (WXHBITMAP) ::LoadBitmap(wxGetInstance(), name);
776 if (M_BITMAPHANDLERDATA->m_hBitmap)
777 {
778 M_BITMAPHANDLERDATA->m_ok = TRUE;
779 BITMAP bm;
780 GetObject((HBITMAP) M_BITMAPHANDLERDATA->m_hBitmap, sizeof(BITMAP), (LPSTR) &bm);
781 M_BITMAPHANDLERDATA->m_width = bm.bmWidth;
782 M_BITMAPHANDLERDATA->m_height = bm.bmHeight;
783 M_BITMAPHANDLERDATA->m_depth = bm.bmBitsPixel;
784 return TRUE;
785 }
786 */
787 // it's probably not found
788 wxLogError("Can't load bitmap '%s' from resources! Check .rc file.", name.c_str());
789
790 return FALSE;
791 }
792
793 class WXDLLEXPORT wxBMPFileHandler: public wxBitmapHandler
794 {
795 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler)
796 public:
797 inline wxBMPFileHandler(void)
798 {
799 m_name = "Windows bitmap file";
800 m_extension = "bmp";
801 m_type = wxBITMAP_TYPE_BMP;
802 };
803
804 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
805 int desiredWidth, int desiredHeight);
806 virtual bool SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette = NULL);
807 };
808
809 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler, wxBitmapHandler)
810
811 bool wxBMPFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
812 int desiredWidth, int desiredHeight)
813 {
814 #if USE_IMAGE_LOADING_IN_MSW
815 wxPalette *palette = NULL;
816 bool success = FALSE;
817 /*
818 if (type & wxBITMAP_DISCARD_COLOURMAP)
819 success = wxLoadIntoBitmap(WXSTRINGCAST name, bitmap);
820 else
821 */
822 success = (wxLoadIntoBitmap(WXSTRINGCAST name, bitmap, &palette) != 0);
823 if (!success && palette)
824 {
825 delete palette;
826 palette = NULL;
827 }
828 if (palette)
829 M_BITMAPHANDLERDATA->m_bitmapPalette = *palette;
830 return success;
831 #else
832 return FALSE;
833 #endif
834 }
835
836 bool wxBMPFileHandler::SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *pal)
837 {
838 #if USE_IMAGE_LOADING_IN_MSW
839 wxPalette *actualPalette = (wxPalette *)pal;
840 if (!actualPalette && (!M_BITMAPHANDLERDATA->m_bitmapPalette.IsNull()))
841 actualPalette = & (M_BITMAPHANDLERDATA->m_bitmapPalette);
842 return (wxSaveBitmap(WXSTRINGCAST name, bitmap, actualPalette) != 0);
843 #else
844 return FALSE;
845 #endif
846 }
847
848
849
850 void wxBitmap::CleanUpHandlers()
851 {
852 wxNode *node = sm_handlers.First();
853 while ( node )
854 {
855 wxBitmapHandler *handler = (wxBitmapHandler *)node->Data();
856 wxNode *next = node->Next();
857 delete handler;
858 delete node;
859 node = next;
860 }
861 }
862
863 void wxBitmap::InitStandardHandlers()
864 {
865 AddHandler( new wxPICTResourceHandler ) ;
866 AddHandler( new wxICONResourceHandler ) ;
867 AddHandler(new wxXPMFileHandler);
868 AddHandler(new wxXPMDataHandler);
869 AddHandler(new wxBMPResourceHandler);
870 AddHandler(new wxBMPFileHandler);
871 }