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