]>
Commit | Line | Data |
---|---|---|
0d0512bd VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: msw/gdiimage.cpp | |
3 | // Purpose: wxGDIImage implementation | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 20.11.99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
6c9a19aa | 9 | // Licence: wxWindows licence |
0d0512bd VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
0d0512bd VZ |
21 | #pragma implementation "gdiimage.h" |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/string.h" | |
f94dfb38 | 33 | #include "wx/log.h" |
0d0512bd VZ |
34 | #endif // WX_PRECOMP |
35 | ||
e42a6c18 VZ |
36 | #include "wx/msw/private.h" |
37 | ||
0d0512bd VZ |
38 | #include "wx/app.h" |
39 | ||
f66f0fd7 | 40 | #include "wx/bitmap.h" |
474df218 | 41 | #include "wx/msw/gdiimage.h" |
4676948b JS |
42 | |
43 | #if wxUSE_WXDIB | |
474df218 | 44 | #include "wx/msw/dib.h" |
4676948b JS |
45 | #endif |
46 | ||
47 | #ifdef __WXWINCE__ | |
48 | #include <winreg.h> | |
49 | #include <shellapi.h> | |
50 | #endif | |
f66f0fd7 | 51 | |
419430a0 JS |
52 | #include "wx/file.h" |
53 | ||
d162a7ee VZ |
54 | #include "wx/listimpl.cpp" |
55 | WX_DEFINE_LIST(wxGDIImageHandlerList); | |
56 | ||
419430a0 JS |
57 | // ---------------------------------------------------------------------------- |
58 | // auxiliary functions | |
59 | // ---------------------------------------------------------------------------- | |
60 | ||
61 | #ifdef __WXWINCE__ | |
62 | // Used in wxBMPFileHandler::LoadFile | |
63 | HBITMAP wxLoadBMP(const wxString& filename) ; | |
64 | #endif | |
65 | ||
0d0512bd VZ |
66 | // ---------------------------------------------------------------------------- |
67 | // private classes | |
68 | // ---------------------------------------------------------------------------- | |
69 | ||
04ef50df JS |
70 | #ifndef __WXMICROWIN__ |
71 | ||
0d0512bd VZ |
72 | // all image handlers are declared/defined in this file because the outside |
73 | // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids) | |
74 | ||
75 | class WXDLLEXPORT wxBMPFileHandler : public wxBitmapHandler | |
76 | { | |
77 | public: | |
78 | wxBMPFileHandler() : wxBitmapHandler(_T("Windows bitmap file"), _T("bmp"), | |
79 | wxBITMAP_TYPE_BMP) | |
80 | { | |
81 | } | |
82 | ||
83 | virtual bool LoadFile(wxBitmap *bitmap, | |
84 | const wxString& name, long flags, | |
85 | int desiredWidth, int desiredHeight); | |
86 | virtual bool SaveFile(wxBitmap *bitmap, | |
87 | const wxString& name, int type, | |
88 | const wxPalette *palette = NULL); | |
89 | ||
90 | private: | |
91 | DECLARE_DYNAMIC_CLASS(wxBMPFileHandler) | |
92 | }; | |
93 | ||
94 | class WXDLLEXPORT wxBMPResourceHandler: public wxBitmapHandler | |
95 | { | |
96 | public: | |
97 | wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"), | |
98 | wxEmptyString, | |
99 | wxBITMAP_TYPE_BMP_RESOURCE) | |
100 | { | |
101 | } | |
102 | ||
103 | virtual bool LoadFile(wxBitmap *bitmap, | |
104 | const wxString& name, long flags, | |
105 | int desiredWidth, int desiredHeight); | |
106 | ||
107 | private: | |
108 | DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler) | |
109 | }; | |
110 | ||
111 | class WXDLLEXPORT wxIconHandler : public wxGDIImageHandler | |
112 | { | |
113 | public: | |
114 | wxIconHandler(const wxString& name, const wxString& ext, long type) | |
115 | : wxGDIImageHandler(name, ext, type) | |
116 | { | |
117 | } | |
118 | ||
119 | // creating and saving icons is not supported | |
120 | virtual bool Create(wxGDIImage *WXUNUSED(image), | |
121 | void *WXUNUSED(data), | |
122 | long WXUNUSED(flags), | |
123 | int WXUNUSED(width), | |
124 | int WXUNUSED(height), | |
125 | int WXUNUSED(depth) = 1) | |
126 | { | |
4f8090e0 | 127 | return false; |
0d0512bd VZ |
128 | } |
129 | ||
130 | virtual bool Save(wxGDIImage *WXUNUSED(image), | |
131 | const wxString& WXUNUSED(name), | |
132 | int WXUNUSED(type)) | |
133 | { | |
4f8090e0 | 134 | return false; |
0d0512bd VZ |
135 | } |
136 | ||
137 | virtual bool Load(wxGDIImage *image, | |
138 | const wxString& name, | |
139 | long flags, | |
140 | int desiredWidth, int desiredHeight) | |
141 | { | |
142 | wxIcon *icon = wxDynamicCast(image, wxIcon); | |
4f8090e0 | 143 | wxCHECK_MSG( icon, false, _T("wxIconHandler only works with icons") ); |
0d0512bd VZ |
144 | |
145 | return LoadIcon(icon, name, flags, desiredWidth, desiredHeight); | |
146 | } | |
147 | ||
148 | protected: | |
149 | virtual bool LoadIcon(wxIcon *icon, | |
150 | const wxString& name, long flags, | |
151 | int desiredWidth = -1, int desiredHeight = -1) = 0; | |
152 | }; | |
153 | ||
154 | class WXDLLEXPORT wxICOFileHandler : public wxIconHandler | |
155 | { | |
156 | public: | |
157 | wxICOFileHandler() : wxIconHandler(_T("ICO icon file"), | |
158 | _T("ico"), | |
159 | wxBITMAP_TYPE_ICO) | |
160 | { | |
161 | } | |
162 | ||
163 | virtual bool LoadIcon(wxIcon *icon, | |
164 | const wxString& name, long flags, | |
165 | int desiredWidth = -1, int desiredHeight = -1); | |
166 | ||
167 | private: | |
168 | DECLARE_DYNAMIC_CLASS(wxICOFileHandler) | |
169 | }; | |
170 | ||
171 | class WXDLLEXPORT wxICOResourceHandler: public wxIconHandler | |
172 | { | |
173 | public: | |
174 | wxICOResourceHandler() : wxIconHandler(_T("ICO resource"), | |
175 | _T("ico"), | |
176 | wxBITMAP_TYPE_ICO_RESOURCE) | |
177 | { | |
178 | } | |
179 | ||
180 | virtual bool LoadIcon(wxIcon *icon, | |
181 | const wxString& name, long flags, | |
182 | int desiredWidth = -1, int desiredHeight = -1); | |
183 | ||
184 | private: | |
185 | DECLARE_DYNAMIC_CLASS(wxICOResourceHandler) | |
186 | }; | |
187 | ||
188 | // ---------------------------------------------------------------------------- | |
189 | // wxWin macros | |
190 | // ---------------------------------------------------------------------------- | |
191 | ||
621b3e21 VZ |
192 | IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler, wxBitmapHandler) |
193 | IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler, wxBitmapHandler) | |
194 | IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler, wxObject) | |
195 | IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler, wxObject) | |
0d0512bd VZ |
196 | |
197 | // ---------------------------------------------------------------------------- | |
198 | // private functions | |
199 | // ---------------------------------------------------------------------------- | |
200 | ||
04ef50df JS |
201 | #endif |
202 | // __MICROWIN__ | |
0d0512bd VZ |
203 | |
204 | // ============================================================================ | |
205 | // implementation | |
206 | // ============================================================================ | |
207 | ||
d162a7ee | 208 | wxGDIImageHandlerList wxGDIImage::ms_handlers; |
0d0512bd VZ |
209 | |
210 | // ---------------------------------------------------------------------------- | |
211 | // wxGDIImage functions forwarded to wxGDIImageRefData | |
212 | // ---------------------------------------------------------------------------- | |
213 | ||
214 | bool wxGDIImage::FreeResource(bool WXUNUSED(force)) | |
215 | { | |
216 | if ( !IsNull() ) | |
217 | { | |
218 | GetGDIImageData()->Free(); | |
219 | GetGDIImageData()->m_handle = 0; | |
220 | } | |
221 | ||
4f8090e0 | 222 | return true; |
0d0512bd VZ |
223 | } |
224 | ||
2b5f62a0 | 225 | WXHANDLE wxGDIImage::GetResourceHandle() const |
0d0512bd VZ |
226 | { |
227 | return GetHandle(); | |
228 | } | |
229 | ||
230 | // ---------------------------------------------------------------------------- | |
231 | // wxGDIImage handler stuff | |
232 | // ---------------------------------------------------------------------------- | |
233 | ||
234 | void wxGDIImage::AddHandler(wxGDIImageHandler *handler) | |
235 | { | |
236 | ms_handlers.Append(handler); | |
237 | } | |
238 | ||
239 | void wxGDIImage::InsertHandler(wxGDIImageHandler *handler) | |
240 | { | |
241 | ms_handlers.Insert(handler); | |
242 | } | |
243 | ||
244 | bool wxGDIImage::RemoveHandler(const wxString& name) | |
245 | { | |
246 | wxGDIImageHandler *handler = FindHandler(name); | |
247 | if ( handler ) | |
248 | { | |
249 | ms_handlers.DeleteObject(handler); | |
4f8090e0 | 250 | return true; |
0d0512bd VZ |
251 | } |
252 | else | |
4f8090e0 | 253 | return false; |
0d0512bd VZ |
254 | } |
255 | ||
256 | wxGDIImageHandler *wxGDIImage::FindHandler(const wxString& name) | |
257 | { | |
222ed1d6 | 258 | wxGDIImageHandlerList::compatibility_iterator node = ms_handlers.GetFirst(); |
0d0512bd VZ |
259 | while ( node ) |
260 | { | |
d162a7ee | 261 | wxGDIImageHandler *handler = node->GetData(); |
0d0512bd VZ |
262 | if ( handler->GetName() == name ) |
263 | return handler; | |
4f8090e0 | 264 | node = node->GetNext(); |
0d0512bd VZ |
265 | } |
266 | ||
267 | return NULL; | |
268 | } | |
269 | ||
270 | wxGDIImageHandler *wxGDIImage::FindHandler(const wxString& extension, | |
271 | long type) | |
272 | { | |
222ed1d6 | 273 | wxGDIImageHandlerList::compatibility_iterator node = ms_handlers.GetFirst(); |
0d0512bd VZ |
274 | while ( node ) |
275 | { | |
d162a7ee | 276 | wxGDIImageHandler *handler = node->GetData(); |
0d0512bd VZ |
277 | if ( (handler->GetExtension() = extension) && |
278 | (type == -1 || handler->GetType() == type) ) | |
279 | { | |
280 | return handler; | |
281 | } | |
282 | ||
4f8090e0 | 283 | node = node->GetNext(); |
0d0512bd VZ |
284 | } |
285 | return NULL; | |
286 | } | |
287 | ||
288 | wxGDIImageHandler *wxGDIImage::FindHandler(long type) | |
289 | { | |
222ed1d6 | 290 | wxGDIImageHandlerList::compatibility_iterator node = ms_handlers.GetFirst(); |
0d0512bd VZ |
291 | while ( node ) |
292 | { | |
d162a7ee | 293 | wxGDIImageHandler *handler = node->GetData(); |
0d0512bd VZ |
294 | if ( handler->GetType() == type ) |
295 | return handler; | |
296 | ||
4f8090e0 | 297 | node = node->GetNext(); |
0d0512bd VZ |
298 | } |
299 | ||
300 | return NULL; | |
301 | } | |
302 | ||
303 | void wxGDIImage::CleanUpHandlers() | |
304 | { | |
222ed1d6 | 305 | wxGDIImageHandlerList::compatibility_iterator node = ms_handlers.GetFirst(); |
0d0512bd VZ |
306 | while ( node ) |
307 | { | |
d162a7ee | 308 | wxGDIImageHandler *handler = node->GetData(); |
222ed1d6 | 309 | wxGDIImageHandlerList::compatibility_iterator next = node->GetNext(); |
0d0512bd | 310 | delete handler; |
222ed1d6 | 311 | ms_handlers.Erase( node ); |
0d0512bd VZ |
312 | node = next; |
313 | } | |
314 | } | |
315 | ||
316 | void wxGDIImage::InitStandardHandlers() | |
317 | { | |
04ef50df | 318 | #ifndef __WXMICROWIN__ |
0d0512bd VZ |
319 | AddHandler(new wxBMPResourceHandler); |
320 | AddHandler(new wxBMPFileHandler); | |
0d0512bd VZ |
321 | AddHandler(new wxICOResourceHandler); |
322 | AddHandler(new wxICOFileHandler); | |
04ef50df | 323 | #endif |
0d0512bd VZ |
324 | } |
325 | ||
04ef50df JS |
326 | #ifndef __WXMICROWIN__ |
327 | ||
0d0512bd VZ |
328 | // ---------------------------------------------------------------------------- |
329 | // wxBitmap handlers | |
330 | // ---------------------------------------------------------------------------- | |
331 | ||
332 | bool wxBMPResourceHandler::LoadFile(wxBitmap *bitmap, | |
333 | const wxString& name, long WXUNUSED(flags), | |
334 | int WXUNUSED(desiredWidth), | |
335 | int WXUNUSED(desiredHeight)) | |
336 | { | |
337 | // TODO: load colourmap. | |
338 | bitmap->SetHBITMAP((WXHBITMAP)::LoadBitmap(wxGetInstance(), name)); | |
339 | ||
8bbbae21 | 340 | if ( !bitmap->Ok() ) |
0d0512bd VZ |
341 | { |
342 | // it's probably not found | |
343 | wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."), | |
344 | name.c_str()); | |
8bbbae21 | 345 | |
4f8090e0 | 346 | return false; |
8bbbae21 VZ |
347 | } |
348 | ||
349 | BITMAP bm; | |
350 | if ( !::GetObject(GetHbitmapOf(*bitmap), sizeof(BITMAP), (LPSTR) &bm) ) | |
351 | { | |
352 | wxLogLastError(wxT("GetObject(HBITMAP)")); | |
0d0512bd VZ |
353 | } |
354 | ||
8bbbae21 VZ |
355 | bitmap->SetWidth(bm.bmWidth); |
356 | bitmap->SetHeight(bm.bmHeight); | |
357 | bitmap->SetDepth(bm.bmBitsPixel); | |
358 | ||
4f8090e0 | 359 | return true; |
0d0512bd VZ |
360 | } |
361 | ||
362 | bool wxBMPFileHandler::LoadFile(wxBitmap *bitmap, | |
363 | const wxString& name, long WXUNUSED(flags), | |
364 | int WXUNUSED(desiredWidth), | |
365 | int WXUNUSED(desiredHeight)) | |
366 | { | |
4676948b | 367 | #if wxUSE_WXDIB |
474df218 | 368 | wxCHECK_MSG( bitmap, false, _T("NULL bitmap in LoadFile") ); |
d275c7eb | 369 | |
474df218 | 370 | wxDIB dib(name); |
0d0512bd | 371 | |
474df218 | 372 | return dib.IsOk() && bitmap->CopyFromDIB(dib); |
4676948b | 373 | #else |
419430a0 JS |
374 | WXHBITMAP hBitmap = (WXHBITMAP)wxLoadBMP(name); |
375 | if(hBitmap) { | |
376 | bitmap->SetHBITMAP(hBitmap); | |
377 | return TRUE; | |
378 | } | |
4676948b JS |
379 | return FALSE; |
380 | #endif | |
0d0512bd VZ |
381 | } |
382 | ||
383 | bool wxBMPFileHandler::SaveFile(wxBitmap *bitmap, | |
384 | const wxString& name, | |
385 | int WXUNUSED(type), | |
2b254edf | 386 | const wxPalette * WXUNUSED(pal)) |
0d0512bd | 387 | { |
4676948b | 388 | #if wxUSE_WXDIB |
2b254edf VZ |
389 | wxCHECK_MSG( bitmap, false, _T("NULL bitmap in SaveFile") ); |
390 | ||
391 | wxDIB dib(*bitmap); | |
392 | ||
393 | return dib.Save(name); | |
4676948b JS |
394 | #else |
395 | return FALSE; | |
396 | #endif | |
0d0512bd VZ |
397 | } |
398 | ||
399 | // ---------------------------------------------------------------------------- | |
400 | // wxIcon handlers | |
401 | // ---------------------------------------------------------------------------- | |
402 | ||
403 | bool wxICOFileHandler::LoadIcon(wxIcon *icon, | |
404 | const wxString& name, | |
33ac7e6f | 405 | long WXUNUSED(flags), |
0d0512bd VZ |
406 | int desiredWidth, int desiredHeight) |
407 | { | |
0d0512bd VZ |
408 | icon->UnRef(); |
409 | ||
410 | // actual size | |
411 | wxSize size; | |
412 | ||
a4352768 VZ |
413 | HICON hicon = NULL; |
414 | ||
2dace059 VZ |
415 | // Parse the filename: it may be of the form "filename;n" in order to |
416 | // specify the nth icon in the file. | |
417 | // | |
418 | // For the moment, ignore the issue of possible semicolons in the | |
419 | // filename. | |
ff9aab3c | 420 | int iconIndex = 0; |
2dace059 | 421 | wxString nameReal(name); |
ff9aab3c | 422 | wxString strIconIndex = name.AfterLast(wxT(';')); |
b642d457 | 423 | if (strIconIndex != name) |
ff9aab3c JS |
424 | { |
425 | iconIndex = wxAtoi(strIconIndex); | |
2dace059 | 426 | nameReal = name.BeforeLast(wxT(';')); |
ff9aab3c JS |
427 | } |
428 | ||
2b5f62a0 VZ |
429 | #if 0 |
430 | // If we don't know what size icon we're looking for, | |
431 | // try to find out what's there. | |
432 | // Unfortunately this doesn't work, because ExtractIconEx | |
433 | // will scale the icon to the 'desired' size, even if that | |
434 | // size of icon isn't explicitly stored. So we would have | |
435 | // to parse the icon file outselves. | |
436 | if ( desiredWidth == -1 && | |
437 | desiredHeight == -1) | |
438 | { | |
439 | // Try loading a large icon first | |
440 | if ( ::ExtractIconEx(nameReal, iconIndex, &hicon, NULL, 1) == 1) | |
441 | { | |
442 | } | |
443 | // Then try loading a small icon | |
444 | else if ( ::ExtractIconEx(nameReal, iconIndex, NULL, &hicon, 1) == 1) | |
445 | { | |
446 | } | |
447 | } | |
448 | else | |
449 | #endif | |
4676948b | 450 | // were we asked for a large icon? |
a4352768 VZ |
451 | if ( desiredWidth == ::GetSystemMetrics(SM_CXICON) && |
452 | desiredHeight == ::GetSystemMetrics(SM_CYICON) ) | |
453 | { | |
ff9aab3c | 454 | // get the specified large icon from file |
2dace059 | 455 | if ( !::ExtractIconEx(nameReal, iconIndex, &hicon, NULL, 1) ) |
a4352768 VZ |
456 | { |
457 | // it is not an error, but it might still be useful to be informed | |
458 | // about it optionally | |
459 | wxLogTrace(_T("iconload"), | |
460 | _T("No large icons found in the file '%s'."), | |
461 | name.c_str()); | |
462 | } | |
463 | } | |
464 | else if ( desiredWidth == ::GetSystemMetrics(SM_CXSMICON) && | |
465 | desiredHeight == ::GetSystemMetrics(SM_CYSMICON) ) | |
466 | { | |
ff9aab3c | 467 | // get the specified small icon from file |
2dace059 | 468 | if ( !::ExtractIconEx(nameReal, iconIndex, NULL, &hicon, 1) ) |
a4352768 VZ |
469 | { |
470 | wxLogTrace(_T("iconload"), | |
471 | _T("No small icons found in the file '%s'."), | |
472 | name.c_str()); | |
473 | } | |
474 | } | |
475 | //else: not standard size, load below | |
476 | ||
4676948b | 477 | #ifndef __WXWINCE__ |
a4352768 VZ |
478 | if ( !hicon ) |
479 | { | |
d8f3f983 RD |
480 | // take any size icon from the file by index |
481 | hicon = ::ExtractIcon(wxGetInstance(), nameReal, iconIndex); | |
a4352768 | 482 | } |
4676948b | 483 | #endif |
a4352768 | 484 | |
0d0512bd VZ |
485 | if ( !hicon ) |
486 | { | |
487 | wxLogSysError(_T("Failed to load icon from the file '%s'"), | |
488 | name.c_str()); | |
489 | ||
4f8090e0 | 490 | return false; |
0d0512bd VZ |
491 | } |
492 | ||
6bad4c32 | 493 | size = wxGetHiconSize(hicon); |
0d0512bd VZ |
494 | |
495 | if ( (desiredWidth != -1 && desiredWidth != size.x) || | |
496 | (desiredHeight != -1 && desiredHeight != size.y) ) | |
497 | { | |
a4352768 | 498 | wxLogTrace(_T("iconload"), |
4f8090e0 | 499 | _T("Returning false from wxICOFileHandler::Load because of the size mismatch: actual (%d, %d), requested (%d, %d)"), |
a4352768 VZ |
500 | size.x, size.y, |
501 | desiredWidth, desiredHeight); | |
0d0512bd VZ |
502 | |
503 | ::DestroyIcon(hicon); | |
504 | ||
4f8090e0 | 505 | return false; |
0d0512bd VZ |
506 | } |
507 | ||
508 | icon->SetHICON((WXHICON)hicon); | |
509 | icon->SetSize(size.x, size.y); | |
510 | ||
511 | return icon->Ok(); | |
0d0512bd VZ |
512 | } |
513 | ||
514 | bool wxICOResourceHandler::LoadIcon(wxIcon *icon, | |
515 | const wxString& name, | |
33ac7e6f | 516 | long WXUNUSED(flags), |
0d0512bd VZ |
517 | int desiredWidth, int desiredHeight) |
518 | { | |
519 | HICON hicon; | |
520 | ||
9cb9fdb3 VZ |
521 | // do we need the icon of the specific size or would any icon do? |
522 | bool hasSize = desiredWidth != -1 || desiredHeight != -1; | |
523 | ||
524 | wxASSERT_MSG( !hasSize || (desiredWidth != -1 && desiredHeight != -1), | |
525 | _T("width and height should be either both -1 or not") ); | |
526 | ||
2dace059 VZ |
527 | // try to load the icon from this program first to allow overriding the |
528 | // standard icons (although why one would want to do it considering that | |
529 | // we already have wxApp::GetStdIcon() is unclear) | |
9990cb50 VZ |
530 | |
531 | // note that we can't just always call LoadImage() because it seems to do | |
532 | // some icon rescaling internally which results in very ugly 16x16 icons | |
9990cb50 | 533 | if ( hasSize ) |
0d0512bd | 534 | { |
9990cb50 VZ |
535 | hicon = (HICON)::LoadImage(wxGetInstance(), name, IMAGE_ICON, |
536 | desiredWidth, desiredHeight, | |
537 | LR_DEFAULTCOLOR); | |
0d0512bd | 538 | } |
9990cb50 | 539 | else |
9990cb50 VZ |
540 | { |
541 | hicon = ::LoadIcon(wxGetInstance(), name); | |
542 | } | |
fea2b62e | 543 | |
2dace059 | 544 | // next check if it's not a standard icon |
4676948b | 545 | #ifndef __WXWINCE__ |
9cb9fdb3 | 546 | if ( !hicon && !hasSize ) |
2dace059 VZ |
547 | { |
548 | static const struct | |
549 | { | |
550 | const wxChar *name; | |
551 | LPTSTR id; | |
552 | } stdIcons[] = | |
553 | { | |
554 | { wxT("wxICON_QUESTION"), IDI_QUESTION }, | |
555 | { wxT("wxICON_WARNING"), IDI_EXCLAMATION }, | |
556 | { wxT("wxICON_ERROR"), IDI_HAND }, | |
c2edb18a | 557 | { wxT("wxICON_INFORMATION"), IDI_ASTERISK }, |
2dace059 VZ |
558 | }; |
559 | ||
560 | for ( size_t nIcon = 0; !hicon && nIcon < WXSIZEOF(stdIcons); nIcon++ ) | |
561 | { | |
562 | if ( name == stdIcons[nIcon].name ) | |
563 | { | |
9cb9fdb3 | 564 | hicon = ::LoadIcon((HINSTANCE)NULL, stdIcons[nIcon].id); |
2dace059 VZ |
565 | } |
566 | } | |
567 | } | |
4676948b | 568 | #endif |
2dace059 | 569 | |
6bad4c32 | 570 | wxSize size = wxGetHiconSize(hicon); |
0d0512bd VZ |
571 | icon->SetSize(size.x, size.y); |
572 | ||
0d0512bd VZ |
573 | icon->SetHICON((WXHICON)hicon); |
574 | ||
575 | return icon->Ok(); | |
576 | } | |
577 | ||
578 | // ---------------------------------------------------------------------------- | |
579 | // private functions | |
580 | // ---------------------------------------------------------------------------- | |
581 | ||
6bad4c32 | 582 | wxSize wxGetHiconSize(HICON hicon) |
0d0512bd VZ |
583 | { |
584 | wxSize size(32, 32); // default | |
4676948b | 585 | #ifndef __WXWINCE__ |
0d0512bd VZ |
586 | if ( hicon && wxGetOsVersion() != wxWIN32S ) |
587 | { | |
588 | ICONINFO info; | |
589 | if ( !::GetIconInfo(hicon, &info) ) | |
590 | { | |
f6bcfd97 | 591 | wxLogLastError(wxT("GetIconInfo")); |
0d0512bd VZ |
592 | } |
593 | else | |
594 | { | |
595 | HBITMAP hbmp = info.hbmMask; | |
596 | if ( hbmp ) | |
597 | { | |
598 | BITMAP bm; | |
599 | if ( ::GetObject(hbmp, sizeof(BITMAP), (LPSTR) &bm) ) | |
600 | { | |
601 | size = wxSize(bm.bmWidth, bm.bmHeight); | |
602 | } | |
603 | ||
604 | ::DeleteObject(info.hbmMask); | |
605 | } | |
606 | if ( info.hbmColor ) | |
607 | ::DeleteObject(info.hbmColor); | |
608 | } | |
609 | } | |
4676948b | 610 | #endif |
0d0512bd VZ |
611 | return size; |
612 | } | |
474df218 VZ |
613 | |
614 | #endif // __WXMICROWIN__ | |
615 | ||
419430a0 JS |
616 | #ifdef __WXWINCE__ |
617 | // Used in wxBMPFileHandler::LoadFile | |
618 | HBITMAP wxLoadBMP(const wxString& filename) | |
619 | { | |
620 | wxFile file; | |
621 | if(!file.Open(filename)) | |
622 | return 0; | |
623 | ||
624 | // The first part of the file contains the file header. | |
625 | // This will tell us if it is a bitmap, how big the header is, and how big | |
626 | // the file is. The header size in the file header includes the color table. | |
627 | BITMAPFILEHEADER BmpFileHdr; | |
628 | BITMAPINFO *pBmpInfo = (BITMAPINFO*)malloc(sizeof(BITMAPINFO)+255*sizeof(RGBQUAD)); | |
629 | BYTE* pBits = 0; | |
630 | HBITMAP hBitmap = 0; | |
631 | ||
632 | if(file.Read(&BmpFileHdr, sizeof(BmpFileHdr))==sizeof(BmpFileHdr) | |
633 | && !strncmp((char*)&BmpFileHdr.bfType,"BM",2) | |
634 | && file.Read(pBmpInfo, sizeof(BITMAPINFOHEADER))==sizeof(BITMAPINFOHEADER) | |
635 | && pBmpInfo->bmiHeader.biSize == sizeof(BITMAPINFOHEADER)) { | |
636 | ||
637 | ||
638 | unsigned int nColors = pBmpInfo->bmiHeader.biClrUsed ? | |
639 | pBmpInfo->bmiHeader.biClrUsed : 1 << pBmpInfo->bmiHeader.biBitCount; | |
640 | if (nColors < 1 | |
641 | || file.Read(pBmpInfo->bmiColors, nColors * sizeof(RGBQUAD)) | |
642 | == (off_t)(nColors * sizeof(RGBQUAD))) { | |
643 | ||
644 | // So how big the bitmap surface is. | |
645 | int nBitsSize = BmpFileHdr.bfSize - BmpFileHdr.bfOffBits; | |
646 | ||
647 | // Allocate the memory for the bits and read the bits from the file. | |
648 | pBits = (BYTE*) malloc(nBitsSize*2); | |
649 | if (pBits) { | |
650 | // Seek to the bits in the file. | |
651 | file.Seek(BmpFileHdr.bfOffBits); | |
652 | ||
653 | // read the bits | |
654 | if(file.Read(pBits, nBitsSize)==nBitsSize) { | |
655 | // Everything went OK. | |
656 | pBmpInfo->bmiHeader.biSizeImage = nBitsSize; | |
657 | ||
658 | //HBITMAP hBitmap=SetBitmap((LPBITMAPINFO)pBmpInfo, pBits); | |
659 | DWORD dwBitmapInfoSize = sizeof(BITMAPINFO) + nColors*sizeof(RGBQUAD); | |
660 | ||
661 | // Create a DC which will be used to get DIB, then create DIBsection | |
662 | HDC hDC = ::GetDC(NULL); | |
663 | if (hDC) { | |
664 | LPVOID bits; | |
665 | hBitmap = CreateDIBSection(hDC, (const BITMAPINFO*) pBmpInfo, | |
666 | DIB_RGB_COLORS, &bits, NULL, 0); | |
667 | ReleaseDC(0,hDC); | |
668 | ||
669 | if (hBitmap) { | |
670 | DWORD dwImageSize = pBmpInfo->bmiHeader.biSizeImage; | |
671 | if (dwImageSize == 0) { | |
672 | int nBytesPerLine = pBmpInfo->bmiHeader.biWidth * pBmpInfo->bmiHeader.biBitCount; | |
673 | nBytesPerLine = ( (nBytesPerLine + 31) & (~31) ) / 8; | |
674 | dwImageSize = nBytesPerLine * pBmpInfo->bmiHeader.biHeight; | |
675 | } | |
676 | memcpy(bits, pBits, dwImageSize); | |
677 | } | |
678 | } | |
679 | } | |
680 | } | |
681 | } | |
682 | } | |
683 | ||
684 | if(pBmpInfo) | |
685 | free(pBmpInfo); | |
686 | if(pBits) | |
687 | free(pBits); | |
688 | ||
689 | return hBitmap; | |
690 | } | |
691 | #endif | |
692 |