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"
49 # include "wx/msw/curico.h"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 #ifndef __WXMICROWIN__
58 // all image handlers are declared/defined in this file because the outside
59 // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids)
61 class WXDLLEXPORT wxBMPFileHandler
: public wxBitmapHandler
64 wxBMPFileHandler() : wxBitmapHandler(_T("Windows bitmap file"), _T("bmp"),
69 virtual bool LoadFile(wxBitmap
*bitmap
,
70 const wxString
& name
, long flags
,
71 int desiredWidth
, int desiredHeight
);
72 virtual bool SaveFile(wxBitmap
*bitmap
,
73 const wxString
& name
, int type
,
74 const wxPalette
*palette
= NULL
);
77 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
)
80 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
83 wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"),
85 wxBITMAP_TYPE_BMP_RESOURCE
)
89 virtual bool LoadFile(wxBitmap
*bitmap
,
90 const wxString
& name
, long flags
,
91 int desiredWidth
, int desiredHeight
);
94 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
97 class WXDLLEXPORT wxIconHandler
: public wxGDIImageHandler
100 wxIconHandler(const wxString
& name
, const wxString
& ext
, long type
)
101 : wxGDIImageHandler(name
, ext
, type
)
105 // creating and saving icons is not supported
106 virtual bool Create(wxGDIImage
*WXUNUSED(image
),
107 void *WXUNUSED(data
),
108 long WXUNUSED(flags
),
110 int WXUNUSED(height
),
111 int WXUNUSED(depth
) = 1)
116 virtual bool Save(wxGDIImage
*WXUNUSED(image
),
117 const wxString
& WXUNUSED(name
),
123 virtual bool Load(wxGDIImage
*image
,
124 const wxString
& name
,
126 int desiredWidth
, int desiredHeight
)
128 wxIcon
*icon
= wxDynamicCast(image
, wxIcon
);
129 wxCHECK_MSG( icon
, FALSE
, _T("wxIconHandler only works with icons") );
131 return LoadIcon(icon
, name
, flags
, desiredWidth
, desiredHeight
);
135 virtual bool LoadIcon(wxIcon
*icon
,
136 const wxString
& name
, long flags
,
137 int desiredWidth
= -1, int desiredHeight
= -1) = 0;
140 class WXDLLEXPORT wxICOFileHandler
: public wxIconHandler
143 wxICOFileHandler() : wxIconHandler(_T("ICO icon file"),
149 virtual bool LoadIcon(wxIcon
*icon
,
150 const wxString
& name
, long flags
,
151 int desiredWidth
= -1, int desiredHeight
= -1);
154 DECLARE_DYNAMIC_CLASS(wxICOFileHandler
)
157 class WXDLLEXPORT wxICOResourceHandler
: public wxIconHandler
160 wxICOResourceHandler() : wxIconHandler(_T("ICO resource"),
162 wxBITMAP_TYPE_ICO_RESOURCE
)
166 virtual bool LoadIcon(wxIcon
*icon
,
167 const wxString
& name
, long flags
,
168 int desiredWidth
= -1, int desiredHeight
= -1);
171 DECLARE_DYNAMIC_CLASS(wxICOResourceHandler
)
174 // ----------------------------------------------------------------------------
176 // ----------------------------------------------------------------------------
178 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
179 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
180 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler
, wxGDIImageHandler
)
181 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler
, wxGDIImageHandler
)
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
187 static wxSize
GetHiconSize(HICON hicon
);
191 // ============================================================================
193 // ============================================================================
195 wxList
wxGDIImage::ms_handlers
;
197 // ----------------------------------------------------------------------------
198 // wxGDIImage functions forwarded to wxGDIImageRefData
199 // ----------------------------------------------------------------------------
201 bool wxGDIImage::FreeResource(bool WXUNUSED(force
))
205 GetGDIImageData()->Free();
206 GetGDIImageData()->m_handle
= 0;
212 WXHANDLE
wxGDIImage::GetResourceHandle()
217 // ----------------------------------------------------------------------------
218 // wxGDIImage handler stuff
219 // ----------------------------------------------------------------------------
221 void wxGDIImage::AddHandler(wxGDIImageHandler
*handler
)
223 ms_handlers
.Append(handler
);
226 void wxGDIImage::InsertHandler(wxGDIImageHandler
*handler
)
228 ms_handlers
.Insert(handler
);
231 bool wxGDIImage::RemoveHandler(const wxString
& name
)
233 wxGDIImageHandler
*handler
= FindHandler(name
);
236 ms_handlers
.DeleteObject(handler
);
243 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& name
)
245 wxNode
*node
= ms_handlers
.First();
248 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
249 if ( handler
->GetName() == name
)
257 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& extension
,
260 wxNode
*node
= ms_handlers
.First();
263 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
264 if ( (handler
->GetExtension() = extension
) &&
265 (type
== -1 || handler
->GetType() == type
) )
275 wxGDIImageHandler
*wxGDIImage::FindHandler(long type
)
277 wxNode
*node
= ms_handlers
.First();
280 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
281 if ( handler
->GetType() == type
)
290 void wxGDIImage::CleanUpHandlers()
292 wxNode
*node
= ms_handlers
.First();
295 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
296 wxNode
*next
= node
->Next();
303 void wxGDIImage::InitStandardHandlers()
305 #ifndef __WXMICROWIN__
306 AddHandler(new wxBMPResourceHandler
);
307 AddHandler(new wxBMPFileHandler
);
308 AddHandler(new wxICOResourceHandler
);
309 AddHandler(new wxICOFileHandler
);
313 #ifndef __WXMICROWIN__
315 // ----------------------------------------------------------------------------
317 // ----------------------------------------------------------------------------
319 bool wxBMPResourceHandler::LoadFile(wxBitmap
*bitmap
,
320 const wxString
& name
, long WXUNUSED(flags
),
321 int WXUNUSED(desiredWidth
),
322 int WXUNUSED(desiredHeight
))
324 // TODO: load colourmap.
325 bitmap
->SetHBITMAP((WXHBITMAP
)::LoadBitmap(wxGetInstance(), name
));
327 wxBitmapRefData
*data
= bitmap
->GetBitmapData();
331 if ( !::GetObject(GetHbitmapOf(*bitmap
), sizeof(BITMAP
), (LPSTR
) &bm
) )
333 wxLogLastError(wxT("GetObject(HBITMAP)"));
336 data
->m_width
= bm
.bmWidth
;
337 data
->m_height
= bm
.bmHeight
;
338 data
->m_depth
= bm
.bmBitsPixel
;
342 // it's probably not found
343 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
350 bool wxBMPFileHandler::LoadFile(wxBitmap
*bitmap
,
351 const wxString
& name
, long WXUNUSED(flags
),
352 int WXUNUSED(desiredWidth
),
353 int WXUNUSED(desiredHeight
))
355 #if wxUSE_IMAGE_LOADING_IN_MSW
356 wxPalette
*palette
= NULL
;
357 bool success
= wxLoadIntoBitmap(WXSTRINGCAST name
, bitmap
, &palette
) != 0;
358 if ( success
&& palette
)
360 bitmap
->SetPalette(*palette
);
363 // it was copied by the bitmap if it was loaded successfully
372 bool wxBMPFileHandler::SaveFile(wxBitmap
*bitmap
,
373 const wxString
& name
,
375 const wxPalette
*pal
)
377 #if wxUSE_IMAGE_LOADING_IN_MSW
378 wxPalette
*actualPalette
= (wxPalette
*)pal
;
379 if ( !actualPalette
)
380 actualPalette
= bitmap
->GetPalette();
381 return wxSaveBitmap(WXSTRINGCAST name
, bitmap
, actualPalette
) != 0;
387 // ----------------------------------------------------------------------------
389 // ----------------------------------------------------------------------------
391 bool wxICOFileHandler::LoadIcon(wxIcon
*icon
,
392 const wxString
& name
,
393 long WXUNUSED(flags
),
394 int desiredWidth
, int desiredHeight
)
396 #if wxUSE_RESOURCE_LOADING_IN_MSW
405 // Parse the filename: it may be of the form "filename;n" in order to
406 // specify the nth icon in the file.
408 // For the moment, ignore the issue of possible semicolons in the
411 wxString
nameReal(name
);
412 wxString strIconIndex
= name
.AfterLast(wxT(';'));
413 if (strIconIndex
!= name
)
415 iconIndex
= wxAtoi(strIconIndex
);
416 nameReal
= name
.BeforeLast(wxT(';'));
419 // were we asked for a large icon?
420 if ( desiredWidth
== ::GetSystemMetrics(SM_CXICON
) &&
421 desiredHeight
== ::GetSystemMetrics(SM_CYICON
) )
423 // get the specified large icon from file
424 if ( !::ExtractIconEx(nameReal
, iconIndex
, &hicon
, NULL
, 1) )
426 // it is not an error, but it might still be useful to be informed
427 // about it optionally
428 wxLogTrace(_T("iconload"),
429 _T("No large icons found in the file '%s'."),
433 else if ( desiredWidth
== ::GetSystemMetrics(SM_CXSMICON
) &&
434 desiredHeight
== ::GetSystemMetrics(SM_CYSMICON
) )
436 // get the specified small icon from file
437 if ( !::ExtractIconEx(nameReal
, iconIndex
, NULL
, &hicon
, 1) )
439 wxLogTrace(_T("iconload"),
440 _T("No small icons found in the file '%s'."),
444 //else: not standard size, load below
448 // take any (the first one) icon from the file by default
449 hicon
= ::ExtractIcon(wxGetInstance(), nameReal
, 0 /* first */);
454 wxLogSysError(_T("Failed to load icon from the file '%s'"),
460 size
= GetHiconSize(hicon
);
462 HICON hicon
= ReadIconFile((wxChar
*)name
.c_str(),
465 #endif // Win32/Win16
467 if ( (desiredWidth
!= -1 && desiredWidth
!= size
.x
) ||
468 (desiredHeight
!= -1 && desiredHeight
!= size
.y
) )
470 wxLogTrace(_T("iconload"),
471 _T("Returning FALSE from wxICOFileHandler::Load because of the size mismatch: actual (%d, %d), requested (%d, %d)"),
473 desiredWidth
, desiredHeight
);
475 ::DestroyIcon(hicon
);
480 icon
->SetHICON((WXHICON
)hicon
);
481 icon
->SetSize(size
.x
, size
.y
);
489 bool wxICOResourceHandler::LoadIcon(wxIcon
*icon
,
490 const wxString
& name
,
491 long WXUNUSED(flags
),
492 int desiredWidth
, int desiredHeight
)
496 // try to load the icon from this program first to allow overriding the
497 // standard icons (although why one would want to do it considering that
498 // we already have wxApp::GetStdIcon() is unclear)
499 #if defined(__WIN32__) && !defined(__SC__)
500 if ( desiredWidth
!= -1 && desiredHeight
!= -1 )
502 hicon
= (HICON
)::LoadImage(wxGetInstance(), name
, IMAGE_ICON
,
503 desiredWidth
, desiredHeight
,
509 hicon
= ::LoadIcon(wxGetInstance(), name
);
512 // next check if it's not a standard icon
521 { wxT("wxICON_QUESTION"), IDI_QUESTION
},
522 { wxT("wxICON_WARNING"), IDI_EXCLAMATION
},
523 { wxT("wxICON_ERROR"), IDI_HAND
},
524 { wxT("wxICON_INFO"), IDI_ASTERISK
},
527 for ( size_t nIcon
= 0; !hicon
&& nIcon
< WXSIZEOF(stdIcons
); nIcon
++ )
529 if ( name
== stdIcons
[nIcon
].name
)
531 hicon
= (HICON
)::LoadImage((HINSTANCE
)NULL
,
534 desiredWidth
, desiredHeight
,
540 wxSize size
= GetHiconSize(hicon
);
541 icon
->SetSize(size
.x
, size
.y
);
543 // Override the found values with desired values
544 if ( desiredWidth
> -1 && desiredHeight
> -1 )
546 icon
->SetSize(desiredWidth
, desiredHeight
);
549 icon
->SetHICON((WXHICON
)hicon
);
554 // ----------------------------------------------------------------------------
556 // ----------------------------------------------------------------------------
558 static wxSize
GetHiconSize(HICON hicon
)
560 wxSize
size(32, 32); // default
563 // Win32s doesn't have GetIconInfo function...
564 if ( hicon
&& wxGetOsVersion() != wxWIN32S
)
567 if ( !::GetIconInfo(hicon
, &info
) )
569 wxLogLastError(wxT("GetIconInfo"));
573 HBITMAP hbmp
= info
.hbmMask
;
577 if ( ::GetObject(hbmp
, sizeof(BITMAP
), (LPSTR
) &bm
) )
579 size
= wxSize(bm
.bmWidth
, bm
.bmHeight
);
582 ::DeleteObject(info
.hbmMask
);
585 ::DeleteObject(info
.hbmColor
);