1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/gdiimage.cpp
3 // Purpose: wxGDIImage implementation
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "gdiimage.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/string.h"
36 #include "wx/msw/private.h"
40 #include "wx/bitmap.h"
41 #include "wx/msw/gdiimage.h"
44 #include "wx/msw/dib.h"
54 #include "wx/listimpl.cpp"
55 WX_DEFINE_LIST(wxGDIImageHandlerList
);
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 #ifndef __WXMICROWIN__
63 // all image handlers are declared/defined in this file because the outside
64 // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids)
66 class WXDLLEXPORT wxBMPFileHandler
: public wxBitmapHandler
69 wxBMPFileHandler() : wxBitmapHandler(_T("Windows bitmap file"), _T("bmp"),
74 virtual bool LoadFile(wxBitmap
*bitmap
,
75 const wxString
& name
, long flags
,
76 int desiredWidth
, int desiredHeight
);
77 virtual bool SaveFile(wxBitmap
*bitmap
,
78 const wxString
& name
, int type
,
79 const wxPalette
*palette
= NULL
);
82 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
)
85 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
88 wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"),
90 wxBITMAP_TYPE_BMP_RESOURCE
)
94 virtual bool LoadFile(wxBitmap
*bitmap
,
95 const wxString
& name
, long flags
,
96 int desiredWidth
, int desiredHeight
);
99 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
102 class WXDLLEXPORT wxIconHandler
: public wxGDIImageHandler
105 wxIconHandler(const wxString
& name
, const wxString
& ext
, long type
)
106 : wxGDIImageHandler(name
, ext
, type
)
110 // creating and saving icons is not supported
111 virtual bool Create(wxGDIImage
*WXUNUSED(image
),
112 void *WXUNUSED(data
),
113 long WXUNUSED(flags
),
115 int WXUNUSED(height
),
116 int WXUNUSED(depth
) = 1)
121 virtual bool Save(wxGDIImage
*WXUNUSED(image
),
122 const wxString
& WXUNUSED(name
),
128 virtual bool Load(wxGDIImage
*image
,
129 const wxString
& name
,
131 int desiredWidth
, int desiredHeight
)
133 wxIcon
*icon
= wxDynamicCast(image
, wxIcon
);
134 wxCHECK_MSG( icon
, false, _T("wxIconHandler only works with icons") );
136 return LoadIcon(icon
, name
, flags
, desiredWidth
, desiredHeight
);
140 virtual bool LoadIcon(wxIcon
*icon
,
141 const wxString
& name
, long flags
,
142 int desiredWidth
= -1, int desiredHeight
= -1) = 0;
145 class WXDLLEXPORT wxICOFileHandler
: public wxIconHandler
148 wxICOFileHandler() : wxIconHandler(_T("ICO icon file"),
155 virtual bool LoadIcon(wxIcon
*icon
,
156 const wxString
& name
, long flags
,
157 int desiredWidth
= -1, int desiredHeight
= -1);
160 DECLARE_DYNAMIC_CLASS(wxICOFileHandler
)
163 class WXDLLEXPORT wxICOResourceHandler
: public wxIconHandler
166 wxICOResourceHandler() : wxIconHandler(_T("ICO resource"),
168 wxBITMAP_TYPE_ICO_RESOURCE
)
173 virtual bool LoadIcon(wxIcon
*icon
,
174 const wxString
& name
, long flags
,
175 int desiredWidth
= -1, int desiredHeight
= -1);
178 DECLARE_DYNAMIC_CLASS(wxICOResourceHandler
)
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
185 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
186 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
187 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler
, wxObject
)
188 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler
, wxObject
)
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
197 // ============================================================================
199 // ============================================================================
201 wxGDIImageHandlerList
wxGDIImage::ms_handlers
;
203 // ----------------------------------------------------------------------------
204 // wxGDIImage functions forwarded to wxGDIImageRefData
205 // ----------------------------------------------------------------------------
207 bool wxGDIImage::FreeResource(bool WXUNUSED(force
))
211 GetGDIImageData()->Free();
212 GetGDIImageData()->m_handle
= 0;
218 WXHANDLE
wxGDIImage::GetResourceHandle() const
223 // ----------------------------------------------------------------------------
224 // wxGDIImage handler stuff
225 // ----------------------------------------------------------------------------
227 void wxGDIImage::AddHandler(wxGDIImageHandler
*handler
)
229 ms_handlers
.Append(handler
);
232 void wxGDIImage::InsertHandler(wxGDIImageHandler
*handler
)
234 ms_handlers
.Insert(handler
);
237 bool wxGDIImage::RemoveHandler(const wxString
& name
)
239 wxGDIImageHandler
*handler
= FindHandler(name
);
242 ms_handlers
.DeleteObject(handler
);
249 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& name
)
251 wxGDIImageHandlerList::compatibility_iterator node
= ms_handlers
.GetFirst();
254 wxGDIImageHandler
*handler
= node
->GetData();
255 if ( handler
->GetName() == name
)
257 node
= node
->GetNext();
263 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& extension
,
266 wxGDIImageHandlerList::compatibility_iterator node
= ms_handlers
.GetFirst();
269 wxGDIImageHandler
*handler
= node
->GetData();
270 if ( (handler
->GetExtension() == extension
) &&
271 (type
== -1 || handler
->GetType() == type
) )
276 node
= node
->GetNext();
281 wxGDIImageHandler
*wxGDIImage::FindHandler(long type
)
283 wxGDIImageHandlerList::compatibility_iterator node
= ms_handlers
.GetFirst();
286 wxGDIImageHandler
*handler
= node
->GetData();
287 if ( handler
->GetType() == type
)
290 node
= node
->GetNext();
296 void wxGDIImage::CleanUpHandlers()
298 wxGDIImageHandlerList::compatibility_iterator node
= ms_handlers
.GetFirst();
301 wxGDIImageHandler
*handler
= node
->GetData();
302 wxGDIImageHandlerList::compatibility_iterator next
= node
->GetNext();
304 ms_handlers
.Erase( node
);
309 void wxGDIImage::InitStandardHandlers()
311 #ifndef __WXMICROWIN__
312 AddHandler(new wxBMPResourceHandler
);
313 AddHandler(new wxBMPFileHandler
);
314 AddHandler(new wxICOResourceHandler
);
315 AddHandler(new wxICOFileHandler
);
319 #ifndef __WXMICROWIN__
321 // ----------------------------------------------------------------------------
323 // ----------------------------------------------------------------------------
325 bool wxBMPResourceHandler::LoadFile(wxBitmap
*bitmap
,
326 const wxString
& name
, long WXUNUSED(flags
),
327 int WXUNUSED(desiredWidth
),
328 int WXUNUSED(desiredHeight
))
330 // TODO: load colourmap.
331 bitmap
->SetHBITMAP((WXHBITMAP
)::LoadBitmap(wxGetInstance(), name
));
335 // it's probably not found
336 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
343 if ( !::GetObject(GetHbitmapOf(*bitmap
), sizeof(BITMAP
), (LPSTR
) &bm
) )
345 wxLogLastError(wxT("GetObject(HBITMAP)"));
348 bitmap
->SetWidth(bm
.bmWidth
);
349 bitmap
->SetHeight(bm
.bmHeight
);
350 bitmap
->SetDepth(bm
.bmBitsPixel
);
352 // use 0xc0c0c0 as transparent colour by default
353 bitmap
->SetMask(new wxMask(*bitmap
, *wxLIGHT_GREY
));
358 bool wxBMPFileHandler::LoadFile(wxBitmap
*bitmap
,
359 const wxString
& name
, long WXUNUSED(flags
),
360 int WXUNUSED(desiredWidth
),
361 int WXUNUSED(desiredHeight
))
364 wxCHECK_MSG( bitmap
, false, _T("NULL bitmap in LoadFile") );
368 return dib
.IsOk() && bitmap
->CopyFromDIB(dib
);
374 bool wxBMPFileHandler::SaveFile(wxBitmap
*bitmap
,
375 const wxString
& name
,
377 const wxPalette
* WXUNUSED(pal
))
380 wxCHECK_MSG( bitmap
, false, _T("NULL bitmap in SaveFile") );
384 return dib
.Save(name
);
390 // ----------------------------------------------------------------------------
392 // ----------------------------------------------------------------------------
394 bool wxICOFileHandler::LoadIcon(wxIcon
*icon
,
395 const wxString
& name
,
396 long WXUNUSED(flags
),
397 int desiredWidth
, int desiredHeight
)
406 // Parse the filename: it may be of the form "filename;n" in order to
407 // specify the nth icon in the file.
409 // For the moment, ignore the issue of possible semicolons in the
412 wxString
nameReal(name
);
413 wxString strIconIndex
= name
.AfterLast(wxT(';'));
414 if (strIconIndex
!= name
)
416 iconIndex
= wxAtoi(strIconIndex
);
417 nameReal
= name
.BeforeLast(wxT(';'));
421 // If we don't know what size icon we're looking for,
422 // try to find out what's there.
423 // Unfortunately this doesn't work, because ExtractIconEx
424 // will scale the icon to the 'desired' size, even if that
425 // size of icon isn't explicitly stored. So we would have
426 // to parse the icon file outselves.
427 if ( desiredWidth
== -1 &&
430 // Try loading a large icon first
431 if ( ::ExtractIconEx(nameReal
, iconIndex
, &hicon
, NULL
, 1) == 1)
434 // Then try loading a small icon
435 else if ( ::ExtractIconEx(nameReal
, iconIndex
, NULL
, &hicon
, 1) == 1)
441 // were we asked for a large icon?
442 if ( desiredWidth
== ::GetSystemMetrics(SM_CXICON
) &&
443 desiredHeight
== ::GetSystemMetrics(SM_CYICON
) )
445 // get the specified large icon from file
446 if ( !::ExtractIconEx(nameReal
, iconIndex
, &hicon
, NULL
, 1) )
448 // it is not an error, but it might still be useful to be informed
449 // about it optionally
450 wxLogTrace(_T("iconload"),
451 _T("No large icons found in the file '%s'."),
455 else if ( desiredWidth
== ::GetSystemMetrics(SM_CXSMICON
) &&
456 desiredHeight
== ::GetSystemMetrics(SM_CYSMICON
) )
458 // get the specified small icon from file
459 if ( !::ExtractIconEx(nameReal
, iconIndex
, NULL
, &hicon
, 1) )
461 wxLogTrace(_T("iconload"),
462 _T("No small icons found in the file '%s'."),
466 //else: not standard size, load below
471 // take any size icon from the file by index
472 hicon
= ::ExtractIcon(wxGetInstance(), nameReal
, iconIndex
);
478 wxLogSysError(_T("Failed to load icon from the file '%s'"),
484 size
= wxGetHiconSize(hicon
);
486 if ( (desiredWidth
!= -1 && desiredWidth
!= size
.x
) ||
487 (desiredHeight
!= -1 && desiredHeight
!= size
.y
) )
489 wxLogTrace(_T("iconload"),
490 _T("Returning false from wxICOFileHandler::Load because of the size mismatch: actual (%d, %d), requested (%d, %d)"),
492 desiredWidth
, desiredHeight
);
494 ::DestroyIcon(hicon
);
499 icon
->SetHICON((WXHICON
)hicon
);
500 icon
->SetSize(size
.x
, size
.y
);
505 bool wxICOResourceHandler::LoadIcon(wxIcon
*icon
,
506 const wxString
& name
,
507 long WXUNUSED(flags
),
508 int desiredWidth
, int desiredHeight
)
512 // do we need the icon of the specific size or would any icon do?
513 bool hasSize
= desiredWidth
!= -1 || desiredHeight
!= -1;
515 wxASSERT_MSG( !hasSize
|| (desiredWidth
!= -1 && desiredHeight
!= -1),
516 _T("width and height should be either both -1 or not") );
518 // try to load the icon from this program first to allow overriding the
519 // standard icons (although why one would want to do it considering that
520 // we already have wxApp::GetStdIcon() is unclear)
522 // note that we can't just always call LoadImage() because it seems to do
523 // some icon rescaling internally which results in very ugly 16x16 icons
526 hicon
= (HICON
)::LoadImage(wxGetInstance(), name
, IMAGE_ICON
,
527 desiredWidth
, desiredHeight
,
532 hicon
= ::LoadIcon(wxGetInstance(), name
);
535 // next check if it's not a standard icon
537 if ( !hicon
&& !hasSize
)
545 { wxT("wxICON_QUESTION"), IDI_QUESTION
},
546 { wxT("wxICON_WARNING"), IDI_EXCLAMATION
},
547 { wxT("wxICON_ERROR"), IDI_HAND
},
548 { wxT("wxICON_INFORMATION"), IDI_ASTERISK
},
551 for ( size_t nIcon
= 0; !hicon
&& nIcon
< WXSIZEOF(stdIcons
); nIcon
++ )
553 if ( name
== stdIcons
[nIcon
].name
)
555 hicon
= ::LoadIcon((HINSTANCE
)NULL
, stdIcons
[nIcon
].id
);
561 wxSize size
= wxGetHiconSize(hicon
);
562 icon
->SetSize(size
.x
, size
.y
);
564 icon
->SetHICON((WXHICON
)hicon
);
569 // ----------------------------------------------------------------------------
571 // ----------------------------------------------------------------------------
573 wxSize
wxGetHiconSize(HICON
WXUNUSED_IN_WINCE(hicon
))
575 // default icon size on this hardware
576 // usually 32x32 but can be other (smaller) on pocket devices
577 wxSize
size(::GetSystemMetrics(SM_CXICON
), ::GetSystemMetrics(SM_CYICON
));
580 if ( hicon
&& wxGetOsVersion() != wxWIN32S
)
583 if ( !::GetIconInfo(hicon
, &info
) )
585 wxLogLastError(wxT("GetIconInfo"));
589 HBITMAP hbmp
= info
.hbmMask
;
593 if ( ::GetObject(hbmp
, sizeof(BITMAP
), (LPSTR
) &bm
) )
595 size
= wxSize(bm
.bmWidth
, bm
.bmHeight
);
598 ::DeleteObject(info
.hbmMask
);
601 ::DeleteObject(info
.hbmColor
);
609 #endif // __WXMICROWIN__