]> git.saurik.com Git - wxWidgets.git/blame - src/msw/bitmap.cpp
determine the slider event type more precisely (i.e. generate LINE/PAGE UP/DOWN and...
[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;
7532f691
JS
829 if (depth == -1)
830 depth = dib.GetDepth(); // Get depth from image if none specified
acf8e3d2
VZ
831
832 // store the bitmap parameters
833 wxBitmapRefData *refData = new wxBitmapRefData;
834 refData->m_width = w;
835 refData->m_height = h;
acf8e3d2
VZ
836 refData->m_hasAlpha = image.HasAlpha();
837
838 m_refData = refData;
839
840
841 // next either store DIB as is or create a DDB from it
be69f971 842 HBITMAP hbitmap wxDUMMY_INITIALIZE(0);
acf8e3d2 843
2cf45d69 844 // are we going to use DIB?
4b8ab2f9
VZ
845 //
846 // NB: DDBs don't support alpha so if we have alpha channel we must use DIB
847 if ( image.HasAlpha() || wxShouldCreateDIB(w, h, depth, hdc) )
2cf45d69
VZ
848 {
849 // don't delete the DIB section in dib object dtor
850 hbitmap = dib.Detach();
851
fcf90ee1 852 refData->m_isDIB = true;
7532f691 853 refData->m_depth = depth;
2cf45d69 854 }
be69f971 855#ifndef ALWAYS_USE_DIB
2cf45d69 856 else // we need to convert DIB to DDB
acf8e3d2 857 {
b0ea5d96 858 hbitmap = dib.CreateDDB((HDC)hdc);
2cf45d69 859
7532f691 860 refData->m_depth = depth;
acf8e3d2 861 }
be69f971 862#endif // !ALWAYS_USE_DIB
acf8e3d2
VZ
863
864 // validate this object
865 SetHBITMAP((WXHBITMAP)hbitmap);
866
acf8e3d2
VZ
867 // finally also set the mask if we have one
868 if ( image.HasMask() )
869 {
eabd3333
VZ
870 const size_t len = 2*((w+15)/16);
871 BYTE *src = image.GetData();
872 BYTE *data = new BYTE[h*len];
873 memset(data, 0, h*len);
874 BYTE r = image.GetMaskRed(),
875 g = image.GetMaskGreen(),
876 b = image.GetMaskBlue();
877 BYTE *dst = data;
878 for ( int y = 0; y < h; y++, dst += len )
879 {
880 BYTE *dstLine = dst;
881 BYTE mask = 0x80;
882 for ( int x = 0; x < w; x++, src += 3 )
883 {
884 if (src[0] != r || src[1] != g || src[2] != b)
885 *dstLine |= mask;
886
887 if ( (mask >>= 1) == 0 )
888 {
889 dstLine++;
890 mask = 0x80;
891 }
892 }
893 }
894
895 hbitmap = ::CreateBitmap(w, h, 1, 1, data);
896 if ( !hbitmap )
897 {
898 wxLogLastError(_T("CreateBitmap(mask)"));
899 }
900 else
901 {
902 SetMask(new wxMask((WXHBITMAP)hbitmap));
903 }
904
d0ee33f5 905 delete[] data;
acf8e3d2
VZ
906 }
907
fcf90ee1 908 return true;
acf8e3d2
VZ
909}
910
911wxImage wxBitmap::ConvertToImage() const
912{
6faac21b 913 // convert DDB to DIB
be69f971 914 wxDIB dib(*this);
c8688869 915
003e6c07
DS
916 if ( !dib.IsOk() )
917 {
918 return wxNullImage;
919 }
920
6faac21b 921 // and then DIB to our wxImage
003e6c07
DS
922 wxImage image = dib.ConvertToImage();
923 if ( !image.Ok() )
924 {
925 return wxNullImage;
926 }
927
6faac21b
VZ
928 // now do the same for the mask, if we have any
929 HBITMAP hbmpMask = GetMask() ? (HBITMAP) GetMask()->GetMaskBitmap() : NULL;
930 if ( hbmpMask )
fec19ea9 931 {
6faac21b
VZ
932 wxDIB dibMask(hbmpMask);
933 if ( dibMask.IsOk() )
003e6c07 934 {
6faac21b
VZ
935 // TODO: use wxRawBitmap to iterate over DIB
936
937 // we hard code the mask colour for now but we could also make an
938 // effort (and waste time) to choose a colour not present in the
939 // image already to avoid having to fudge the pixels below --
940 // whether it's worth to do it is unclear however
941 static const int MASK_RED = 1;
942 static const int MASK_GREEN = 2;
943 static const int MASK_BLUE = 3;
944 static const int MASK_BLUE_REPLACEMENT = 2;
945
946 const int h = dibMask.GetHeight();
947 const int w = dibMask.GetWidth();
948 const int bpp = dibMask.GetDepth();
949 const int maskBytesPerPixel = bpp >> 3;
950 const int maskBytesPerLine = wxDIB::GetLineSize(w, bpp);
951 unsigned char *data = image.GetData();
952
953 // remember that DIBs are stored in bottom to top order
954 unsigned char *
955 maskLineStart = dibMask.GetData() + ((h - 1) * maskBytesPerLine);
956
957 for ( int y = 0; y < h; y++, maskLineStart -= maskBytesPerLine )
003e6c07 958 {
6faac21b
VZ
959 // traverse one mask DIB line
960 unsigned char *mask = maskLineStart;
961 for ( int x = 0; x < w; x++, mask += maskBytesPerPixel )
003e6c07 962 {
6faac21b
VZ
963 // should this pixel be transparent?
964 if ( *mask )
003e6c07 965 {
6faac21b
VZ
966 // no, check that it isn't transparent by accident
967 if ( (data[0] == MASK_RED) &&
968 (data[1] == MASK_GREEN) &&
969 (data[2] == MASK_BLUE) )
970 {
971 // we have to fudge the colour a bit to prevent
972 // this pixel from appearing transparent
973 data[2] = MASK_BLUE_REPLACEMENT;
974 }
975
976 data += 3;
977 }
978 else // yes, transparent pixel
979 {
980 *data++ = MASK_RED;
981 *data++ = MASK_GREEN;
982 *data++ = MASK_BLUE;
003e6c07 983 }
003e6c07 984 }
003e6c07 985 }
003e6c07 986
6faac21b
VZ
987 image.SetMaskColour(MASK_RED, MASK_GREEN, MASK_BLUE);
988 }
fec19ea9 989 }
fec19ea9
VS
990
991 return image;
992}
993
be69f971
VZ
994#endif // wxUSE_WXDIB
995
6d51f220
VZ
996#endif // wxUSE_IMAGE
997
acf8e3d2
VZ
998// ----------------------------------------------------------------------------
999// loading and saving bitmaps
1000// ----------------------------------------------------------------------------
1001
debe6624 1002bool wxBitmap::LoadFile(const wxString& filename, long type)
2bda0e17 1003{
6d167489 1004 UnRef();
2bda0e17 1005
6d167489 1006 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
2bda0e17 1007
6d167489
VZ
1008 if ( handler )
1009 {
1010 m_refData = new wxBitmapRefData;
2bda0e17 1011
6d167489
VZ
1012 return handler->LoadFile(this, filename, type, -1, -1);
1013 }
6d51f220 1014#if wxUSE_IMAGE
be69f971 1015 else // no bitmap handler found
b75dd496 1016 {
6d167489 1017 wxImage image;
6d51f220
VZ
1018 if ( image.LoadFile( filename, type ) && image.Ok() )
1019 {
368d59f0 1020 *this = wxBitmap(image);
6d167489 1021
fcf90ee1 1022 return true;
6d51f220 1023 }
b75dd496 1024 }
6d51f220
VZ
1025#endif // wxUSE_IMAGE
1026
fcf90ee1 1027 return false;
2bda0e17
KB
1028}
1029
debe6624 1030bool wxBitmap::Create(void *data, long type, int width, int height, int depth)
2bda0e17 1031{
6d167489 1032 UnRef();
2bda0e17 1033
6d167489 1034 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
2bda0e17 1035
6d167489
VZ
1036 if ( !handler )
1037 {
9b601c24 1038 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for type %ld defined."), type);
2bda0e17 1039
fcf90ee1 1040 return false;
6d167489 1041 }
1d792928 1042
6d167489 1043 m_refData = new wxBitmapRefData;
1d792928 1044
6d167489 1045 return handler->Create(this, data, type, width, height, depth);
2bda0e17
KB
1046}
1047
d275c7eb
VZ
1048bool wxBitmap::SaveFile(const wxString& filename,
1049 int type,
1050 const wxPalette *palette)
2bda0e17 1051{
6d167489 1052 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
2bda0e17 1053
6d167489
VZ
1054 if ( handler )
1055 {
1056 return handler->SaveFile(this, filename, type, palette);
1057 }
6d51f220 1058#if wxUSE_IMAGE
be69f971 1059 else // no bitmap handler found
6d167489
VZ
1060 {
1061 // FIXME what about palette? shouldn't we use it?
368d59f0 1062 wxImage image = ConvertToImage();
6d51f220
VZ
1063 if ( image.Ok() )
1064 {
1065 return image.SaveFile(filename, type);
1066 }
6d167489 1067 }
6d51f220
VZ
1068#endif // wxUSE_IMAGE
1069
fcf90ee1 1070 return false;
2bda0e17
KB
1071}
1072
4b7f2165
VZ
1073// ----------------------------------------------------------------------------
1074// sub bitmap extraction
1075// ----------------------------------------------------------------------------
1076
1077wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
1078{
1079 wxCHECK_MSG( Ok() &&
1080 (rect.x >= 0) && (rect.y >= 0) &&
1081 (rect.x+rect.width <= GetWidth()) &&
1082 (rect.y+rect.height <= GetHeight()),
1083 wxNullBitmap, wxT("Invalid bitmap or bitmap region") );
1084
e5d4ef74 1085 wxBitmap ret( rect.width, rect.height, GetDepth() );
4b7f2165
VZ
1086 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
1087
8e9ff815 1088#ifndef __WXMICROWIN__
e5d4ef74
VZ
1089 // handle alpha channel, if any
1090 if (HasAlpha())
92218ce6 1091 ret.UseAlpha();
acf8e3d2 1092
4b7f2165 1093 // copy bitmap data
acf8e3d2
VZ
1094 MemoryHDC dcSrc,
1095 dcDst;
8e9ff815
VZ
1096
1097 {
1098 SelectInHDC selectSrc(dcSrc, GetHbitmap()),
1099 selectDst(dcDst, GetHbitmapOf(ret));
1100
acf8e3d2
VZ
1101 if ( !selectSrc || !selectDst )
1102 {
1103 wxLogLastError(_T("SelectObjct(hBitmap)"));
1104 }
1105
8e9ff815
VZ
1106 if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height,
1107 dcSrc, rect.x, rect.y, SRCCOPY) )
1108 {
1109 wxLogLastError(_T("BitBlt"));
1110 }
1111 }
4b7f2165
VZ
1112
1113 // copy mask if there is one
8e9ff815 1114 if ( GetMask() )
4b7f2165
VZ
1115 {
1116 HBITMAP hbmpMask = ::CreateBitmap(rect.width, rect.height, 1, 1, 0);
1117
8e9ff815
VZ
1118 SelectInHDC selectSrc(dcSrc, (HBITMAP) GetMask()->GetMaskBitmap()),
1119 selectDst(dcDst, hbmpMask);
1120
1121 if ( !::BitBlt(dcDst, 0, 0, rect.width, rect.height,
1122 dcSrc, rect.x, rect.y, SRCCOPY) )
1123 {
1124 wxLogLastError(_T("BitBlt"));
1125 }
4b7f2165
VZ
1126
1127 wxMask *mask = new wxMask((WXHBITMAP) hbmpMask);
1128 ret.SetMask(mask);
1129 }
8e9ff815 1130#endif // !__WXMICROWIN__
4b7f2165
VZ
1131
1132 return ret;
1133}
1134
6d167489
VZ
1135// ----------------------------------------------------------------------------
1136// wxBitmap accessors
1137// ----------------------------------------------------------------------------
2bda0e17 1138
adf9e099 1139#if wxUSE_PALETTE
8bbbae21 1140wxPalette* wxBitmap::GetPalette() const
2bda0e17 1141{
8bbbae21
VZ
1142 return GetBitmapData() ? &GetBitmapData()->m_bitmapPalette
1143 : (wxPalette *) NULL;
1144}
adf9e099 1145#endif
8bbbae21
VZ
1146
1147wxMask *wxBitmap::GetMask() const
1148{
1149 return GetBitmapData() ? GetBitmapData()->GetMask() : (wxMask *) NULL;
1150}
2bda0e17 1151
ec023a6e
VZ
1152wxBitmap wxBitmap::GetMaskBitmap() const
1153{
1154 wxBitmap bmp;
1155 wxMask *mask = GetMask();
1156 if ( mask )
1157 bmp.SetHBITMAP(mask->GetMaskBitmap());
1158 return bmp;
1159}
1160
3ca22d5e
MB
1161#ifdef __WXDEBUG__
1162
8bbbae21
VZ
1163wxDC *wxBitmap::GetSelectedInto() const
1164{
1165 return GetBitmapData() ? GetBitmapData()->m_selectedInto : (wxDC *) NULL;
2bda0e17
KB
1166}
1167
3ca22d5e
MB
1168#endif
1169
8bbbae21
VZ
1170#if WXWIN_COMPATIBILITY_2_4
1171
1172int wxBitmap::GetQuality() const
1173{
1174 return 0;
1175}
1176
1177#endif // WXWIN_COMPATIBILITY_2_4
1178
10b41b53
VZ
1179void wxBitmap::UseAlpha()
1180{
1181 if ( GetBitmapData() )
1182 GetBitmapData()->m_hasAlpha = true;
1183}
1184
acf8e3d2
VZ
1185bool wxBitmap::HasAlpha() const
1186{
1187 return GetBitmapData() && GetBitmapData()->m_hasAlpha;
1188}
1189
8bbbae21
VZ
1190// ----------------------------------------------------------------------------
1191// wxBitmap setters
1192// ----------------------------------------------------------------------------
1193
3ca22d5e
MB
1194#ifdef __WXDEBUG__
1195
8bbbae21
VZ
1196void wxBitmap::SetSelectedInto(wxDC *dc)
1197{
1198 if ( GetBitmapData() )
1199 GetBitmapData()->m_selectedInto = dc;
2bda0e17
KB
1200}
1201
3ca22d5e
MB
1202#endif
1203
d275c7eb
VZ
1204#if wxUSE_PALETTE
1205
2bda0e17
KB
1206void wxBitmap::SetPalette(const wxPalette& palette)
1207{
7ff64980 1208 AllocExclusive();
2bda0e17 1209
6d167489 1210 GetBitmapData()->m_bitmapPalette = palette;
2bda0e17
KB
1211}
1212
d275c7eb
VZ
1213#endif // wxUSE_PALETTE
1214
2bda0e17
KB
1215void wxBitmap::SetMask(wxMask *mask)
1216{
7ff64980 1217 AllocExclusive();
2bda0e17 1218
8bbbae21
VZ
1219 GetBitmapData()->SetMask(mask);
1220}
1221
8bbbae21
VZ
1222#if WXWIN_COMPATIBILITY_2_4
1223
1224void wxBitmap::SetQuality(int WXUNUSED(quality))
1225{
2bda0e17
KB
1226}
1227
8bbbae21
VZ
1228#endif // WXWIN_COMPATIBILITY_2_4
1229
2cf45d69
VZ
1230// ----------------------------------------------------------------------------
1231// raw bitmap access support
1232// ----------------------------------------------------------------------------
1233
1e3c12d7 1234#ifdef wxHAVE_RAW_BITMAP
b9bcaf11 1235void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
2cf45d69 1236{
4676948b 1237#if wxUSE_WXDIB
2cf45d69
VZ
1238 if ( !Ok() )
1239 {
1240 // no bitmap, no data (raw or otherwise)
b9bcaf11 1241 return NULL;
2cf45d69
VZ
1242 }
1243
fa275e86
VZ
1244 // if we're already a DIB we can access our data directly, but if not we
1245 // need to convert this DDB to a DIB section and use it for raw access and
1246 // then convert it back
1247 HBITMAP hDIB;
1248 if ( !GetBitmapData()->m_isDIB )
1249 {
d0ee33f5 1250 wxCHECK_MSG( !GetBitmapData()->m_dib, NULL,
fa275e86
VZ
1251 _T("GetRawData() may be called only once") );
1252
1253 wxDIB *dib = new wxDIB(*this);
1254 if ( !dib->IsOk() )
1255 {
1256 delete dib;
1257
b9bcaf11 1258 return NULL;
fa275e86
VZ
1259 }
1260
1261 // we'll free it in UngetRawData()
1262 GetBitmapData()->m_dib = dib;
1263
1264 hDIB = dib->GetHandle();
1265 }
1266 else // we're a DIB
1267 {
1268 hDIB = GetHbitmap();
1269 }
1270
2cf45d69 1271 DIBSECTION ds;
fa275e86 1272 if ( ::GetObject(hDIB, sizeof(ds), &ds) != sizeof(DIBSECTION) )
2cf45d69 1273 {
fa275e86
VZ
1274 wxFAIL_MSG( _T("failed to get DIBSECTION from a DIB?") );
1275
b9bcaf11
VZ
1276 return NULL;
1277 }
1278
1279 // check that the bitmap is in correct format
1280 if ( ds.dsBm.bmBitsPixel != bpp )
1281 {
1282 wxFAIL_MSG( _T("incorrect bitmap type in wxBitmap::GetRawData()") );
1283
1284 return NULL;
2cf45d69
VZ
1285 }
1286
b9bcaf11 1287 // ok, store the relevant info in wxPixelDataBase
2cf45d69
VZ
1288 const LONG h = ds.dsBm.bmHeight;
1289
b9bcaf11
VZ
1290 data.m_width = ds.dsBm.bmWidth;
1291 data.m_height = h;
2cf45d69
VZ
1292
1293 // remember that DIBs are stored in top to bottom order!
e5d4ef74
VZ
1294 // (We can't just use ds.dsBm.bmWidthBytes here, because it isn't always a
1295 // multiple of 2, as required by the documentation. So we use the official
1296 // formula, which we already use elsewhere.)
1297 const LONG bytesPerRow =
1298 wxDIB::GetLineSize(ds.dsBm.bmWidth, ds.dsBm.bmBitsPixel);
b9bcaf11
VZ
1299 data.m_stride = -bytesPerRow;
1300
1301 char *bits = (char *)ds.dsBm.bmBits;
2cf45d69
VZ
1302 if ( h > 1 )
1303 {
b9bcaf11 1304 bits += (h - 1)*bytesPerRow;
2cf45d69
VZ
1305 }
1306
b9bcaf11 1307 return bits;
4676948b
JS
1308#else
1309 return NULL;
1310#endif
2cf45d69
VZ
1311}
1312
b9bcaf11 1313void wxBitmap::UngetRawData(wxPixelDataBase& dataBase)
13c13ecb 1314{
4676948b 1315#if wxUSE_WXDIB
fa275e86
VZ
1316 if ( !Ok() )
1317 return;
1318
e5d4ef74 1319 if ( !&dataBase )
7af64aac
VZ
1320 {
1321 // invalid data, don't crash -- but don't assert neither as we're
b9bcaf11 1322 // called automatically from wxPixelDataBase dtor and so there is no
7af64aac
VZ
1323 // way to prevent this from happening
1324 return;
1325 }
1326
289307fa
VZ
1327 // if we're a DDB we need to convert DIB back to DDB now to make the
1328 // changes made via raw bitmap access effective
1329 if ( !GetBitmapData()->m_isDIB )
1330 {
1331 wxDIB *dib = GetBitmapData()->m_dib;
1332 GetBitmapData()->m_dib = NULL;
fa275e86 1333
289307fa 1334 // TODO: convert
fa275e86 1335
289307fa 1336 delete dib;
fa275e86 1337 }
289307fa 1338#endif // wxUSE_WXDIB
13c13ecb 1339}
1e3c12d7 1340#endif // #ifdef wxHAVE_RAW_BITMAP
13c13ecb 1341
6d167489
VZ
1342// ----------------------------------------------------------------------------
1343// wxMask
1344// ----------------------------------------------------------------------------
2bda0e17 1345
10fcf31a 1346wxMask::wxMask()
2bda0e17
KB
1347{
1348 m_maskBitmap = 0;
1349}
1350
1351// Construct a mask from a bitmap and a colour indicating
1352// the transparent area
1353wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
1354{
1355 m_maskBitmap = 0;
6d167489 1356 Create(bitmap, colour);
2bda0e17
KB
1357}
1358
1359// Construct a mask from a bitmap and a palette index indicating
1360// the transparent area
debe6624 1361wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
2bda0e17
KB
1362{
1363 m_maskBitmap = 0;
6d167489 1364 Create(bitmap, paletteIndex);
2bda0e17
KB
1365}
1366
1367// Construct a mask from a mono bitmap (copies the bitmap).
1368wxMask::wxMask(const wxBitmap& bitmap)
1369{
1370 m_maskBitmap = 0;
6d167489 1371 Create(bitmap);
2bda0e17
KB
1372}
1373
10fcf31a 1374wxMask::~wxMask()
2bda0e17
KB
1375{
1376 if ( m_maskBitmap )
1377 ::DeleteObject((HBITMAP) m_maskBitmap);
1378}
1379
1380// Create a mask from a mono bitmap (copies the bitmap).
1381bool wxMask::Create(const wxBitmap& bitmap)
1382{
04ef50df 1383#ifndef __WXMICROWIN__
fcf90ee1 1384 wxCHECK_MSG( bitmap.Ok() && bitmap.GetDepth() == 1, false,
a58a12e9
VZ
1385 _T("can't create mask from invalid or not monochrome bitmap") );
1386
2bda0e17 1387 if ( m_maskBitmap )
6d167489
VZ
1388 {
1389 ::DeleteObject((HBITMAP) m_maskBitmap);
1390 m_maskBitmap = 0;
1391 }
a58a12e9 1392
6d167489
VZ
1393 m_maskBitmap = (WXHBITMAP) CreateBitmap(
1394 bitmap.GetWidth(),
1395 bitmap.GetHeight(),
1396 1, 1, 0
1397 );
1398 HDC srcDC = CreateCompatibleDC(0);
1399 SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP());
1400 HDC destDC = CreateCompatibleDC(0);
1401 SelectObject(destDC, (HBITMAP) m_maskBitmap);
1402 BitBlt(destDC, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), srcDC, 0, 0, SRCCOPY);
1403 SelectObject(srcDC, 0);
1404 DeleteDC(srcDC);
1405 SelectObject(destDC, 0);
1406 DeleteDC(destDC);
fcf90ee1 1407 return true;
04ef50df 1408#else
fcf90ee1
WS
1409 wxUnusedVar(bitmap);
1410 return false;
04ef50df 1411#endif
2bda0e17
KB
1412}
1413
1414// Create a mask from a bitmap and a palette index indicating
1415// the transparent area
debe6624 1416bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
2bda0e17
KB
1417{
1418 if ( m_maskBitmap )
1d792928 1419 {
6d167489
VZ
1420 ::DeleteObject((HBITMAP) m_maskBitmap);
1421 m_maskBitmap = 0;
1d792928 1422 }
d275c7eb
VZ
1423
1424#if wxUSE_PALETTE
6d167489
VZ
1425 if (bitmap.Ok() && bitmap.GetPalette()->Ok())
1426 {
1427 unsigned char red, green, blue;
1428 if (bitmap.GetPalette()->GetRGB(paletteIndex, &red, &green, &blue))
1429 {
1430 wxColour transparentColour(red, green, blue);
1431 return Create(bitmap, transparentColour);
1432 }
1433 }
d275c7eb
VZ
1434#endif // wxUSE_PALETTE
1435
fcf90ee1 1436 return false;
2bda0e17
KB
1437}
1438
1439// Create a mask from a bitmap and a colour indicating
1440// the transparent area
1441bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
1442{
04ef50df 1443#ifndef __WXMICROWIN__
fcf90ee1 1444 wxCHECK_MSG( bitmap.Ok(), false, _T("invalid bitmap in wxMask::Create") );
4b7f2165 1445
2bda0e17 1446 if ( m_maskBitmap )
1d792928 1447 {
6d167489
VZ
1448 ::DeleteObject((HBITMAP) m_maskBitmap);
1449 m_maskBitmap = 0;
1450 }
4b7f2165
VZ
1451
1452 int width = bitmap.GetWidth(),
1453 height = bitmap.GetHeight();
1454
1455 // scan the bitmap for the transparent colour and set the corresponding
1456 // pixels in the mask to BLACK and the rest to WHITE
f423f4db 1457 COLORREF maskColour = wxColourToPalRGB(colour);
4b7f2165
VZ
1458 m_maskBitmap = (WXHBITMAP)::CreateBitmap(width, height, 1, 1, 0);
1459
1460 HDC srcDC = ::CreateCompatibleDC(NULL);
1461 HDC destDC = ::CreateCompatibleDC(NULL);
1462 if ( !srcDC || !destDC )
1463 {
f6bcfd97 1464 wxLogLastError(wxT("CreateCompatibleDC"));
4b7f2165
VZ
1465 }
1466
fcf90ee1 1467 bool ok = true;
0bafad0c 1468
1e6feb95
VZ
1469 // SelectObject() will fail
1470 wxASSERT_MSG( !bitmap.GetSelectedInto(),
1471 _T("bitmap can't be selected in another DC") );
1472
0bafad0c
VZ
1473 HGDIOBJ hbmpSrcOld = ::SelectObject(srcDC, GetHbitmapOf(bitmap));
1474 if ( !hbmpSrcOld )
6d167489 1475 {
f6bcfd97 1476 wxLogLastError(wxT("SelectObject"));
0bafad0c 1477
fcf90ee1 1478 ok = false;
4b7f2165 1479 }
0bafad0c
VZ
1480
1481 HGDIOBJ hbmpDstOld = ::SelectObject(destDC, (HBITMAP)m_maskBitmap);
1482 if ( !hbmpDstOld )
4b7f2165 1483 {
f6bcfd97 1484 wxLogLastError(wxT("SelectObject"));
0bafad0c 1485
fcf90ee1 1486 ok = false;
1d792928 1487 }
2bda0e17 1488
59ff46cb 1489 if ( ok )
2bda0e17 1490 {
59ff46cb
VZ
1491 // this will create a monochrome bitmap with 0 points for the pixels
1492 // which have the same value as the background colour and 1 for the
1493 // others
1494 ::SetBkColor(srcDC, maskColour);
1495 ::BitBlt(destDC, 0, 0, width, height, srcDC, 0, 0, NOTSRCCOPY);
2bda0e17 1496 }
4b7f2165 1497
0bafad0c 1498 ::SelectObject(srcDC, hbmpSrcOld);
6d167489 1499 ::DeleteDC(srcDC);
0bafad0c 1500 ::SelectObject(destDC, hbmpDstOld);
6d167489 1501 ::DeleteDC(destDC);
4b7f2165 1502
0bafad0c 1503 return ok;
59ff46cb 1504#else // __WXMICROWIN__
fcf90ee1
WS
1505 wxUnusedVar(bitmap);
1506 wxUnusedVar(colour);
1507 return false;
59ff46cb 1508#endif // __WXMICROWIN__/!__WXMICROWIN__
6d167489
VZ
1509}
1510
1511// ----------------------------------------------------------------------------
1512// wxBitmapHandler
1513// ----------------------------------------------------------------------------
1d792928 1514
6d167489
VZ
1515bool wxBitmapHandler::Create(wxGDIImage *image,
1516 void *data,
1517 long flags,
1518 int width, int height, int depth)
1519{
1520 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1d792928 1521
fcf90ee1 1522 return bitmap ? Create(bitmap, data, flags, width, height, depth) : false;
2bda0e17
KB
1523}
1524
6d167489
VZ
1525bool wxBitmapHandler::Load(wxGDIImage *image,
1526 const wxString& name,
1527 long flags,
1528 int width, int height)
2bda0e17 1529{
6d167489 1530 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
2bda0e17 1531
fcf90ee1 1532 return bitmap ? LoadFile(bitmap, name, flags, width, height) : false;
6d167489 1533}
2bda0e17 1534
6d167489
VZ
1535bool wxBitmapHandler::Save(wxGDIImage *image,
1536 const wxString& name,
1537 int type)
2bda0e17 1538{
6d167489
VZ
1539 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
1540
fcf90ee1 1541 return bitmap ? SaveFile(bitmap, name, type) : false;
2bda0e17
KB
1542}
1543
6d167489
VZ
1544bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap),
1545 void *WXUNUSED(data),
1546 long WXUNUSED(type),
1547 int WXUNUSED(width),
1548 int WXUNUSED(height),
1549 int WXUNUSED(depth))
2bda0e17 1550{
fcf90ee1 1551 return false;
2bda0e17
KB
1552}
1553
6d167489
VZ
1554bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap),
1555 const wxString& WXUNUSED(name),
1556 long WXUNUSED(type),
1557 int WXUNUSED(desiredWidth),
1558 int WXUNUSED(desiredHeight))
2bda0e17 1559{
fcf90ee1 1560 return false;
2bda0e17
KB
1561}
1562
6d167489
VZ
1563bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap),
1564 const wxString& WXUNUSED(name),
1565 int WXUNUSED(type),
1566 const wxPalette *WXUNUSED(palette))
2bda0e17 1567{
fcf90ee1 1568 return false;
7b46ecac
JS
1569}
1570
6d167489
VZ
1571// ----------------------------------------------------------------------------
1572// DIB functions
1573// ----------------------------------------------------------------------------
1574
04ef50df 1575#ifndef __WXMICROWIN__
6d167489
VZ
1576bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel,
1577 HPALETTE hPal, LPBITMAPINFO* lpDIBHeader)
7b46ecac
JS
1578{
1579 unsigned long i, headerSize;
7b46ecac
JS
1580
1581 // Allocate space for a DIB header
1582 headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY)));
999836aa
VZ
1583 LPBITMAPINFO lpDIBheader = (BITMAPINFO *) malloc(headerSize);
1584 LPPALETTEENTRY lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER));
7b46ecac
JS
1585
1586 GetPaletteEntries(hPal, 0, 256, lpPe);
1587
7b46ecac
JS
1588 memset(lpDIBheader, 0x00, sizeof(BITMAPINFOHEADER));
1589
7b46ecac
JS
1590 // Fill in the static parts of the DIB header
1591 lpDIBheader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1592 lpDIBheader->bmiHeader.biWidth = xSize;
1593 lpDIBheader->bmiHeader.biHeight = ySize;
1594 lpDIBheader->bmiHeader.biPlanes = 1;
1595
1596 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
1597 lpDIBheader->bmiHeader.biBitCount = (WORD)(bitsPerPixel);
1598 lpDIBheader->bmiHeader.biCompression = BI_RGB;
d0ee33f5 1599 lpDIBheader->bmiHeader.biSizeImage = (xSize * abs(ySize) * bitsPerPixel) >> 3;
7b46ecac
JS
1600 lpDIBheader->bmiHeader.biClrUsed = 256;
1601
1602
1603 // Initialize the DIB palette
1604 for (i = 0; i < 256; i++) {
1605 lpDIBheader->bmiColors[i].rgbReserved = lpPe[i].peFlags;
1606 lpDIBheader->bmiColors[i].rgbRed = lpPe[i].peRed;
1607 lpDIBheader->bmiColors[i].rgbGreen = lpPe[i].peGreen;
1608 lpDIBheader->bmiColors[i].rgbBlue = lpPe[i].peBlue;
1609 }
1610
1611 *lpDIBHeader = lpDIBheader;
1612
fcf90ee1 1613 return true;
7b46ecac
JS
1614}
1615
6d167489 1616void wxFreeDIB(LPBITMAPINFO lpDIBHeader)
7b46ecac 1617{
6d167489 1618 free(lpDIBHeader);
7b46ecac 1619}
04ef50df 1620#endif
7b46ecac 1621
4b7f2165 1622// ----------------------------------------------------------------------------
211b54b1 1623// global helper functions implemented here
4b7f2165
VZ
1624// ----------------------------------------------------------------------------
1625
211b54b1
VZ
1626// helper of wxBitmapToHICON/HCURSOR
1627static
1628HICON wxBitmapToIconOrCursor(const wxBitmap& bmp,
1629 bool iconWanted,
1630 int hotSpotX,
1631 int hotSpotY)
1632{
1633 if ( !bmp.Ok() )
1634 {
1635 // we can't create an icon/cursor form nothing
1636 return 0;
1637 }
1638
0806fc20
RD
1639 wxMask* mask;
1640 wxBitmap newbmp;
1641 if ( bmp.HasAlpha() )
1642 {
1643 // Convert alpha to a mask. NOTE: It would be better to actually put
1644 // the alpha into the icon instead of making a mask, but I don't have
1645 // time to figure that out today.
1646 wxImage img = bmp.ConvertToImage();
1647 img.ConvertAlphaToMask();
1648 newbmp = wxBitmap(img);
1649 mask = newbmp.GetMask();
1650 }
1651 else
1652 {
1653 mask = bmp.GetMask();
1654 }
d0ee33f5 1655
211b54b1
VZ
1656 if ( !mask )
1657 {
1658 // we must have a mask for an icon, so even if it's probably incorrect,
1659 // do create it (grey is the "standard" transparent colour)
1660 mask = new wxMask(bmp, *wxLIGHT_GREY);
1661 }
1662
1663 ICONINFO iconInfo;
4104d2b3 1664 wxZeroMemory(iconInfo);
211b54b1
VZ
1665 iconInfo.fIcon = iconWanted; // do we want an icon or a cursor?
1666 if ( !iconWanted )
1667 {
1668 iconInfo.xHotspot = hotSpotX;
1669 iconInfo.yHotspot = hotSpotY;
1670 }
1671
1672 iconInfo.hbmMask = wxInvertMask((HBITMAP)mask->GetMaskBitmap());
1673 iconInfo.hbmColor = GetHbitmapOf(bmp);
1674
1675 // black out the transparent area to preserve background colour, because
1676 // Windows blits the original bitmap using SRCINVERT (XOR) after applying
1677 // the mask to the dest rect.
1678 {
1679 MemoryHDC dcSrc, dcDst;
1680 SelectInHDC selectMask(dcSrc, (HBITMAP)mask->GetMaskBitmap()),
1681 selectBitmap(dcDst, iconInfo.hbmColor);
1682
1683 if ( !::BitBlt(dcDst, 0, 0, bmp.GetWidth(), bmp.GetHeight(),
1684 dcSrc, 0, 0, SRCAND) )
1685 {
1686 wxLogLastError(_T("BitBlt"));
1687 }
1688 }
1689
1690 HICON hicon = ::CreateIconIndirect(&iconInfo);
1691
0806fc20 1692 if ( !bmp.GetMask() && !bmp.HasAlpha() )
211b54b1
VZ
1693 {
1694 // we created the mask, now delete it
1695 delete mask;
1696 }
1697
1698 // delete the inverted mask bitmap we created as well
1699 ::DeleteObject(iconInfo.hbmMask);
1700
1701 return hicon;
1702}
1703
1704HICON wxBitmapToHICON(const wxBitmap& bmp)
1705{
fcf90ee1 1706 return wxBitmapToIconOrCursor(bmp, true, 0, 0);
211b54b1
VZ
1707}
1708
1709HCURSOR wxBitmapToHCURSOR(const wxBitmap& bmp, int hotSpotX, int hotSpotY)
1710{
fcf90ee1 1711 return (HCURSOR)wxBitmapToIconOrCursor(bmp, false, hotSpotX, hotSpotY);
211b54b1
VZ
1712}
1713
1714HBITMAP wxInvertMask(HBITMAP hbmpMask, int w, int h)
4b7f2165 1715{
04ef50df 1716#ifndef __WXMICROWIN__
4b7f2165
VZ
1717 wxCHECK_MSG( hbmpMask, 0, _T("invalid bitmap in wxInvertMask") );
1718
1719 // get width/height from the bitmap if not given
1720 if ( !w || !h )
1721 {
1722 BITMAP bm;
1723 ::GetObject(hbmpMask, sizeof(BITMAP), (LPVOID)&bm);
1724 w = bm.bmWidth;
1725 h = bm.bmHeight;
1726 }
1727
1728 HDC hdcSrc = ::CreateCompatibleDC(NULL);
1729 HDC hdcDst = ::CreateCompatibleDC(NULL);
1730 if ( !hdcSrc || !hdcDst )
1731 {
f6bcfd97 1732 wxLogLastError(wxT("CreateCompatibleDC"));
4b7f2165
VZ
1733 }
1734
1735 HBITMAP hbmpInvMask = ::CreateBitmap(w, h, 1, 1, 0);
1736 if ( !hbmpInvMask )
1737 {
f6bcfd97 1738 wxLogLastError(wxT("CreateBitmap"));
4b7f2165
VZ
1739 }
1740
e7222d65
JS
1741 HGDIOBJ srcTmp = ::SelectObject(hdcSrc, hbmpMask);
1742 HGDIOBJ dstTmp = ::SelectObject(hdcDst, hbmpInvMask);
4b7f2165
VZ
1743 if ( !::BitBlt(hdcDst, 0, 0, w, h,
1744 hdcSrc, 0, 0,
1745 NOTSRCCOPY) )
1746 {
f6bcfd97 1747 wxLogLastError(wxT("BitBlt"));
4b7f2165 1748 }
7b46ecac 1749
e7222d65
JS
1750 // Deselect objects
1751 SelectObject(hdcSrc,srcTmp);
1752 SelectObject(hdcDst,dstTmp);
fcf90ee1 1753
4b7f2165
VZ
1754 ::DeleteDC(hdcSrc);
1755 ::DeleteDC(hdcDst);
1756
1757 return hbmpInvMask;
04ef50df
JS
1758#else
1759 return 0;
1760#endif
4b7f2165 1761}