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