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 // ----------------------------------------------------------------------------
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"
42 #include "wx/msw/dib.h"
44 #include "wx/listimpl.cpp"
45 WX_DEFINE_LIST(wxGDIImageHandlerList
);
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 #ifndef __WXMICROWIN__
53 // all image handlers are declared/defined in this file because the outside
54 // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids)
56 class WXDLLEXPORT wxBMPFileHandler
: public wxBitmapHandler
59 wxBMPFileHandler() : wxBitmapHandler(_T("Windows bitmap file"), _T("bmp"),
64 virtual bool LoadFile(wxBitmap
*bitmap
,
65 const wxString
& name
, long flags
,
66 int desiredWidth
, int desiredHeight
);
67 virtual bool SaveFile(wxBitmap
*bitmap
,
68 const wxString
& name
, int type
,
69 const wxPalette
*palette
= NULL
);
72 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
)
75 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
78 wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"),
80 wxBITMAP_TYPE_BMP_RESOURCE
)
84 virtual bool LoadFile(wxBitmap
*bitmap
,
85 const wxString
& name
, long flags
,
86 int desiredWidth
, int desiredHeight
);
89 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
92 class WXDLLEXPORT wxIconHandler
: public wxGDIImageHandler
95 wxIconHandler(const wxString
& name
, const wxString
& ext
, long type
)
96 : wxGDIImageHandler(name
, ext
, type
)
100 // creating and saving icons is not supported
101 virtual bool Create(wxGDIImage
*WXUNUSED(image
),
102 void *WXUNUSED(data
),
103 long WXUNUSED(flags
),
105 int WXUNUSED(height
),
106 int WXUNUSED(depth
) = 1)
111 virtual bool Save(wxGDIImage
*WXUNUSED(image
),
112 const wxString
& WXUNUSED(name
),
118 virtual bool Load(wxGDIImage
*image
,
119 const wxString
& name
,
121 int desiredWidth
, int desiredHeight
)
123 wxIcon
*icon
= wxDynamicCast(image
, wxIcon
);
124 wxCHECK_MSG( icon
, false, _T("wxIconHandler only works with icons") );
126 return LoadIcon(icon
, name
, flags
, desiredWidth
, desiredHeight
);
130 virtual bool LoadIcon(wxIcon
*icon
,
131 const wxString
& name
, long flags
,
132 int desiredWidth
= -1, int desiredHeight
= -1) = 0;
135 class WXDLLEXPORT wxICOFileHandler
: public wxIconHandler
138 wxICOFileHandler() : wxIconHandler(_T("ICO icon file"),
144 virtual bool LoadIcon(wxIcon
*icon
,
145 const wxString
& name
, long flags
,
146 int desiredWidth
= -1, int desiredHeight
= -1);
149 DECLARE_DYNAMIC_CLASS(wxICOFileHandler
)
152 class WXDLLEXPORT wxICOResourceHandler
: public wxIconHandler
155 wxICOResourceHandler() : wxIconHandler(_T("ICO resource"),
157 wxBITMAP_TYPE_ICO_RESOURCE
)
161 virtual bool LoadIcon(wxIcon
*icon
,
162 const wxString
& name
, long flags
,
163 int desiredWidth
= -1, int desiredHeight
= -1);
166 DECLARE_DYNAMIC_CLASS(wxICOResourceHandler
)
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
173 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
174 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
175 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler
, wxObject
)
176 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler
, wxObject
)
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
185 // ============================================================================
187 // ============================================================================
189 wxGDIImageHandlerList
wxGDIImage::ms_handlers
;
191 // ----------------------------------------------------------------------------
192 // wxGDIImage functions forwarded to wxGDIImageRefData
193 // ----------------------------------------------------------------------------
195 bool wxGDIImage::FreeResource(bool WXUNUSED(force
))
199 GetGDIImageData()->Free();
200 GetGDIImageData()->m_handle
= 0;
206 WXHANDLE
wxGDIImage::GetResourceHandle() const
211 // ----------------------------------------------------------------------------
212 // wxGDIImage handler stuff
213 // ----------------------------------------------------------------------------
215 void wxGDIImage::AddHandler(wxGDIImageHandler
*handler
)
217 ms_handlers
.Append(handler
);
220 void wxGDIImage::InsertHandler(wxGDIImageHandler
*handler
)
222 ms_handlers
.Insert(handler
);
225 bool wxGDIImage::RemoveHandler(const wxString
& name
)
227 wxGDIImageHandler
*handler
= FindHandler(name
);
230 ms_handlers
.DeleteObject(handler
);
237 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& name
)
239 wxGDIImageHandlerList::compatibility_iterator node
= ms_handlers
.GetFirst();
242 wxGDIImageHandler
*handler
= node
->GetData();
243 if ( handler
->GetName() == name
)
245 node
= node
->GetNext();
251 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& extension
,
254 wxGDIImageHandlerList::compatibility_iterator node
= ms_handlers
.GetFirst();
257 wxGDIImageHandler
*handler
= node
->GetData();
258 if ( (handler
->GetExtension() = extension
) &&
259 (type
== -1 || handler
->GetType() == type
) )
264 node
= node
->GetNext();
269 wxGDIImageHandler
*wxGDIImage::FindHandler(long type
)
271 wxGDIImageHandlerList::compatibility_iterator node
= ms_handlers
.GetFirst();
274 wxGDIImageHandler
*handler
= node
->GetData();
275 if ( handler
->GetType() == type
)
278 node
= node
->GetNext();
284 void wxGDIImage::CleanUpHandlers()
286 wxGDIImageHandlerList::compatibility_iterator node
= ms_handlers
.GetFirst();
289 wxGDIImageHandler
*handler
= node
->GetData();
290 wxGDIImageHandlerList::compatibility_iterator next
= node
->GetNext();
292 ms_handlers
.Erase( node
);
297 void wxGDIImage::InitStandardHandlers()
299 #ifndef __WXMICROWIN__
300 AddHandler(new wxBMPResourceHandler
);
301 AddHandler(new wxBMPFileHandler
);
302 AddHandler(new wxICOResourceHandler
);
303 AddHandler(new wxICOFileHandler
);
307 #ifndef __WXMICROWIN__
309 // ----------------------------------------------------------------------------
311 // ----------------------------------------------------------------------------
313 bool wxBMPResourceHandler::LoadFile(wxBitmap
*bitmap
,
314 const wxString
& name
, long WXUNUSED(flags
),
315 int WXUNUSED(desiredWidth
),
316 int WXUNUSED(desiredHeight
))
318 // TODO: load colourmap.
319 bitmap
->SetHBITMAP((WXHBITMAP
)::LoadBitmap(wxGetInstance(), name
));
323 // it's probably not found
324 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
331 if ( !::GetObject(GetHbitmapOf(*bitmap
), sizeof(BITMAP
), (LPSTR
) &bm
) )
333 wxLogLastError(wxT("GetObject(HBITMAP)"));
336 bitmap
->SetWidth(bm
.bmWidth
);
337 bitmap
->SetHeight(bm
.bmHeight
);
338 bitmap
->SetDepth(bm
.bmBitsPixel
);
343 bool wxBMPFileHandler::LoadFile(wxBitmap
*bitmap
,
344 const wxString
& name
, long WXUNUSED(flags
),
345 int WXUNUSED(desiredWidth
),
346 int WXUNUSED(desiredHeight
))
348 wxCHECK_MSG( bitmap
, false, _T("NULL bitmap in LoadFile") );
352 return dib
.IsOk() && bitmap
->CopyFromDIB(dib
);
355 bool wxBMPFileHandler::SaveFile(wxBitmap
*bitmap
,
356 const wxString
& name
,
358 const wxPalette
* WXUNUSED(pal
))
360 wxCHECK_MSG( bitmap
, false, _T("NULL bitmap in SaveFile") );
364 return dib
.Save(name
);
367 // ----------------------------------------------------------------------------
369 // ----------------------------------------------------------------------------
371 bool wxICOFileHandler::LoadIcon(wxIcon
*icon
,
372 const wxString
& name
,
373 long WXUNUSED(flags
),
374 int desiredWidth
, int desiredHeight
)
383 // Parse the filename: it may be of the form "filename;n" in order to
384 // specify the nth icon in the file.
386 // For the moment, ignore the issue of possible semicolons in the
389 wxString
nameReal(name
);
390 wxString strIconIndex
= name
.AfterLast(wxT(';'));
391 if (strIconIndex
!= name
)
393 iconIndex
= wxAtoi(strIconIndex
);
394 nameReal
= name
.BeforeLast(wxT(';'));
398 // If we don't know what size icon we're looking for,
399 // try to find out what's there.
400 // Unfortunately this doesn't work, because ExtractIconEx
401 // will scale the icon to the 'desired' size, even if that
402 // size of icon isn't explicitly stored. So we would have
403 // to parse the icon file outselves.
404 if ( desiredWidth
== -1 &&
407 // Try loading a large icon first
408 if ( ::ExtractIconEx(nameReal
, iconIndex
, &hicon
, NULL
, 1) == 1)
411 // Then try loading a small icon
412 else if ( ::ExtractIconEx(nameReal
, iconIndex
, NULL
, &hicon
, 1) == 1)
418 // were we asked for a large icon?
419 if ( desiredWidth
== ::GetSystemMetrics(SM_CXICON
) &&
420 desiredHeight
== ::GetSystemMetrics(SM_CYICON
) )
422 // get the specified large icon from file
423 if ( !::ExtractIconEx(nameReal
, iconIndex
, &hicon
, NULL
, 1) )
425 // it is not an error, but it might still be useful to be informed
426 // about it optionally
427 wxLogTrace(_T("iconload"),
428 _T("No large icons found in the file '%s'."),
432 else if ( desiredWidth
== ::GetSystemMetrics(SM_CXSMICON
) &&
433 desiredHeight
== ::GetSystemMetrics(SM_CYSMICON
) )
435 // get the specified small icon from file
436 if ( !::ExtractIconEx(nameReal
, iconIndex
, NULL
, &hicon
, 1) )
438 wxLogTrace(_T("iconload"),
439 _T("No small icons found in the file '%s'."),
443 //else: not standard size, load below
447 // take any size icon from the file by index
448 hicon
= ::ExtractIcon(wxGetInstance(), nameReal
, iconIndex
);
453 wxLogSysError(_T("Failed to load icon from the file '%s'"),
459 size
= wxGetHiconSize(hicon
);
461 if ( (desiredWidth
!= -1 && desiredWidth
!= size
.x
) ||
462 (desiredHeight
!= -1 && desiredHeight
!= size
.y
) )
464 wxLogTrace(_T("iconload"),
465 _T("Returning false from wxICOFileHandler::Load because of the size mismatch: actual (%d, %d), requested (%d, %d)"),
467 desiredWidth
, desiredHeight
);
469 ::DestroyIcon(hicon
);
474 icon
->SetHICON((WXHICON
)hicon
);
475 icon
->SetSize(size
.x
, size
.y
);
480 bool wxICOResourceHandler::LoadIcon(wxIcon
*icon
,
481 const wxString
& name
,
482 long WXUNUSED(flags
),
483 int desiredWidth
, int desiredHeight
)
487 // do we need the icon of the specific size or would any icon do?
488 bool hasSize
= desiredWidth
!= -1 || desiredHeight
!= -1;
490 wxASSERT_MSG( !hasSize
|| (desiredWidth
!= -1 && desiredHeight
!= -1),
491 _T("width and height should be either both -1 or not") );
493 // try to load the icon from this program first to allow overriding the
494 // standard icons (although why one would want to do it considering that
495 // we already have wxApp::GetStdIcon() is unclear)
497 // note that we can't just always call LoadImage() because it seems to do
498 // some icon rescaling internally which results in very ugly 16x16 icons
501 hicon
= (HICON
)::LoadImage(wxGetInstance(), name
, IMAGE_ICON
,
502 desiredWidth
, desiredHeight
,
507 hicon
= ::LoadIcon(wxGetInstance(), name
);
510 // next check if it's not a standard icon
511 if ( !hicon
&& !hasSize
)
519 { wxT("wxICON_QUESTION"), IDI_QUESTION
},
520 { wxT("wxICON_WARNING"), IDI_EXCLAMATION
},
521 { wxT("wxICON_ERROR"), IDI_HAND
},
522 { wxT("wxICON_INFORMATION"), IDI_ASTERISK
},
525 for ( size_t nIcon
= 0; !hicon
&& nIcon
< WXSIZEOF(stdIcons
); nIcon
++ )
527 if ( name
== stdIcons
[nIcon
].name
)
529 hicon
= ::LoadIcon((HINSTANCE
)NULL
, stdIcons
[nIcon
].id
);
534 wxSize size
= wxGetHiconSize(hicon
);
535 icon
->SetSize(size
.x
, size
.y
);
537 icon
->SetHICON((WXHICON
)hicon
);
542 // ----------------------------------------------------------------------------
544 // ----------------------------------------------------------------------------
546 wxSize
wxGetHiconSize(HICON hicon
)
548 wxSize
size(32, 32); // default
550 if ( hicon
&& wxGetOsVersion() != wxWIN32S
)
553 if ( !::GetIconInfo(hicon
, &info
) )
555 wxLogLastError(wxT("GetIconInfo"));
559 HBITMAP hbmp
= info
.hbmMask
;
563 if ( ::GetObject(hbmp
, sizeof(BITMAP
), (LPSTR
) &bm
) )
565 size
= wxSize(bm
.bmWidth
, bm
.bmHeight
);
568 ::DeleteObject(info
.hbmMask
);
571 ::DeleteObject(info
.hbmColor
);
578 #endif // __WXMICROWIN__