]> git.saurik.com Git - wxWidgets.git/blame - src/msw/bitmap.cpp
Added a couple more numeric character references
[wxWidgets.git] / src / msw / bitmap.cpp
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: 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 and Markus Holzem
1d792928 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
10fcf31a
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17 20#ifdef __GNUG__
10fcf31a 21 #pragma implementation "bitmap.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
10fcf31a 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
31#ifndef WX_PRECOMP
10fcf31a
VZ
32 #include <stdio.h>
33
34 #include "wx/list.h"
35 #include "wx/utils.h"
36 #include "wx/app.h"
37 #include "wx/palette.h"
38 #include "wx/dcmemory.h"
39 #include "wx/bitmap.h"
40 #include "wx/icon.h"
2bda0e17
KB
41#endif
42
43#include "wx/msw/private.h"
1d792928
VZ
44#include "wx/log.h"
45
2bda0e17 46#include "wx/msw/dib.h"
b75dd496 47#include "wx/image.h"
66e23ad2 48#include "wx/xpmdecod.h"
2bda0e17 49
3c1a88d8
VZ
50// missing from mingw32 header
51#ifndef CLR_INVALID
52 #define CLR_INVALID ((COLORREF)-1)
53#endif // no CLR_INVALID
54
10fcf31a
VZ
55// ----------------------------------------------------------------------------
56// macros
57// ----------------------------------------------------------------------------
58
4b7f2165
VZ
59IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
60IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
6d167489 61
4b7f2165 62IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject)
2bda0e17 63
10fcf31a
VZ
64// ============================================================================
65// implementation
66// ============================================================================
67
68// ----------------------------------------------------------------------------
69// wxBitmapRefData
70// ----------------------------------------------------------------------------
71
72wxBitmapRefData::wxBitmapRefData()
2bda0e17 73{
6d167489
VZ
74 m_quality = 0;
75 m_selectedInto = NULL;
76 m_numColors = 0;
77 m_bitmapMask = NULL;
340196c0 78 m_hBitmap = (WXHBITMAP) NULL;
2bda0e17
KB
79}
80
6d167489 81void wxBitmapRefData::Free()
2bda0e17 82{
d59ceba5
VZ
83 wxASSERT_MSG( !m_selectedInto,
84 wxT("deleting bitmap still selected into wxMemoryDC") );
2bda0e17 85
d59ceba5 86 if ( m_hBitmap)
6d167489
VZ
87 {
88 if ( !::DeleteObject((HBITMAP)m_hBitmap) )
89 {
f6bcfd97 90 wxLogLastError(wxT("DeleteObject(hbitmap)"));
6d167489
VZ
91 }
92 }
e7003166 93
6d167489
VZ
94 delete m_bitmapMask;
95 m_bitmapMask = NULL;
2bda0e17
KB
96}
97
10fcf31a 98// ----------------------------------------------------------------------------
6d167489 99// wxBitmap creation
10fcf31a
VZ
100// ----------------------------------------------------------------------------
101
4fe5383d
VZ
102// this function should be called from all wxBitmap ctors
103void wxBitmap::Init()
2bda0e17 104{
4fe5383d 105 // m_refData = NULL; done in the base class ctor
2bda0e17 106
07cf98cb
VZ
107 if ( wxTheBitmapList )
108 wxTheBitmapList->AddBitmap(this);
4fe5383d
VZ
109}
110
6d167489
VZ
111#ifdef __WIN32__
112
113bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage& icon)
114{
115 // it may be either HICON or HCURSOR
116 HICON hicon = (HICON)icon.GetHandle();
117
118 ICONINFO iconInfo;
119 if ( !::GetIconInfo(hicon, &iconInfo) )
120 {
f6bcfd97 121 wxLogLastError(wxT("GetIconInfo"));
6d167489
VZ
122
123 return FALSE;
124 }
125
126 wxBitmapRefData *refData = new wxBitmapRefData;
127 m_refData = refData;
128
d9c8e68e
VZ
129 int w = icon.GetWidth(),
130 h = icon.GetHeight();
131
132 refData->m_width = w;
133 refData->m_height = h;
6d167489
VZ
134 refData->m_depth = wxDisplayDepth();
135
136 refData->m_hBitmap = (WXHBITMAP)iconInfo.hbmColor;
d9c8e68e
VZ
137
138 // the mask returned by GetIconInfo() is inversed compared to the usual
139 // wxWin convention
4b7f2165
VZ
140 refData->m_bitmapMask = new wxMask((WXHBITMAP)
141 wxInvertMask(iconInfo.hbmMask, w, h));
6d167489
VZ
142
143#if WXWIN_COMPATIBILITY_2
144 refData->m_ok = TRUE;
145#endif // WXWIN_COMPATIBILITY_2
146
147 return TRUE;
148}
149
150#endif // Win32
151
152bool wxBitmap::CopyFromCursor(const wxCursor& cursor)
4fe5383d
VZ
153{
154 UnRef();
07cf98cb 155
6d167489 156 if ( !cursor.Ok() )
4fe5383d 157 return FALSE;
07cf98cb 158
6d167489
VZ
159#ifdef __WIN16__
160 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
161
162 return FALSE;
8f177c8e 163#else
6d167489 164 return CopyFromIconOrCursor(cursor);
8f177c8e 165#endif // Win16
6d167489
VZ
166}
167
168bool wxBitmap::CopyFromIcon(const wxIcon& icon)
169{
170 UnRef();
07cf98cb 171
6d167489
VZ
172 if ( !icon.Ok() )
173 return FALSE;
4fe5383d
VZ
174
175 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
176 // to create a bitmap from icon there - but using this way we won't have
177 // the mask (FIXME)
178#ifdef __WIN16__
6d167489
VZ
179 int width = icon.GetWidth(),
180 height = icon.GetHeight();
181
4fe5383d 182 // copy the icon to the bitmap
6d167489 183 ScreenHDC hdcScreen;
4fe5383d
VZ
184 HDC hdc = ::CreateCompatibleDC(hdcScreen);
185 HBITMAP hbitmap = ::CreateCompatibleBitmap(hdcScreen, width, height);
07cf98cb
VZ
186 HBITMAP hbmpOld = (HBITMAP)::SelectObject(hdc, hbitmap);
187
6d167489 188 ::DrawIcon(hdc, 0, 0, GetHiconOf(icon));
07cf98cb
VZ
189
190 ::SelectObject(hdc, hbmpOld);
191 ::DeleteDC(hdc);
4fe5383d 192
6d167489
VZ
193 wxBitmapRefData *refData = new wxBitmapRefData;
194 m_refData = refData;
4fe5383d 195
6d167489
VZ
196 refData->m_width = width;
197 refData->m_height = height;
198 refData->m_depth = wxDisplayDepth();
07cf98cb 199
6d167489 200 refData->m_hBitmap = (WXHBITMAP)hbitmap;
07cf98cb 201
6d167489
VZ
202#if WXWIN_COMPATIBILITY_2
203 refData->m_ok = TRUE;
204#endif // WXWIN_COMPATIBILITY_2
222594ea 205
4fe5383d 206 return TRUE;
6d167489
VZ
207#else // Win32
208 return CopyFromIconOrCursor(icon);
209#endif // Win16/Win32
10fcf31a
VZ
210}
211
212wxBitmap::~wxBitmap()
2bda0e17
KB
213{
214 if (wxTheBitmapList)
215 wxTheBitmapList->DeleteObject(this);
216}
217
5bd3a2da 218wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
2bda0e17 219{
4fe5383d 220 Init();
2bda0e17 221
6d167489
VZ
222 wxBitmapRefData *refData = new wxBitmapRefData;
223 m_refData = refData;
2bda0e17 224
5bd3a2da
VZ
225 refData->m_width = width;
226 refData->m_height = height;
227 refData->m_depth = depth;
6d167489
VZ
228 refData->m_numColors = 0;
229 refData->m_selectedInto = NULL;
2bda0e17 230
5bd3a2da
VZ
231 char *data;
232 if ( depth == 1 )
233 {
234 // we assume that it is in XBM format which is not quite the same as
235 // the format CreateBitmap() wants because the order of bytes in the
236 // line is inversed!
237 static const size_t bytesPerLine = (width + 7) / 8;
238 static const size_t padding = bytesPerLine % 2;
239 static const size_t len = height * ( padding + bytesPerLine );
240 data = (char *)malloc(len);
241 const char *src = bits;
242 char *dst = data;
243
244 for ( int rows = 0; rows < height; rows++ )
245 {
0765adca 246 for ( size_t cols = 0; cols < bytesPerLine; cols++ )
5bd3a2da 247 {
0765adca
VZ
248 unsigned char val = *src++;
249 unsigned char reversed = 0;
250
251 for ( int bits = 0; bits < 8; bits++)
252 {
253 reversed <<= 1;
254 reversed |= (val & 0x01);
255 val >>= 1;
256 }
257 *dst++ = reversed;
5bd3a2da
VZ
258 }
259
260 if ( padding )
261 *dst++ = 0;
5bd3a2da
VZ
262 }
263 }
264 else
265 {
266 // bits should already be in Windows standard format
267 data = (char *)bits; // const_cast is harmless
268 }
269
270 HBITMAP hbmp = ::CreateBitmap(width, height, 1, depth, data);
6d167489
VZ
271 if ( !hbmp )
272 {
f6bcfd97 273 wxLogLastError(wxT("CreateBitmap"));
6d167489 274 }
2bda0e17 275
5bd3a2da
VZ
276 if ( data != bits )
277 {
278 free(data);
279 }
280
6d167489 281 SetHBITMAP((WXHBITMAP)hbmp);
2bda0e17
KB
282}
283
2fd284a4 284// Create from XPM data
4b7f2165 285bool wxBitmap::CreateFromXpm(const char **data)
2fd284a4 286{
66e23ad2 287#if wxUSE_IMAGE && wxUSE_XPM
4fe5383d
VZ
288 Init();
289
66e23ad2
VS
290 wxCHECK_MSG( data != NULL, FALSE, wxT("invalid bitmap data") )
291
292 wxXPMDecoder decoder;
293 wxImage img = decoder.ReadData(data);
294 wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid bitmap data") )
295
296 *this = wxBitmap(img);
297
298 if ( wxTheBitmapList ) wxTheBitmapList->AddBitmap(this);
299
300 return TRUE;
301#else
302 return FALSE;
303#endif
2fd284a4
JS
304}
305
debe6624 306wxBitmap::wxBitmap(int w, int h, int d)
2bda0e17 307{
4fe5383d 308 Init();
2bda0e17 309
4fe5383d 310 (void)Create(w, h, d);
2bda0e17
KB
311}
312
debe6624 313wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth)
2bda0e17 314{
4fe5383d 315 Init();
2bda0e17 316
6d167489 317 (void)Create(data, type, width, height, depth);
2bda0e17
KB
318}
319
2aeec9ec 320wxBitmap::wxBitmap(const wxString& filename, wxBitmapType type)
2bda0e17 321{
4fe5383d 322 Init();
2bda0e17 323
4fe5383d 324 LoadFile(filename, (int)type);
2bda0e17
KB
325}
326
debe6624 327bool wxBitmap::Create(int w, int h, int d)
2bda0e17 328{
6d167489
VZ
329 UnRef();
330
331 m_refData = new wxBitmapRefData;
332
333 GetBitmapData()->m_width = w;
334 GetBitmapData()->m_height = h;
335 GetBitmapData()->m_depth = d;
336
337 HBITMAP hbmp;
338
339 if ( d > 0 )
340 {
341 hbmp = ::CreateBitmap(w, h, 1, d, NULL);
342 if ( !hbmp )
343 {
f6bcfd97 344 wxLogLastError(wxT("CreateBitmap"));
6d167489
VZ
345 }
346 }
347 else
348 {
349 ScreenHDC dc;
350 hbmp = ::CreateCompatibleBitmap(dc, w, h);
351 if ( !hbmp )
352 {
f6bcfd97 353 wxLogLastError(wxT("CreateCompatibleBitmap"));
6d167489
VZ
354 }
355
356 GetBitmapData()->m_depth = wxDisplayDepth();
357 }
2bda0e17 358
6d167489 359 SetHBITMAP((WXHBITMAP)hbmp);
2bda0e17 360
6d167489
VZ
361#if WXWIN_COMPATIBILITY_2
362 GetBitmapData()->m_ok = hbmp != 0;
363#endif // WXWIN_COMPATIBILITY_2
2bda0e17 364
6d167489 365 return Ok();
2bda0e17
KB
366}
367
6d51f220
VZ
368// ----------------------------------------------------------------------------
369// wxImage to/from conversions
370// ----------------------------------------------------------------------------
371
372#if wxUSE_IMAGE
373
fec19ea9
VS
374bool wxBitmap::CreateFromImage( const wxImage& image, int depth )
375{
376 wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") )
377
378 m_refData = new wxBitmapRefData();
379
380 // sizeLimit is the MS upper limit for the DIB size
381#ifdef WIN32
382 int sizeLimit = 1024*768*3;
383#else
384 int sizeLimit = 0x7fff ;
385#endif
386
387 // width and height of the device-dependent bitmap
388 int width = image.GetWidth();
389 int bmpHeight = image.GetHeight();
390
391 // calc the number of bytes per scanline and padding
392 int bytePerLine = width*3;
393 int sizeDWORD = sizeof( DWORD );
394 int lineBoundary = bytePerLine % sizeDWORD;
395 int padding = 0;
396 if( lineBoundary > 0 )
397 {
398 padding = sizeDWORD - lineBoundary;
399 bytePerLine += padding;
400 }
401 // calc the number of DIBs and heights of DIBs
402 int numDIB = 1;
403 int hRemain = 0;
404 int height = sizeLimit/bytePerLine;
405 if( height >= bmpHeight )
406 height = bmpHeight;
407 else
408 {
409 numDIB = bmpHeight / height;
410 hRemain = bmpHeight % height;
411 if( hRemain >0 ) numDIB++;
412 }
413
414 // set bitmap parameters
c447ce17 415 wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") );
fec19ea9
VS
416 SetWidth( width );
417 SetHeight( bmpHeight );
418 if (depth == -1) depth = wxDisplayDepth();
419 SetDepth( depth );
420
421 // create a DIB header
422 int headersize = sizeof(BITMAPINFOHEADER);
423 BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize );
097aeb99 424 wxCHECK_MSG( lpDIBh, FALSE, wxT("could not allocate memory for DIB header") );
fec19ea9
VS
425 // Fill in the DIB header
426 lpDIBh->bmiHeader.biSize = headersize;
427 lpDIBh->bmiHeader.biWidth = (DWORD)width;
428 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
429 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
430 // the general formula for biSizeImage:
431 // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
432 lpDIBh->bmiHeader.biPlanes = 1;
433 lpDIBh->bmiHeader.biBitCount = 24;
434 lpDIBh->bmiHeader.biCompression = BI_RGB;
435 lpDIBh->bmiHeader.biClrUsed = 0;
436 // These seem not really needed for our purpose here.
437 lpDIBh->bmiHeader.biClrImportant = 0;
438 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
439 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
440 // memory for DIB data
441 unsigned char *lpBits;
442 lpBits = (unsigned char *)malloc( lpDIBh->bmiHeader.biSizeImage );
443 if( !lpBits )
444 {
445 wxFAIL_MSG( wxT("could not allocate memory for DIB") );
446 free( lpDIBh );
447 return FALSE;
448 }
449
450 // create and set the device-dependent bitmap
451 HDC hdc = ::GetDC(NULL);
452 HDC memdc = ::CreateCompatibleDC( hdc );
453 HBITMAP hbitmap;
454 hbitmap = ::CreateCompatibleBitmap( hdc, width, bmpHeight );
455 ::SelectObject( memdc, hbitmap);
456
457 HPALETTE hOldPalette = 0;
458 if (image.GetPalette().Ok())
459 {
460 hOldPalette = ::SelectPalette(memdc, (HPALETTE) image.GetPalette().GetHPALETTE(), FALSE);
461 ::RealizePalette(memdc);
462 }
463
464 // copy image data into DIB data and then into DDB (in a loop)
465 unsigned char *data = image.GetData();
466 int i, j, n;
467 int origin = 0;
468 unsigned char *ptdata = data;
469 unsigned char *ptbits;
470
471 for( n=0; n<numDIB; n++ )
472 {
473 if( numDIB > 1 && n == numDIB-1 && hRemain > 0 )
474 {
475 // redefine height and size of the (possibly) last smaller DIB
476 // memory is not reallocated
477 height = hRemain;
478 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
479 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
480 }
481 ptbits = lpBits;
482
483 for( j=0; j<height; j++ )
484 {
485 for( i=0; i<width; i++ )
486 {
487 *(ptbits++) = *(ptdata+2);
488 *(ptbits++) = *(ptdata+1);
489 *(ptbits++) = *(ptdata );
490 ptdata += 3;
491 }
492 for( i=0; i< padding; i++ ) *(ptbits++) = 0;
493 }
494 ::StretchDIBits( memdc, 0, origin, width, height,\
495 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
496 origin += height;
497 // if numDIB = 1, lines below can also be used
498 // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
499 // The above line is equivalent to the following two lines.
500 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
501 // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
502 // or the following lines
503 // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
504 // HDC memdc = ::CreateCompatibleDC( hdc );
505 // ::SelectObject( memdc, hbitmap);
506 // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
507 // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
508 // ::SelectObject( memdc, 0 );
509 // ::DeleteDC( memdc );
510 }
511 SetHBITMAP( (WXHBITMAP) hbitmap );
512
513 if (hOldPalette)
514 SelectPalette(memdc, hOldPalette, FALSE);
515
516 // similarly, created an mono-bitmap for the possible mask
517 if( image.HasMask() )
518 {
519 hbitmap = ::CreateBitmap( (WORD)width, (WORD)bmpHeight, 1, 1, NULL );
520 HGDIOBJ hbmpOld = ::SelectObject( memdc, hbitmap);
521 if( numDIB == 1 ) height = bmpHeight;
522 else height = sizeLimit/bytePerLine;
523 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
524 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
525 origin = 0;
526 unsigned char r = image.GetMaskRed();
527 unsigned char g = image.GetMaskGreen();
528 unsigned char b = image.GetMaskBlue();
529 unsigned char zero = 0, one = 255;
530 ptdata = data;
531 for( n=0; n<numDIB; n++ )
532 {
533 if( numDIB > 1 && n == numDIB - 1 && hRemain > 0 )
534 {
535 // redefine height and size of the (possibly) last smaller DIB
536 // memory is not reallocated
537 height = hRemain;
538 lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
539 lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
540 }
541 ptbits = lpBits;
542 for( int j=0; j<height; j++ )
543 {
544 for(i=0; i<width; i++ )
545 {
546 // was causing a code gen bug in cw : if( ( cr !=r) || (cg!=g) || (cb!=b) )
547 unsigned char cr = (*(ptdata++)) ;
548 unsigned char cg = (*(ptdata++)) ;
549 unsigned char cb = (*(ptdata++)) ;
550
551 if( ( cr !=r) || (cg!=g) || (cb!=b) )
552 {
553 *(ptbits++) = one;
554 *(ptbits++) = one;
555 *(ptbits++) = one;
556 }
557 else
558 {
559 *(ptbits++) = zero;
560 *(ptbits++) = zero;
561 *(ptbits++) = zero;
562 }
563 }
564 for( i=0; i< padding; i++ ) *(ptbits++) = zero;
565 }
566 ::StretchDIBits( memdc, 0, origin, width, height,\
567 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
568 origin += height;
569 }
570 // create a wxMask object
571 wxMask *mask = new wxMask();
572 mask->SetMaskBitmap( (WXHBITMAP) hbitmap );
573 SetMask( mask );
574 // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
575 /* The following can also be used but is slow to run
576 wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
577 wxMask *mask = new wxMask( *this, colour );
578 SetMask( mask );
579 */
580
581 ::SelectObject( memdc, hbmpOld );
582 }
583
584 // free allocated resources
585 ::DeleteDC( memdc );
586 ::ReleaseDC(NULL, hdc);
587 free(lpDIBh);
588 free(lpBits);
589
590#if WXWIN_COMPATIBILITY_2
591 // check the wxBitmap object
592 GetBitmapData()->SetOk();
593#endif // WXWIN_COMPATIBILITY_2
6d51f220 594
fec19ea9
VS
595 if (wxTheBitmapList) wxTheBitmapList->AddBitmap(this);
596
597 return TRUE;
598}
599
600wxImage wxBitmap::ConvertToImage() const
601{
602 wxImage image;
6d51f220 603
fec19ea9
VS
604 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
605
606 // create an wxImage object
607 int width = GetWidth();
608 int height = GetHeight();
609 image.Create( width, height );
610 unsigned char *data = image.GetData();
611 if( !data )
612 {
613 wxFAIL_MSG( wxT("could not allocate data for image") );
614 return wxNullImage;
615 }
616
617 // calc the number of bytes per scanline and padding in the DIB
618 int bytePerLine = width*3;
619 int sizeDWORD = sizeof( DWORD );
620 int lineBoundary = bytePerLine % sizeDWORD;
621 int padding = 0;
622 if( lineBoundary > 0 )
623 {
624 padding = sizeDWORD - lineBoundary;
625 bytePerLine += padding;
626 }
627
628 // create a DIB header
629 int headersize = sizeof(BITMAPINFOHEADER);
630 BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize );
631 if( !lpDIBh )
632 {
633 wxFAIL_MSG( wxT("could not allocate data for DIB header") );
634 free( data );
635 return wxNullImage;
636 }
637 // Fill in the DIB header
638 lpDIBh->bmiHeader.biSize = headersize;
639 lpDIBh->bmiHeader.biWidth = width;
640 lpDIBh->bmiHeader.biHeight = -height;
641 lpDIBh->bmiHeader.biSizeImage = bytePerLine * height;
642 lpDIBh->bmiHeader.biPlanes = 1;
643 lpDIBh->bmiHeader.biBitCount = 24;
644 lpDIBh->bmiHeader.biCompression = BI_RGB;
645 lpDIBh->bmiHeader.biClrUsed = 0;
646 // These seem not really needed for our purpose here.
647 lpDIBh->bmiHeader.biClrImportant = 0;
648 lpDIBh->bmiHeader.biXPelsPerMeter = 0;
649 lpDIBh->bmiHeader.biYPelsPerMeter = 0;
650 // memory for DIB data
651 unsigned char *lpBits;
652 lpBits = (unsigned char *) malloc( lpDIBh->bmiHeader.biSizeImage );
653 if( !lpBits )
654 {
655 wxFAIL_MSG( wxT("could not allocate data for DIB") );
656 free( data );
657 free( lpDIBh );
658 return wxNullImage;
659 }
660
661 // copy data from the device-dependent bitmap to the DIB
662 HDC hdc = ::GetDC(NULL);
663 HBITMAP hbitmap;
664 hbitmap = (HBITMAP) GetHBITMAP();
665 ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
666
667 // copy DIB data into the wxImage object
668 int i, j;
669 unsigned char *ptdata = data;
670 unsigned char *ptbits = lpBits;
671 for( i=0; i<height; i++ )
672 {
673 for( j=0; j<width; j++ )
674 {
675 *(ptdata++) = *(ptbits+2);
676 *(ptdata++) = *(ptbits+1);
677 *(ptdata++) = *(ptbits );
678 ptbits += 3;
679 }
680 ptbits += padding;
681 }
682
683 // similarly, set data according to the possible mask bitmap
684 if( GetMask() && GetMask()->GetMaskBitmap() )
685 {
686 hbitmap = (HBITMAP) GetMask()->GetMaskBitmap();
687 // memory DC created, color set, data copied, and memory DC deleted
688 HDC memdc = ::CreateCompatibleDC( hdc );
689 ::SetTextColor( memdc, RGB( 0, 0, 0 ) );
690 ::SetBkColor( memdc, RGB( 255, 255, 255 ) );
691 ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
692 ::DeleteDC( memdc );
693 // background color set to RGB(16,16,16) in consistent with wxGTK
694 unsigned char r=16, g=16, b=16;
695 ptdata = data;
696 ptbits = lpBits;
697 for( i=0; i<height; i++ )
698 {
699 for( j=0; j<width; j++ )
700 {
701 if( *ptbits != 0 )
702 ptdata += 3;
703 else
704 {
705 *(ptdata++) = r;
706 *(ptdata++) = g;
707 *(ptdata++) = b;
708 }
709 ptbits += 3;
710 }
711 ptbits += padding;
712 }
713 image.SetMaskColour( r, g, b );
714 image.SetMask( TRUE );
715 }
716 else
717 {
718 image.SetMask( FALSE );
719 }
720 // free allocated resources
721 ::ReleaseDC(NULL, hdc);
722 free(lpDIBh);
723 free(lpBits);
724
725 return image;
726}
727
6d51f220
VZ
728#endif // wxUSE_IMAGE
729
debe6624 730bool wxBitmap::LoadFile(const wxString& filename, long type)
2bda0e17 731{
6d167489 732 UnRef();
2bda0e17 733
6d167489 734 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
2bda0e17 735
6d167489
VZ
736 if ( handler )
737 {
738 m_refData = new wxBitmapRefData;
2bda0e17 739
6d167489
VZ
740 return handler->LoadFile(this, filename, type, -1, -1);
741 }
6d51f220 742#if wxUSE_IMAGE
6d167489 743 else
b75dd496 744 {
6d167489 745 wxImage image;
6d51f220
VZ
746 if ( image.LoadFile( filename, type ) && image.Ok() )
747 {
748 *this = image.ConvertToBitmap();
6d167489 749
6d51f220
VZ
750 return TRUE;
751 }
b75dd496 752 }
6d51f220
VZ
753#endif // wxUSE_IMAGE
754
755 return FALSE;
2bda0e17
KB
756}
757
debe6624 758bool wxBitmap::Create(void *data, long type, int width, int height, int depth)
2bda0e17 759{
6d167489 760 UnRef();
2bda0e17 761
6d167489 762 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
2bda0e17 763
6d167489
VZ
764 if ( !handler )
765 {
f6bcfd97 766 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %d defined."), type);
2bda0e17 767
6d167489
VZ
768 return FALSE;
769 }
1d792928 770
6d167489 771 m_refData = new wxBitmapRefData;
1d792928 772
6d167489 773 return handler->Create(this, data, type, width, height, depth);
2bda0e17
KB
774}
775
debe6624 776bool wxBitmap::SaveFile(const wxString& filename, int type, const wxPalette *palette)
2bda0e17 777{
6d167489 778 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
2bda0e17 779
6d167489
VZ
780 if ( handler )
781 {
782 return handler->SaveFile(this, filename, type, palette);
783 }
6d51f220 784#if wxUSE_IMAGE
6d167489
VZ
785 else
786 {
787 // FIXME what about palette? shouldn't we use it?
788 wxImage image( *this );
6d51f220
VZ
789 if ( image.Ok() )
790 {
791 return image.SaveFile(filename, type);
792 }
6d167489 793 }
6d51f220
VZ
794#endif // wxUSE_IMAGE
795
796 return FALSE;
2bda0e17
KB
797}
798
4b7f2165
VZ
799// ----------------------------------------------------------------------------
800// sub bitmap extraction
801// ----------------------------------------------------------------------------
802
803wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
804{
805 wxCHECK_MSG( Ok() &&
806 (rect.x >= 0) && (rect.y >= 0) &&
807 (rect.x+rect.width <= GetWidth()) &&
808 (rect.y+rect.height <= GetHeight()),
809 wxNullBitmap, wxT("Invalid bitmap or bitmap region") );
810
811 wxBitmap ret( rect.width, rect.height, GetDepth() );
812 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
813
814 // copy bitmap data
815 HDC dcSrc = ::CreateCompatibleDC(NULL);
816 HDC dcDst = ::CreateCompatibleDC(NULL);
817 SelectObject(dcSrc, (HBITMAP) GetHBITMAP());
818 SelectObject(dcDst, (HBITMAP) ret.GetHBITMAP());
819 BitBlt(dcDst, 0, 0, rect.width, rect.height, dcSrc, rect.x, rect.y, SRCCOPY);
820
821 // copy mask if there is one
822 if (GetMask())
823 {
824 HBITMAP hbmpMask = ::CreateBitmap(rect.width, rect.height, 1, 1, 0);
825
826 SelectObject(dcSrc, (HBITMAP) GetMask()->GetMaskBitmap());
827 SelectObject(dcDst, (HBITMAP) hbmpMask);
828 BitBlt(dcDst, 0, 0, rect.width, rect.height, dcSrc, rect.x, rect.y, SRCCOPY);
829
830 wxMask *mask = new wxMask((WXHBITMAP) hbmpMask);
831 ret.SetMask(mask);
832 }
833
834 SelectObject(dcDst, NULL);
835 SelectObject(dcSrc, NULL);
836 DeleteDC(dcDst);
837 DeleteDC(dcSrc);
838
839 return ret;
840}
841
6d167489
VZ
842// ----------------------------------------------------------------------------
843// wxBitmap accessors
844// ----------------------------------------------------------------------------
2bda0e17
KB
845
846void wxBitmap::SetQuality(int q)
847{
6d167489 848 EnsureHasData();
2bda0e17 849
6d167489 850 GetBitmapData()->m_quality = q;
2bda0e17
KB
851}
852
6d167489 853#if WXWIN_COMPATIBILITY_2
2bda0e17
KB
854void wxBitmap::SetOk(bool isOk)
855{
6d167489 856 EnsureHasData();
2bda0e17 857
6d167489 858 GetBitmapData()->m_ok = isOk;
2bda0e17 859}
6d167489 860#endif // WXWIN_COMPATIBILITY_2
2bda0e17
KB
861
862void wxBitmap::SetPalette(const wxPalette& palette)
863{
6d167489 864 EnsureHasData();
2bda0e17 865
6d167489 866 GetBitmapData()->m_bitmapPalette = palette;
2bda0e17
KB
867}
868
869void wxBitmap::SetMask(wxMask *mask)
870{
6d167489 871 EnsureHasData();
2bda0e17 872
6d167489 873 GetBitmapData()->m_bitmapMask = mask;
2bda0e17
KB
874}
875
7b46ecac
JS
876// Creates a bitmap that matches the device context, from
877// an arbitray bitmap. At present, the original bitmap must have an
878// associated palette. TODO: use a default palette if no palette exists.
879// Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
880wxBitmap wxBitmap::GetBitmapForDC(wxDC& dc) const
881{
882 wxMemoryDC memDC;
4b7f2165 883 wxBitmap tmpBitmap(GetWidth(), GetHeight(), dc.GetDepth());
57c208c5 884 HPALETTE hPal = (HPALETTE) NULL;
7b46ecac 885 LPBITMAPINFO lpDib;
57c208c5 886 void *lpBits = (void*) NULL;
7b46ecac 887
6d167489 888 if( GetPalette() && GetPalette()->Ok() )
a367b9b3 889 {
6d167489 890 tmpBitmap.SetPalette(*GetPalette());
a367b9b3 891 memDC.SelectObject(tmpBitmap);
6d167489
VZ
892 memDC.SetPalette(*GetPalette());
893 hPal = (HPALETTE)GetPalette()->GetHPALETTE();
a367b9b3
JS
894 }
895 else
896 {
897 hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE);
898 wxPalette palette;
899 palette.SetHPALETTE( (WXHPALETTE)hPal );
900 tmpBitmap.SetPalette( palette );
901 memDC.SelectObject(tmpBitmap);
902 memDC.SetPalette( palette );
903 }
7b46ecac 904
6d167489
VZ
905 // set the height negative because in a DIB the order of the lines is
906 // reversed
907 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal, &lpDib) )
908 {
909 return wxNullBitmap;
910 }
7b46ecac
JS
911
912 lpBits = malloc(lpDib->bmiHeader.biSizeImage);
913
6d167489 914 ::GetBitmapBits(GetHbitmap(), lpDib->bmiHeader.biSizeImage, lpBits);
7b46ecac 915
6d167489
VZ
916 ::SetDIBitsToDevice(GetHdcOf(memDC), 0, 0,
917 GetWidth(), GetHeight(),
918 0, 0, 0, GetHeight(),
919 lpBits, lpDib, DIB_RGB_COLORS);
7b46ecac
JS
920
921 free(lpBits);
922
6d167489
VZ
923 wxFreeDIB(lpDib);
924
925 return tmpBitmap;
7b46ecac
JS
926}
927
6d167489
VZ
928// ----------------------------------------------------------------------------
929// wxMask
930// ----------------------------------------------------------------------------
2bda0e17 931
10fcf31a 932wxMask::wxMask()
2bda0e17
KB
933{
934 m_maskBitmap = 0;
935}
936
937// Construct a mask from a bitmap and a colour indicating
938// the transparent area
939wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
940{
941 m_maskBitmap = 0;
6d167489 942 Create(bitmap, colour);
2bda0e17
KB
943}
944
945// Construct a mask from a bitmap and a palette index indicating
946// the transparent area
debe6624 947wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
2bda0e17
KB
948{
949 m_maskBitmap = 0;
6d167489 950 Create(bitmap, paletteIndex);
2bda0e17
KB
951}
952
953// Construct a mask from a mono bitmap (copies the bitmap).
954wxMask::wxMask(const wxBitmap& bitmap)
955{
956 m_maskBitmap = 0;
6d167489 957 Create(bitmap);
2bda0e17
KB
958}
959
10fcf31a 960wxMask::~wxMask()
2bda0e17
KB
961{
962 if ( m_maskBitmap )
963 ::DeleteObject((HBITMAP) m_maskBitmap);
964}
965
966// Create a mask from a mono bitmap (copies the bitmap).
967bool wxMask::Create(const wxBitmap& bitmap)
968{
a58a12e9
VZ
969 wxCHECK_MSG( bitmap.Ok() && bitmap.GetDepth() == 1, FALSE,
970 _T("can't create mask from invalid or not monochrome bitmap") );
971
2bda0e17 972 if ( m_maskBitmap )
6d167489
VZ
973 {
974 ::DeleteObject((HBITMAP) m_maskBitmap);
975 m_maskBitmap = 0;
976 }
a58a12e9 977
6d167489
VZ
978 m_maskBitmap = (WXHBITMAP) CreateBitmap(
979 bitmap.GetWidth(),
980 bitmap.GetHeight(),
981 1, 1, 0
982 );
983 HDC srcDC = CreateCompatibleDC(0);
984 SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP());
985 HDC destDC = CreateCompatibleDC(0);
986 SelectObject(destDC, (HBITMAP) m_maskBitmap);
987 BitBlt(destDC, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), srcDC, 0, 0, SRCCOPY);
988 SelectObject(srcDC, 0);
989 DeleteDC(srcDC);
990 SelectObject(destDC, 0);
991 DeleteDC(destDC);
992 return TRUE;
2bda0e17
KB
993}
994
995// Create a mask from a bitmap and a palette index indicating
996// the transparent area
debe6624 997bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
2bda0e17
KB
998{
999 if ( m_maskBitmap )
1d792928 1000 {
6d167489
VZ
1001 ::DeleteObject((HBITMAP) m_maskBitmap);
1002 m_maskBitmap = 0;
1d792928 1003 }
6d167489
VZ
1004 if (bitmap.Ok() && bitmap.GetPalette()->Ok())
1005 {
1006 unsigned char red, green, blue;
1007 if (bitmap.GetPalette()->GetRGB(paletteIndex, &red, &green, &blue))
1008 {
1009 wxColour transparentColour(red, green, blue);
1010 return Create(bitmap, transparentColour);
1011 }
1012 }
1013 return FALSE;
2bda0e17
KB
1014}
1015
1016// Create a mask from a bitmap and a colour indicating
1017// the transparent area
1018bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1019{
4b7f2165
VZ
1020 wxCHECK_MSG( bitmap.Ok(), FALSE, _T("invalid bitmap in wxMask::Create") );
1021
2bda0e17 1022 if ( m_maskBitmap )
1d792928 1023 {
6d167489
VZ
1024 ::DeleteObject((HBITMAP) m_maskBitmap);
1025 m_maskBitmap = 0;
1026 }
4b7f2165
VZ
1027
1028 int width = bitmap.GetWidth(),
1029 height = bitmap.GetHeight();
1030
1031 // scan the bitmap for the transparent colour and set the corresponding
1032 // pixels in the mask to BLACK and the rest to WHITE
1033 COLORREF maskColour = wxColourToRGB(colour);
1034 m_maskBitmap = (WXHBITMAP)::CreateBitmap(width, height, 1, 1, 0);
1035
1036 HDC srcDC = ::CreateCompatibleDC(NULL);
1037 HDC destDC = ::CreateCompatibleDC(NULL);
1038 if ( !srcDC || !destDC )
1039 {
f6bcfd97 1040 wxLogLastError(wxT("CreateCompatibleDC"));
4b7f2165
VZ
1041 }
1042
0bafad0c
VZ
1043 bool ok = TRUE;
1044
1045 HGDIOBJ hbmpSrcOld = ::SelectObject(srcDC, GetHbitmapOf(bitmap));
1046 if ( !hbmpSrcOld )
6d167489 1047 {
f6bcfd97 1048 wxLogLastError(wxT("SelectObject"));
0bafad0c
VZ
1049
1050 ok = FALSE;
4b7f2165 1051 }
0bafad0c
VZ
1052
1053 HGDIOBJ hbmpDstOld = ::SelectObject(destDC, (HBITMAP)m_maskBitmap);
1054 if ( !hbmpDstOld )
4b7f2165 1055 {
f6bcfd97 1056 wxLogLastError(wxT("SelectObject"));
0bafad0c
VZ
1057
1058 ok = FALSE;
1d792928 1059 }
2bda0e17 1060
4b7f2165
VZ
1061 // this is not very efficient, but I can't think of a better way of doing
1062 // it
0bafad0c 1063 for ( int w = 0; ok && (w < width); w++ )
2bda0e17 1064 {
0bafad0c 1065 for ( int h = 0; ok && (h < height); h++ )
c793fa87 1066 {
6d167489 1067 COLORREF col = GetPixel(srcDC, w, h);
4b7f2165
VZ
1068 if ( col == CLR_INVALID )
1069 {
f6bcfd97 1070 wxLogLastError(wxT("GetPixel"));
4b7f2165
VZ
1071
1072 // doesn't make sense to continue
0bafad0c 1073 ok = FALSE;
4b7f2165 1074
0bafad0c 1075 break;
4b7f2165
VZ
1076 }
1077
1078 if ( col == maskColour )
6d167489
VZ
1079 {
1080 ::SetPixel(destDC, w, h, RGB(0, 0, 0));
1081 }
1082 else
1083 {
1084 ::SetPixel(destDC, w, h, RGB(255, 255, 255));
1085 }
c793fa87 1086 }
2bda0e17 1087 }
4b7f2165 1088
0bafad0c 1089 ::SelectObject(srcDC, hbmpSrcOld);
6d167489 1090 ::DeleteDC(srcDC);
0bafad0c 1091 ::SelectObject(destDC, hbmpDstOld);
6d167489 1092 ::DeleteDC(destDC);
4b7f2165 1093
0bafad0c 1094 return ok;
6d167489
VZ
1095}
1096
1097// ----------------------------------------------------------------------------
1098// wxBitmapHandler
1099// ----------------------------------------------------------------------------
1d792928 1100
6d167489
VZ
1101bool wxBitmapHandler::Create(wxGDIImage *image,
1102 void *data,
1103 long flags,
1104 int width, int height, int depth)
1105{
1106 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1d792928 1107
8c1375b9 1108 return bitmap ? Create(bitmap, data, flags, width, height, depth) : FALSE;
2bda0e17
KB
1109}
1110
6d167489
VZ
1111bool wxBitmapHandler::Load(wxGDIImage *image,
1112 const wxString& name,
1113 long flags,
1114 int width, int height)
2bda0e17 1115{
6d167489 1116 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
2bda0e17 1117
6d167489
VZ
1118 return bitmap ? LoadFile(bitmap, name, flags, width, height) : FALSE;
1119}
2bda0e17 1120
6d167489
VZ
1121bool wxBitmapHandler::Save(wxGDIImage *image,
1122 const wxString& name,
1123 int type)
2bda0e17 1124{
6d167489
VZ
1125 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1126
1127 return bitmap ? SaveFile(bitmap, name, type) : FALSE;
2bda0e17
KB
1128}
1129
6d167489
VZ
1130bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap),
1131 void *WXUNUSED(data),
1132 long WXUNUSED(type),
1133 int WXUNUSED(width),
1134 int WXUNUSED(height),
1135 int WXUNUSED(depth))
2bda0e17 1136{
6d167489 1137 return FALSE;
2bda0e17
KB
1138}
1139
6d167489
VZ
1140bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap),
1141 const wxString& WXUNUSED(name),
1142 long WXUNUSED(type),
1143 int WXUNUSED(desiredWidth),
1144 int WXUNUSED(desiredHeight))
2bda0e17 1145{
6d167489 1146 return FALSE;
2bda0e17
KB
1147}
1148
6d167489
VZ
1149bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap),
1150 const wxString& WXUNUSED(name),
1151 int WXUNUSED(type),
1152 const wxPalette *WXUNUSED(palette))
2bda0e17 1153{
6d167489 1154 return FALSE;
7b46ecac
JS
1155}
1156
6d167489
VZ
1157// ----------------------------------------------------------------------------
1158// DIB functions
1159// ----------------------------------------------------------------------------
1160
1161bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel,
1162 HPALETTE hPal, LPBITMAPINFO* lpDIBHeader)
7b46ecac
JS
1163{
1164 unsigned long i, headerSize;
1165 LPBITMAPINFO lpDIBheader = NULL;
1166 LPPALETTEENTRY lpPe = NULL;
1167
1168
1169 // Allocate space for a DIB header
1170 headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY)));
1171 lpDIBheader = (BITMAPINFO *) malloc(headerSize);
1172 lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER));
1173
1174 GetPaletteEntries(hPal, 0, 256, lpPe);
1175
7b46ecac
JS
1176 memset(lpDIBheader, 0x00, sizeof(BITMAPINFOHEADER));
1177
7b46ecac
JS
1178 // Fill in the static parts of the DIB header
1179 lpDIBheader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1180 lpDIBheader->bmiHeader.biWidth = xSize;
1181 lpDIBheader->bmiHeader.biHeight = ySize;
1182 lpDIBheader->bmiHeader.biPlanes = 1;
1183
1184 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1185 lpDIBheader->bmiHeader.biBitCount = (WORD)(bitsPerPixel);
1186 lpDIBheader->bmiHeader.biCompression = BI_RGB;
6d167489 1187 lpDIBheader->bmiHeader.biSizeImage = xSize * abs(ySize) * bitsPerPixel >> 3;
7b46ecac
JS
1188 lpDIBheader->bmiHeader.biClrUsed = 256;
1189
1190
1191 // Initialize the DIB palette
1192 for (i = 0; i < 256; i++) {
1193 lpDIBheader->bmiColors[i].rgbReserved = lpPe[i].peFlags;
1194 lpDIBheader->bmiColors[i].rgbRed = lpPe[i].peRed;
1195 lpDIBheader->bmiColors[i].rgbGreen = lpPe[i].peGreen;
1196 lpDIBheader->bmiColors[i].rgbBlue = lpPe[i].peBlue;
1197 }
1198
1199 *lpDIBHeader = lpDIBheader;
1200
6d167489 1201 return TRUE;
7b46ecac
JS
1202}
1203
6d167489 1204void wxFreeDIB(LPBITMAPINFO lpDIBHeader)
7b46ecac 1205{
6d167489 1206 free(lpDIBHeader);
7b46ecac
JS
1207}
1208
4b7f2165
VZ
1209// ----------------------------------------------------------------------------
1210// other helper functions
1211// ----------------------------------------------------------------------------
1212
1213extern HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h)
1214{
1215 wxCHECK_MSG( hbmpMask, 0, _T("invalid bitmap in wxInvertMask") );
1216
1217 // get width/height from the bitmap if not given
1218 if ( !w || !h )
1219 {
1220 BITMAP bm;
1221 ::GetObject(hbmpMask, sizeof(BITMAP), (LPVOID)&bm);
1222 w = bm.bmWidth;
1223 h = bm.bmHeight;
1224 }
1225
1226 HDC hdcSrc = ::CreateCompatibleDC(NULL);
1227 HDC hdcDst = ::CreateCompatibleDC(NULL);
1228 if ( !hdcSrc || !hdcDst )
1229 {
f6bcfd97 1230 wxLogLastError(wxT("CreateCompatibleDC"));
4b7f2165
VZ
1231 }
1232
1233 HBITMAP hbmpInvMask = ::CreateBitmap(w, h, 1, 1, 0);
1234 if ( !hbmpInvMask )
1235 {
f6bcfd97 1236 wxLogLastError(wxT("CreateBitmap"));
4b7f2165
VZ
1237 }
1238
1239 ::SelectObject(hdcSrc, hbmpMask);
1240 ::SelectObject(hdcDst, hbmpInvMask);
1241 if ( !::BitBlt(hdcDst, 0, 0, w, h,
1242 hdcSrc, 0, 0,
1243 NOTSRCCOPY) )
1244 {
f6bcfd97 1245 wxLogLastError(wxT("BitBlt"));
4b7f2165 1246 }
7b46ecac 1247
4b7f2165
VZ
1248 ::DeleteDC(hdcSrc);
1249 ::DeleteDC(hdcDst);
1250
1251 return hbmpInvMask;
1252}