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