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