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