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