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