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