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