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