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 license
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 #ifndef __WXMICROWIN__
41 #include "wx/msw/dib.h"
44 #include "wx/msw/bitmap.h"
45 #include "wx/msw/gdiimage.h"
46 #include "wx/bitmap.h"
48 #include "wx/listimpl.cpp"
49 WX_DEFINE_LIST(wxGDIImageHandlerList
);
53 # include "wx/msw/curico.h"
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 #ifndef __WXMICROWIN__
62 // all image handlers are declared/defined in this file because the outside
63 // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids)
65 class WXDLLEXPORT wxBMPFileHandler
: public wxBitmapHandler
68 wxBMPFileHandler() : wxBitmapHandler(_T("Windows bitmap file"), _T("bmp"),
73 virtual bool LoadFile(wxBitmap
*bitmap
,
74 const wxString
& name
, long flags
,
75 int desiredWidth
, int desiredHeight
);
76 virtual bool SaveFile(wxBitmap
*bitmap
,
77 const wxString
& name
, int type
,
78 const wxPalette
*palette
= NULL
);
81 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
)
84 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
87 wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"),
89 wxBITMAP_TYPE_BMP_RESOURCE
)
93 virtual bool LoadFile(wxBitmap
*bitmap
,
94 const wxString
& name
, long flags
,
95 int desiredWidth
, int desiredHeight
);
98 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
101 class WXDLLEXPORT wxIconHandler
: public wxGDIImageHandler
104 wxIconHandler(const wxString
& name
, const wxString
& ext
, long type
)
105 : wxGDIImageHandler(name
, ext
, type
)
109 // creating and saving icons is not supported
110 virtual bool Create(wxGDIImage
*WXUNUSED(image
),
111 void *WXUNUSED(data
),
112 long WXUNUSED(flags
),
114 int WXUNUSED(height
),
115 int WXUNUSED(depth
) = 1)
120 virtual bool Save(wxGDIImage
*WXUNUSED(image
),
121 const wxString
& WXUNUSED(name
),
127 virtual bool Load(wxGDIImage
*image
,
128 const wxString
& name
,
130 int desiredWidth
, int desiredHeight
)
132 wxIcon
*icon
= wxDynamicCast(image
, wxIcon
);
133 wxCHECK_MSG( icon
, false, _T("wxIconHandler only works with icons") );
135 return LoadIcon(icon
, name
, flags
, desiredWidth
, desiredHeight
);
139 virtual bool LoadIcon(wxIcon
*icon
,
140 const wxString
& name
, long flags
,
141 int desiredWidth
= -1, int desiredHeight
= -1) = 0;
144 class WXDLLEXPORT wxICOFileHandler
: public wxIconHandler
147 wxICOFileHandler() : wxIconHandler(_T("ICO icon file"),
153 virtual bool LoadIcon(wxIcon
*icon
,
154 const wxString
& name
, long flags
,
155 int desiredWidth
= -1, int desiredHeight
= -1);
158 DECLARE_DYNAMIC_CLASS(wxICOFileHandler
)
161 class WXDLLEXPORT wxICOResourceHandler
: public wxIconHandler
164 wxICOResourceHandler() : wxIconHandler(_T("ICO resource"),
166 wxBITMAP_TYPE_ICO_RESOURCE
)
170 virtual bool LoadIcon(wxIcon
*icon
,
171 const wxString
& name
, long flags
,
172 int desiredWidth
= -1, int desiredHeight
= -1);
175 DECLARE_DYNAMIC_CLASS(wxICOResourceHandler
)
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
182 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
183 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
184 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler
, wxObject
)
185 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler
, wxObject
)
187 // ----------------------------------------------------------------------------
189 // ----------------------------------------------------------------------------
194 // ============================================================================
196 // ============================================================================
198 wxGDIImageHandlerList
wxGDIImage::ms_handlers
;
200 // ----------------------------------------------------------------------------
201 // wxGDIImage functions forwarded to wxGDIImageRefData
202 // ----------------------------------------------------------------------------
204 bool wxGDIImage::FreeResource(bool WXUNUSED(force
))
208 GetGDIImageData()->Free();
209 GetGDIImageData()->m_handle
= 0;
215 WXHANDLE
wxGDIImage::GetResourceHandle() const
220 // ----------------------------------------------------------------------------
221 // wxGDIImage handler stuff
222 // ----------------------------------------------------------------------------
224 void wxGDIImage::AddHandler(wxGDIImageHandler
*handler
)
226 ms_handlers
.Append(handler
);
229 void wxGDIImage::InsertHandler(wxGDIImageHandler
*handler
)
231 ms_handlers
.Insert(handler
);
234 bool wxGDIImage::RemoveHandler(const wxString
& name
)
236 wxGDIImageHandler
*handler
= FindHandler(name
);
239 ms_handlers
.DeleteObject(handler
);
246 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& name
)
248 wxGDIImageHandlerList::Node
*node
= ms_handlers
.GetFirst();
251 wxGDIImageHandler
*handler
= node
->GetData();
252 if ( handler
->GetName() == name
)
254 node
= node
->GetNext();
260 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& extension
,
263 wxGDIImageHandlerList::Node
*node
= ms_handlers
.GetFirst();
266 wxGDIImageHandler
*handler
= node
->GetData();
267 if ( (handler
->GetExtension() = extension
) &&
268 (type
== -1 || handler
->GetType() == type
) )
273 node
= node
->GetNext();
278 wxGDIImageHandler
*wxGDIImage::FindHandler(long type
)
280 wxGDIImageHandlerList::Node
*node
= ms_handlers
.GetFirst();
283 wxGDIImageHandler
*handler
= node
->GetData();
284 if ( handler
->GetType() == type
)
287 node
= node
->GetNext();
293 void wxGDIImage::CleanUpHandlers()
295 wxGDIImageHandlerList::Node
*node
= ms_handlers
.GetFirst();
298 wxGDIImageHandler
*handler
= node
->GetData();
299 wxGDIImageHandlerList::Node
*next
= node
->GetNext();
306 void wxGDIImage::InitStandardHandlers()
308 #ifndef __WXMICROWIN__
309 AddHandler(new wxBMPResourceHandler
);
310 AddHandler(new wxBMPFileHandler
);
311 AddHandler(new wxICOResourceHandler
);
312 AddHandler(new wxICOFileHandler
);
316 #ifndef __WXMICROWIN__
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
322 bool wxBMPResourceHandler::LoadFile(wxBitmap
*bitmap
,
323 const wxString
& name
, long WXUNUSED(flags
),
324 int WXUNUSED(desiredWidth
),
325 int WXUNUSED(desiredHeight
))
327 // TODO: load colourmap.
328 bitmap
->SetHBITMAP((WXHBITMAP
)::LoadBitmap(wxGetInstance(), name
));
332 // it's probably not found
333 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
340 if ( !::GetObject(GetHbitmapOf(*bitmap
), sizeof(BITMAP
), (LPSTR
) &bm
) )
342 wxLogLastError(wxT("GetObject(HBITMAP)"));
345 bitmap
->SetWidth(bm
.bmWidth
);
346 bitmap
->SetHeight(bm
.bmHeight
);
347 bitmap
->SetDepth(bm
.bmBitsPixel
);
352 bool wxBMPFileHandler::LoadFile(wxBitmap
*bitmap
,
353 const wxString
& name
, long WXUNUSED(flags
),
354 int WXUNUSED(desiredWidth
),
355 int WXUNUSED(desiredHeight
))
357 #if wxUSE_IMAGE_LOADING_IN_MSW
358 wxPalette
*palette
= NULL
;
359 bool success
= wxLoadIntoBitmap(WXSTRINGCAST name
, bitmap
, &palette
) != 0;
362 if ( success
&& palette
)
364 bitmap
->SetPalette(*palette
);
367 // it was copied by the bitmap if it was loaded successfully
369 #endif // wxUSE_PALETTE
377 bool wxBMPFileHandler::SaveFile(wxBitmap
*bitmap
,
378 const wxString
& name
,
380 const wxPalette
*pal
)
382 #if wxUSE_IMAGE_LOADING_IN_MSW
385 wxPalette
*actualPalette
= (wxPalette
*)pal
;
386 if ( !actualPalette
)
387 actualPalette
= bitmap
->GetPalette();
389 wxPalette
*actualPalette
= NULL
;
390 #endif // wxUSE_PALETTE
392 return wxSaveBitmap(WXSTRINGCAST name
, bitmap
, actualPalette
) != 0;
398 // ----------------------------------------------------------------------------
400 // ----------------------------------------------------------------------------
402 bool wxICOFileHandler::LoadIcon(wxIcon
*icon
,
403 const wxString
& name
,
404 long WXUNUSED(flags
),
405 int desiredWidth
, int desiredHeight
)
407 #if wxUSE_RESOURCE_LOADING_IN_MSW
416 // Parse the filename: it may be of the form "filename;n" in order to
417 // specify the nth icon in the file.
419 // For the moment, ignore the issue of possible semicolons in the
422 wxString
nameReal(name
);
423 wxString strIconIndex
= name
.AfterLast(wxT(';'));
424 if (strIconIndex
!= name
)
426 iconIndex
= wxAtoi(strIconIndex
);
427 nameReal
= name
.BeforeLast(wxT(';'));
431 // If we don't know what size icon we're looking for,
432 // try to find out what's there.
433 // Unfortunately this doesn't work, because ExtractIconEx
434 // will scale the icon to the 'desired' size, even if that
435 // size of icon isn't explicitly stored. So we would have
436 // to parse the icon file outselves.
437 if ( desiredWidth
== -1 &&
440 // Try loading a large icon first
441 if ( ::ExtractIconEx(nameReal
, iconIndex
, &hicon
, NULL
, 1) == 1)
444 // Then try loading a small icon
445 else if ( ::ExtractIconEx(nameReal
, iconIndex
, NULL
, &hicon
, 1) == 1)
451 // were we asked for a large icon?
452 if ( desiredWidth
== ::GetSystemMetrics(SM_CXICON
) &&
453 desiredHeight
== ::GetSystemMetrics(SM_CYICON
) )
455 // get the specified large icon from file
456 if ( !::ExtractIconEx(nameReal
, iconIndex
, &hicon
, NULL
, 1) )
458 // it is not an error, but it might still be useful to be informed
459 // about it optionally
460 wxLogTrace(_T("iconload"),
461 _T("No large icons found in the file '%s'."),
465 else if ( desiredWidth
== ::GetSystemMetrics(SM_CXSMICON
) &&
466 desiredHeight
== ::GetSystemMetrics(SM_CYSMICON
) )
468 // get the specified small icon from file
469 if ( !::ExtractIconEx(nameReal
, iconIndex
, NULL
, &hicon
, 1) )
471 wxLogTrace(_T("iconload"),
472 _T("No small icons found in the file '%s'."),
476 //else: not standard size, load below
480 // take any size icon from the file by index
481 hicon
= ::ExtractIcon(wxGetInstance(), nameReal
, iconIndex
);
486 wxLogSysError(_T("Failed to load icon from the file '%s'"),
492 size
= wxGetHiconSize(hicon
);
494 HICON hicon
= ReadIconFile((wxChar
*)name
.c_str(),
497 #endif // Win32/Win16
499 if ( (desiredWidth
!= -1 && desiredWidth
!= size
.x
) ||
500 (desiredHeight
!= -1 && desiredHeight
!= size
.y
) )
502 wxLogTrace(_T("iconload"),
503 _T("Returning false from wxICOFileHandler::Load because of the size mismatch: actual (%d, %d), requested (%d, %d)"),
505 desiredWidth
, desiredHeight
);
507 ::DestroyIcon(hicon
);
512 icon
->SetHICON((WXHICON
)hicon
);
513 icon
->SetSize(size
.x
, size
.y
);
521 bool wxICOResourceHandler::LoadIcon(wxIcon
*icon
,
522 const wxString
& name
,
523 long WXUNUSED(flags
),
524 int desiredWidth
, int desiredHeight
)
528 // do we need the icon of the specific size or would any icon do?
529 bool hasSize
= desiredWidth
!= -1 || desiredHeight
!= -1;
531 wxASSERT_MSG( !hasSize
|| (desiredWidth
!= -1 && desiredHeight
!= -1),
532 _T("width and height should be either both -1 or not") );
534 // try to load the icon from this program first to allow overriding the
535 // standard icons (although why one would want to do it considering that
536 // we already have wxApp::GetStdIcon() is unclear)
538 // note that we can't just always call LoadImage() because it seems to do
539 // some icon rescaling internally which results in very ugly 16x16 icons
540 #if defined(__WIN32__) && !defined(__SC__)
543 hicon
= (HICON
)::LoadImage(wxGetInstance(), name
, IMAGE_ICON
,
544 desiredWidth
, desiredHeight
,
548 #endif // Win32/!Win32
550 hicon
= ::LoadIcon(wxGetInstance(), name
);
553 // next check if it's not a standard icon
554 if ( !hicon
&& !hasSize
)
562 { wxT("wxICON_QUESTION"), IDI_QUESTION
},
563 { wxT("wxICON_WARNING"), IDI_EXCLAMATION
},
564 { wxT("wxICON_ERROR"), IDI_HAND
},
565 { wxT("wxICON_INFORMATION"), IDI_ASTERISK
},
568 for ( size_t nIcon
= 0; !hicon
&& nIcon
< WXSIZEOF(stdIcons
); nIcon
++ )
570 if ( name
== stdIcons
[nIcon
].name
)
572 hicon
= ::LoadIcon((HINSTANCE
)NULL
, stdIcons
[nIcon
].id
);
577 wxSize size
= wxGetHiconSize(hicon
);
578 icon
->SetSize(size
.x
, size
.y
);
580 icon
->SetHICON((WXHICON
)hicon
);
585 // ----------------------------------------------------------------------------
587 // ----------------------------------------------------------------------------
589 wxSize
wxGetHiconSize(HICON hicon
)
591 wxSize
size(32, 32); // default
594 // Win32s doesn't have GetIconInfo function...
595 if ( hicon
&& wxGetOsVersion() != wxWIN32S
)
598 if ( !::GetIconInfo(hicon
, &info
) )
600 wxLogLastError(wxT("GetIconInfo"));
604 HBITMAP hbmp
= info
.hbmMask
;
608 if ( ::GetObject(hbmp
, sizeof(BITMAP
), (LPSTR
) &bm
) )
610 size
= wxSize(bm
.bmWidth
, bm
.bmHeight
);
613 ::DeleteObject(info
.hbmMask
);
616 ::DeleteObject(info
.hbmColor
);