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 #include "wx/msw/dib.h"
41 #include "wx/msw/bitmap.h"
42 #include "wx/msw/gdiimage.h"
43 #include "wx/bitmap.h"
46 # include "wx/xpmhand.h"
47 #endif // wxUSE_XPM_IN_MSW
50 # include "wx/msw/curico.h"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 // all image handlers are declared/defined in this file because the outside
58 // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids)
60 class WXDLLEXPORT wxBMPFileHandler
: public wxBitmapHandler
63 wxBMPFileHandler() : wxBitmapHandler(_T("Windows bitmap file"), _T("bmp"),
68 virtual bool LoadFile(wxBitmap
*bitmap
,
69 const wxString
& name
, long flags
,
70 int desiredWidth
, int desiredHeight
);
71 virtual bool SaveFile(wxBitmap
*bitmap
,
72 const wxString
& name
, int type
,
73 const wxPalette
*palette
= NULL
);
76 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
)
79 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
82 wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"),
84 wxBITMAP_TYPE_BMP_RESOURCE
)
88 virtual bool LoadFile(wxBitmap
*bitmap
,
89 const wxString
& name
, long flags
,
90 int desiredWidth
, int desiredHeight
);
93 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
96 class WXDLLEXPORT wxIconHandler
: public wxGDIImageHandler
99 wxIconHandler(const wxString
& name
, const wxString
& ext
, long type
)
100 : wxGDIImageHandler(name
, ext
, type
)
104 // creating and saving icons is not supported
105 virtual bool Create(wxGDIImage
*WXUNUSED(image
),
106 void *WXUNUSED(data
),
107 long WXUNUSED(flags
),
109 int WXUNUSED(height
),
110 int WXUNUSED(depth
) = 1)
115 virtual bool Save(wxGDIImage
*WXUNUSED(image
),
116 const wxString
& WXUNUSED(name
),
122 virtual bool Load(wxGDIImage
*image
,
123 const wxString
& name
,
125 int desiredWidth
, int desiredHeight
)
127 wxIcon
*icon
= wxDynamicCast(image
, wxIcon
);
128 wxCHECK_MSG( icon
, FALSE
, _T("wxIconHandler only works with icons") );
130 return LoadIcon(icon
, name
, flags
, desiredWidth
, desiredHeight
);
134 virtual bool LoadIcon(wxIcon
*icon
,
135 const wxString
& name
, long flags
,
136 int desiredWidth
= -1, int desiredHeight
= -1) = 0;
139 class WXDLLEXPORT wxICOFileHandler
: public wxIconHandler
142 wxICOFileHandler() : wxIconHandler(_T("ICO icon file"),
148 virtual bool LoadIcon(wxIcon
*icon
,
149 const wxString
& name
, long flags
,
150 int desiredWidth
= -1, int desiredHeight
= -1);
153 DECLARE_DYNAMIC_CLASS(wxICOFileHandler
)
156 class WXDLLEXPORT wxICOResourceHandler
: public wxIconHandler
159 wxICOResourceHandler() : wxIconHandler(_T("ICO resource"),
161 wxBITMAP_TYPE_ICO_RESOURCE
)
165 virtual bool LoadIcon(wxIcon
*icon
,
166 const wxString
& name
, long flags
,
167 int desiredWidth
= -1, int desiredHeight
= -1);
170 DECLARE_DYNAMIC_CLASS(wxICOResourceHandler
)
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
177 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
178 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
179 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler
, wxGDIImageHandler
)
180 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler
, wxGDIImageHandler
)
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
186 static wxSize
GetHiconSize(HICON hicon
);
188 // ============================================================================
190 // ============================================================================
192 wxList
wxGDIImage::ms_handlers
;
194 // ----------------------------------------------------------------------------
195 // wxGDIImage functions forwarded to wxGDIImageRefData
196 // ----------------------------------------------------------------------------
198 bool wxGDIImage::FreeResource(bool WXUNUSED(force
))
202 GetGDIImageData()->Free();
203 GetGDIImageData()->m_handle
= 0;
209 WXHANDLE
wxGDIImage::GetResourceHandle()
214 // ----------------------------------------------------------------------------
215 // wxGDIImage handler stuff
216 // ----------------------------------------------------------------------------
218 void wxGDIImage::AddHandler(wxGDIImageHandler
*handler
)
220 ms_handlers
.Append(handler
);
223 void wxGDIImage::InsertHandler(wxGDIImageHandler
*handler
)
225 ms_handlers
.Insert(handler
);
228 bool wxGDIImage::RemoveHandler(const wxString
& name
)
230 wxGDIImageHandler
*handler
= FindHandler(name
);
233 ms_handlers
.DeleteObject(handler
);
240 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& name
)
242 wxNode
*node
= ms_handlers
.First();
245 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
246 if ( handler
->GetName() == name
)
254 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& extension
,
257 wxNode
*node
= ms_handlers
.First();
260 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
261 if ( (handler
->GetExtension() = extension
) &&
262 (type
== -1 || handler
->GetType() == type
) )
272 wxGDIImageHandler
*wxGDIImage::FindHandler(long type
)
274 wxNode
*node
= ms_handlers
.First();
277 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
278 if ( handler
->GetType() == type
)
287 void wxGDIImage::CleanUpHandlers()
289 wxNode
*node
= ms_handlers
.First();
292 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
293 wxNode
*next
= node
->Next();
300 void wxGDIImage::InitStandardHandlers()
302 AddHandler(new wxBMPResourceHandler
);
303 AddHandler(new wxBMPFileHandler
);
305 // GRG: Add these handlers by default if XPM support is enabled
308 AddHandler(new wxXPMFileHandler
);
309 AddHandler(new wxXPMDataHandler
);
310 #endif // wxUSE_XPM_IN_MSW
312 AddHandler(new wxICOResourceHandler
);
313 AddHandler(new wxICOFileHandler
);
316 // ----------------------------------------------------------------------------
318 // ----------------------------------------------------------------------------
320 bool wxBMPResourceHandler::LoadFile(wxBitmap
*bitmap
,
321 const wxString
& name
, long WXUNUSED(flags
),
322 int WXUNUSED(desiredWidth
),
323 int WXUNUSED(desiredHeight
))
325 // TODO: load colourmap.
326 bitmap
->SetHBITMAP((WXHBITMAP
)::LoadBitmap(wxGetInstance(), name
));
328 wxBitmapRefData
*data
= bitmap
->GetBitmapData();
332 if ( !::GetObject(GetHbitmapOf(*bitmap
), sizeof(BITMAP
), (LPSTR
) &bm
) )
334 wxLogLastError(wxT("GetObject(HBITMAP)"));
337 data
->m_width
= bm
.bmWidth
;
338 data
->m_height
= bm
.bmHeight
;
339 data
->m_depth
= bm
.bmBitsPixel
;
343 // it's probably not found
344 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
351 bool wxBMPFileHandler::LoadFile(wxBitmap
*bitmap
,
352 const wxString
& name
, long WXUNUSED(flags
),
353 int WXUNUSED(desiredWidth
),
354 int WXUNUSED(desiredHeight
))
356 #if wxUSE_IMAGE_LOADING_IN_MSW
357 wxPalette
*palette
= NULL
;
358 bool success
= wxLoadIntoBitmap(WXSTRINGCAST name
, bitmap
, &palette
) != 0;
359 if ( success
&& palette
)
361 bitmap
->SetPalette(*palette
);
364 // it was copied by the bitmap if it was loaded successfully
373 bool wxBMPFileHandler::SaveFile(wxBitmap
*bitmap
,
374 const wxString
& name
,
376 const wxPalette
*pal
)
378 #if wxUSE_IMAGE_LOADING_IN_MSW
379 wxPalette
*actualPalette
= (wxPalette
*)pal
;
380 if ( !actualPalette
)
381 actualPalette
= bitmap
->GetPalette();
382 return wxSaveBitmap(WXSTRINGCAST name
, bitmap
, actualPalette
) != 0;
388 // ----------------------------------------------------------------------------
390 // ----------------------------------------------------------------------------
392 bool wxICOFileHandler::LoadIcon(wxIcon
*icon
,
393 const wxString
& name
,
395 int desiredWidth
, int desiredHeight
)
397 #if wxUSE_RESOURCE_LOADING_IN_MSW
406 // were we asked for a large icon?
407 if ( desiredWidth
== ::GetSystemMetrics(SM_CXICON
) &&
408 desiredHeight
== ::GetSystemMetrics(SM_CYICON
) )
410 // get the first large icon from file
411 if ( !::ExtractIconEx(name
, 0, &hicon
, NULL
, 1) )
413 // it is not an error, but it might still be useful to be informed
414 // about it optionally
415 wxLogTrace(_T("iconload"),
416 _T("No large icons found in the file '%s'."),
420 else if ( desiredWidth
== ::GetSystemMetrics(SM_CXSMICON
) &&
421 desiredHeight
== ::GetSystemMetrics(SM_CYSMICON
) )
423 // get the first small icon from file
424 if ( !::ExtractIconEx(name
, 0, NULL
, &hicon
, 1) )
426 wxLogTrace(_T("iconload"),
427 _T("No small icons found in the file '%s'."),
431 //else: not standard size, load below
435 // take any (the first one) icon from the file by default
436 hicon
= ::ExtractIcon(wxGetInstance(), name
, 0 /* first */);
441 wxLogSysError(_T("Failed to load icon from the file '%s'"),
447 size
= GetHiconSize(hicon
);
449 HICON hicon
= ReadIconFile((wxChar
*)name
.c_str(),
452 #endif // Win32/Win16
454 if ( (desiredWidth
!= -1 && desiredWidth
!= size
.x
) ||
455 (desiredHeight
!= -1 && desiredHeight
!= size
.y
) )
457 wxLogTrace(_T("iconload"),
458 _T("Returning FALSE from wxICOFileHandler::Load because of the size mismatch: actual (%d, %d), requested (%d, %d)"),
460 desiredWidth
, desiredHeight
);
462 ::DestroyIcon(hicon
);
467 icon
->SetHICON((WXHICON
)hicon
);
468 icon
->SetSize(size
.x
, size
.y
);
476 bool wxICOResourceHandler::LoadIcon(wxIcon
*icon
,
477 const wxString
& name
,
479 int desiredWidth
, int desiredHeight
)
483 #if defined(__WIN32__) && !defined(__SC__)
484 if ( desiredWidth
!= -1 && desiredHeight
!= -1 )
486 hicon
= (HICON
)::LoadImage(wxGetInstance(), name
, IMAGE_ICON
,
487 desiredWidth
, desiredHeight
,
493 hicon
= ::LoadIcon(wxGetInstance(), name
);
496 wxSize size
= GetHiconSize(hicon
);
497 icon
->SetSize(size
.x
, size
.y
);
499 // Override the found values with desired values
500 if ( desiredWidth
> -1 && desiredHeight
> -1 )
502 icon
->SetSize(desiredWidth
, desiredHeight
);
505 icon
->SetHICON((WXHICON
)hicon
);
510 // ----------------------------------------------------------------------------
512 // ----------------------------------------------------------------------------
514 static wxSize
GetHiconSize(HICON hicon
)
516 wxSize
size(32, 32); // default
519 // Win32s doesn't have GetIconInfo function...
520 if ( hicon
&& wxGetOsVersion() != wxWIN32S
)
523 if ( !::GetIconInfo(hicon
, &info
) )
525 wxLogLastError(wxT("GetIconInfo"));
529 HBITMAP hbmp
= info
.hbmMask
;
533 if ( ::GetObject(hbmp
, sizeof(BITMAP
), (LPSTR
) &bm
) )
535 size
= wxSize(bm
.bmWidth
, bm
.bmHeight
);
538 ::DeleteObject(info
.hbmMask
);
541 ::DeleteObject(info
.hbmColor
);