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