1 ////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "bitmap.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
37 #include "wx/palette.h"
38 #include "wx/dcmemory.h"
39 #include "wx/bitmap.h"
43 #include "wx/msw/private.h"
47 #include "wx/msw/dib.h"
51 #include "wx/xpmdecod.h"
53 #ifdef wxHAVE_RAW_BITMAP
54 #include "wx/rawbmp.h"
57 // missing from mingw32 header
59 #define CLR_INVALID ((COLORREF)-1)
60 #endif // no CLR_INVALID
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 class WXDLLEXPORT wxBitmapRefData
: public wxGDIImageRefData
70 virtual ~wxBitmapRefData() { Free(); }
74 // set the mask object to use as the mask, we take ownership of it
75 void SetMask(wxMask
*mask
)
81 // set the HBITMAP to use as the mask
82 void SetMask(HBITMAP hbmpMask
)
84 SetMask(new wxMask((WXHBITMAP
)hbmpMask
));
88 wxMask
*GetMask() const { return m_bitmapMask
; }
92 wxPalette m_bitmapPalette
;
93 #endif // wxUSE_PALETTE
99 // this field is solely for error checking: we detect selecting a bitmap
100 // into more than one DC at once or deleting a bitmap still selected into a
101 // DC (both are serious programming errors under Windows)
102 wxDC
*m_selectedInto
;
103 #endif // __WXDEBUG__
106 // when GetRawData() is called for a DDB we need to convert it to a DIB
107 // first to be able to provide direct access to it and we cache that DIB
108 // here and convert it back to DDB when UngetRawData() is called
112 // true if we have alpha transparency info and can be drawn using
116 // true if our HBITMAP is a DIB section, false if it is a DDB
120 // optional mask for transparent drawing
121 wxMask
*m_bitmapMask
;
123 DECLARE_NO_COPY_CLASS(wxBitmapRefData
)
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
, wxGDIObject
)
131 IMPLEMENT_DYNAMIC_CLASS(wxMask
, wxObject
)
133 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
, wxObject
)
135 // ============================================================================
137 // ============================================================================
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
143 // decide whether we should create a DIB or a DDB for the given parameters
144 static bool wxShouldCreateDIB(int w
, int h
, int d
, WXHDC hdc
)
147 // here is the logic:
149 // (a) if hdc is specified, the caller explicitly wants DDB
150 // (b) otherwise, create a DIB if depth >= 24 (we don't support 16bpp or
152 // (c) finally, create DIBs under Win9x even if the depth hasn't been
153 // explicitly specified but the current display depth is 24 or more
154 // and the image is "big", i.e. > 16Mb which is the theoretical limit
155 // for DDBs under Win9x
157 // consequences (all of which seem to make sense):
159 // (i) by default, DDBs are created (depth == -1 usually)
160 // (ii) DIBs can be created by explicitly specifying the depth
161 // (iii) using a DC always forces creating a DDB
165 wxDIB::GetLineSize(w
, wxDisplayDepth())*h
> 16*1024*1024));
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
175 wxBitmapRefData::wxBitmapRefData()
178 m_selectedInto
= NULL
;
182 m_hBitmap
= (WXHBITMAP
) NULL
;
191 void wxBitmapRefData::Free()
193 wxASSERT_MSG( !m_selectedInto
,
194 wxT("deleting bitmap still selected into wxMemoryDC") );
197 wxASSERT_MSG( !m_dib
, _T("forgot to call wxBitmap::UngetRawData()!") );
202 if ( !::DeleteObject((HBITMAP
)m_hBitmap
) )
204 wxLogLastError(wxT("DeleteObject(hbitmap)"));
212 // ----------------------------------------------------------------------------
214 // ----------------------------------------------------------------------------
216 // this function should be called from all wxBitmap ctors
217 void wxBitmap::Init()
219 // m_refData = NULL; done in the base class ctor
222 wxGDIImageRefData
*wxBitmap::CreateData() const
224 return new wxBitmapRefData
;
229 bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage
& icon
)
231 #if !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
232 // it may be either HICON or HCURSOR
233 HICON hicon
= (HICON
)icon
.GetHandle();
236 if ( !::GetIconInfo(hicon
, &iconInfo
) )
238 wxLogLastError(wxT("GetIconInfo"));
243 wxBitmapRefData
*refData
= new wxBitmapRefData
;
246 int w
= icon
.GetWidth(),
247 h
= icon
.GetHeight();
249 refData
->m_width
= w
;
250 refData
->m_height
= h
;
251 refData
->m_depth
= wxDisplayDepth();
253 refData
->m_hBitmap
= (WXHBITMAP
)iconInfo
.hbmColor
;
255 // the mask returned by GetIconInfo() is inversed compared to the usual
257 refData
->SetMask(wxInvertMask(iconInfo
.hbmMask
, w
, h
));
260 // delete the old one now as we don't need it any more
261 ::DeleteObject(iconInfo
.hbmMask
);
271 bool wxBitmap::CopyFromCursor(const wxCursor
& cursor
)
279 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
283 return CopyFromIconOrCursor(cursor
);
287 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
294 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
295 // to create a bitmap from icon there - but using this way we won't have
298 int width
= icon
.GetWidth(),
299 height
= icon
.GetHeight();
301 // copy the icon to the bitmap
303 HDC hdc
= ::CreateCompatibleDC(hdcScreen
);
304 HBITMAP hbitmap
= ::CreateCompatibleBitmap(hdcScreen
, width
, height
);
305 HBITMAP hbmpOld
= (HBITMAP
)::SelectObject(hdc
, hbitmap
);
307 ::DrawIcon(hdc
, 0, 0, GetHiconOf(icon
));
309 ::SelectObject(hdc
, hbmpOld
);
312 wxBitmapRefData
*refData
= new wxBitmapRefData
;
315 refData
->m_width
= width
;
316 refData
->m_height
= height
;
317 refData
->m_depth
= wxDisplayDepth();
319 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
323 return CopyFromIconOrCursor(icon
);
324 #endif // Win16/Win32
328 bool wxBitmap::CopyFromDIB(const wxDIB
& dib
)
330 wxCHECK_MSG( dib
.IsOk(), FALSE
, _T("invalid DIB in CopyFromDIB") );
332 HBITMAP hbitmap
= dib
.CreateDDB();
338 wxBitmapRefData
*refData
= new wxBitmapRefData
;
341 refData
->m_width
= dib
.GetWidth();
342 refData
->m_height
= dib
.GetHeight();
343 refData
->m_depth
= dib
.GetDepth();
345 refData
->m_hBitmap
= (WXHBITMAP
)hbitmap
;
348 wxPalette
*palette
= dib
.CreatePalette();
351 refData
->m_bitmapPalette
= *palette
;
355 #endif // wxUSE_PALETTE
361 wxBitmap::~wxBitmap()
365 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
369 #ifndef __WXMICROWIN__
370 wxBitmapRefData
*refData
= new wxBitmapRefData
;
373 refData
->m_width
= width
;
374 refData
->m_height
= height
;
375 refData
->m_depth
= depth
;
380 // we assume that it is in XBM format which is not quite the same as
381 // the format CreateBitmap() wants because the order of bytes in the
383 const size_t bytesPerLine
= (width
+ 7) / 8;
384 const size_t padding
= bytesPerLine
% 2;
385 const size_t len
= height
* ( padding
+ bytesPerLine
);
386 data
= (char *)malloc(len
);
387 const char *src
= bits
;
390 for ( int rows
= 0; rows
< height
; rows
++ )
392 for ( size_t cols
= 0; cols
< bytesPerLine
; cols
++ )
394 unsigned char val
= *src
++;
395 unsigned char reversed
= 0;
397 for ( int bits
= 0; bits
< 8; bits
++)
400 reversed
|= (val
& 0x01);
412 // bits should already be in Windows standard format
413 data
= (char *)bits
; // const_cast is harmless
416 HBITMAP hbmp
= ::CreateBitmap(width
, height
, 1, depth
, data
);
419 wxLogLastError(wxT("CreateBitmap"));
427 SetHBITMAP((WXHBITMAP
)hbmp
);
431 // Create from XPM data
432 bool wxBitmap::CreateFromXpm(const char **data
)
434 #if wxUSE_IMAGE && wxUSE_XPM
437 wxCHECK_MSG( data
!= NULL
, FALSE
, wxT("invalid bitmap data") )
439 wxXPMDecoder decoder
;
440 wxImage img
= decoder
.ReadData(data
);
441 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid bitmap data") )
443 *this = wxBitmap(img
);
450 wxBitmap::wxBitmap(int w
, int h
, int d
)
454 (void)Create(w
, h
, d
);
457 wxBitmap::wxBitmap(int w
, int h
, const wxDC
& dc
)
461 (void)Create(w
, h
, dc
);
464 wxBitmap::wxBitmap(void *data
, long type
, int width
, int height
, int depth
)
468 (void)Create(data
, type
, width
, height
, depth
);
471 wxBitmap::wxBitmap(const wxString
& filename
, wxBitmapType type
)
475 LoadFile(filename
, (int)type
);
478 bool wxBitmap::Create(int width
, int height
, int depth
)
480 return DoCreate(width
, height
, depth
, 0);
483 bool wxBitmap::Create(int width
, int height
, const wxDC
& dc
)
485 wxCHECK_MSG( dc
.Ok(), FALSE
, _T("invalid HDC in wxBitmap::Create()") );
487 return DoCreate(width
, height
, -1, dc
.GetHDC());
490 bool wxBitmap::DoCreate(int w
, int h
, int d
, WXHDC hdc
)
494 m_refData
= new wxBitmapRefData
;
496 GetBitmapData()->m_width
= w
;
497 GetBitmapData()->m_height
= h
;
502 if ( wxShouldCreateDIB(w
, h
, d
, hdc
) )
506 // create DIBs without alpha channel by default
514 // don't delete the DIB section in dib object dtor
517 GetBitmapData()->m_isDIB
= TRUE
;
518 GetBitmapData()->m_depth
= d
;
524 #ifndef __WXMICROWIN__
527 hbmp
= ::CreateBitmap(w
, h
, 1, d
, NULL
);
530 wxLogLastError(wxT("CreateBitmap"));
534 #endif // !__WXMICROWIN__
537 hbmp
= ::CreateCompatibleBitmap(dc
, w
, h
);
540 wxLogLastError(wxT("CreateCompatibleBitmap"));
543 GetBitmapData()->m_depth
= wxDisplayDepth();
547 SetHBITMAP((WXHBITMAP
)hbmp
);
554 // ----------------------------------------------------------------------------
555 // wxImage to/from conversions for Microwin
556 // ----------------------------------------------------------------------------
558 // Microwin versions are so different from normal ones that it really doesn't
559 // make sense to use #ifdefs inside the function bodies
560 #ifdef __WXMICROWIN__
562 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, const wxDC
& dc
)
564 // Set this to 1 to experiment with mask code,
565 // which currently doesn't work
568 m_refData
= new wxBitmapRefData();
570 // Initial attempt at a simple-minded implementation.
571 // The bitmap will always be created at the screen depth,
572 // so the 'depth' argument is ignored.
574 HDC hScreenDC
= ::GetDC(NULL
);
575 int screenDepth
= ::GetDeviceCaps(hScreenDC
, BITSPIXEL
);
577 HBITMAP hBitmap
= ::CreateCompatibleBitmap(hScreenDC
, image
.GetWidth(), image
.GetHeight());
578 HBITMAP hMaskBitmap
= NULL
;
579 HBITMAP hOldMaskBitmap
= NULL
;
581 unsigned char maskR
= 0;
582 unsigned char maskG
= 0;
583 unsigned char maskB
= 0;
585 // printf("Created bitmap %d\n", (int) hBitmap);
588 ::ReleaseDC(NULL
, hScreenDC
);
591 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
593 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
594 ::ReleaseDC(NULL
, hScreenDC
);
596 // created an mono-bitmap for the possible mask
597 bool hasMask
= image
.HasMask();
602 // FIXME: we should be able to pass bpp = 1, but
603 // GdBlit can't handle a different depth
605 hMaskBitmap
= ::CreateBitmap( (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight(), 1, 1, NULL
);
607 hMaskBitmap
= ::CreateCompatibleBitmap( hMemDC
, (WORD
)image
.GetWidth(), (WORD
)image
.GetHeight());
609 maskR
= image
.GetMaskRed();
610 maskG
= image
.GetMaskGreen();
611 maskB
= image
.GetMaskBlue();
619 hScreenDC
= ::GetDC(NULL
);
620 hMaskDC
= ::CreateCompatibleDC(hScreenDC
);
621 ::ReleaseDC(NULL
, hScreenDC
);
623 hOldMaskBitmap
= ::SelectObject( hMaskDC
, hMaskBitmap
);
631 for (i
= 0; i
< image
.GetWidth(); i
++)
633 for (j
= 0; j
< image
.GetHeight(); j
++)
635 unsigned char red
= image
.GetRed(i
, j
);
636 unsigned char green
= image
.GetGreen(i
, j
);
637 unsigned char blue
= image
.GetBlue(i
, j
);
639 ::SetPixel(hMemDC
, i
, j
, PALETTERGB(red
, green
, blue
));
643 // scan the bitmap for the transparent colour and set the corresponding
644 // pixels in the mask to BLACK and the rest to WHITE
645 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
646 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(0, 0, 0));
648 ::SetPixel(hMaskDC
, i
, j
, PALETTERGB(255, 255, 255));
653 ::SelectObject(hMemDC
, hOldBitmap
);
657 ::SelectObject(hMaskDC
, hOldMaskBitmap
);
660 ((wxBitmapRefData
*)m_refData
)->SetMask(hMaskBitmap
);
663 SetWidth(image
.GetWidth());
664 SetHeight(image
.GetHeight());
665 SetDepth(screenDepth
);
666 SetHBITMAP( (WXHBITMAP
) hBitmap
);
669 // Copy the palette from the source image
670 SetPalette(image
.GetPalette());
671 #endif // wxUSE_PALETTE
676 wxImage
wxBitmap::ConvertToImage() const
678 // Initial attempt at a simple-minded implementation.
679 // The bitmap will always be created at the screen depth,
680 // so the 'depth' argument is ignored.
681 // TODO: transparency (create a mask image)
685 wxFAIL_MSG( wxT("bitmap is invalid") );
691 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
693 // create an wxImage object
694 int width
= GetWidth();
695 int height
= GetHeight();
696 image
.Create( width
, height
);
697 unsigned char *data
= image
.GetData();
700 wxFAIL_MSG( wxT("could not allocate data for image") );
704 HDC hScreenDC
= ::GetDC(NULL
);
706 HDC hMemDC
= ::CreateCompatibleDC(hScreenDC
);
707 ::ReleaseDC(NULL
, hScreenDC
);
709 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
711 HBITMAP hOldBitmap
= ::SelectObject(hMemDC
, hBitmap
);
714 for (i
= 0; i
< GetWidth(); i
++)
716 for (j
= 0; j
< GetHeight(); j
++)
718 COLORREF color
= ::GetPixel(hMemDC
, i
, j
);
719 unsigned char red
= GetRValue(color
);
720 unsigned char green
= GetGValue(color
);
721 unsigned char blue
= GetBValue(color
);
723 image
.SetRGB(i
, j
, red
, green
, blue
);
727 ::SelectObject(hMemDC
, hOldBitmap
);
731 // Copy the palette from the source image
733 image
.SetPalette(* GetPalette());
734 #endif // wxUSE_PALETTE
739 #endif // __WXMICROWIN__
741 // ----------------------------------------------------------------------------
742 // wxImage to/from conversions
743 // ----------------------------------------------------------------------------
745 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
747 return CreateFromImage(image
, depth
, 0);
750 bool wxBitmap::CreateFromImage(const wxImage
& image
, const wxDC
& dc
)
752 wxCHECK_MSG( dc
.Ok(), FALSE
,
753 _T("invalid HDC in wxBitmap::CreateFromImage()") );
755 return CreateFromImage(image
, -1, dc
.GetHDC());
758 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
, WXHDC hdc
)
761 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") );
765 // first convert the image to DIB
766 const int h
= image
.GetHeight();
767 const int w
= image
.GetWidth();
774 // store the bitmap parameters
775 wxBitmapRefData
*refData
= new wxBitmapRefData
;
776 refData
->m_width
= w
;
777 refData
->m_height
= h
;
778 refData
->m_hasAlpha
= image
.HasAlpha();
783 // next either store DIB as is or create a DDB from it
786 // are we going to use DIB?
788 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
789 if ( image
.HasAlpha() || wxShouldCreateDIB(w
, h
, depth
, hdc
) )
791 // don't delete the DIB section in dib object dtor
792 hbitmap
= dib
.Detach();
794 refData
->m_isDIB
= TRUE
;
795 refData
->m_depth
= dib
.GetDepth();
797 else // we need to convert DIB to DDB
799 hbitmap
= dib
.CreateDDB((HDC
)hdc
);
801 refData
->m_depth
= depth
== -1 ? wxDisplayDepth() : depth
;
804 // validate this object
805 SetHBITMAP((WXHBITMAP
)hbitmap
);
807 // finally also set the mask if we have one
808 if ( image
.HasMask() )
810 SetMask(new wxMask(*this, wxColour(image
.GetMaskRed(),
811 image
.GetMaskGreen(),
812 image
.GetMaskBlue())));
817 // FIXME: wxWinCE doesn't support wxDIB yet
822 wxImage
wxBitmap::ConvertToImage() const
824 // FIXME: this is untested code for WinCE, and
825 // the mask is not yet handled.
827 // http://www.codeproject.com/bitmap/dibsection.asp?print=true
830 // the colour used as transparent one in wxImage and the one it is replaced
831 // with when it really occurs in the bitmap
832 static const int MASK_RED
= 1;
833 static const int MASK_GREEN
= 2;
834 static const int MASK_BLUE
= 3;
835 static const int MASK_BLUE_REPLACEMENT
= 2;
839 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
841 // create an wxImage object
842 int width
= GetWidth();
843 int height
= GetHeight();
844 image
.Create( width
, height
);
845 unsigned char *data
= image
.GetData();
848 wxFAIL_MSG( wxT("could not allocate data for image") );
852 // calc the number of bytes per scanline and padding in the DIB
853 int bytePerLine
= width
*3;
854 int sizeDWORD
= sizeof( DWORD
);
855 int lineBoundary
= bytePerLine
% sizeDWORD
;
857 if( lineBoundary
> 0 )
859 padding
= sizeDWORD
- lineBoundary
;
860 bytePerLine
+= padding
;
863 // create a DIB header
864 int headersize
= sizeof(BITMAPINFOHEADER
);
865 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
868 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
872 // Fill in the DIB header
873 lpDIBh
->bmiHeader
.biSize
= headersize
;
874 lpDIBh
->bmiHeader
.biWidth
= width
;
875 lpDIBh
->bmiHeader
.biHeight
= -height
;
876 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
877 lpDIBh
->bmiHeader
.biPlanes
= 1;
878 lpDIBh
->bmiHeader
.biBitCount
= 24;
879 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
880 lpDIBh
->bmiHeader
.biClrUsed
= 0;
881 // These seem not really needed for our purpose here.
882 lpDIBh
->bmiHeader
.biClrImportant
= 0;
883 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
884 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
886 // memory for DIB data is allocated by CreateDIBSection
889 // copy data from the device-dependent bitmap to the DIB
890 HDC hdc
= ::GetDC(NULL
);
891 HBITMAP hBitmap
= (HBITMAP
) GetHBITMAP();
893 HBITMAP hBitmapSection
= ::CreateDIBSection( hdc
, lpDIBh
, DIB_RGB_COLORS
, & lpBits
, NULL
, 0 );
896 wxFAIL_MSG( wxT("could not create a DIB section") );
900 // Copy the image from the DDB to the DIBSection
901 // Need to copy the supplied bitmap onto the newly created DIBsection
902 HDC hMemDC
= CreateCompatibleDC(hdc
);
903 HDC hCopyDC
= CreateCompatibleDC(hdc
);
905 if (! hMemDC
|| ! hCopyDC
)
907 wxFAIL_MSG( wxT("unable to create compatible DCs") );
914 SelectPalette(hMemDC
, m_hPal
, FALSE
); RealizePalette(hMemDC
);
915 SelectPalette(hCopyDC
, m_hPal
, FALSE
); RealizePalette(hCopyDC
);
919 HBITMAP hOldMemBitmap
= (HBITMAP
) SelectObject(hMemDC
, hBitmap
);
920 HBITMAP hOldCopyBitmap
= (HBITMAP
) SelectObject(hCopyDC
, hBitmapSection
);
922 BitBlt(hCopyDC
, 0, 0, GetWidth(), GetHeight(), hMemDC
, 0, 0, SRCCOPY
);
924 SelectObject(hMemDC
, hOldMemBitmap
);
925 SelectObject(hCopyDC
, hOldCopyBitmap
);
932 HGDIOBJ hObj
= ::GetStockObject(DEFAULT_PALETTE
);
933 SelectObject(hMemDC
, hObj
);
934 SelectObject(hCopyDC
, hObj
);
938 ReleaseDC(NULL
, hdc
);
940 // copy DIB data into the wxImage object
942 unsigned char *ptdata
= data
;
943 unsigned char *ptbits
= (unsigned char*) lpBits
;
944 for( i
=0; i
<height
; i
++ )
946 for( j
=0; j
<width
; j
++ )
948 *(ptdata
++) = *(ptbits
+2);
949 *(ptdata
++) = *(ptbits
+1);
950 *(ptdata
++) = *(ptbits
);
958 // similarly, set data according to the possible mask bitmap
959 if( GetMask() && GetMask()->GetMaskBitmap() )
961 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
962 // memory DC created, color set, data copied, and memory DC deleted
963 HDC memdc
= ::CreateCompatibleDC( hdc
);
964 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
965 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
966 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
970 for( i
=0; i
<height
; i
++ )
972 for( j
=0; j
<width
; j
++ )
974 // is this pixel transparent?
977 if ( (ptdata
[0] == MASK_RED
) &&
978 (ptdata
[1] == MASK_GREEN
) &&
979 (ptdata
[2] == MASK_BLUE
) )
981 // we have to fudge the colour a bit to prevent this
982 // pixel from appearing transparent
983 ptdata
[2] = MASK_BLUE_REPLACEMENT
;
989 *(ptdata
++) = MASK_RED
;
990 *(ptdata
++) = MASK_GREEN
;
991 *(ptdata
++) = MASK_BLUE
;
998 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
999 image
.SetMask( TRUE
);
1004 image
.SetMask( FALSE
);
1007 // free allocated resources
1008 ::ReleaseDC(NULL
, hdc
);
1011 // Delete the DIB section
1012 ::DeleteObject(hBitmapSection
);
1016 // the colour used as transparent one in wxImage and the one it is replaced
1017 // with when it really occurs in the bitmap
1018 static const int MASK_RED
= 1;
1019 static const int MASK_GREEN
= 2;
1020 static const int MASK_BLUE
= 3;
1021 static const int MASK_BLUE_REPLACEMENT
= 2;
1025 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
1027 // create an wxImage object
1028 int width
= GetWidth();
1029 int height
= GetHeight();
1030 image
.Create( width
, height
);
1031 unsigned char *data
= image
.GetData();
1034 wxFAIL_MSG( wxT("could not allocate data for image") );
1038 // calc the number of bytes per scanline and padding in the DIB
1039 int bytePerLine
= width
*3;
1040 int sizeDWORD
= sizeof( DWORD
);
1041 int lineBoundary
= bytePerLine
% sizeDWORD
;
1043 if( lineBoundary
> 0 )
1045 padding
= sizeDWORD
- lineBoundary
;
1046 bytePerLine
+= padding
;
1049 // create a DIB header
1050 int headersize
= sizeof(BITMAPINFOHEADER
);
1051 BITMAPINFO
*lpDIBh
= (BITMAPINFO
*) malloc( headersize
);
1054 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
1058 // Fill in the DIB header
1059 lpDIBh
->bmiHeader
.biSize
= headersize
;
1060 lpDIBh
->bmiHeader
.biWidth
= width
;
1061 lpDIBh
->bmiHeader
.biHeight
= -height
;
1062 lpDIBh
->bmiHeader
.biSizeImage
= bytePerLine
* height
;
1063 lpDIBh
->bmiHeader
.biPlanes
= 1;
1064 lpDIBh
->bmiHeader
.biBitCount
= 24;
1065 lpDIBh
->bmiHeader
.biCompression
= BI_RGB
;
1066 lpDIBh
->bmiHeader
.biClrUsed
= 0;
1067 // These seem not really needed for our purpose here.
1068 lpDIBh
->bmiHeader
.biClrImportant
= 0;
1069 lpDIBh
->bmiHeader
.biXPelsPerMeter
= 0;
1070 lpDIBh
->bmiHeader
.biYPelsPerMeter
= 0;
1071 // memory for DIB data
1072 unsigned char *lpBits
;
1073 lpBits
= (unsigned char *) malloc( lpDIBh
->bmiHeader
.biSizeImage
);
1076 wxFAIL_MSG( wxT("could not allocate data for DIB") );
1082 // copy data from the device-dependent bitmap to the DIB
1083 HDC hdc
= ::GetDC(NULL
);
1085 hbitmap
= (HBITMAP
) GetHBITMAP();
1086 ::GetDIBits( hdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1088 // copy DIB data into the wxImage object
1090 unsigned char *ptdata
= data
;
1091 unsigned char *ptbits
= lpBits
;
1092 for( i
=0; i
<height
; i
++ )
1094 for( j
=0; j
<width
; j
++ )
1096 *(ptdata
++) = *(ptbits
+2);
1097 *(ptdata
++) = *(ptbits
+1);
1098 *(ptdata
++) = *(ptbits
);
1104 // similarly, set data according to the possible mask bitmap
1105 if( GetMask() && GetMask()->GetMaskBitmap() )
1107 hbitmap
= (HBITMAP
) GetMask()->GetMaskBitmap();
1108 // memory DC created, color set, data copied, and memory DC deleted
1109 HDC memdc
= ::CreateCompatibleDC( hdc
);
1110 ::SetTextColor( memdc
, RGB( 0, 0, 0 ) );
1111 ::SetBkColor( memdc
, RGB( 255, 255, 255 ) );
1112 ::GetDIBits( memdc
, hbitmap
, 0, height
, lpBits
, lpDIBh
, DIB_RGB_COLORS
);
1113 ::DeleteDC( memdc
);
1116 for( i
=0; i
<height
; i
++ )
1118 for( j
=0; j
<width
; j
++ )
1120 // is this pixel transparent?
1123 if ( (ptdata
[0] == MASK_RED
) &&
1124 (ptdata
[1] == MASK_GREEN
) &&
1125 (ptdata
[2] == MASK_BLUE
) )
1127 // we have to fudge the colour a bit to prevent this
1128 // pixel from appearing transparent
1129 ptdata
[2] = MASK_BLUE_REPLACEMENT
;
1133 else // masked pixel
1135 *(ptdata
++) = MASK_RED
;
1136 *(ptdata
++) = MASK_GREEN
;
1137 *(ptdata
++) = MASK_BLUE
;
1144 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1145 image
.SetMask( TRUE
);
1149 image
.SetMask( FALSE
);
1151 // free allocated resources
1152 ::ReleaseDC(NULL
, hdc
);
1160 #endif // wxUSE_IMAGE
1162 // ----------------------------------------------------------------------------
1163 // loading and saving bitmaps
1164 // ----------------------------------------------------------------------------
1166 bool wxBitmap::LoadFile(const wxString
& filename
, long type
)
1170 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1174 m_refData
= new wxBitmapRefData
;
1176 return handler
->LoadFile(this, filename
, type
, -1, -1);
1182 if ( image
.LoadFile( filename
, type
) && image
.Ok() )
1184 *this = wxBitmap(image
);
1189 #endif // wxUSE_IMAGE
1194 bool wxBitmap::Create(void *data
, long type
, int width
, int height
, int depth
)
1198 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1202 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type
);
1207 m_refData
= new wxBitmapRefData
;
1209 return handler
->Create(this, data
, type
, width
, height
, depth
);
1212 bool wxBitmap::SaveFile(const wxString
& filename
,
1214 const wxPalette
*palette
)
1216 wxBitmapHandler
*handler
= wxDynamicCast(FindHandler(type
), wxBitmapHandler
);
1220 return handler
->SaveFile(this, filename
, type
, palette
);
1225 // FIXME what about palette? shouldn't we use it?
1226 wxImage image
= ConvertToImage();
1229 return image
.SaveFile(filename
, type
);
1232 #endif // wxUSE_IMAGE
1237 // ----------------------------------------------------------------------------
1238 // sub bitmap extraction
1239 // ----------------------------------------------------------------------------
1241 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1243 wxCHECK_MSG( Ok() &&
1244 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1245 (rect
.x
+rect
.width
<= GetWidth()) &&
1246 (rect
.y
+rect
.height
<= GetHeight()),
1247 wxNullBitmap
, wxT("Invalid bitmap or bitmap region") );
1249 wxBitmap
ret( rect
.width
, rect
.height
);
1250 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1252 #ifndef __WXMICROWIN__
1253 // TODO: copy alpha channel data if any
1260 SelectInHDC
selectSrc(dcSrc
, GetHbitmap()),
1261 selectDst(dcDst
, GetHbitmapOf(ret
));
1263 if ( !selectSrc
|| !selectDst
)
1265 wxLogLastError(_T("SelectObjct(hBitmap)"));
1268 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1269 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1271 wxLogLastError(_T("BitBlt"));
1275 // copy mask if there is one
1278 HBITMAP hbmpMask
= ::CreateBitmap(rect
.width
, rect
.height
, 1, 1, 0);
1280 SelectInHDC
selectSrc(dcSrc
, (HBITMAP
) GetMask()->GetMaskBitmap()),
1281 selectDst(dcDst
, hbmpMask
);
1283 if ( !::BitBlt(dcDst
, 0, 0, rect
.width
, rect
.height
,
1284 dcSrc
, rect
.x
, rect
.y
, SRCCOPY
) )
1286 wxLogLastError(_T("BitBlt"));
1289 wxMask
*mask
= new wxMask((WXHBITMAP
) hbmpMask
);
1292 #endif // !__WXMICROWIN__
1297 // ----------------------------------------------------------------------------
1298 // wxBitmap accessors
1299 // ----------------------------------------------------------------------------
1302 wxPalette
* wxBitmap::GetPalette() const
1304 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1305 : (wxPalette
*) NULL
;
1309 wxMask
*wxBitmap::GetMask() const
1311 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask
*) NULL
;
1316 wxDC
*wxBitmap::GetSelectedInto() const
1318 return GetBitmapData() ? GetBitmapData()->m_selectedInto
: (wxDC
*) NULL
;
1323 #if WXWIN_COMPATIBILITY_2_4
1325 int wxBitmap::GetQuality() const
1330 #endif // WXWIN_COMPATIBILITY_2_4
1332 void wxBitmap::UseAlpha()
1334 if ( GetBitmapData() )
1335 GetBitmapData()->m_hasAlpha
= true;
1338 bool wxBitmap::HasAlpha() const
1340 return GetBitmapData() && GetBitmapData()->m_hasAlpha
;
1343 // ----------------------------------------------------------------------------
1345 // ----------------------------------------------------------------------------
1349 void wxBitmap::SetSelectedInto(wxDC
*dc
)
1351 if ( GetBitmapData() )
1352 GetBitmapData()->m_selectedInto
= dc
;
1359 void wxBitmap::SetPalette(const wxPalette
& palette
)
1363 GetBitmapData()->m_bitmapPalette
= palette
;
1366 #endif // wxUSE_PALETTE
1368 void wxBitmap::SetMask(wxMask
*mask
)
1372 GetBitmapData()->SetMask(mask
);
1375 #if WXWIN_COMPATIBILITY_2_4
1377 void wxBitmap::SetQuality(int WXUNUSED(quality
))
1381 #endif // WXWIN_COMPATIBILITY_2_4
1383 // ----------------------------------------------------------------------------
1384 // raw bitmap access support
1385 // ----------------------------------------------------------------------------
1387 #ifdef wxHAVE_RAW_BITMAP
1388 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1393 // no bitmap, no data (raw or otherwise)
1397 // if we're already a DIB we can access our data directly, but if not we
1398 // need to convert this DDB to a DIB section and use it for raw access and
1399 // then convert it back
1401 if ( !GetBitmapData()->m_isDIB
)
1403 wxCHECK_MSG( !GetBitmapData()->m_dib
, FALSE
,
1404 _T("GetRawData() may be called only once") );
1406 wxDIB
*dib
= new wxDIB(*this);
1414 // we'll free it in UngetRawData()
1415 GetBitmapData()->m_dib
= dib
;
1417 hDIB
= dib
->GetHandle();
1421 hDIB
= GetHbitmap();
1425 if ( ::GetObject(hDIB
, sizeof(ds
), &ds
) != sizeof(DIBSECTION
) )
1427 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1432 // check that the bitmap is in correct format
1433 if ( ds
.dsBm
.bmBitsPixel
!= bpp
)
1435 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1440 // ok, store the relevant info in wxPixelDataBase
1441 const LONG h
= ds
.dsBm
.bmHeight
;
1443 data
.m_width
= ds
.dsBm
.bmWidth
;
1446 // remember that DIBs are stored in top to bottom order!
1447 const LONG bytesPerRow
= ds
.dsBm
.bmWidthBytes
;
1448 data
.m_stride
= -bytesPerRow
;
1450 char *bits
= (char *)ds
.dsBm
.bmBits
;
1453 bits
+= (h
- 1)*bytesPerRow
;
1462 void wxBitmap::UngetRawData(wxPixelDataBase
& dataBase
)
1468 // the cast is ugly but we can't do without it and without making this
1469 // function template (and hence inline) unfortunately
1470 typedef wxPixelData
<wxBitmap
, wxAlphaPixelFormat
> PixelData
;
1471 PixelData
& data
= (PixelData
&)dataBase
;
1475 // invalid data, don't crash -- but don't assert neither as we're
1476 // called automatically from wxPixelDataBase dtor and so there is no
1477 // way to prevent this from happening
1481 if ( GetBitmapData()->m_hasAlpha
)
1483 // AlphaBlend() wants to have premultiplied source alpha but
1484 // wxRawBitmap API uses normal, not premultiplied, colours, so adjust
1486 PixelData::Iterator
p(data
);
1488 const int w
= data
.GetWidth();
1489 const int h
= data
.GetHeight();
1491 for ( int y
= 0; y
< h
; y
++ )
1493 PixelData::Iterator rowStart
= p
;
1495 for ( int x
= 0; x
< w
; x
++ )
1497 const unsigned alpha
= p
.Alpha();
1499 p
.Red() = (p
.Red() * alpha
+ 127) / 255;
1500 p
.Blue() = (p
.Blue() * alpha
+ 127) / 255;
1501 p
.Green() = (p
.Green() * alpha
+ 127) / 255;
1511 // if we're a DDB we need to convert DIB back to DDB now to make the
1512 // changes made via raw bitmap access effective
1513 if ( !GetBitmapData()->m_isDIB
)
1515 wxDIB
*dib
= GetBitmapData()->m_dib
;
1516 GetBitmapData()->m_dib
= NULL
;
1522 #endif // wxUSE_WXDIB
1524 #endif // #ifdef wxHAVE_RAW_BITMAP
1526 // ----------------------------------------------------------------------------
1528 // ----------------------------------------------------------------------------
1535 // Construct a mask from a bitmap and a colour indicating
1536 // the transparent area
1537 wxMask::wxMask(const wxBitmap
& bitmap
, const wxColour
& colour
)
1540 Create(bitmap
, colour
);
1543 // Construct a mask from a bitmap and a palette index indicating
1544 // the transparent area
1545 wxMask::wxMask(const wxBitmap
& bitmap
, int paletteIndex
)
1548 Create(bitmap
, paletteIndex
);
1551 // Construct a mask from a mono bitmap (copies the bitmap).
1552 wxMask::wxMask(const wxBitmap
& bitmap
)
1561 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1564 // Create a mask from a mono bitmap (copies the bitmap).
1565 bool wxMask::Create(const wxBitmap
& bitmap
)
1567 #ifndef __WXMICROWIN__
1568 wxCHECK_MSG( bitmap
.Ok() && bitmap
.GetDepth() == 1, FALSE
,
1569 _T("can't create mask from invalid or not monochrome bitmap") );
1573 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1577 m_maskBitmap
= (WXHBITMAP
) CreateBitmap(
1582 HDC srcDC
= CreateCompatibleDC(0);
1583 SelectObject(srcDC
, (HBITMAP
) bitmap
.GetHBITMAP());
1584 HDC destDC
= CreateCompatibleDC(0);
1585 SelectObject(destDC
, (HBITMAP
) m_maskBitmap
);
1586 BitBlt(destDC
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), srcDC
, 0, 0, SRCCOPY
);
1587 SelectObject(srcDC
, 0);
1589 SelectObject(destDC
, 0);
1597 // Create a mask from a bitmap and a palette index indicating
1598 // the transparent area
1599 bool wxMask::Create(const wxBitmap
& bitmap
, int paletteIndex
)
1603 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1608 if (bitmap
.Ok() && bitmap
.GetPalette()->Ok())
1610 unsigned char red
, green
, blue
;
1611 if (bitmap
.GetPalette()->GetRGB(paletteIndex
, &red
, &green
, &blue
))
1613 wxColour
transparentColour(red
, green
, blue
);
1614 return Create(bitmap
, transparentColour
);
1617 #endif // wxUSE_PALETTE
1622 // Create a mask from a bitmap and a colour indicating
1623 // the transparent area
1624 bool wxMask::Create(const wxBitmap
& bitmap
, const wxColour
& colour
)
1626 #ifndef __WXMICROWIN__
1627 wxCHECK_MSG( bitmap
.Ok(), FALSE
, _T("invalid bitmap in wxMask::Create") );
1631 ::DeleteObject((HBITMAP
) m_maskBitmap
);
1635 int width
= bitmap
.GetWidth(),
1636 height
= bitmap
.GetHeight();
1638 // scan the bitmap for the transparent colour and set the corresponding
1639 // pixels in the mask to BLACK and the rest to WHITE
1640 COLORREF maskColour
= wxColourToPalRGB(colour
);
1641 m_maskBitmap
= (WXHBITMAP
)::CreateBitmap(width
, height
, 1, 1, 0);
1643 HDC srcDC
= ::CreateCompatibleDC(NULL
);
1644 HDC destDC
= ::CreateCompatibleDC(NULL
);
1645 if ( !srcDC
|| !destDC
)
1647 wxLogLastError(wxT("CreateCompatibleDC"));
1652 // SelectObject() will fail
1653 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1654 _T("bitmap can't be selected in another DC") );
1656 HGDIOBJ hbmpSrcOld
= ::SelectObject(srcDC
, GetHbitmapOf(bitmap
));
1659 wxLogLastError(wxT("SelectObject"));
1664 HGDIOBJ hbmpDstOld
= ::SelectObject(destDC
, (HBITMAP
)m_maskBitmap
);
1667 wxLogLastError(wxT("SelectObject"));
1674 // this will create a monochrome bitmap with 0 points for the pixels
1675 // which have the same value as the background colour and 1 for the
1677 ::SetBkColor(srcDC
, maskColour
);
1678 ::BitBlt(destDC
, 0, 0, width
, height
, srcDC
, 0, 0, NOTSRCCOPY
);
1681 ::SelectObject(srcDC
, hbmpSrcOld
);
1683 ::SelectObject(destDC
, hbmpDstOld
);
1687 #else // __WXMICROWIN__
1689 #endif // __WXMICROWIN__/!__WXMICROWIN__
1692 // ----------------------------------------------------------------------------
1694 // ----------------------------------------------------------------------------
1696 bool wxBitmapHandler::Create(wxGDIImage
*image
,
1699 int width
, int height
, int depth
)
1701 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1703 return bitmap
? Create(bitmap
, data
, flags
, width
, height
, depth
) : FALSE
;
1706 bool wxBitmapHandler::Load(wxGDIImage
*image
,
1707 const wxString
& name
,
1709 int width
, int height
)
1711 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1713 return bitmap
? LoadFile(bitmap
, name
, flags
, width
, height
) : FALSE
;
1716 bool wxBitmapHandler::Save(wxGDIImage
*image
,
1717 const wxString
& name
,
1720 wxBitmap
*bitmap
= wxDynamicCast(image
, wxBitmap
);
1722 return bitmap
? SaveFile(bitmap
, name
, type
) : FALSE
;
1725 bool wxBitmapHandler::Create(wxBitmap
*WXUNUSED(bitmap
),
1726 void *WXUNUSED(data
),
1727 long WXUNUSED(type
),
1728 int WXUNUSED(width
),
1729 int WXUNUSED(height
),
1730 int WXUNUSED(depth
))
1735 bool wxBitmapHandler::LoadFile(wxBitmap
*WXUNUSED(bitmap
),
1736 const wxString
& WXUNUSED(name
),
1737 long WXUNUSED(type
),
1738 int WXUNUSED(desiredWidth
),
1739 int WXUNUSED(desiredHeight
))
1744 bool wxBitmapHandler::SaveFile(wxBitmap
*WXUNUSED(bitmap
),
1745 const wxString
& WXUNUSED(name
),
1747 const wxPalette
*WXUNUSED(palette
))
1752 // ----------------------------------------------------------------------------
1754 // ----------------------------------------------------------------------------
1756 #ifndef __WXMICROWIN__
1757 bool wxCreateDIB(long xSize
, long ySize
, long bitsPerPixel
,
1758 HPALETTE hPal
, LPBITMAPINFO
* lpDIBHeader
)
1760 unsigned long i
, headerSize
;
1762 // Allocate space for a DIB header
1763 headerSize
= (sizeof(BITMAPINFOHEADER
) + (256 * sizeof(PALETTEENTRY
)));
1764 LPBITMAPINFO lpDIBheader
= (BITMAPINFO
*) malloc(headerSize
);
1765 LPPALETTEENTRY lpPe
= (PALETTEENTRY
*)((BYTE
*)lpDIBheader
+ sizeof(BITMAPINFOHEADER
));
1767 GetPaletteEntries(hPal
, 0, 256, lpPe
);
1769 memset(lpDIBheader
, 0x00, sizeof(BITMAPINFOHEADER
));
1771 // Fill in the static parts of the DIB header
1772 lpDIBheader
->bmiHeader
.biSize
= sizeof(BITMAPINFOHEADER
);
1773 lpDIBheader
->bmiHeader
.biWidth
= xSize
;
1774 lpDIBheader
->bmiHeader
.biHeight
= ySize
;
1775 lpDIBheader
->bmiHeader
.biPlanes
= 1;
1777 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1778 lpDIBheader
->bmiHeader
.biBitCount
= (WORD
)(bitsPerPixel
);
1779 lpDIBheader
->bmiHeader
.biCompression
= BI_RGB
;
1780 lpDIBheader
->bmiHeader
.biSizeImage
= xSize
* abs(ySize
) * bitsPerPixel
>> 3;
1781 lpDIBheader
->bmiHeader
.biClrUsed
= 256;
1784 // Initialize the DIB palette
1785 for (i
= 0; i
< 256; i
++) {
1786 lpDIBheader
->bmiColors
[i
].rgbReserved
= lpPe
[i
].peFlags
;
1787 lpDIBheader
->bmiColors
[i
].rgbRed
= lpPe
[i
].peRed
;
1788 lpDIBheader
->bmiColors
[i
].rgbGreen
= lpPe
[i
].peGreen
;
1789 lpDIBheader
->bmiColors
[i
].rgbBlue
= lpPe
[i
].peBlue
;
1792 *lpDIBHeader
= lpDIBheader
;
1797 void wxFreeDIB(LPBITMAPINFO lpDIBHeader
)
1803 // ----------------------------------------------------------------------------
1804 // global helper functions implemented here
1805 // ----------------------------------------------------------------------------
1807 // helper of wxBitmapToHICON/HCURSOR
1809 HICON
wxBitmapToIconOrCursor(const wxBitmap
& bmp
,
1816 // we can't create an icon/cursor form nothing
1820 wxMask
*mask
= bmp
.GetMask();
1823 // we must have a mask for an icon, so even if it's probably incorrect,
1824 // do create it (grey is the "standard" transparent colour)
1825 mask
= new wxMask(bmp
, *wxLIGHT_GREY
);
1829 wxZeroMemory(iconInfo
);
1830 iconInfo
.fIcon
= iconWanted
; // do we want an icon or a cursor?
1833 iconInfo
.xHotspot
= hotSpotX
;
1834 iconInfo
.yHotspot
= hotSpotY
;
1837 iconInfo
.hbmMask
= wxInvertMask((HBITMAP
)mask
->GetMaskBitmap());
1838 iconInfo
.hbmColor
= GetHbitmapOf(bmp
);
1840 // black out the transparent area to preserve background colour, because
1841 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1842 // the mask to the dest rect.
1844 MemoryHDC dcSrc
, dcDst
;
1845 SelectInHDC
selectMask(dcSrc
, (HBITMAP
)mask
->GetMaskBitmap()),
1846 selectBitmap(dcDst
, iconInfo
.hbmColor
);
1848 if ( !::BitBlt(dcDst
, 0, 0, bmp
.GetWidth(), bmp
.GetHeight(),
1849 dcSrc
, 0, 0, SRCAND
) )
1851 wxLogLastError(_T("BitBlt"));
1855 HICON hicon
= ::CreateIconIndirect(&iconInfo
);
1857 if ( !bmp
.GetMask() )
1859 // we created the mask, now delete it
1863 // delete the inverted mask bitmap we created as well
1864 ::DeleteObject(iconInfo
.hbmMask
);
1869 HICON
wxBitmapToHICON(const wxBitmap
& bmp
)
1871 return wxBitmapToIconOrCursor(bmp
, TRUE
, 0, 0);
1874 HCURSOR
wxBitmapToHCURSOR(const wxBitmap
& bmp
, int hotSpotX
, int hotSpotY
)
1876 return (HCURSOR
)wxBitmapToIconOrCursor(bmp
, FALSE
, hotSpotX
, hotSpotY
);
1879 HBITMAP
wxInvertMask(HBITMAP hbmpMask
, int w
, int h
)
1881 #ifndef __WXMICROWIN__
1882 wxCHECK_MSG( hbmpMask
, 0, _T("invalid bitmap in wxInvertMask") );
1884 // get width/height from the bitmap if not given
1888 ::GetObject(hbmpMask
, sizeof(BITMAP
), (LPVOID
)&bm
);
1893 HDC hdcSrc
= ::CreateCompatibleDC(NULL
);
1894 HDC hdcDst
= ::CreateCompatibleDC(NULL
);
1895 if ( !hdcSrc
|| !hdcDst
)
1897 wxLogLastError(wxT("CreateCompatibleDC"));
1900 HBITMAP hbmpInvMask
= ::CreateBitmap(w
, h
, 1, 1, 0);
1903 wxLogLastError(wxT("CreateBitmap"));
1906 HGDIOBJ srcTmp
= ::SelectObject(hdcSrc
, hbmpMask
);
1907 HGDIOBJ dstTmp
= ::SelectObject(hdcDst
, hbmpInvMask
);
1908 if ( !::BitBlt(hdcDst
, 0, 0, w
, h
,
1912 wxLogLastError(wxT("BitBlt"));
1916 SelectObject(hdcSrc
,srcTmp
);
1917 SelectObject(hdcDst
,dstTmp
);