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