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