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