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::Node
*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::Node
*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::Node
*node
= ms_handlers
.GetFirst();
274 wxGDIImageHandler
*handler
= node
->GetData();
275 if ( handler
->GetType() == type
)
278 node
= node
->GetNext();
284 void wxGDIImage::CleanUpHandlers()
286 wxGDIImageHandlerList::Node
*node
= ms_handlers
.GetFirst();
289 wxGDIImageHandler
*handler
= node
->GetData();
290 wxGDIImageHandlerList::Node
*next
= node
->GetNext();
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
)
376 #ifdef __DIGITALMARS__
377 //FIXME __DIGITALMARS__ April 2003 CE
378 // why no ExtractIcon in library
379 wxLogTrace(_T("iconload"),
380 _T("Returning false from wxICOFileHandler::Load because of DigitalMars compiler bug"));
391 // Parse the filename: it may be of the form "filename;n" in order to
392 // specify the nth icon in the file.
394 // For the moment, ignore the issue of possible semicolons in the
397 wxString
nameReal(name
);
398 wxString strIconIndex
= name
.AfterLast(wxT(';'));
399 if (strIconIndex
!= name
)
401 iconIndex
= wxAtoi(strIconIndex
);
402 nameReal
= name
.BeforeLast(wxT(';'));
406 // If we don't know what size icon we're looking for,
407 // try to find out what's there.
408 // Unfortunately this doesn't work, because ExtractIconEx
409 // will scale the icon to the 'desired' size, even if that
410 // size of icon isn't explicitly stored. So we would have
411 // to parse the icon file outselves.
412 if ( desiredWidth
== -1 &&
415 // Try loading a large icon first
416 if ( ::ExtractIconEx(nameReal
, iconIndex
, &hicon
, NULL
, 1) == 1)
419 // Then try loading a small icon
420 else if ( ::ExtractIconEx(nameReal
, iconIndex
, NULL
, &hicon
, 1) == 1)
426 // were we asked for a large icon?
427 if ( desiredWidth
== ::GetSystemMetrics(SM_CXICON
) &&
428 desiredHeight
== ::GetSystemMetrics(SM_CYICON
) )
430 // get the specified large icon from file
431 if ( !::ExtractIconEx(nameReal
, iconIndex
, &hicon
, NULL
, 1) )
433 // it is not an error, but it might still be useful to be informed
434 // about it optionally
435 wxLogTrace(_T("iconload"),
436 _T("No large icons found in the file '%s'."),
440 else if ( desiredWidth
== ::GetSystemMetrics(SM_CXSMICON
) &&
441 desiredHeight
== ::GetSystemMetrics(SM_CYSMICON
) )
443 // get the specified small icon from file
444 if ( !::ExtractIconEx(nameReal
, iconIndex
, NULL
, &hicon
, 1) )
446 wxLogTrace(_T("iconload"),
447 _T("No small icons found in the file '%s'."),
451 //else: not standard size, load below
455 // take any size icon from the file by index
456 hicon
= ::ExtractIcon(wxGetInstance(), nameReal
, iconIndex
);
461 wxLogSysError(_T("Failed to load icon from the file '%s'"),
467 size
= wxGetHiconSize(hicon
);
469 if ( (desiredWidth
!= -1 && desiredWidth
!= size
.x
) ||
470 (desiredHeight
!= -1 && desiredHeight
!= size
.y
) )
472 wxLogTrace(_T("iconload"),
473 _T("Returning false from wxICOFileHandler::Load because of the size mismatch: actual (%d, %d), requested (%d, %d)"),
475 desiredWidth
, desiredHeight
);
477 ::DestroyIcon(hicon
);
482 icon
->SetHICON((WXHICON
)hicon
);
483 icon
->SetSize(size
.x
, size
.y
);
486 #endif // Digitalmars
489 bool wxICOResourceHandler::LoadIcon(wxIcon
*icon
,
490 const wxString
& name
,
491 long WXUNUSED(flags
),
492 int desiredWidth
, int desiredHeight
)
496 // do we need the icon of the specific size or would any icon do?
497 bool hasSize
= desiredWidth
!= -1 || desiredHeight
!= -1;
499 wxASSERT_MSG( !hasSize
|| (desiredWidth
!= -1 && desiredHeight
!= -1),
500 _T("width and height should be either both -1 or not") );
502 // try to load the icon from this program first to allow overriding the
503 // standard icons (although why one would want to do it considering that
504 // we already have wxApp::GetStdIcon() is unclear)
506 // note that we can't just always call LoadImage() because it seems to do
507 // some icon rescaling internally which results in very ugly 16x16 icons
510 hicon
= (HICON
)::LoadImage(wxGetInstance(), name
, IMAGE_ICON
,
511 desiredWidth
, desiredHeight
,
516 hicon
= ::LoadIcon(wxGetInstance(), name
);
519 // next check if it's not a standard icon
520 if ( !hicon
&& !hasSize
)
528 { wxT("wxICON_QUESTION"), IDI_QUESTION
},
529 { wxT("wxICON_WARNING"), IDI_EXCLAMATION
},
530 { wxT("wxICON_ERROR"), IDI_HAND
},
531 { wxT("wxICON_INFORMATION"), IDI_ASTERISK
},
534 for ( size_t nIcon
= 0; !hicon
&& nIcon
< WXSIZEOF(stdIcons
); nIcon
++ )
536 if ( name
== stdIcons
[nIcon
].name
)
538 hicon
= ::LoadIcon((HINSTANCE
)NULL
, stdIcons
[nIcon
].id
);
543 wxSize size
= wxGetHiconSize(hicon
);
544 icon
->SetSize(size
.x
, size
.y
);
546 icon
->SetHICON((WXHICON
)hicon
);
551 // ----------------------------------------------------------------------------
553 // ----------------------------------------------------------------------------
555 wxSize
wxGetHiconSize(HICON hicon
)
557 wxSize
size(32, 32); // default
559 if ( hicon
&& wxGetOsVersion() != wxWIN32S
)
562 if ( !::GetIconInfo(hicon
, &info
) )
564 wxLogLastError(wxT("GetIconInfo"));
568 HBITMAP hbmp
= info
.hbmMask
;
572 if ( ::GetObject(hbmp
, sizeof(BITMAP
), (LPSTR
) &bm
) )
574 size
= wxSize(bm
.bmWidth
, bm
.bmHeight
);
577 ::DeleteObject(info
.hbmMask
);
580 ::DeleteObject(info
.hbmColor
);
587 #endif // __WXMICROWIN__