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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 #include "wx/string.h"
32 #include "wx/msw/private.h"
36 #include "wx/bitmap.h"
37 #include "wx/msw/gdiimage.h"
40 #include "wx/msw/dib.h"
50 #include "wx/listimpl.cpp"
51 WX_DEFINE_LIST(wxGDIImageHandlerList
);
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 #ifndef __WXMICROWIN__
59 // all image handlers are declared/defined in this file because the outside
60 // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids)
62 class WXDLLEXPORT wxBMPFileHandler
: public wxBitmapHandler
65 wxBMPFileHandler() : wxBitmapHandler(_T("Windows bitmap file"), _T("bmp"),
70 virtual bool LoadFile(wxBitmap
*bitmap
,
71 const wxString
& name
, long flags
,
72 int desiredWidth
, int desiredHeight
);
73 virtual bool SaveFile(wxBitmap
*bitmap
,
74 const wxString
& name
, int type
,
75 const wxPalette
*palette
= NULL
);
78 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
)
81 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
84 wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"),
86 wxBITMAP_TYPE_BMP_RESOURCE
)
90 virtual bool LoadFile(wxBitmap
*bitmap
,
91 const wxString
& name
, long flags
,
92 int desiredWidth
, int desiredHeight
);
95 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
98 class WXDLLEXPORT wxIconHandler
: public wxGDIImageHandler
101 wxIconHandler(const wxString
& name
, const wxString
& ext
, long type
)
102 : wxGDIImageHandler(name
, ext
, type
)
106 // creating and saving icons is not supported
107 virtual bool Create(wxGDIImage
*WXUNUSED(image
),
108 void *WXUNUSED(data
),
109 long WXUNUSED(flags
),
111 int WXUNUSED(height
),
112 int WXUNUSED(depth
) = 1)
117 virtual bool Save(wxGDIImage
*WXUNUSED(image
),
118 const wxString
& WXUNUSED(name
),
124 virtual bool Load(wxGDIImage
*image
,
125 const wxString
& name
,
127 int desiredWidth
, int desiredHeight
)
129 wxIcon
*icon
= wxDynamicCast(image
, wxIcon
);
130 wxCHECK_MSG( icon
, false, _T("wxIconHandler only works with icons") );
132 return LoadIcon(icon
, name
, flags
, desiredWidth
, desiredHeight
);
136 virtual bool LoadIcon(wxIcon
*icon
,
137 const wxString
& name
, long flags
,
138 int desiredWidth
= -1, int desiredHeight
= -1) = 0;
141 class WXDLLEXPORT wxICOFileHandler
: public wxIconHandler
144 wxICOFileHandler() : wxIconHandler(_T("ICO icon file"),
151 virtual bool LoadIcon(wxIcon
*icon
,
152 const wxString
& name
, long flags
,
153 int desiredWidth
= -1, int desiredHeight
= -1);
156 DECLARE_DYNAMIC_CLASS(wxICOFileHandler
)
159 class WXDLLEXPORT wxICOResourceHandler
: public wxIconHandler
162 wxICOResourceHandler() : wxIconHandler(_T("ICO resource"),
164 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
);
348 // use 0xc0c0c0 as transparent colour by default
349 bitmap
->SetMask(new wxMask(*bitmap
, *wxLIGHT_GREY
));
354 bool wxBMPFileHandler::LoadFile(wxBitmap
*bitmap
,
355 const wxString
& name
, long WXUNUSED(flags
),
356 int WXUNUSED(desiredWidth
),
357 int WXUNUSED(desiredHeight
))
360 wxCHECK_MSG( bitmap
, false, _T("NULL bitmap in LoadFile") );
364 return dib
.IsOk() && bitmap
->CopyFromDIB(dib
);
370 bool wxBMPFileHandler::SaveFile(wxBitmap
*bitmap
,
371 const wxString
& name
,
373 const wxPalette
* WXUNUSED(pal
))
376 wxCHECK_MSG( bitmap
, false, _T("NULL bitmap in SaveFile") );
380 return dib
.Save(name
);
386 // ----------------------------------------------------------------------------
388 // ----------------------------------------------------------------------------
390 bool wxICOFileHandler::LoadIcon(wxIcon
*icon
,
391 const wxString
& name
,
392 long WXUNUSED(flags
),
393 int desiredWidth
, int desiredHeight
)
402 // Parse the filename: it may be of the form "filename;n" in order to
403 // specify the nth icon in the file.
405 // For the moment, ignore the issue of possible semicolons in the
408 wxString
nameReal(name
);
409 wxString strIconIndex
= name
.AfterLast(wxT(';'));
410 if (strIconIndex
!= name
)
412 iconIndex
= wxAtoi(strIconIndex
);
413 nameReal
= name
.BeforeLast(wxT(';'));
417 // If we don't know what size icon we're looking for,
418 // try to find out what's there.
419 // Unfortunately this doesn't work, because ExtractIconEx
420 // will scale the icon to the 'desired' size, even if that
421 // size of icon isn't explicitly stored. So we would have
422 // to parse the icon file outselves.
423 if ( desiredWidth
== -1 &&
426 // Try loading a large icon first
427 if ( ::ExtractIconEx(nameReal
, iconIndex
, &hicon
, NULL
, 1) == 1)
430 // Then try loading a small icon
431 else if ( ::ExtractIconEx(nameReal
, iconIndex
, NULL
, &hicon
, 1) == 1)
437 // were we asked for a large icon?
438 if ( desiredWidth
== ::GetSystemMetrics(SM_CXICON
) &&
439 desiredHeight
== ::GetSystemMetrics(SM_CYICON
) )
441 // get the specified large icon from file
442 if ( !::ExtractIconEx(nameReal
, iconIndex
, &hicon
, NULL
, 1) )
444 // it is not an error, but it might still be useful to be informed
445 // about it optionally
446 wxLogTrace(_T("iconload"),
447 _T("No large icons found in the file '%s'."),
451 else if ( desiredWidth
== ::GetSystemMetrics(SM_CXSMICON
) &&
452 desiredHeight
== ::GetSystemMetrics(SM_CYSMICON
) )
454 // get the specified small icon from file
455 if ( !::ExtractIconEx(nameReal
, iconIndex
, NULL
, &hicon
, 1) )
457 wxLogTrace(_T("iconload"),
458 _T("No small icons found in the file '%s'."),
462 //else: not standard size, load below
467 // take any size icon from the file by index
468 hicon
= ::ExtractIcon(wxGetInstance(), nameReal
, iconIndex
);
474 wxLogSysError(_T("Failed to load icon from the file '%s'"),
480 size
= wxGetHiconSize(hicon
);
482 if ( (desiredWidth
!= -1 && desiredWidth
!= size
.x
) ||
483 (desiredHeight
!= -1 && desiredHeight
!= size
.y
) )
485 wxLogTrace(_T("iconload"),
486 _T("Returning false from wxICOFileHandler::Load because of the size mismatch: actual (%d, %d), requested (%d, %d)"),
488 desiredWidth
, desiredHeight
);
490 ::DestroyIcon(hicon
);
495 icon
->SetHICON((WXHICON
)hicon
);
496 icon
->SetSize(size
.x
, size
.y
);
501 bool wxICOResourceHandler::LoadIcon(wxIcon
*icon
,
502 const wxString
& name
,
503 long WXUNUSED(flags
),
504 int desiredWidth
, int desiredHeight
)
508 // do we need the icon of the specific size or would any icon do?
509 bool hasSize
= desiredWidth
!= -1 || desiredHeight
!= -1;
511 wxASSERT_MSG( !hasSize
|| (desiredWidth
!= -1 && desiredHeight
!= -1),
512 _T("width and height should be either both -1 or not") );
514 // try to load the icon from this program first to allow overriding the
515 // standard icons (although why one would want to do it considering that
516 // we already have wxApp::GetStdIcon() is unclear)
518 // note that we can't just always call LoadImage() because it seems to do
519 // some icon rescaling internally which results in very ugly 16x16 icons
522 hicon
= (HICON
)::LoadImage(wxGetInstance(), name
, IMAGE_ICON
,
523 desiredWidth
, desiredHeight
,
528 hicon
= ::LoadIcon(wxGetInstance(), name
);
531 // next check if it's not a standard icon
533 if ( !hicon
&& !hasSize
)
541 { wxT("wxICON_QUESTION"), IDI_QUESTION
},
542 { wxT("wxICON_WARNING"), IDI_EXCLAMATION
},
543 { wxT("wxICON_ERROR"), IDI_HAND
},
544 { wxT("wxICON_INFORMATION"), IDI_ASTERISK
},
547 for ( size_t nIcon
= 0; !hicon
&& nIcon
< WXSIZEOF(stdIcons
); nIcon
++ )
549 if ( name
== stdIcons
[nIcon
].name
)
551 hicon
= ::LoadIcon((HINSTANCE
)NULL
, stdIcons
[nIcon
].id
);
557 wxSize size
= wxGetHiconSize(hicon
);
558 icon
->SetSize(size
.x
, size
.y
);
560 icon
->SetHICON((WXHICON
)hicon
);
565 // ----------------------------------------------------------------------------
567 // ----------------------------------------------------------------------------
569 wxSize
wxGetHiconSize(HICON
WXUNUSED_IN_WINCE(hicon
))
571 // default icon size on this hardware
572 // usually 32x32 but can be other (smaller) on pocket devices
573 wxSize
size(::GetSystemMetrics(SM_CXICON
), ::GetSystemMetrics(SM_CYICON
));
576 if ( hicon
&& wxGetOsVersion() != wxWIN32S
)
579 if ( !::GetIconInfo(hicon
, &info
) )
581 wxLogLastError(wxT("GetIconInfo"));
585 HBITMAP hbmp
= info
.hbmMask
;
589 if ( ::GetObject(hbmp
, sizeof(BITMAP
), (LPSTR
) &bm
) )
591 size
= wxSize(bm
.bmWidth
, bm
.bmHeight
);
594 ::DeleteObject(info
.hbmMask
);
597 ::DeleteObject(info
.hbmColor
);
605 #endif // __WXMICROWIN__