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