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