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