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