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