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"),
154 virtual bool LoadIcon(wxIcon
*icon
,
155 const wxString
& name
, long flags
,
156 int desiredWidth
= -1, int desiredHeight
= -1);
159 DECLARE_DYNAMIC_CLASS(wxICOFileHandler
)
162 class WXDLLEXPORT wxICOResourceHandler
: public wxIconHandler
165 wxICOResourceHandler() : wxIconHandler(_T("ICO resource"),
167 wxBITMAP_TYPE_ICO_RESOURCE
)
171 virtual bool LoadIcon(wxIcon
*icon
,
172 const wxString
& name
, long flags
,
173 int desiredWidth
= -1, int desiredHeight
= -1);
176 DECLARE_DYNAMIC_CLASS(wxICOResourceHandler
)
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
183 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
184 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
185 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler
, wxObject
)
186 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler
, wxObject
)
188 // ----------------------------------------------------------------------------
190 // ----------------------------------------------------------------------------
195 // ============================================================================
197 // ============================================================================
199 wxGDIImageHandlerList
wxGDIImage::ms_handlers
;
201 // ----------------------------------------------------------------------------
202 // wxGDIImage functions forwarded to wxGDIImageRefData
203 // ----------------------------------------------------------------------------
205 bool wxGDIImage::FreeResource(bool WXUNUSED(force
))
209 GetGDIImageData()->Free();
210 GetGDIImageData()->m_handle
= 0;
216 WXHANDLE
wxGDIImage::GetResourceHandle() const
221 // ----------------------------------------------------------------------------
222 // wxGDIImage handler stuff
223 // ----------------------------------------------------------------------------
225 void wxGDIImage::AddHandler(wxGDIImageHandler
*handler
)
227 ms_handlers
.Append(handler
);
230 void wxGDIImage::InsertHandler(wxGDIImageHandler
*handler
)
232 ms_handlers
.Insert(handler
);
235 bool wxGDIImage::RemoveHandler(const wxString
& name
)
237 wxGDIImageHandler
*handler
= FindHandler(name
);
240 ms_handlers
.DeleteObject(handler
);
247 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& name
)
249 wxGDIImageHandlerList::compatibility_iterator node
= ms_handlers
.GetFirst();
252 wxGDIImageHandler
*handler
= node
->GetData();
253 if ( handler
->GetName() == name
)
255 node
= node
->GetNext();
261 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& extension
,
264 wxGDIImageHandlerList::compatibility_iterator node
= ms_handlers
.GetFirst();
267 wxGDIImageHandler
*handler
= node
->GetData();
268 if ( (handler
->GetExtension() = extension
) &&
269 (type
== -1 || handler
->GetType() == type
) )
274 node
= node
->GetNext();
279 wxGDIImageHandler
*wxGDIImage::FindHandler(long type
)
281 wxGDIImageHandlerList::compatibility_iterator node
= ms_handlers
.GetFirst();
284 wxGDIImageHandler
*handler
= node
->GetData();
285 if ( handler
->GetType() == type
)
288 node
= node
->GetNext();
294 void wxGDIImage::CleanUpHandlers()
296 wxGDIImageHandlerList::compatibility_iterator node
= ms_handlers
.GetFirst();
299 wxGDIImageHandler
*handler
= node
->GetData();
300 wxGDIImageHandlerList::compatibility_iterator next
= node
->GetNext();
302 ms_handlers
.Erase( node
);
307 void wxGDIImage::InitStandardHandlers()
309 #ifndef __WXMICROWIN__
310 AddHandler(new wxBMPResourceHandler
);
311 AddHandler(new wxBMPFileHandler
);
312 AddHandler(new wxICOResourceHandler
);
313 AddHandler(new wxICOFileHandler
);
317 #ifndef __WXMICROWIN__
319 // ----------------------------------------------------------------------------
321 // ----------------------------------------------------------------------------
323 bool wxBMPResourceHandler::LoadFile(wxBitmap
*bitmap
,
324 const wxString
& name
, long WXUNUSED(flags
),
325 int WXUNUSED(desiredWidth
),
326 int WXUNUSED(desiredHeight
))
328 // TODO: load colourmap.
329 bitmap
->SetHBITMAP((WXHBITMAP
)::LoadBitmap(wxGetInstance(), name
));
333 // it's probably not found
334 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
341 if ( !::GetObject(GetHbitmapOf(*bitmap
), sizeof(BITMAP
), (LPSTR
) &bm
) )
343 wxLogLastError(wxT("GetObject(HBITMAP)"));
346 bitmap
->SetWidth(bm
.bmWidth
);
347 bitmap
->SetHeight(bm
.bmHeight
);
348 bitmap
->SetDepth(bm
.bmBitsPixel
);
350 // use 0xc0c0c0 as transparent colour by default
351 bitmap
->SetMask(new wxMask(*bitmap
, *wxLIGHT_GREY
));
356 bool wxBMPFileHandler::LoadFile(wxBitmap
*bitmap
,
357 const wxString
& name
, long WXUNUSED(flags
),
358 int WXUNUSED(desiredWidth
),
359 int WXUNUSED(desiredHeight
))
362 wxCHECK_MSG( bitmap
, false, _T("NULL bitmap in LoadFile") );
366 return dib
.IsOk() && bitmap
->CopyFromDIB(dib
);
372 bool wxBMPFileHandler::SaveFile(wxBitmap
*bitmap
,
373 const wxString
& name
,
375 const wxPalette
* WXUNUSED(pal
))
378 wxCHECK_MSG( bitmap
, false, _T("NULL bitmap in SaveFile") );
382 return dib
.Save(name
);
388 // ----------------------------------------------------------------------------
390 // ----------------------------------------------------------------------------
392 bool wxICOFileHandler::LoadIcon(wxIcon
*icon
,
393 const wxString
& name
,
394 long WXUNUSED(flags
),
395 int desiredWidth
, int desiredHeight
)
404 // Parse the filename: it may be of the form "filename;n" in order to
405 // specify the nth icon in the file.
407 // For the moment, ignore the issue of possible semicolons in the
410 wxString
nameReal(name
);
411 wxString strIconIndex
= name
.AfterLast(wxT(';'));
412 if (strIconIndex
!= name
)
414 iconIndex
= wxAtoi(strIconIndex
);
415 nameReal
= name
.BeforeLast(wxT(';'));
419 // If we don't know what size icon we're looking for,
420 // try to find out what's there.
421 // Unfortunately this doesn't work, because ExtractIconEx
422 // will scale the icon to the 'desired' size, even if that
423 // size of icon isn't explicitly stored. So we would have
424 // to parse the icon file outselves.
425 if ( desiredWidth
== -1 &&
428 // Try loading a large icon first
429 if ( ::ExtractIconEx(nameReal
, iconIndex
, &hicon
, NULL
, 1) == 1)
432 // Then try loading a small icon
433 else if ( ::ExtractIconEx(nameReal
, iconIndex
, NULL
, &hicon
, 1) == 1)
439 // were we asked for a large icon?
440 if ( desiredWidth
== ::GetSystemMetrics(SM_CXICON
) &&
441 desiredHeight
== ::GetSystemMetrics(SM_CYICON
) )
443 // get the specified large icon from file
444 if ( !::ExtractIconEx(nameReal
, iconIndex
, &hicon
, NULL
, 1) )
446 // it is not an error, but it might still be useful to be informed
447 // about it optionally
448 wxLogTrace(_T("iconload"),
449 _T("No large icons found in the file '%s'."),
453 else if ( desiredWidth
== ::GetSystemMetrics(SM_CXSMICON
) &&
454 desiredHeight
== ::GetSystemMetrics(SM_CYSMICON
) )
456 // get the specified small icon from file
457 if ( !::ExtractIconEx(nameReal
, iconIndex
, NULL
, &hicon
, 1) )
459 wxLogTrace(_T("iconload"),
460 _T("No small icons found in the file '%s'."),
464 //else: not standard size, load below
469 // take any size icon from the file by index
470 hicon
= ::ExtractIcon(wxGetInstance(), nameReal
, iconIndex
);
476 wxLogSysError(_T("Failed to load icon from the file '%s'"),
482 size
= wxGetHiconSize(hicon
);
484 if ( (desiredWidth
!= -1 && desiredWidth
!= size
.x
) ||
485 (desiredHeight
!= -1 && desiredHeight
!= size
.y
) )
487 wxLogTrace(_T("iconload"),
488 _T("Returning false from wxICOFileHandler::Load because of the size mismatch: actual (%d, %d), requested (%d, %d)"),
490 desiredWidth
, desiredHeight
);
492 ::DestroyIcon(hicon
);
497 icon
->SetHICON((WXHICON
)hicon
);
498 icon
->SetSize(size
.x
, size
.y
);
503 bool wxICOResourceHandler::LoadIcon(wxIcon
*icon
,
504 const wxString
& name
,
505 long WXUNUSED(flags
),
506 int desiredWidth
, int desiredHeight
)
510 // do we need the icon of the specific size or would any icon do?
511 bool hasSize
= desiredWidth
!= -1 || desiredHeight
!= -1;
513 wxASSERT_MSG( !hasSize
|| (desiredWidth
!= -1 && desiredHeight
!= -1),
514 _T("width and height should be either both -1 or not") );
516 // try to load the icon from this program first to allow overriding the
517 // standard icons (although why one would want to do it considering that
518 // we already have wxApp::GetStdIcon() is unclear)
520 // note that we can't just always call LoadImage() because it seems to do
521 // some icon rescaling internally which results in very ugly 16x16 icons
524 hicon
= (HICON
)::LoadImage(wxGetInstance(), name
, IMAGE_ICON
,
525 desiredWidth
, desiredHeight
,
530 hicon
= ::LoadIcon(wxGetInstance(), name
);
533 // next check if it's not a standard icon
535 if ( !hicon
&& !hasSize
)
543 { wxT("wxICON_QUESTION"), IDI_QUESTION
},
544 { wxT("wxICON_WARNING"), IDI_EXCLAMATION
},
545 { wxT("wxICON_ERROR"), IDI_HAND
},
546 { wxT("wxICON_INFORMATION"), IDI_ASTERISK
},
549 for ( size_t nIcon
= 0; !hicon
&& nIcon
< WXSIZEOF(stdIcons
); nIcon
++ )
551 if ( name
== stdIcons
[nIcon
].name
)
553 hicon
= ::LoadIcon((HINSTANCE
)NULL
, stdIcons
[nIcon
].id
);
559 wxSize size
= wxGetHiconSize(hicon
);
560 icon
->SetSize(size
.x
, size
.y
);
562 icon
->SetHICON((WXHICON
)hicon
);
567 // ----------------------------------------------------------------------------
569 // ----------------------------------------------------------------------------
571 wxSize
wxGetHiconSize(HICON hicon
)
573 // default icon size on this hardware
574 // usually 32x32 but can be other (smaller) on pocket devices
575 wxSize
size(::GetSystemMetrics(SM_CXICON
), ::GetSystemMetrics(SM_CYICON
));
578 if ( hicon
&& wxGetOsVersion() != wxWIN32S
)
581 if ( !::GetIconInfo(hicon
, &info
) )
583 wxLogLastError(wxT("GetIconInfo"));
587 HBITMAP hbmp
= info
.hbmMask
;
591 if ( ::GetObject(hbmp
, sizeof(BITMAP
), (LPSTR
) &bm
) )
593 size
= wxSize(bm
.bmWidth
, bm
.bmHeight
);
596 ::DeleteObject(info
.hbmMask
);
599 ::DeleteObject(info
.hbmColor
);
608 #endif // __WXMICROWIN__