]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/bitmap.cpp
*** empty log message ***
[wxWidgets.git] / src / msw / bitmap.cpp
... / ...
CommitLineData
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 and Markus Holzem
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
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#include "wx/msw/dib.h"
47#include "wx/image.h"
48
49// ----------------------------------------------------------------------------
50// macros
51// ----------------------------------------------------------------------------
52
53 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxGDIObject)
54 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
55
56 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler, wxObject)
57
58// ============================================================================
59// implementation
60// ============================================================================
61
62// ----------------------------------------------------------------------------
63// wxBitmapRefData
64// ----------------------------------------------------------------------------
65
66wxBitmapRefData::wxBitmapRefData()
67{
68 m_quality = 0;
69 m_selectedInto = NULL;
70 m_numColors = 0;
71 m_bitmapMask = NULL;
72 m_hBitmap = (WXHBITMAP) NULL;
73}
74
75void wxBitmapRefData::Free()
76{
77 wxASSERT_MSG( !m_selectedInto,
78 wxT("deleting bitmap still selected into wxMemoryDC") );
79
80 if ( m_hBitmap)
81 {
82 if ( !::DeleteObject((HBITMAP)m_hBitmap) )
83 {
84 wxLogLastError("DeleteObject(hbitmap)");
85 }
86 }
87
88 delete m_bitmapMask;
89 m_bitmapMask = NULL;
90}
91
92// ----------------------------------------------------------------------------
93// wxBitmap creation
94// ----------------------------------------------------------------------------
95
96// this function should be called from all wxBitmap ctors
97void wxBitmap::Init()
98{
99 // m_refData = NULL; done in the base class ctor
100
101 if ( wxTheBitmapList )
102 wxTheBitmapList->AddBitmap(this);
103}
104
105#ifdef __WIN32__
106
107bool wxBitmap::CopyFromIconOrCursor(const wxGDIImage& icon)
108{
109 // it may be either HICON or HCURSOR
110 HICON hicon = (HICON)icon.GetHandle();
111
112 ICONINFO iconInfo;
113 if ( !::GetIconInfo(hicon, &iconInfo) )
114 {
115 wxLogLastError("GetIconInfo");
116
117 return FALSE;
118 }
119
120 wxBitmapRefData *refData = new wxBitmapRefData;
121 m_refData = refData;
122
123 int w = icon.GetWidth(),
124 h = icon.GetHeight();
125
126 refData->m_width = w;
127 refData->m_height = h;
128 refData->m_depth = wxDisplayDepth();
129
130 refData->m_hBitmap = (WXHBITMAP)iconInfo.hbmColor;
131
132 // the mask returned by GetIconInfo() is inversed compared to the usual
133 // wxWin convention
134 HBITMAP hbmpMask = ::CreateBitmap(w, h, 1, 1, 0);
135
136 // the icons mask is opposite to the usual wxWin convention
137 HDC dcSrc = ::CreateCompatibleDC(NULL);
138 HDC dcDst = ::CreateCompatibleDC(NULL);
139 (void)SelectObject(dcSrc, iconInfo.hbmMask);
140 (void)SelectObject(dcDst, hbmpMask);
141
142 HBRUSH brush = ::CreateSolidBrush(RGB(255, 255, 255));
143 RECT rect = { 0, 0, w, h };
144 FillRect(dcDst, &rect, brush);
145
146 BitBlt(dcDst, 0, 0, w, h, dcSrc, 0, 0, SRCINVERT);
147
148 SelectObject(dcDst, NULL);
149 SelectObject(dcSrc, NULL);
150 DeleteDC(dcDst);
151 DeleteDC(dcSrc);
152
153 refData->m_bitmapMask = new wxMask((WXHBITMAP)hbmpMask);
154
155#if WXWIN_COMPATIBILITY_2
156 refData->m_ok = TRUE;
157#endif // WXWIN_COMPATIBILITY_2
158
159 return TRUE;
160}
161
162#endif // Win32
163
164bool wxBitmap::CopyFromCursor(const wxCursor& cursor)
165{
166 UnRef();
167
168 if ( !cursor.Ok() )
169 return FALSE;
170
171#ifdef __WIN16__
172 wxFAIL_MSG( _T("don't know how to convert cursor to bitmap") );
173
174 return FALSE;
175#else
176 return CopyFromIconOrCursor(cursor);
177#endif // Win16
178}
179
180bool wxBitmap::CopyFromIcon(const wxIcon& icon)
181{
182 UnRef();
183
184 if ( !icon.Ok() )
185 return FALSE;
186
187 // GetIconInfo() doesn't exist under Win16 and I don't know any other way
188 // to create a bitmap from icon there - but using this way we won't have
189 // the mask (FIXME)
190#ifdef __WIN16__
191 int width = icon.GetWidth(),
192 height = icon.GetHeight();
193
194 // copy the icon to the bitmap
195 ScreenHDC hdcScreen;
196 HDC hdc = ::CreateCompatibleDC(hdcScreen);
197 HBITMAP hbitmap = ::CreateCompatibleBitmap(hdcScreen, width, height);
198 HBITMAP hbmpOld = (HBITMAP)::SelectObject(hdc, hbitmap);
199
200 ::DrawIcon(hdc, 0, 0, GetHiconOf(icon));
201
202 ::SelectObject(hdc, hbmpOld);
203 ::DeleteDC(hdc);
204
205 wxBitmapRefData *refData = new wxBitmapRefData;
206 m_refData = refData;
207
208 refData->m_width = width;
209 refData->m_height = height;
210 refData->m_depth = wxDisplayDepth();
211
212 refData->m_hBitmap = (WXHBITMAP)hbitmap;
213
214#if WXWIN_COMPATIBILITY_2
215 refData->m_ok = TRUE;
216#endif // WXWIN_COMPATIBILITY_2
217
218 return TRUE;
219#else // Win32
220 return CopyFromIconOrCursor(icon);
221#endif // Win16/Win32
222}
223
224wxBitmap::~wxBitmap()
225{
226 if (wxTheBitmapList)
227 wxTheBitmapList->DeleteObject(this);
228}
229
230wxBitmap::wxBitmap(const char bits[], int the_width, int the_height, int no_bits)
231{
232 Init();
233
234 wxBitmapRefData *refData = new wxBitmapRefData;
235 m_refData = refData;
236
237 refData->m_width = the_width;
238 refData->m_height = the_height;
239 refData->m_depth = no_bits;
240 refData->m_numColors = 0;
241 refData->m_selectedInto = NULL;
242
243 HBITMAP hbmp = ::CreateBitmap(the_width, the_height, 1, no_bits, bits);
244 if ( !hbmp )
245 {
246 wxLogLastError("CreateBitmap");
247 }
248
249 SetHBITMAP((WXHBITMAP)hbmp);
250}
251
252// Create from XPM data
253wxBitmap::wxBitmap(char **data, wxControl *WXUNUSED(anItem))
254{
255 Init();
256
257 (void)Create((void *)data, wxBITMAP_TYPE_XPM_DATA, 0, 0, 0);
258}
259
260wxBitmap::wxBitmap(int w, int h, int d)
261{
262 Init();
263
264 (void)Create(w, h, d);
265}
266
267wxBitmap::wxBitmap(void *data, long type, int width, int height, int depth)
268{
269 Init();
270
271 (void)Create(data, type, width, height, depth);
272}
273
274wxBitmap::wxBitmap(const wxString& filename, long type)
275{
276 Init();
277
278 LoadFile(filename, (int)type);
279}
280
281bool wxBitmap::Create(int w, int h, int d)
282{
283 UnRef();
284
285 m_refData = new wxBitmapRefData;
286
287 GetBitmapData()->m_width = w;
288 GetBitmapData()->m_height = h;
289 GetBitmapData()->m_depth = d;
290
291 HBITMAP hbmp;
292
293 if ( d > 0 )
294 {
295 hbmp = ::CreateBitmap(w, h, 1, d, NULL);
296 if ( !hbmp )
297 {
298 wxLogLastError("CreateBitmap");
299 }
300 }
301 else
302 {
303 ScreenHDC dc;
304 hbmp = ::CreateCompatibleBitmap(dc, w, h);
305 if ( !hbmp )
306 {
307 wxLogLastError("CreateCompatibleBitmap");
308 }
309
310 GetBitmapData()->m_depth = wxDisplayDepth();
311 }
312
313 SetHBITMAP((WXHBITMAP)hbmp);
314
315#if WXWIN_COMPATIBILITY_2
316 GetBitmapData()->m_ok = hbmp != 0;
317#endif // WXWIN_COMPATIBILITY_2
318
319 return Ok();
320}
321
322bool wxBitmap::LoadFile(const wxString& filename, long type)
323{
324 UnRef();
325
326 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
327
328 if ( handler )
329 {
330 m_refData = new wxBitmapRefData;
331
332 return handler->LoadFile(this, filename, type, -1, -1);
333 }
334 else
335 {
336 wxImage image;
337 if ( !image.LoadFile( filename, type ) || !image.Ok() )
338 return FALSE;
339
340 *this = image.ConvertToBitmap();
341
342 return TRUE;
343 }
344}
345
346bool wxBitmap::Create(void *data, long type, int width, int height, int depth)
347{
348 UnRef();
349
350 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
351
352 if ( !handler )
353 {
354 wxLogDebug(wxT("Failed to create bitmap: no bitmap handler for "
355 "type %d defined."), type);
356
357 return FALSE;
358 }
359
360 m_refData = new wxBitmapRefData;
361
362 return handler->Create(this, data, type, width, height, depth);
363}
364
365bool wxBitmap::SaveFile(const wxString& filename, int type, const wxPalette *palette)
366{
367 wxBitmapHandler *handler = wxDynamicCast(FindHandler(type), wxBitmapHandler);
368
369 if ( handler )
370 {
371 return handler->SaveFile(this, filename, type, palette);
372 }
373 else
374 {
375 // FIXME what about palette? shouldn't we use it?
376 wxImage image( *this );
377 if (!image.Ok())
378 return FALSE;
379
380 return image.SaveFile( filename, type );
381 }
382}
383
384// ----------------------------------------------------------------------------
385// wxBitmap accessors
386// ----------------------------------------------------------------------------
387
388void wxBitmap::SetQuality(int q)
389{
390 EnsureHasData();
391
392 GetBitmapData()->m_quality = q;
393}
394
395#if WXWIN_COMPATIBILITY_2
396void wxBitmap::SetOk(bool isOk)
397{
398 EnsureHasData();
399
400 GetBitmapData()->m_ok = isOk;
401}
402#endif // WXWIN_COMPATIBILITY_2
403
404void wxBitmap::SetPalette(const wxPalette& palette)
405{
406 EnsureHasData();
407
408 GetBitmapData()->m_bitmapPalette = palette;
409}
410
411void wxBitmap::SetMask(wxMask *mask)
412{
413 EnsureHasData();
414
415 GetBitmapData()->m_bitmapMask = mask;
416}
417
418// Creates a bitmap that matches the device context, from
419// an arbitray bitmap. At present, the original bitmap must have an
420// associated palette. TODO: use a default palette if no palette exists.
421// Contributed by Frederic Villeneuve <frederic.villeneuve@natinst.com>
422wxBitmap wxBitmap::GetBitmapForDC(wxDC& dc) const
423{
424 wxMemoryDC memDC;
425 wxBitmap tmpBitmap(this->GetWidth(), this->GetHeight(), dc.GetDepth());
426 HPALETTE hPal = (HPALETTE) NULL;
427 LPBITMAPINFO lpDib;
428 void *lpBits = (void*) NULL;
429
430 if( GetPalette() && GetPalette()->Ok() )
431 {
432 tmpBitmap.SetPalette(*GetPalette());
433 memDC.SelectObject(tmpBitmap);
434 memDC.SetPalette(*GetPalette());
435 hPal = (HPALETTE)GetPalette()->GetHPALETTE();
436 }
437 else
438 {
439 hPal = (HPALETTE) ::GetStockObject(DEFAULT_PALETTE);
440 wxPalette palette;
441 palette.SetHPALETTE( (WXHPALETTE)hPal );
442 tmpBitmap.SetPalette( palette );
443 memDC.SelectObject(tmpBitmap);
444 memDC.SetPalette( palette );
445 }
446
447 // set the height negative because in a DIB the order of the lines is
448 // reversed
449 if ( !wxCreateDIB(GetWidth(), -GetHeight(), GetDepth(), hPal, &lpDib) )
450 {
451 return wxNullBitmap;
452 }
453
454 lpBits = malloc(lpDib->bmiHeader.biSizeImage);
455
456 ::GetBitmapBits(GetHbitmap(), lpDib->bmiHeader.biSizeImage, lpBits);
457
458 ::SetDIBitsToDevice(GetHdcOf(memDC), 0, 0,
459 GetWidth(), GetHeight(),
460 0, 0, 0, GetHeight(),
461 lpBits, lpDib, DIB_RGB_COLORS);
462
463 free(lpBits);
464
465 wxFreeDIB(lpDib);
466
467 return tmpBitmap;
468}
469
470// ----------------------------------------------------------------------------
471// wxMask
472// ----------------------------------------------------------------------------
473
474wxMask::wxMask()
475{
476 m_maskBitmap = 0;
477}
478
479// Construct a mask from a bitmap and a colour indicating
480// the transparent area
481wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
482{
483 m_maskBitmap = 0;
484 Create(bitmap, colour);
485}
486
487// Construct a mask from a bitmap and a palette index indicating
488// the transparent area
489wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
490{
491 m_maskBitmap = 0;
492 Create(bitmap, paletteIndex);
493}
494
495// Construct a mask from a mono bitmap (copies the bitmap).
496wxMask::wxMask(const wxBitmap& bitmap)
497{
498 m_maskBitmap = 0;
499 Create(bitmap);
500}
501
502wxMask::~wxMask()
503{
504 if ( m_maskBitmap )
505 ::DeleteObject((HBITMAP) m_maskBitmap);
506}
507
508// Create a mask from a mono bitmap (copies the bitmap).
509bool wxMask::Create(const wxBitmap& bitmap)
510{
511 if ( m_maskBitmap )
512 {
513 ::DeleteObject((HBITMAP) m_maskBitmap);
514 m_maskBitmap = 0;
515 }
516 if (!bitmap.Ok() || bitmap.GetDepth() != 1)
517 {
518 return FALSE;
519 }
520 m_maskBitmap = (WXHBITMAP) CreateBitmap(
521 bitmap.GetWidth(),
522 bitmap.GetHeight(),
523 1, 1, 0
524 );
525 HDC srcDC = CreateCompatibleDC(0);
526 SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP());
527 HDC destDC = CreateCompatibleDC(0);
528 SelectObject(destDC, (HBITMAP) m_maskBitmap);
529 BitBlt(destDC, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), srcDC, 0, 0, SRCCOPY);
530 SelectObject(srcDC, 0);
531 DeleteDC(srcDC);
532 SelectObject(destDC, 0);
533 DeleteDC(destDC);
534 return TRUE;
535}
536
537// Create a mask from a bitmap and a palette index indicating
538// the transparent area
539bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
540{
541 if ( m_maskBitmap )
542 {
543 ::DeleteObject((HBITMAP) m_maskBitmap);
544 m_maskBitmap = 0;
545 }
546 if (bitmap.Ok() && bitmap.GetPalette()->Ok())
547 {
548 unsigned char red, green, blue;
549 if (bitmap.GetPalette()->GetRGB(paletteIndex, &red, &green, &blue))
550 {
551 wxColour transparentColour(red, green, blue);
552 return Create(bitmap, transparentColour);
553 }
554 }
555 return FALSE;
556}
557
558// Create a mask from a bitmap and a colour indicating
559// the transparent area
560bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
561{
562 if ( m_maskBitmap )
563 {
564 ::DeleteObject((HBITMAP) m_maskBitmap);
565 m_maskBitmap = 0;
566 }
567 if (!bitmap.Ok())
568 {
569 return FALSE;
570 }
571
572 // scan the bitmap for the transparent colour and set
573 // the corresponding pixels in the mask to BLACK and
574 // the rest to WHITE
575 COLORREF maskColour = RGB(colour.Red(), colour.Green(), colour.Blue());
576 m_maskBitmap = (WXHBITMAP) ::CreateBitmap(
577 bitmap.GetWidth(),
578 bitmap.GetHeight(),
579 1, 1, 0
580 );
581 HDC srcDC = ::CreateCompatibleDC(0);
582 ::SelectObject(srcDC, (HBITMAP) bitmap.GetHBITMAP());
583 HDC destDC = ::CreateCompatibleDC(0);
584 ::SelectObject(destDC, (HBITMAP) m_maskBitmap);
585
586 // this is not very efficient, but I can't think
587 // of a better way of doing it
588 for (int w = 0; w < bitmap.GetWidth(); w++)
589 {
590 for (int h = 0; h < bitmap.GetHeight(); h++)
591 {
592 COLORREF col = GetPixel(srcDC, w, h);
593 if (col == maskColour)
594 {
595 ::SetPixel(destDC, w, h, RGB(0, 0, 0));
596 }
597 else
598 {
599 ::SetPixel(destDC, w, h, RGB(255, 255, 255));
600 }
601 }
602 }
603 ::SelectObject(srcDC, 0);
604 ::DeleteDC(srcDC);
605 ::SelectObject(destDC, 0);
606 ::DeleteDC(destDC);
607 return TRUE;
608}
609
610// ----------------------------------------------------------------------------
611// wxBitmapHandler
612// ----------------------------------------------------------------------------
613
614bool wxBitmapHandler::Create(wxGDIImage *image,
615 void *data,
616 long flags,
617 int width, int height, int depth)
618{
619 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
620
621 return bitmap ? Create(bitmap, data, width, height, depth) : FALSE;
622}
623
624bool wxBitmapHandler::Load(wxGDIImage *image,
625 const wxString& name,
626 long flags,
627 int width, int height)
628{
629 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
630
631 return bitmap ? LoadFile(bitmap, name, flags, width, height) : FALSE;
632}
633
634bool wxBitmapHandler::Save(wxGDIImage *image,
635 const wxString& name,
636 int type)
637{
638 wxBitmap *bitmap = wxDynamicCast(image, wxBitmap);
639
640 return bitmap ? SaveFile(bitmap, name, type) : FALSE;
641}
642
643bool wxBitmapHandler::Create(wxBitmap *WXUNUSED(bitmap),
644 void *WXUNUSED(data),
645 long WXUNUSED(type),
646 int WXUNUSED(width),
647 int WXUNUSED(height),
648 int WXUNUSED(depth))
649{
650 return FALSE;
651}
652
653bool wxBitmapHandler::LoadFile(wxBitmap *WXUNUSED(bitmap),
654 const wxString& WXUNUSED(name),
655 long WXUNUSED(type),
656 int WXUNUSED(desiredWidth),
657 int WXUNUSED(desiredHeight))
658{
659 return FALSE;
660}
661
662bool wxBitmapHandler::SaveFile(wxBitmap *WXUNUSED(bitmap),
663 const wxString& WXUNUSED(name),
664 int WXUNUSED(type),
665 const wxPalette *WXUNUSED(palette))
666{
667 return FALSE;
668}
669
670// ----------------------------------------------------------------------------
671// DIB functions
672// ----------------------------------------------------------------------------
673
674bool wxCreateDIB(long xSize, long ySize, long bitsPerPixel,
675 HPALETTE hPal, LPBITMAPINFO* lpDIBHeader)
676{
677 unsigned long i, headerSize;
678 LPBITMAPINFO lpDIBheader = NULL;
679 LPPALETTEENTRY lpPe = NULL;
680
681
682 // Allocate space for a DIB header
683 headerSize = (sizeof(BITMAPINFOHEADER) + (256 * sizeof(PALETTEENTRY)));
684 lpDIBheader = (BITMAPINFO *) malloc(headerSize);
685 lpPe = (PALETTEENTRY *)((BYTE*)lpDIBheader + sizeof(BITMAPINFOHEADER));
686
687 GetPaletteEntries(hPal, 0, 256, lpPe);
688
689 memset(lpDIBheader, 0x00, sizeof(BITMAPINFOHEADER));
690
691 // Fill in the static parts of the DIB header
692 lpDIBheader->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
693 lpDIBheader->bmiHeader.biWidth = xSize;
694 lpDIBheader->bmiHeader.biHeight = ySize;
695 lpDIBheader->bmiHeader.biPlanes = 1;
696
697 // this value must be 1, 4, 8 or 24 so PixelDepth can only be
698 lpDIBheader->bmiHeader.biBitCount = (WORD)(bitsPerPixel);
699 lpDIBheader->bmiHeader.biCompression = BI_RGB;
700 lpDIBheader->bmiHeader.biSizeImage = xSize * abs(ySize) * bitsPerPixel >> 3;
701 lpDIBheader->bmiHeader.biClrUsed = 256;
702
703
704 // Initialize the DIB palette
705 for (i = 0; i < 256; i++) {
706 lpDIBheader->bmiColors[i].rgbReserved = lpPe[i].peFlags;
707 lpDIBheader->bmiColors[i].rgbRed = lpPe[i].peRed;
708 lpDIBheader->bmiColors[i].rgbGreen = lpPe[i].peGreen;
709 lpDIBheader->bmiColors[i].rgbBlue = lpPe[i].peBlue;
710 }
711
712 *lpDIBHeader = lpDIBheader;
713
714 return TRUE;
715}
716
717void wxFreeDIB(LPBITMAPINFO lpDIBHeader)
718{
719 free(lpDIBHeader);
720}
721
722