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