]> git.saurik.com Git - wxWidgets.git/blob - src/msw/bitmap.cpp
307c67bada721baf44f2500a9b284dde080044a5
[wxWidgets.git] / src / msw / bitmap.cpp
1 ////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/bitmap.cpp
3 // Purpose: wxBitmap
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/bitmap.h"
28
29 #ifndef WX_PRECOMP
30 #include <stdio.h>
31
32 #include "wx/list.h"
33 #include "wx/utils.h"
34 #include "wx/app.h"
35 #include "wx/palette.h"
36 #include "wx/dcmemory.h"
37 #include "wx/icon.h"
38 #include "wx/log.h"
39 #include "wx/image.h"
40 #endif
41
42 #include "wx/msw/private.h"
43 #include "wx/msw/dc.h"
44
45 #if wxUSE_WXDIB
46 #include "wx/msw/dib.h"
47 #endif
48
49 #ifdef wxHAS_RAW_BITMAP
50 #include "wx/rawbmp.h"
51 #endif
52
53 // missing from mingw32 header
54 #ifndef CLR_INVALID
55 #define CLR_INVALID ((COLORREF)-1)
56 #endif // no CLR_INVALID
57
58 // ----------------------------------------------------------------------------
59 // Bitmap data
60 // ----------------------------------------------------------------------------
61
62 class WXDLLEXPORT wxBitmapRefData : public wxGDIImageRefData
63 {
64 public:
65 wxBitmapRefData();
66 wxBitmapRefData(const wxBitmapRefData& data);
67 virtual ~wxBitmapRefData() { Free(); }
68
69 virtual void Free();
70
71 // set the mask object to use as the mask, we take ownership of it
72 void SetMask(wxMask *mask)
73 {
74 delete m_bitmapMask;
75 m_bitmapMask = mask;
76 }
77
78 // set the HBITMAP to use as the mask
79 void SetMask(HBITMAP hbmpMask)
80 {
81 SetMask(new wxMask((WXHBITMAP)hbmpMask));
82 }
83
84 // return the mask
85 wxMask *GetMask() const { return m_bitmapMask; }
86
87 public:
88 #if wxUSE_PALETTE
89 wxPalette m_bitmapPalette;
90 #endif // wxUSE_PALETTE
91
92 // MSW-specific
93 // ------------
94
95 #ifdef __WXDEBUG__
96 // this field is solely for error checking: we detect selecting a bitmap
97 // into more than one DC at once or deleting a bitmap still selected into a
98 // DC (both are serious programming errors under Windows)
99 wxDC *m_selectedInto;
100 #endif // __WXDEBUG__
101
102 #if wxUSE_WXDIB
103 // when GetRawData() is called for a DDB we need to convert it to a DIB
104 // first to be able to provide direct access to it and we cache that DIB
105 // here and convert it back to DDB when UngetRawData() is called
106 wxDIB *m_dib;
107 #endif
108
109 // true if we have alpha transparency info and can be drawn using
110 // AlphaBlend()
111 bool m_hasAlpha;
112
113 // true if our HBITMAP is a DIB section, false if it is a DDB
114 bool m_isDIB;
115
116 private:
117 // optional mask for transparent drawing
118 wxMask *m_bitmapMask;
119
120
121 // not implemented
122 wxBitmapRefData& operator=(const wxBitmapRefData&);
123 };
124
125 // ----------------------------------------------------------------------------
126 // macros
127 // ----------------------------------------------------------------------------
128
129 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
130 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
131
132 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject)
133
134 // ============================================================================
135 // implementation
136 // ============================================================================
137
138 // ----------------------------------------------------------------------------
139 // helper functions
140 // ----------------------------------------------------------------------------
141
142 // decide whether we should create a DIB or a DDB for the given parameters
143 //
144 // NB: we always use DIBs under Windows CE as this is much simpler (even if
145 // also less efficient...) and we obviously can't use them if there is no
146 // DIB support compiled in at all
147 #ifdef __WXWINCE__
148 static inline bool wxShouldCreateDIB(int, int, int, WXHDC) { return true; }
149
150 #define ALWAYS_USE_DIB
151 #elif !wxUSE_WXDIB
152 // no sense in defining wxShouldCreateDIB() as we can't compile code
153 // executed if it is true, so we have to use #if's anyhow
154 #define NEVER_USE_DIB
155 #else // wxUSE_WXDIB && !__WXWINCE__
156 static inline bool wxShouldCreateDIB(int w, int h, int d, WXHDC hdc)
157 {
158 // here is the logic:
159 //
160 // (a) if hdc is specified, the caller explicitly wants DDB
161 // (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp
162 // or less DIBs anyhow)
163 // (c) finally, create DIBs under Win9x even if the depth hasn't been
164 // explicitly specified but the current display depth is 24 or
165 // more and the image is "big", i.e. > 16Mb which is the
166 // theoretical limit for DDBs under Win9x
167 //
168 // consequences (all of which seem to make sense):
169 //
170 // (i) by default, DDBs are created (depth == -1 usually)
171 // (ii) DIBs can be created by explicitly specifying the depth
172 // (iii) using a DC always forces creating a DDB
173 return !hdc &&
174 (d >= 24 ||
175 (d == -1 &&
176 wxDIB::GetLineSize(w, wxDisplayDepth())*h > 16*1024*1024));
177 }
178
179 #define SOMETIMES_USE_DIB
180 #endif // different DIB usage scenarious
181
182 // ----------------------------------------------------------------------------
183 // wxBitmapRefData
184 // ----------------------------------------------------------------------------
185
186 wxBitmapRefData::wxBitmapRefData()
187 {
188 #ifdef __WXDEBUG__
189 m_selectedInto = NULL;
190 #endif
191 m_bitmapMask = NULL;
192
193 m_hBitmap = (WXHBITMAP) NULL;
194 #if wxUSE_WXDIB
195 m_dib = NULL;
196 #endif
197
198 m_isDIB =
199 m_hasAlpha = false;
200 }
201
202 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData& data)
203 : wxGDIImageRefData(data)
204 {
205 #ifdef __WXDEBUG__
206 m_selectedInto = NULL;
207 #endif
208
209 // (deep) copy the mask if present
210 m_bitmapMask = NULL;
211 if (data.m_bitmapMask)
212 m_bitmapMask = new wxMask(*data.m_bitmapMask);
213
214 // FIXME: we don't copy m_hBitmap currently but we should, see wxBitmap::
215 // CloneGDIRefData()
216
217 wxASSERT_MSG( !data.m_isDIB,
218 _T("can't copy bitmap locked for raw access!") );
219 m_isDIB = false;
220
221 m_hasAlpha = data.m_hasAlpha;
222 }
223
224 void wxBitmapRefData::Free()
225 {
226 wxASSERT_MSG( !m_selectedInto,
227 wxT("deleting bitmap still selected into wxMemoryDC") );
228
229 #if wxUSE_WXDIB
230 wxASSERT_MSG( !m_dib, _T("forgot to call wxBitmap::UngetRawData()!") );
231 #endif
232
233 if ( m_hBitmap)
234 {
235 if ( !::DeleteObject((HBITMAP)m_hBitmap) )
236 {
237 wxLogLastError(wxT("DeleteObject(hbitmap)"));
238 }
239 }
240
241 delete m_bitmapMask;
242 m_bitmapMask = NULL;
243 }
244
245 // ----------------------------------------------------------------------------
246 // wxBitmap creation
247 // ----------------------------------------------------------------------------
248
249 wxGDIImageRefData *wxBitmap::CreateData() const
250 {
251 return new wxBitmapRefData;
252 }
253
254 wxGDIRefData *wxBitmap::CloneGDIRefData(const wxGDIRefData *dataOrig) const
255 {
256 const wxBitmapRefData *
257 data = wx_static_cast(const wxBitmapRefData *, dataOrig);
258 if ( !data )
259 return NULL;
260
261 // FIXME: this method is backwards, it should just create a new
262 // wxBitmapRefData using its copy ctor but instead it modifies this
263 // bitmap itself and then returns its m_refData -- which works, of
264 // course (except in !wxUSE_WXDIB), but is completely illogical
265 wxBitmap *self = wx_const_cast(wxBitmap *, this);
266
267 wxBitmapRefData *selfdata;
268 #if wxUSE_WXDIB
269 // copy the other bitmap
270 if ( data->m_hBitmap )
271 {
272 wxDIB dib((HBITMAP)(data->m_hBitmap));
273 self->CopyFromDIB(dib);
274
275 selfdata = wx_static_cast(wxBitmapRefData *, m_refData);
276 selfdata->m_hasAlpha = data->m_hasAlpha;
277 }
278 else
279 #endif // wxUSE_WXDIB
280 {
281 // copy the bitmap data
282 selfdata = new wxBitmapRefData(*data);
283 self->m_refData = selfdata;
284 }
285
286 // copy also the mask
287 wxMask * const maskSrc = data->GetMask();
288 if ( maskSrc )
289 {
290 selfdata->SetMask(new wxMask(*maskSrc));
291 }
292
293 return selfdata;
294 }
295
296 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage& icon,
297 wxBitmapTransparency transp)
298 {
299 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
300 // it may be either HICON or HCURSOR
301 HICON hicon = (HICON)icon.GetHandle();
302
303 ICONINFO iconInfo;
304 if ( !::GetIconInfo(hicon, &iconInfo) )
305 {
306 wxLogLastError(wxT("GetIconInfo"));
307
308 return false;
309 }
310
311 wxBitmapRefData *refData = new wxBitmapRefData;
312 m_refData = refData;
313
314 int w = icon.GetWidth(),
315 h = icon.GetHeight();
316
317 refData->m_width = w;
318 refData->m_height = h;
319 refData->m_depth = wxDisplayDepth();
320
321 refData->m_hBitmap = (WXHBITMAP)iconInfo.hbmColor;
322
323 switch ( transp )
324 {
325 default:
326 wxFAIL_MSG( _T("unknown wxBitmapTransparency value") );
327
328 case wxBitmapTransparency_None:
329 // nothing to do, refData->m_hasAlpha is false by default
330 break;
331
332 case wxBitmapTransparency_Auto:
333 #if wxUSE_WXDIB
334 // If the icon is 32 bits per pixel then it may have alpha channel
335 // data, although there are some icons that are 32 bpp but have no
336 // alpha... So convert to a DIB and manually check the 4th byte for
337 // each pixel.
338 {
339 BITMAP bm;
340 if ( ::GetObject(iconInfo.hbmColor, sizeof(bm), &bm) &&
341 (bm.bmBitsPixel == 32) )
342 {
343 wxDIB dib(iconInfo.hbmColor);
344 if (dib.IsOk())
345 {
346 const unsigned char* pixels = dib.GetData();
347 for (int idx = 0; idx < w*h*4; idx+=4)
348 {
349 if (pixels[idx+3] != 0)
350 {
351 // If there is an alpha byte that is non-zero
352 // then set the alpha flag and stop checking
353 refData->m_hasAlpha = true;
354 break;
355 }
356 }
357 }
358 }
359 }
360 break;
361 #endif // wxUSE_WXDIB
362
363 case wxBitmapTransparency_Always:
364 refData->m_hasAlpha = true;
365 break;
366 }
367
368 if ( !refData->m_hasAlpha )
369 {
370 // the mask returned by GetIconInfo() is inverted compared to the usual
371 // wxWin convention
372 refData->SetMask(wxInvertMask(iconInfo.hbmMask, w, h));
373 }
374
375 // delete the old one now as we don't need it any more
376 ::DeleteObject(iconInfo.hbmMask);
377
378 return true;
379 #else // __WXMICROWIN__ || __WXWINCE__
380 wxUnusedVar(icon);
381 wxUnusedVar(transp);
382
383 return false;
384 #endif // !__WXWINCE__/__WXWINCE__
385 }
386
387 bool wxBitmap::CopyFromCursor(const wxCursor& cursor, wxBitmapTransparency transp)
388 {
389 UnRef();
390
391 if ( !cursor.Ok() )
392 return false;
393
394 return CopyFromIconOrCursor(cursor, transp);
395 }
396
397 bool wxBitmap::CopyFromIcon(const wxIcon& icon, wxBitmapTransparency transp)
398 {
399 UnRef();
400
401 if ( !icon.Ok() )
402 return false;
403
404 return CopyFromIconOrCursor(icon, transp);
405 }
406
407 #ifndef NEVER_USE_DIB
408
409 bool wxBitmap::CopyFromDIB(const wxDIB& dib)
410 {
411 wxCHECK_MSG( dib.IsOk(), false, _T("invalid DIB in CopyFromDIB") );
412
413 #ifdef SOMETIMES_USE_DIB
414 HBITMAP hbitmap = dib.CreateDDB();
415 if ( !hbitmap )
416 return false;
417 #else // ALWAYS_USE_DIB
418 HBITMAP hbitmap = ((wxDIB &)dib).Detach(); // const_cast
419 #endif // SOMETIMES_USE_DIB/ALWAYS_USE_DIB
420
421 UnRef();
422
423 wxBitmapRefData *refData = new wxBitmapRefData;
424 m_refData = refData;
425
426 refData->m_width = dib.GetWidth();
427 refData->m_height = dib.GetHeight();
428 refData->m_depth = dib.GetDepth();
429
430 refData->m_hBitmap = (WXHBITMAP)hbitmap;
431
432 #if wxUSE_PALETTE
433 wxPalette *palette = dib.CreatePalette();
434 if ( palette )
435 {
436 refData->m_bitmapPalette = *palette;
437 }
438
439 delete palette;
440 #endif // wxUSE_PALETTE
441
442 return true;
443 }
444
445 #endif // NEVER_USE_DIB
446
447 wxBitmap::~wxBitmap()
448 {
449 }
450
451 wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
452 {
453 #ifndef __WXMICROWIN__
454 wxBitmapRefData *refData = new wxBitmapRefData;
455 m_refData = refData;
456
457 refData->m_width = width;
458 refData->m_height = height;
459 refData->m_depth = depth;
460
461 char *data;
462 if ( depth == 1 )
463 {
464 // we assume that it is in XBM format which is not quite the same as
465 // the format CreateBitmap() wants because the order of bytes in the
466 // line is reversed!
467 const size_t bytesPerLine = (width + 7) / 8;
468 const size_t padding = bytesPerLine % 2;
469 const size_t len = height * ( padding + bytesPerLine );
470 data = (char *)malloc(len);
471 const char *src = bits;
472 char *dst = data;
473
474 for ( int rows = 0; rows < height; rows++ )
475 {
476 for ( size_t cols = 0; cols < bytesPerLine; cols++ )
477 {
478 unsigned char val = *src++;
479 unsigned char reversed = 0;
480
481 for ( int bits = 0; bits < 8; bits++)
482 {
483 reversed <<= 1;
484 reversed |= (unsigned char)(val & 0x01);
485 val >>= 1;
486 }
487 *dst++ = ~reversed;
488 }
489
490 if ( padding )
491 *dst++ = 0;
492 }
493 }
494 else
495 {
496 // bits should already be in Windows standard format
497 data = (char *)bits; // const_cast is harmless
498 }
499
500 HBITMAP hbmp = ::CreateBitmap(width, height, 1, depth, data);
501 if ( !hbmp )
502 {
503 wxLogLastError(wxT("CreateBitmap"));
504 }
505
506 if ( data != bits )
507 {
508 free(data);
509 }
510
511 SetHBITMAP((WXHBITMAP)hbmp);
512 #endif
513 }
514
515 wxBitmap::wxBitmap(int w, int h, int d)
516 {
517 (void)Create(w, h, d);
518 }
519
520 wxBitmap::wxBitmap(int w, int h, const wxDC& dc)
521 {
522 (void)Create(w, h, dc);
523 }
524
525 wxBitmap::wxBitmap(const void* data, wxBitmapType type, int width, int height, int depth)
526 {
527 (void)Create(data, type, width, height, depth);
528 }
529
530 wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
531 {
532 LoadFile(filename, type);
533 }
534
535 bool wxBitmap::Create(int width, int height, int depth)
536 {
537 return DoCreate(width, height, depth, 0);
538 }
539
540 bool wxBitmap::Create(int width, int height, const wxDC& dc)
541 {
542 wxCHECK_MSG( dc.IsOk(), false, _T("invalid HDC in wxBitmap::Create()") );
543
544 const wxMSWDCImpl *impl = wxDynamicCast( dc.GetImpl(), wxMSWDCImpl );
545
546 if (impl)
547 return DoCreate(width, height, -1, impl->GetHDC());
548 else
549 return false;
550 }
551
552 bool wxBitmap::DoCreate(int w, int h, int d, WXHDC hdc)
553 {
554 UnRef();
555
556 m_refData = new wxBitmapRefData;
557
558 GetBitmapData()->m_width = w;
559 GetBitmapData()->m_height = h;
560
561 HBITMAP hbmp wxDUMMY_INITIALIZE(0);
562
563 #ifndef NEVER_USE_DIB
564 if ( wxShouldCreateDIB(w, h, d, hdc) )
565 {
566 if ( d == -1 )
567 {
568 // create DIBs without alpha channel by default
569 d = 24;
570 }
571
572 wxDIB dib(w, h, d);
573 if ( !dib.IsOk() )
574 return false;
575
576 // don't delete the DIB section in dib object dtor
577 hbmp = dib.Detach();
578
579 GetBitmapData()->m_isDIB = true;
580 GetBitmapData()->m_depth = d;
581 }
582 else // create a DDB
583 #endif // NEVER_USE_DIB
584 {
585 #ifndef ALWAYS_USE_DIB
586 #ifndef __WXMICROWIN__
587 if ( d > 0 )
588 {
589 hbmp = ::CreateBitmap(w, h, 1, d, NULL);
590 if ( !hbmp )
591 {
592 wxLogLastError(wxT("CreateBitmap"));
593 }
594
595 GetBitmapData()->m_depth = d;
596 }
597 else // d == 0, create bitmap compatible with the screen
598 #endif // !__WXMICROWIN__
599 {
600 ScreenHDC dc;
601 hbmp = ::CreateCompatibleBitmap(dc, w, h);
602 if ( !hbmp )
603 {
604 wxLogLastError(wxT("CreateCompatibleBitmap"));
605 }
606
607 GetBitmapData()->m_depth = wxDisplayDepth();
608 }
609 #endif // !ALWAYS_USE_DIB
610 }
611
612 SetHBITMAP((WXHBITMAP)hbmp);
613
614 return Ok();
615 }
616
617 #if wxUSE_IMAGE
618
619 // ----------------------------------------------------------------------------
620 // wxImage to/from conversions for Microwin
621 // ----------------------------------------------------------------------------
622
623 // Microwin versions are so different from normal ones that it really doesn't
624 // make sense to use #ifdefs inside the function bodies
625 #ifdef __WXMICROWIN__
626
627 bool wxBitmap::CreateFromImage(const wxImage& image, int depth, const wxDC& dc)
628 {
629 // Set this to 1 to experiment with mask code,
630 // which currently doesn't work
631 #define USE_MASKS 0
632
633 m_refData = new wxBitmapRefData();
634
635 // Initial attempt at a simple-minded implementation.
636 // The bitmap will always be created at the screen depth,
637 // so the 'depth' argument is ignored.
638
639 HDC hScreenDC = ::GetDC(NULL);
640 int screenDepth = ::GetDeviceCaps(hScreenDC, BITSPIXEL);
641
642 HBITMAP hBitmap = ::CreateCompatibleBitmap(hScreenDC, image.GetWidth(), image.GetHeight());
643 HBITMAP hMaskBitmap = NULL;
644 HBITMAP hOldMaskBitmap = NULL;
645 HDC hMaskDC = NULL;
646 unsigned char maskR = 0;
647 unsigned char maskG = 0;
648 unsigned char maskB = 0;
649
650 // printf("Created bitmap %d\n", (int) hBitmap);
651 if (hBitmap == NULL)
652 {
653 ::ReleaseDC(NULL, hScreenDC);
654 return false;
655 }
656 HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
657
658 HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
659 ::ReleaseDC(NULL, hScreenDC);
660
661 // created an mono-bitmap for the possible mask
662 bool hasMask = image.HasMask();
663
664 if ( hasMask )
665 {
666 #if USE_MASKS
667 // FIXME: we should be able to pass bpp = 1, but
668 // GdBlit can't handle a different depth
669 #if 0
670 hMaskBitmap = ::CreateBitmap( (WORD)image.GetWidth(), (WORD)image.GetHeight(), 1, 1, NULL );
671 #else
672 hMaskBitmap = ::CreateCompatibleBitmap( hMemDC, (WORD)image.GetWidth(), (WORD)image.GetHeight());
673 #endif
674 maskR = image.GetMaskRed();
675 maskG = image.GetMaskGreen();
676 maskB = image.GetMaskBlue();
677
678 if (!hMaskBitmap)
679 {
680 hasMask = false;
681 }
682 else
683 {
684 hScreenDC = ::GetDC(NULL);
685 hMaskDC = ::CreateCompatibleDC(hScreenDC);
686 ::ReleaseDC(NULL, hScreenDC);
687
688 hOldMaskBitmap = ::SelectObject( hMaskDC, hMaskBitmap);
689 }
690 #else
691 hasMask = false;
692 #endif
693 }
694
695 int i, j;
696 for (i = 0; i < image.GetWidth(); i++)
697 {
698 for (j = 0; j < image.GetHeight(); j++)
699 {
700 unsigned char red = image.GetRed(i, j);
701 unsigned char green = image.GetGreen(i, j);
702 unsigned char blue = image.GetBlue(i, j);
703
704 ::SetPixel(hMemDC, i, j, PALETTERGB(red, green, blue));
705
706 if (hasMask)
707 {
708 // scan the bitmap for the transparent colour and set the corresponding
709 // pixels in the mask to BLACK and the rest to WHITE
710 if (maskR == red && maskG == green && maskB == blue)
711 ::SetPixel(hMaskDC, i, j, PALETTERGB(0, 0, 0));
712 else
713 ::SetPixel(hMaskDC, i, j, PALETTERGB(255, 255, 255));
714 }
715 }
716 }
717
718 ::SelectObject(hMemDC, hOldBitmap);
719 ::DeleteDC(hMemDC);
720 if (hasMask)
721 {
722 ::SelectObject(hMaskDC, hOldMaskBitmap);
723 ::DeleteDC(hMaskDC);
724
725 ((wxBitmapRefData*)m_refData)->SetMask(hMaskBitmap);
726 }
727
728 SetWidth(image.GetWidth());
729 SetHeight(image.GetHeight());
730 SetDepth(screenDepth);
731 SetHBITMAP( (WXHBITMAP) hBitmap );
732
733 #if wxUSE_PALETTE
734 // Copy the palette from the source image
735 SetPalette(image.GetPalette());
736 #endif // wxUSE_PALETTE
737
738 return true;
739 }
740
741 wxImage wxBitmap::ConvertToImage() const
742 {
743 // Initial attempt at a simple-minded implementation.
744 // The bitmap will always be created at the screen depth,
745 // so the 'depth' argument is ignored.
746 // TODO: transparency (create a mask image)
747
748 if (!Ok())
749 {
750 wxFAIL_MSG( wxT("bitmap is invalid") );
751 return wxNullImage;
752 }
753
754 wxImage image;
755
756 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
757
758 // create an wxImage object
759 int width = GetWidth();
760 int height = GetHeight();
761 image.Create( width, height );
762 unsigned char *data = image.GetData();
763 if( !data )
764 {
765 wxFAIL_MSG( wxT("could not allocate data for image") );
766 return wxNullImage;
767 }
768
769 HDC hScreenDC = ::GetDC(NULL);
770
771 HDC hMemDC = ::CreateCompatibleDC(hScreenDC);
772 ::ReleaseDC(NULL, hScreenDC);
773
774 HBITMAP hBitmap = (HBITMAP) GetHBITMAP();
775
776 HBITMAP hOldBitmap = ::SelectObject(hMemDC, hBitmap);
777
778 int i, j;
779 for (i = 0; i < GetWidth(); i++)
780 {
781 for (j = 0; j < GetHeight(); j++)
782 {
783 COLORREF color = ::GetPixel(hMemDC, i, j);
784 unsigned char red = GetRValue(color);
785 unsigned char green = GetGValue(color);
786 unsigned char blue = GetBValue(color);
787
788 image.SetRGB(i, j, red, green, blue);
789 }
790 }
791
792 ::SelectObject(hMemDC, hOldBitmap);
793 ::DeleteDC(hMemDC);
794
795 #if wxUSE_PALETTE
796 // Copy the palette from the source image
797 if (GetPalette())
798 image.SetPalette(* GetPalette());
799 #endif // wxUSE_PALETTE
800
801 return image;
802 }
803
804 #endif // __WXMICROWIN__
805
806 // ----------------------------------------------------------------------------
807 // wxImage to/from conversions
808 // ----------------------------------------------------------------------------
809
810 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
811 {
812 return CreateFromImage(image, depth, 0);
813 }
814
815 bool wxBitmap::CreateFromImage(const wxImage& image, const wxDC& dc)
816 {
817 wxCHECK_MSG( dc.IsOk(), false,
818 _T("invalid HDC in wxBitmap::CreateFromImage()") );
819
820 const wxMSWDCImpl *impl = wxDynamicCast( dc.GetImpl(), wxMSWDCImpl );
821
822 if (impl)
823 return CreateFromImage(image, -1, impl->GetHDC());
824 else
825 return false;
826 }
827
828 #if wxUSE_WXDIB
829
830 bool wxBitmap::CreateFromImage(const wxImage& image, int depth, WXHDC hdc)
831 {
832 wxCHECK_MSG( image.Ok(), false, wxT("invalid image") );
833
834 UnRef();
835
836 // first convert the image to DIB
837 const int h = image.GetHeight();
838 const int w = image.GetWidth();
839
840 wxDIB dib(image);
841 if ( !dib.IsOk() )
842 return false;
843
844 const bool hasAlpha = image.HasAlpha();
845
846 if (depth == -1)
847 depth = dib.GetDepth();
848
849 // store the bitmap parameters
850 wxBitmapRefData * const refData = new wxBitmapRefData;
851 refData->m_width = w;
852 refData->m_height = h;
853 refData->m_hasAlpha = hasAlpha;
854 refData->m_depth = depth;
855
856 m_refData = refData;
857
858
859 // next either store DIB as is or create a DDB from it
860 HBITMAP hbitmap wxDUMMY_INITIALIZE(0);
861
862 // are we going to use DIB?
863 //
864 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
865 if ( hasAlpha || wxShouldCreateDIB(w, h, depth, hdc) )
866 {
867 // don't delete the DIB section in dib object dtor
868 hbitmap = dib.Detach();
869
870 refData->m_isDIB = true;
871 }
872 #ifndef ALWAYS_USE_DIB
873 else // we need to convert DIB to DDB
874 {
875 hbitmap = dib.CreateDDB((HDC)hdc);
876 }
877 #endif // !ALWAYS_USE_DIB
878
879 // validate this object
880 SetHBITMAP((WXHBITMAP)hbitmap);
881
882 // finally also set the mask if we have one
883 if ( image.HasMask() )
884 {
885 const size_t len = 2*((w+15)/16);
886 BYTE *src = image.GetData();
887 BYTE *data = new BYTE[h*len];
888 memset(data, 0, h*len);
889 BYTE r = image.GetMaskRed(),
890 g = image.GetMaskGreen(),
891 b = image.GetMaskBlue();
892 BYTE *dst = data;
893 for ( int y = 0; y < h; y++, dst += len )
894 {
895 BYTE *dstLine = dst;
896 BYTE mask = 0x80;
897 for ( int x = 0; x < w; x++, src += 3 )
898 {
899 if (src[0] != r || src[1] != g || src[2] != b)
900 *dstLine |= mask;
901
902 if ( (mask >>= 1) == 0 )
903 {
904 dstLine++;
905 mask = 0x80;
906 }
907 }
908 }
909
910 hbitmap = ::CreateBitmap(w, h, 1, 1, data);
911 if ( !hbitmap )
912 {
913 wxLogLastError(_T("CreateBitmap(mask)"));
914 }
915 else
916 {
917 SetMask(new wxMask((WXHBITMAP)hbitmap));
918 }
919
920 delete[] data;
921 }
922
923 return true;
924 }
925
926 wxImage wxBitmap::ConvertToImage() const
927 {
928 // convert DDB to DIB
929 wxDIB dib(*this);
930
931 if ( !dib.IsOk() )
932 {
933 return wxNullImage;
934 }
935
936 // and then DIB to our wxImage
937 wxImage image = dib.ConvertToImage();
938 if ( !image.Ok() )
939 {
940 return wxNullImage;
941 }
942
943 // now do the same for the mask, if we have any
944 HBITMAP hbmpMask = GetMask() ? (HBITMAP) GetMask()->GetMaskBitmap() : NULL;
945 if ( hbmpMask )
946 {
947 wxDIB dibMask(hbmpMask);
948 if ( dibMask.IsOk() )
949 {
950 // TODO: use wxRawBitmap to iterate over DIB
951
952 // we hard code the mask colour for now but we could also make an
953 // effort (and waste time) to choose a colour not present in the
954 // image already to avoid having to fudge the pixels below --
955 // whether it's worth to do it is unclear however
956 static const int MASK_RED = 1;
957 static const int MASK_GREEN = 2;
958 static const int MASK_BLUE = 3;
959 static const int MASK_BLUE_REPLACEMENT = 2;
960
961 const int h = dibMask.GetHeight();
962 const int w = dibMask.GetWidth();
963 const int bpp = dibMask.GetDepth();
964 const int maskBytesPerPixel = bpp >> 3;
965 const int maskBytesPerLine = wxDIB::GetLineSize(w, bpp);
966 unsigned char *data = image.GetData();
967
968 // remember that DIBs are stored in bottom to top order
969 unsigned char *
970 maskLineStart = dibMask.GetData() + ((h - 1) * maskBytesPerLine);
971
972 for ( int y = 0; y < h; y++, maskLineStart -= maskBytesPerLine )
973 {
974 // traverse one mask DIB line
975 unsigned char *mask = maskLineStart;
976 for ( int x = 0; x < w; x++, mask += maskBytesPerPixel )
977 {
978 // should this pixel be transparent?
979 if ( *mask )
980 {
981 // no, check that it isn't transparent by accident
982 if ( (data[0] == MASK_RED) &&
983 (data[1] == MASK_GREEN) &&
984 (data[2] == MASK_BLUE) )
985 {
986 // we have to fudge the colour a bit to prevent
987 // this pixel from appearing transparent
988 data[2] = MASK_BLUE_REPLACEMENT;
989 }
990
991 data += 3;
992 }
993 else // yes, transparent pixel
994 {
995 *data++ = MASK_RED;
996 *data++ = MASK_GREEN;
997 *data++ = MASK_BLUE;
998 }
999 }
1000 }
1001
1002 image.SetMaskColour(MASK_RED, MASK_GREEN, MASK_BLUE);
1003 }
1004 }
1005
1006 return image;
1007 }
1008
1009 #else // !wxUSE_WXDIB
1010
1011 bool
1012 wxBitmap::CreateFromImage(const wxImage& WXUNUSED(image),
1013 int WXUNUSED(depth),
1014 WXHDC WXUNUSED(hdc))
1015 {
1016 return false;
1017 }
1018
1019 wxImage wxBitmap::ConvertToImage() const
1020 {
1021 return wxImage();
1022 }
1023
1024 #endif // wxUSE_WXDIB/!wxUSE_WXDIB
1025
1026 #endif // wxUSE_IMAGE
1027
1028 // ----------------------------------------------------------------------------
1029 // loading and saving bitmaps
1030 // ----------------------------------------------------------------------------
1031
1032 bool wxBitmap::LoadFile(const wxString& filename, wxBitmapType type)
1033 {
1034 UnRef();
1035
1036 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
1037
1038 if ( handler )
1039 {
1040 m_refData = new wxBitmapRefData;
1041
1042 return handler->LoadFile(this, filename, type, -1, -1);
1043 }
1044 #if wxUSE_IMAGE && wxUSE_WXDIB
1045 else // no bitmap handler found
1046 {
1047 wxImage image;
1048 if ( image.LoadFile( filename, type ) && image.Ok() )
1049 {
1050 *this = wxBitmap(image);
1051
1052 return true;
1053 }
1054 }
1055 #endif // wxUSE_IMAGE
1056
1057 return false;
1058 }
1059
1060 bool wxBitmap::Create(const void* data, wxBitmapType type, int width, int height, int depth)
1061 {
1062 UnRef();
1063
1064 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
1065
1066 if ( !handler )
1067 {
1068 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type);
1069
1070 return false;
1071 }
1072
1073 m_refData = new wxBitmapRefData;
1074
1075 return handler->Create(this, data, type, width, height, depth);
1076 }
1077
1078 bool wxBitmap::SaveFile(const wxString& filename,
1079 wxBitmapType type,
1080 const wxPalette *palette) const
1081 {
1082 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
1083
1084 if ( handler )
1085 {
1086 return handler->SaveFile(this, filename, type, palette);
1087 }
1088 #if wxUSE_IMAGE && wxUSE_WXDIB
1089 else // no bitmap handler found
1090 {
1091 // FIXME what about palette? shouldn't we use it?
1092 wxImage image = ConvertToImage();
1093 if ( image.Ok() )
1094 {
1095 return image.SaveFile(filename, type);
1096 }
1097 }
1098 #endif // wxUSE_IMAGE
1099
1100 return false;
1101 }
1102
1103 // ----------------------------------------------------------------------------
1104 // sub bitmap extraction
1105 // ----------------------------------------------------------------------------
1106 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect ) const
1107 {
1108 MemoryHDC dcSrc;
1109 SelectInHDC selectSrc(dcSrc, GetHbitmap());
1110 return GetSubBitmapOfHDC( rect, (WXHDC)dcSrc );
1111 }
1112
1113 wxBitmap wxBitmap::GetSubBitmapOfHDC( const wxRect& rect, WXHDC hdc ) const
1114 {
1115 wxCHECK_MSG( Ok() &&
1116 (rect.x >= 0) && (rect.y >= 0) &&
1117 (rect.x+rect.width <= GetWidth()) &&
1118 (rect.y+rect.height <= GetHeight()),
1119 wxNullBitmap, wxT("Invalid bitmap or bitmap region") );
1120
1121 wxBitmap ret( rect.width, rect.height, GetDepth() );
1122 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
1123
1124 #ifndef __WXMICROWIN__
1125 // handle alpha channel, if any
1126 if (HasAlpha())
1127 ret.UseAlpha();
1128
1129 // copy bitmap data
1130 MemoryHDC dcSrc,
1131 dcDst;
1132
1133 {
1134 SelectInHDC selectDst(dcDst, GetHbitmapOf(ret));
1135
1136 if ( !selectDst )
1137 {
1138 wxLogLastError(_T("SelectObject(destBitmap)"));
1139 }
1140
1141 if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height,
1142 (HDC)hdc, rect.x, rect.y, SRCCOPY) )
1143 {
1144 wxLogLastError(_T("BitBlt"));
1145 }
1146 }
1147
1148 // copy mask if there is one
1149 if ( GetMask() )
1150 {
1151 HBITMAP hbmpMask = ::CreateBitmap(rect.width, rect.height, 1, 1, 0);
1152
1153 SelectInHDC selectSrc(dcSrc, (HBITMAP) GetMask()->GetMaskBitmap()),
1154 selectDst(dcDst, hbmpMask);
1155
1156 if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height,
1157 dcSrc, rect.x, rect.y, SRCCOPY) )
1158 {
1159 wxLogLastError(_T("BitBlt"));
1160 }
1161
1162 wxMask *mask = new wxMask((WXHBITMAP) hbmpMask);
1163 ret.SetMask(mask);
1164 }
1165 #endif // !__WXMICROWIN__
1166
1167 return ret;
1168 }
1169
1170 // ----------------------------------------------------------------------------
1171 // wxBitmap accessors
1172 // ----------------------------------------------------------------------------
1173
1174 #if wxUSE_PALETTE
1175 wxPalette* wxBitmap::GetPalette() const
1176 {
1177 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1178 : (wxPalette *) NULL;
1179 }
1180 #endif
1181
1182 wxMask *wxBitmap::GetMask() const
1183 {
1184 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask *) NULL;
1185 }
1186
1187 wxBitmap wxBitmap::GetMaskBitmap() const
1188 {
1189 wxBitmap bmp;
1190 wxMask *mask = GetMask();
1191 if ( mask )
1192 bmp.SetHBITMAP(mask->GetMaskBitmap());
1193 return bmp;
1194 }
1195
1196 #ifdef __WXDEBUG__
1197
1198 wxDC *wxBitmap::GetSelectedInto() const
1199 {
1200 return GetBitmapData() ? GetBitmapData()->m_selectedInto : (wxDC *) NULL;
1201 }
1202
1203 #endif
1204
1205 void wxBitmap::UseAlpha()
1206 {
1207 if ( GetBitmapData() )
1208 GetBitmapData()->m_hasAlpha = true;
1209 }
1210
1211 bool wxBitmap::HasAlpha() const
1212 {
1213 return GetBitmapData() && GetBitmapData()->m_hasAlpha;
1214 }
1215
1216 // ----------------------------------------------------------------------------
1217 // wxBitmap setters
1218 // ----------------------------------------------------------------------------
1219
1220 #ifdef __WXDEBUG__
1221
1222 void wxBitmap::SetSelectedInto(wxDC *dc)
1223 {
1224 if ( GetBitmapData() )
1225 GetBitmapData()->m_selectedInto = dc;
1226 }
1227
1228 #endif
1229
1230 #if wxUSE_PALETTE
1231
1232 void wxBitmap::SetPalette(const wxPalette& palette)
1233 {
1234 AllocExclusive();
1235
1236 GetBitmapData()->m_bitmapPalette = palette;
1237 }
1238
1239 #endif // wxUSE_PALETTE
1240
1241 void wxBitmap::SetMask(wxMask *mask)
1242 {
1243 AllocExclusive();
1244
1245 GetBitmapData()->SetMask(mask);
1246 }
1247
1248 // ----------------------------------------------------------------------------
1249 // raw bitmap access support
1250 // ----------------------------------------------------------------------------
1251
1252 #ifdef wxHAS_RAW_BITMAP
1253
1254 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
1255 {
1256 #if wxUSE_WXDIB
1257 if ( !Ok() )
1258 {
1259 // no bitmap, no data (raw or otherwise)
1260 return NULL;
1261 }
1262
1263 // if we're already a DIB we can access our data directly, but if not we
1264 // need to convert this DDB to a DIB section and use it for raw access and
1265 // then convert it back
1266 HBITMAP hDIB;
1267 if ( !GetBitmapData()->m_isDIB )
1268 {
1269 wxCHECK_MSG( !GetBitmapData()->m_dib, NULL,
1270 _T("GetRawData() may be called only once") );
1271
1272 wxDIB *dib = new wxDIB(*this);
1273 if ( !dib->IsOk() )
1274 {
1275 delete dib;
1276
1277 return NULL;
1278 }
1279
1280 // we'll free it in UngetRawData()
1281 GetBitmapData()->m_dib = dib;
1282
1283 hDIB = dib->GetHandle();
1284 }
1285 else // we're a DIB
1286 {
1287 hDIB = GetHbitmap();
1288 }
1289
1290 DIBSECTION ds;
1291 if ( ::GetObject(hDIB, sizeof(ds), &ds) != sizeof(DIBSECTION) )
1292 {
1293 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1294
1295 return NULL;
1296 }
1297
1298 // check that the bitmap is in correct format
1299 if ( ds.dsBm.bmBitsPixel != bpp )
1300 {
1301 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1302
1303 return NULL;
1304 }
1305
1306 // ok, store the relevant info in wxPixelDataBase
1307 const LONG h = ds.dsBm.bmHeight;
1308
1309 data.m_width = ds.dsBm.bmWidth;
1310 data.m_height = h;
1311
1312 // remember that DIBs are stored in top to bottom order!
1313 // (We can't just use ds.dsBm.bmWidthBytes here, because it isn't always a
1314 // multiple of 2, as required by the documentation. So we use the official
1315 // formula, which we already use elsewhere.)
1316 const LONG bytesPerRow =
1317 wxDIB::GetLineSize(ds.dsBm.bmWidth, ds.dsBm.bmBitsPixel);
1318 data.m_stride = -bytesPerRow;
1319
1320 char *bits = (char *)ds.dsBm.bmBits;
1321 if ( h > 1 )
1322 {
1323 bits += (h - 1)*bytesPerRow;
1324 }
1325
1326 return bits;
1327 #else
1328 return NULL;
1329 #endif
1330 }
1331
1332 void wxBitmap::UngetRawData(wxPixelDataBase& dataBase)
1333 {
1334 #if wxUSE_WXDIB
1335 if ( !Ok() )
1336 return;
1337
1338 if ( !&dataBase )
1339 {
1340 // invalid data, don't crash -- but don't assert neither as we're
1341 // called automatically from wxPixelDataBase dtor and so there is no
1342 // way to prevent this from happening
1343 return;
1344 }
1345
1346 // if we're a DDB we need to convert DIB back to DDB now to make the
1347 // changes made via raw bitmap access effective
1348 if ( !GetBitmapData()->m_isDIB )
1349 {
1350 wxDIB *dib = GetBitmapData()->m_dib;
1351 GetBitmapData()->m_dib = NULL;
1352
1353 // TODO: convert
1354
1355 delete dib;
1356 }
1357 #endif // wxUSE_WXDIB
1358 }
1359 #endif // wxHAS_RAW_BITMAP
1360
1361 // ----------------------------------------------------------------------------
1362 // wxMask
1363 // ----------------------------------------------------------------------------
1364
1365 wxMask::wxMask()
1366 {
1367 m_maskBitmap = 0;
1368 }
1369
1370 // Copy constructor
1371 wxMask::wxMask(const wxMask &mask)
1372 : wxObject()
1373 {
1374 BITMAP bmp;
1375
1376 HDC srcDC = CreateCompatibleDC(0);
1377 HDC destDC = CreateCompatibleDC(0);
1378
1379 // GetBitmapDimensionEx won't work if SetBitmapDimensionEx wasn't used
1380 // so we'll use GetObject() API here:
1381 if (::GetObject((HGDIOBJ)mask.m_maskBitmap, sizeof(bmp), &bmp) == 0)
1382 {
1383 wxFAIL_MSG(wxT("Cannot retrieve the dimensions of the wxMask to copy"));
1384 return;
1385 }
1386
1387 // create our HBITMAP
1388 int w = bmp.bmWidth, h = bmp.bmHeight;
1389 m_maskBitmap = (WXHBITMAP)CreateCompatibleBitmap(srcDC, w, h);
1390
1391 // copy the mask's HBITMAP into our HBITMAP
1392 SelectObject(srcDC, (HBITMAP) mask.m_maskBitmap);
1393 SelectObject(destDC, (HBITMAP) m_maskBitmap);
1394
1395 BitBlt(destDC, 0, 0, w, h, srcDC, 0, 0, SRCCOPY);
1396
1397 SelectObject(srcDC, 0);
1398 DeleteDC(srcDC);
1399 SelectObject(destDC, 0);
1400 DeleteDC(destDC);
1401 }
1402
1403 // Construct a mask from a bitmap and a colour indicating
1404 // the transparent area
1405 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
1406 {
1407 m_maskBitmap = 0;
1408 Create(bitmap, colour);
1409 }
1410
1411 // Construct a mask from a bitmap and a palette index indicating
1412 // the transparent area
1413 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
1414 {
1415 m_maskBitmap = 0;
1416 Create(bitmap, paletteIndex);
1417 }
1418
1419 // Construct a mask from a mono bitmap (copies the bitmap).
1420 wxMask::wxMask(const wxBitmap& bitmap)
1421 {
1422 m_maskBitmap = 0;
1423 Create(bitmap);
1424 }
1425
1426 wxMask::~wxMask()
1427 {
1428 if ( m_maskBitmap )
1429 ::DeleteObject((HBITMAP) m_maskBitmap);
1430 }
1431
1432 // Create a mask from a mono bitmap (copies the bitmap).
1433 bool wxMask::Create(const wxBitmap& bitmap)
1434 {
1435 #ifndef __WXMICROWIN__
1436 wxCHECK_MSG( bitmap.Ok() && bitmap.GetDepth() == 1, false,
1437 _T("can't create mask from invalid or not monochrome bitmap") );
1438
1439 if ( m_maskBitmap )
1440 {
1441 ::DeleteObject((HBITMAP) m_maskBitmap);
1442 m_maskBitmap = 0;
1443 }
1444
1445 m_maskBitmap = (WXHBITMAP) CreateBitmap(
1446 bitmap.GetWidth(),
1447 bitmap.GetHeight(),
1448 1, 1, 0
1449 );
1450 HDC srcDC = CreateCompatibleDC(0);
1451 SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP());
1452 HDC destDC = CreateCompatibleDC(0);
1453 SelectObject(destDC, (HBITMAP) m_maskBitmap);
1454 BitBlt(destDC, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), srcDC, 0, 0, SRCCOPY);
1455 SelectObject(srcDC, 0);
1456 DeleteDC(srcDC);
1457 SelectObject(destDC, 0);
1458 DeleteDC(destDC);
1459 return true;
1460 #else
1461 wxUnusedVar(bitmap);
1462 return false;
1463 #endif
1464 }
1465
1466 // Create a mask from a bitmap and a palette index indicating
1467 // the transparent area
1468 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
1469 {
1470 if ( m_maskBitmap )
1471 {
1472 ::DeleteObject((HBITMAP) m_maskBitmap);
1473 m_maskBitmap = 0;
1474 }
1475
1476 #if wxUSE_PALETTE
1477 if (bitmap.Ok() && bitmap.GetPalette()->Ok())
1478 {
1479 unsigned char red, green, blue;
1480 if (bitmap.GetPalette()->GetRGB(paletteIndex, &red, &green, &blue))
1481 {
1482 wxColour transparentColour(red, green, blue);
1483 return Create(bitmap, transparentColour);
1484 }
1485 }
1486 #endif // wxUSE_PALETTE
1487
1488 return false;
1489 }
1490
1491 // Create a mask from a bitmap and a colour indicating
1492 // the transparent area
1493 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1494 {
1495 #ifndef __WXMICROWIN__
1496 wxCHECK_MSG( bitmap.Ok(), false, _T("invalid bitmap in wxMask::Create") );
1497
1498 if ( m_maskBitmap )
1499 {
1500 ::DeleteObject((HBITMAP) m_maskBitmap);
1501 m_maskBitmap = 0;
1502 }
1503
1504 int width = bitmap.GetWidth(),
1505 height = bitmap.GetHeight();
1506
1507 // scan the bitmap for the transparent colour and set the corresponding
1508 // pixels in the mask to BLACK and the rest to WHITE
1509 COLORREF maskColour = wxColourToPalRGB(colour);
1510 m_maskBitmap = (WXHBITMAP)::CreateBitmap(width, height, 1, 1, 0);
1511
1512 HDC srcDC = ::CreateCompatibleDC(NULL);
1513 HDC destDC = ::CreateCompatibleDC(NULL);
1514 if ( !srcDC || !destDC )
1515 {
1516 wxLogLastError(wxT("CreateCompatibleDC"));
1517 }
1518
1519 bool ok = true;
1520
1521 // SelectObject() will fail
1522 wxASSERT_MSG( !bitmap.GetSelectedInto(),
1523 _T("bitmap can't be selected in another DC") );
1524
1525 HGDIOBJ hbmpSrcOld = ::SelectObject(srcDC, GetHbitmapOf(bitmap));
1526 if ( !hbmpSrcOld )
1527 {
1528 wxLogLastError(wxT("SelectObject"));
1529
1530 ok = false;
1531 }
1532
1533 HGDIOBJ hbmpDstOld = ::SelectObject(destDC, (HBITMAP)m_maskBitmap);
1534 if ( !hbmpDstOld )
1535 {
1536 wxLogLastError(wxT("SelectObject"));
1537
1538 ok = false;
1539 }
1540
1541 if ( ok )
1542 {
1543 // this will create a monochrome bitmap with 0 points for the pixels
1544 // which have the same value as the background colour and 1 for the
1545 // others
1546 ::SetBkColor(srcDC, maskColour);
1547 ::BitBlt(destDC, 0, 0, width, height, srcDC, 0, 0, NOTSRCCOPY);
1548 }
1549
1550 ::SelectObject(srcDC, hbmpSrcOld);
1551 ::DeleteDC(srcDC);
1552 ::SelectObject(destDC, hbmpDstOld);
1553 ::DeleteDC(destDC);
1554
1555 return ok;
1556 #else // __WXMICROWIN__
1557 wxUnusedVar(bitmap);
1558 wxUnusedVar(colour);
1559 return false;
1560 #endif // __WXMICROWIN__/!__WXMICROWIN__
1561 }
1562
1563 // ----------------------------------------------------------------------------
1564 // wxBitmapHandler
1565 // ----------------------------------------------------------------------------
1566
1567 bool wxBitmapHandler::Create(wxGDIImage *image,
1568 const void* data,
1569 wxBitmapType type,
1570 int width, int height, int depth)
1571 {
1572 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1573
1574 return bitmap && Create(bitmap, data, type, width, height, depth);
1575 }
1576
1577 bool wxBitmapHandler::Load(wxGDIImage *image,
1578 const wxString& name,
1579 wxBitmapType type,
1580 int width, int height)
1581 {
1582 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1583
1584 return bitmap && LoadFile(bitmap, name, type, width, height);
1585 }
1586
1587 bool wxBitmapHandler::Save(const wxGDIImage *image,
1588 const wxString& name,
1589 wxBitmapType type) const
1590 {
1591 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1592
1593 return bitmap && SaveFile(bitmap, name, type);
1594 }
1595
1596 bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap),
1597 const void* WXUNUSED(data),
1598 wxBitmapType WXUNUSED(type),
1599 int WXUNUSED(width),
1600 int WXUNUSED(height),
1601 int WXUNUSED(depth))
1602 {
1603 return false;
1604 }
1605
1606 bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap),
1607 const wxString& WXUNUSED(name),
1608 wxBitmapType WXUNUSED(type),
1609 int WXUNUSED(desiredWidth),
1610 int WXUNUSED(desiredHeight))
1611 {
1612 return false;
1613 }
1614
1615 bool wxBitmapHandler::SaveFile(const wxBitmap *WXUNUSED(bitmap),
1616 const wxString& WXUNUSED(name),
1617 wxBitmapType WXUNUSED(type),
1618 const wxPalette *WXUNUSED(palette)) const
1619 {
1620 return false;
1621 }
1622
1623 // ----------------------------------------------------------------------------
1624 // global helper functions implemented here
1625 // ----------------------------------------------------------------------------
1626
1627 // helper of wxBitmapToHICON/HCURSOR
1628 static
1629 HICON wxBitmapToIconOrCursor(const wxBitmap& bmp,
1630 bool iconWanted,
1631 int hotSpotX,
1632 int hotSpotY)
1633 {
1634 if ( !bmp.Ok() )
1635 {
1636 // we can't create an icon/cursor form nothing
1637 return 0;
1638 }
1639
1640 if ( bmp.HasAlpha() )
1641 {
1642 // Create an empty mask bitmap.
1643 // it doesn't seem to work if we mess with the mask at all.
1644 HBITMAP hMonoBitmap = CreateBitmap(bmp.GetWidth(),bmp.GetHeight(),1,1,NULL);
1645
1646 ICONINFO iconInfo;
1647 wxZeroMemory(iconInfo);
1648 iconInfo.fIcon = iconWanted; // do we want an icon or a cursor?
1649 if ( !iconWanted )
1650 {
1651 iconInfo.xHotspot = hotSpotX;
1652 iconInfo.yHotspot = hotSpotY;
1653 }
1654
1655 iconInfo.hbmMask = hMonoBitmap;
1656 iconInfo.hbmColor = GetHbitmapOf(bmp);
1657
1658 HICON hicon = ::CreateIconIndirect(&iconInfo);
1659
1660 ::DeleteObject(hMonoBitmap);
1661
1662 return hicon;
1663 }
1664
1665 wxMask* mask = bmp.GetMask();
1666
1667 if ( !mask )
1668 {
1669 // we must have a mask for an icon, so even if it's probably incorrect,
1670 // do create it (grey is the "standard" transparent colour)
1671 mask = new wxMask(bmp, *wxLIGHT_GREY);
1672 }
1673
1674 ICONINFO iconInfo;
1675 wxZeroMemory(iconInfo);
1676 iconInfo.fIcon = iconWanted; // do we want an icon or a cursor?
1677 if ( !iconWanted )
1678 {
1679 iconInfo.xHotspot = hotSpotX;
1680 iconInfo.yHotspot = hotSpotY;
1681 }
1682
1683 iconInfo.hbmMask = wxInvertMask((HBITMAP)mask->GetMaskBitmap());
1684 iconInfo.hbmColor = GetHbitmapOf(bmp);
1685
1686 // black out the transparent area to preserve background colour, because
1687 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1688 // the mask to the dest rect.
1689 {
1690 MemoryHDC dcSrc, dcDst;
1691 SelectInHDC selectMask(dcSrc, (HBITMAP)mask->GetMaskBitmap()),
1692 selectBitmap(dcDst, iconInfo.hbmColor);
1693
1694 if ( !::BitBlt(dcDst, 0, 0, bmp.GetWidth(), bmp.GetHeight(),
1695 dcSrc, 0, 0, SRCAND) )
1696 {
1697 wxLogLastError(_T("BitBlt"));
1698 }
1699 }
1700
1701 HICON hicon = ::CreateIconIndirect(&iconInfo);
1702
1703 if ( !bmp.GetMask() && !bmp.HasAlpha() )
1704 {
1705 // we created the mask, now delete it
1706 delete mask;
1707 }
1708
1709 // delete the inverted mask bitmap we created as well
1710 ::DeleteObject(iconInfo.hbmMask);
1711
1712 return hicon;
1713 }
1714
1715 HICON wxBitmapToHICON(const wxBitmap& bmp)
1716 {
1717 return wxBitmapToIconOrCursor(bmp, true, 0, 0);
1718 }
1719
1720 HCURSOR wxBitmapToHCURSOR(const wxBitmap& bmp, int hotSpotX, int hotSpotY)
1721 {
1722 return (HCURSOR)wxBitmapToIconOrCursor(bmp, false, hotSpotX, hotSpotY);
1723 }
1724
1725 HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h)
1726 {
1727 #ifndef __WXMICROWIN__
1728 wxCHECK_MSG( hbmpMask, 0, _T("invalid bitmap in wxInvertMask") );
1729
1730 // get width/height from the bitmap if not given
1731 if ( !w || !h )
1732 {
1733 BITMAP bm;
1734 ::GetObject(hbmpMask, sizeof(BITMAP), (LPVOID)&bm);
1735 w = bm.bmWidth;
1736 h = bm.bmHeight;
1737 }
1738
1739 HDC hdcSrc = ::CreateCompatibleDC(NULL);
1740 HDC hdcDst = ::CreateCompatibleDC(NULL);
1741 if ( !hdcSrc || !hdcDst )
1742 {
1743 wxLogLastError(wxT("CreateCompatibleDC"));
1744 }
1745
1746 HBITMAP hbmpInvMask = ::CreateBitmap(w, h, 1, 1, 0);
1747 if ( !hbmpInvMask )
1748 {
1749 wxLogLastError(wxT("CreateBitmap"));
1750 }
1751
1752 HGDIOBJ srcTmp = ::SelectObject(hdcSrc, hbmpMask);
1753 HGDIOBJ dstTmp = ::SelectObject(hdcDst, hbmpInvMask);
1754 if ( !::BitBlt(hdcDst, 0, 0, w, h,
1755 hdcSrc, 0, 0,
1756 NOTSRCCOPY) )
1757 {
1758 wxLogLastError(wxT("BitBlt"));
1759 }
1760
1761 // Deselect objects
1762 SelectObject(hdcSrc,srcTmp);
1763 SelectObject(hdcDst,dstTmp);
1764
1765 ::DeleteDC(hdcSrc);
1766 ::DeleteDC(hdcDst);
1767
1768 return hbmpInvMask;
1769 #else
1770 return 0;
1771 #endif
1772 }