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"
52 #include "wx/listimpl.cpp"
53 WX_DEFINE_LIST(wxGDIImageHandlerList
);
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 #ifndef __WXMICROWIN__
61 // all image handlers are declared/defined in this file because the outside
62 // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids)
64 class WXDLLEXPORT wxBMPFileHandler
: public wxBitmapHandler
67 wxBMPFileHandler() : wxBitmapHandler(_T("Windows bitmap file"), _T("bmp"),
72 virtual bool LoadFile(wxBitmap
*bitmap
,
73 const wxString
& name
, long flags
,
74 int desiredWidth
, int desiredHeight
);
75 virtual bool SaveFile(wxBitmap
*bitmap
,
76 const wxString
& name
, int type
,
77 const wxPalette
*palette
= NULL
);
80 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
)
83 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
86 wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"),
88 wxBITMAP_TYPE_BMP_RESOURCE
)
92 virtual bool LoadFile(wxBitmap
*bitmap
,
93 const wxString
& name
, long flags
,
94 int desiredWidth
, int desiredHeight
);
97 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
100 class WXDLLEXPORT wxIconHandler
: public wxGDIImageHandler
103 wxIconHandler(const wxString
& name
, const wxString
& ext
, long type
)
104 : wxGDIImageHandler(name
, ext
, type
)
108 // creating and saving icons is not supported
109 virtual bool Create(wxGDIImage
*WXUNUSED(image
),
110 void *WXUNUSED(data
),
111 long WXUNUSED(flags
),
113 int WXUNUSED(height
),
114 int WXUNUSED(depth
) = 1)
119 virtual bool Save(wxGDIImage
*WXUNUSED(image
),
120 const wxString
& WXUNUSED(name
),
126 virtual bool Load(wxGDIImage
*image
,
127 const wxString
& name
,
129 int desiredWidth
, int desiredHeight
)
131 wxIcon
*icon
= wxDynamicCast(image
, wxIcon
);
132 wxCHECK_MSG( icon
, false, _T("wxIconHandler only works with icons") );
134 return LoadIcon(icon
, name
, flags
, desiredWidth
, desiredHeight
);
138 virtual bool LoadIcon(wxIcon
*icon
,
139 const wxString
& name
, long flags
,
140 int desiredWidth
= -1, int desiredHeight
= -1) = 0;
143 class WXDLLEXPORT wxICOFileHandler
: public wxIconHandler
146 wxICOFileHandler() : wxIconHandler(_T("ICO icon file"),
152 virtual bool LoadIcon(wxIcon
*icon
,
153 const wxString
& name
, long flags
,
154 int desiredWidth
= -1, int desiredHeight
= -1);
157 DECLARE_DYNAMIC_CLASS(wxICOFileHandler
)
160 class WXDLLEXPORT wxICOResourceHandler
: public wxIconHandler
163 wxICOResourceHandler() : wxIconHandler(_T("ICO resource"),
165 wxBITMAP_TYPE_ICO_RESOURCE
)
169 virtual bool LoadIcon(wxIcon
*icon
,
170 const wxString
& name
, long flags
,
171 int desiredWidth
= -1, int desiredHeight
= -1);
174 DECLARE_DYNAMIC_CLASS(wxICOResourceHandler
)
177 // ----------------------------------------------------------------------------
179 // ----------------------------------------------------------------------------
181 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
182 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
183 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler
, wxObject
)
184 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler
, wxObject
)
186 // ----------------------------------------------------------------------------
188 // ----------------------------------------------------------------------------
193 // ============================================================================
195 // ============================================================================
197 wxGDIImageHandlerList
wxGDIImage::ms_handlers
;
199 // ----------------------------------------------------------------------------
200 // wxGDIImage functions forwarded to wxGDIImageRefData
201 // ----------------------------------------------------------------------------
203 bool wxGDIImage::FreeResource(bool WXUNUSED(force
))
207 GetGDIImageData()->Free();
208 GetGDIImageData()->m_handle
= 0;
214 WXHANDLE
wxGDIImage::GetResourceHandle() const
219 // ----------------------------------------------------------------------------
220 // wxGDIImage handler stuff
221 // ----------------------------------------------------------------------------
223 void wxGDIImage::AddHandler(wxGDIImageHandler
*handler
)
225 ms_handlers
.Append(handler
);
228 void wxGDIImage::InsertHandler(wxGDIImageHandler
*handler
)
230 ms_handlers
.Insert(handler
);
233 bool wxGDIImage::RemoveHandler(const wxString
& name
)
235 wxGDIImageHandler
*handler
= FindHandler(name
);
238 ms_handlers
.DeleteObject(handler
);
245 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& name
)
247 wxGDIImageHandlerList::compatibility_iterator node
= ms_handlers
.GetFirst();
250 wxGDIImageHandler
*handler
= node
->GetData();
251 if ( handler
->GetName() == name
)
253 node
= node
->GetNext();
259 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& extension
,
262 wxGDIImageHandlerList::compatibility_iterator node
= ms_handlers
.GetFirst();
265 wxGDIImageHandler
*handler
= node
->GetData();
266 if ( (handler
->GetExtension() = extension
) &&
267 (type
== -1 || handler
->GetType() == type
) )
272 node
= node
->GetNext();
277 wxGDIImageHandler
*wxGDIImage::FindHandler(long type
)
279 wxGDIImageHandlerList::compatibility_iterator node
= ms_handlers
.GetFirst();
282 wxGDIImageHandler
*handler
= node
->GetData();
283 if ( handler
->GetType() == type
)
286 node
= node
->GetNext();
292 void wxGDIImage::CleanUpHandlers()
294 wxGDIImageHandlerList::compatibility_iterator node
= ms_handlers
.GetFirst();
297 wxGDIImageHandler
*handler
= node
->GetData();
298 wxGDIImageHandlerList::compatibility_iterator next
= node
->GetNext();
300 ms_handlers
.Erase( node
);
305 void wxGDIImage::InitStandardHandlers()
307 #ifndef __WXMICROWIN__
308 AddHandler(new wxBMPResourceHandler
);
309 AddHandler(new wxBMPFileHandler
);
310 AddHandler(new wxICOResourceHandler
);
311 AddHandler(new wxICOFileHandler
);
315 #ifndef __WXMICROWIN__
317 // ----------------------------------------------------------------------------
319 // ----------------------------------------------------------------------------
321 bool wxBMPResourceHandler::LoadFile(wxBitmap
*bitmap
,
322 const wxString
& name
, long WXUNUSED(flags
),
323 int WXUNUSED(desiredWidth
),
324 int WXUNUSED(desiredHeight
))
326 // TODO: load colourmap.
327 bitmap
->SetHBITMAP((WXHBITMAP
)::LoadBitmap(wxGetInstance(), name
));
331 // it's probably not found
332 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
339 if ( !::GetObject(GetHbitmapOf(*bitmap
), sizeof(BITMAP
), (LPSTR
) &bm
) )
341 wxLogLastError(wxT("GetObject(HBITMAP)"));
344 bitmap
->SetWidth(bm
.bmWidth
);
345 bitmap
->SetHeight(bm
.bmHeight
);
346 bitmap
->SetDepth(bm
.bmBitsPixel
);
351 bool wxBMPFileHandler::LoadFile(wxBitmap
*bitmap
,
352 const wxString
& name
, long WXUNUSED(flags
),
353 int WXUNUSED(desiredWidth
),
354 int WXUNUSED(desiredHeight
))
357 wxCHECK_MSG( bitmap
, false, _T("NULL bitmap in LoadFile") );
361 return dib
.IsOk() && bitmap
->CopyFromDIB(dib
);
367 bool wxBMPFileHandler::SaveFile(wxBitmap
*bitmap
,
368 const wxString
& name
,
370 const wxPalette
* WXUNUSED(pal
))
373 wxCHECK_MSG( bitmap
, false, _T("NULL bitmap in SaveFile") );
377 return dib
.Save(name
);
383 // ----------------------------------------------------------------------------
385 // ----------------------------------------------------------------------------
387 bool wxICOFileHandler::LoadIcon(wxIcon
*icon
,
388 const wxString
& name
,
389 long WXUNUSED(flags
),
390 int desiredWidth
, int desiredHeight
)
399 // Parse the filename: it may be of the form "filename;n" in order to
400 // specify the nth icon in the file.
402 // For the moment, ignore the issue of possible semicolons in the
405 wxString
nameReal(name
);
406 wxString strIconIndex
= name
.AfterLast(wxT(';'));
407 if (strIconIndex
!= name
)
409 iconIndex
= wxAtoi(strIconIndex
);
410 nameReal
= name
.BeforeLast(wxT(';'));
414 // If we don't know what size icon we're looking for,
415 // try to find out what's there.
416 // Unfortunately this doesn't work, because ExtractIconEx
417 // will scale the icon to the 'desired' size, even if that
418 // size of icon isn't explicitly stored. So we would have
419 // to parse the icon file outselves.
420 if ( desiredWidth
== -1 &&
423 // Try loading a large icon first
424 if ( ::ExtractIconEx(nameReal
, iconIndex
, &hicon
, NULL
, 1) == 1)
427 // Then try loading a small icon
428 else if ( ::ExtractIconEx(nameReal
, iconIndex
, NULL
, &hicon
, 1) == 1)
434 // were we asked for a large icon?
435 if ( desiredWidth
== ::GetSystemMetrics(SM_CXICON
) &&
436 desiredHeight
== ::GetSystemMetrics(SM_CYICON
) )
438 // get the specified large icon from file
439 if ( !::ExtractIconEx(nameReal
, iconIndex
, &hicon
, NULL
, 1) )
441 // it is not an error, but it might still be useful to be informed
442 // about it optionally
443 wxLogTrace(_T("iconload"),
444 _T("No large icons found in the file '%s'."),
448 else if ( desiredWidth
== ::GetSystemMetrics(SM_CXSMICON
) &&
449 desiredHeight
== ::GetSystemMetrics(SM_CYSMICON
) )
451 // get the specified small icon from file
452 if ( !::ExtractIconEx(nameReal
, iconIndex
, NULL
, &hicon
, 1) )
454 wxLogTrace(_T("iconload"),
455 _T("No small icons found in the file '%s'."),
459 //else: not standard size, load below
464 // take any size icon from the file by index
465 hicon
= ::ExtractIcon(wxGetInstance(), nameReal
, iconIndex
);
471 wxLogSysError(_T("Failed to load icon from the file '%s'"),
477 size
= wxGetHiconSize(hicon
);
479 if ( (desiredWidth
!= -1 && desiredWidth
!= size
.x
) ||
480 (desiredHeight
!= -1 && desiredHeight
!= size
.y
) )
482 wxLogTrace(_T("iconload"),
483 _T("Returning false from wxICOFileHandler::Load because of the size mismatch: actual (%d, %d), requested (%d, %d)"),
485 desiredWidth
, desiredHeight
);
487 ::DestroyIcon(hicon
);
492 icon
->SetHICON((WXHICON
)hicon
);
493 icon
->SetSize(size
.x
, size
.y
);
498 bool wxICOResourceHandler::LoadIcon(wxIcon
*icon
,
499 const wxString
& name
,
500 long WXUNUSED(flags
),
501 int desiredWidth
, int desiredHeight
)
505 // do we need the icon of the specific size or would any icon do?
506 bool hasSize
= desiredWidth
!= -1 || desiredHeight
!= -1;
508 wxASSERT_MSG( !hasSize
|| (desiredWidth
!= -1 && desiredHeight
!= -1),
509 _T("width and height should be either both -1 or not") );
511 // try to load the icon from this program first to allow overriding the
512 // standard icons (although why one would want to do it considering that
513 // we already have wxApp::GetStdIcon() is unclear)
515 // note that we can't just always call LoadImage() because it seems to do
516 // some icon rescaling internally which results in very ugly 16x16 icons
519 hicon
= (HICON
)::LoadImage(wxGetInstance(), name
, IMAGE_ICON
,
520 desiredWidth
, desiredHeight
,
525 hicon
= ::LoadIcon(wxGetInstance(), name
);
528 // next check if it's not a standard icon
530 if ( !hicon
&& !hasSize
)
538 { wxT("wxICON_QUESTION"), IDI_QUESTION
},
539 { wxT("wxICON_WARNING"), IDI_EXCLAMATION
},
540 { wxT("wxICON_ERROR"), IDI_HAND
},
541 { wxT("wxICON_INFORMATION"), IDI_ASTERISK
},
544 for ( size_t nIcon
= 0; !hicon
&& nIcon
< WXSIZEOF(stdIcons
); nIcon
++ )
546 if ( name
== stdIcons
[nIcon
].name
)
548 hicon
= ::LoadIcon((HINSTANCE
)NULL
, stdIcons
[nIcon
].id
);
554 wxSize size
= wxGetHiconSize(hicon
);
555 icon
->SetSize(size
.x
, size
.y
);
557 icon
->SetHICON((WXHICON
)hicon
);
562 // ----------------------------------------------------------------------------
564 // ----------------------------------------------------------------------------
566 wxSize
wxGetHiconSize(HICON hicon
)
568 wxSize
size(32, 32); // default
570 if ( hicon
&& wxGetOsVersion() != wxWIN32S
)
573 if ( !::GetIconInfo(hicon
, &info
) )
575 wxLogLastError(wxT("GetIconInfo"));
579 HBITMAP hbmp
= info
.hbmMask
;
583 if ( ::GetObject(hbmp
, sizeof(BITMAP
), (LPSTR
) &bm
) )
585 size
= wxSize(bm
.bmWidth
, bm
.bmHeight
);
588 ::DeleteObject(info
.hbmMask
);
591 ::DeleteObject(info
.hbmColor
);
598 #endif // __WXMICROWIN__