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