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