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