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