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
404 HICON hicon
= ::ExtractIcon(wxGetInstance(), name
, 0 /* first */);
407 wxLogSysError(_T("Failed to load icon from the file '%s'"),
413 size
= GetHiconSize(hicon
);
415 HICON hicon
= ReadIconFile((wxChar
*)name
.c_str(),
418 #endif // Win32/Win16
420 if ( (desiredWidth
!= -1 && desiredWidth
!= size
.x
) ||
421 (desiredHeight
!= -1 && desiredHeight
!= size
.y
) )
423 wxLogDebug(_T("Returning FALSE from wxICOFileHandler::Load because of the size mismatch: actual (%d, %d), requested (%d, %d)"),
425 desiredWidth
, desiredHeight
);
427 ::DestroyIcon(hicon
);
432 icon
->SetHICON((WXHICON
)hicon
);
433 icon
->SetSize(size
.x
, size
.y
);
441 bool wxICOResourceHandler::LoadIcon(wxIcon
*icon
,
442 const wxString
& name
,
444 int desiredWidth
, int desiredHeight
)
448 #if defined(__WIN32__) && !defined(__SC__)
449 if ( desiredWidth
!= -1 && desiredHeight
!= -1 )
451 hicon
= (HICON
)::LoadImage(wxGetInstance(), name
, IMAGE_ICON
,
452 desiredWidth
, desiredHeight
,
458 hicon
= ::LoadIcon(wxGetInstance(), name
);
461 wxSize size
= GetHiconSize(hicon
);
462 icon
->SetSize(size
.x
, size
.y
);
464 // Override the found values with desired values
465 if ( desiredWidth
> -1 && desiredHeight
> -1 )
467 icon
->SetSize(desiredWidth
, desiredHeight
);
470 icon
->SetHICON((WXHICON
)hicon
);
475 // ----------------------------------------------------------------------------
477 // ----------------------------------------------------------------------------
479 static wxSize
GetHiconSize(HICON hicon
)
481 wxSize
size(32, 32); // default
484 // Win32s doesn't have GetIconInfo function...
485 if ( hicon
&& wxGetOsVersion() != wxWIN32S
)
488 if ( !::GetIconInfo(hicon
, &info
) )
490 wxLogLastError(wxT("GetIconInfo"));
494 HBITMAP hbmp
= info
.hbmMask
;
498 if ( ::GetObject(hbmp
, sizeof(BITMAP
), (LPSTR
) &bm
) )
500 size
= wxSize(bm
.bmWidth
, bm
.bmHeight
);
503 ::DeleteObject(info
.hbmMask
);
506 ::DeleteObject(info
.hbmColor
);