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"
35 #include "wx/msw/private.h"
39 #include "wx/msw/dib.h"
40 #include "wx/msw/gdiimage.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 // all image handlers are declared/defined in this file because the outside
47 // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids)
49 class WXDLLEXPORT wxBMPFileHandler
: public wxBitmapHandler
52 wxBMPFileHandler() : wxBitmapHandler(_T("Windows bitmap file"), _T("bmp"),
57 virtual bool LoadFile(wxBitmap
*bitmap
,
58 const wxString
& name
, long flags
,
59 int desiredWidth
, int desiredHeight
);
60 virtual bool SaveFile(wxBitmap
*bitmap
,
61 const wxString
& name
, int type
,
62 const wxPalette
*palette
= NULL
);
65 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
)
68 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
71 wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"),
73 wxBITMAP_TYPE_BMP_RESOURCE
)
77 virtual bool LoadFile(wxBitmap
*bitmap
,
78 const wxString
& name
, long flags
,
79 int desiredWidth
, int desiredHeight
);
82 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
85 class WXDLLEXPORT wxIconHandler
: public wxGDIImageHandler
88 wxIconHandler(const wxString
& name
, const wxString
& ext
, long type
)
89 : wxGDIImageHandler(name
, ext
, type
)
93 // creating and saving icons is not supported
94 virtual bool Create(wxGDIImage
*WXUNUSED(image
),
99 int WXUNUSED(depth
) = 1)
104 virtual bool Save(wxGDIImage
*WXUNUSED(image
),
105 const wxString
& WXUNUSED(name
),
111 virtual bool Load(wxGDIImage
*image
,
112 const wxString
& name
,
114 int desiredWidth
, int desiredHeight
)
116 wxIcon
*icon
= wxDynamicCast(image
, wxIcon
);
117 wxCHECK_MSG( icon
, FALSE
, _T("wxIconHandler only works with icons") );
119 return LoadIcon(icon
, name
, flags
, desiredWidth
, desiredHeight
);
123 virtual bool LoadIcon(wxIcon
*icon
,
124 const wxString
& name
, long flags
,
125 int desiredWidth
= -1, int desiredHeight
= -1) = 0;
128 class WXDLLEXPORT wxICOFileHandler
: public wxIconHandler
131 wxICOFileHandler() : wxIconHandler(_T("ICO icon file"),
137 virtual bool LoadIcon(wxIcon
*icon
,
138 const wxString
& name
, long flags
,
139 int desiredWidth
= -1, int desiredHeight
= -1);
142 DECLARE_DYNAMIC_CLASS(wxICOFileHandler
)
145 class WXDLLEXPORT wxICOResourceHandler
: public wxIconHandler
148 wxICOResourceHandler() : wxIconHandler(_T("ICO resource"),
150 wxBITMAP_TYPE_ICO_RESOURCE
)
154 virtual bool LoadIcon(wxIcon
*icon
,
155 const wxString
& name
, long flags
,
156 int desiredWidth
= -1, int desiredHeight
= -1);
159 DECLARE_DYNAMIC_CLASS(wxICOResourceHandler
)
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
166 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
167 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
168 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler
, wxGDIImageHandler
)
169 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler
, wxGDIImageHandler
)
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
175 static wxSize
GetHiconSize(HICON hicon
);
177 // ============================================================================
179 // ============================================================================
181 wxList
wxGDIImage::ms_handlers
;
183 // ----------------------------------------------------------------------------
184 // wxGDIImage functions forwarded to wxGDIImageRefData
185 // ----------------------------------------------------------------------------
187 bool wxGDIImage::FreeResource(bool WXUNUSED(force
))
191 GetGDIImageData()->Free();
192 GetGDIImageData()->m_handle
= 0;
198 WXHANDLE
wxGDIImage::GetResourceHandle()
203 // ----------------------------------------------------------------------------
204 // wxGDIImage handler stuff
205 // ----------------------------------------------------------------------------
207 void wxGDIImage::AddHandler(wxGDIImageHandler
*handler
)
209 ms_handlers
.Append(handler
);
212 void wxGDIImage::InsertHandler(wxGDIImageHandler
*handler
)
214 ms_handlers
.Insert(handler
);
217 bool wxGDIImage::RemoveHandler(const wxString
& name
)
219 wxGDIImageHandler
*handler
= FindHandler(name
);
222 ms_handlers
.DeleteObject(handler
);
229 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& name
)
231 wxNode
*node
= ms_handlers
.First();
234 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
235 if ( handler
->GetName() == name
)
243 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& extension
,
246 wxNode
*node
= ms_handlers
.First();
249 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
250 if ( (handler
->GetExtension() = extension
) &&
251 (type
== -1 || handler
->GetType() == type
) )
261 wxGDIImageHandler
*wxGDIImage::FindHandler(long type
)
263 wxNode
*node
= ms_handlers
.First();
266 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
267 if ( handler
->GetType() == type
)
276 void wxGDIImage::CleanUpHandlers()
278 wxNode
*node
= ms_handlers
.First();
281 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
282 wxNode
*next
= node
->Next();
289 void wxGDIImage::InitStandardHandlers()
291 AddHandler(new wxBMPResourceHandler
);
292 AddHandler(new wxBMPFileHandler
);
294 // Not added by default: include xpmhand.h in your app
295 // and call these in your wxApp::OnInit.
296 // AddHandler(new wxXPMFileHandler);
297 // AddHandler(new wxXPMDataHandler);
299 AddHandler(new wxICOResourceHandler
);
300 AddHandler(new wxICOFileHandler
);
303 // ----------------------------------------------------------------------------
305 // ----------------------------------------------------------------------------
307 bool wxBMPResourceHandler::LoadFile(wxBitmap
*bitmap
,
308 const wxString
& name
, long WXUNUSED(flags
),
309 int WXUNUSED(desiredWidth
),
310 int WXUNUSED(desiredHeight
))
312 // TODO: load colourmap.
313 bitmap
->SetHBITMAP((WXHBITMAP
)::LoadBitmap(wxGetInstance(), name
));
315 wxBitmapRefData
*data
= bitmap
->GetBitmapData();
319 if ( !::GetObject(GetHbitmapOf(*bitmap
), sizeof(BITMAP
), (LPSTR
) &bm
) )
321 wxLogLastError("GetObject(HBITMAP)");
324 data
->m_width
= bm
.bmWidth
;
325 data
->m_height
= bm
.bmHeight
;
326 data
->m_depth
= bm
.bmBitsPixel
;
330 // it's probably not found
331 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
338 bool wxBMPFileHandler::LoadFile(wxBitmap
*bitmap
,
339 const wxString
& name
, long WXUNUSED(flags
),
340 int WXUNUSED(desiredWidth
),
341 int WXUNUSED(desiredHeight
))
343 #if wxUSE_IMAGE_LOADING_IN_MSW
344 wxPalette
*palette
= NULL
;
345 bool success
= wxLoadIntoBitmap(WXSTRINGCAST name
, bitmap
, &palette
) != 0;
346 if ( success
&& palette
)
348 bitmap
->SetPalette(*palette
);
351 // it was copied by the bitmap if it was loaded successfully
360 bool wxBMPFileHandler::SaveFile(wxBitmap
*bitmap
,
361 const wxString
& name
,
363 const wxPalette
*pal
)
365 #if wxUSE_IMAGE_LOADING_IN_MSW
366 wxPalette
*actualPalette
= (wxPalette
*)pal
;
367 if ( !actualPalette
)
368 actualPalette
= bitmap
->GetPalette();
369 return wxSaveBitmap(WXSTRINGCAST name
, bitmap
, actualPalette
) != 0;
375 // ----------------------------------------------------------------------------
377 // ----------------------------------------------------------------------------
379 bool wxICOFileHandler::LoadIcon(wxIcon
*icon
,
380 const wxString
& name
,
382 int desiredWidth
, int desiredHeight
)
384 #if wxUSE_RESOURCE_LOADING_IN_MSW
391 HICON hicon
= ::ExtractIcon(wxGetInstance(), name
, 0 /* first */);
394 wxLogSysError(_T("Failed to load icon from the file '%s'"),
400 size
= GetHiconSize(hicon
);
402 HICON hicon
= ReadIconFile((wxChar
*)name
.c_str(),
405 #endif // Win32/Win16
407 if ( (desiredWidth
!= -1 && desiredWidth
!= size
.x
) ||
408 (desiredHeight
!= -1 && desiredHeight
!= size
.y
) )
410 wxLogDebug(_T("Returning FALSE from wxICOFileHandler::Load because "
411 "of the size mismatch: actual (%d, %d), "
412 "requested (%d, %d)"),
414 desiredWidth
, desiredHeight
);
416 ::DestroyIcon(hicon
);
421 icon
->SetHICON((WXHICON
)hicon
);
422 icon
->SetSize(size
.x
, size
.y
);
430 bool wxICOResourceHandler::LoadIcon(wxIcon
*icon
,
431 const wxString
& name
,
433 int desiredWidth
, int desiredHeight
)
437 #if defined(__WIN32__) && !defined(__SC__)
438 if ( desiredWidth
!= -1 && desiredHeight
!= -1 )
440 hicon
= (HICON
)::LoadImage(wxGetInstance(), name
, IMAGE_ICON
,
441 desiredWidth
, desiredHeight
,
447 hicon
= ::LoadIcon(wxGetInstance(), name
);
450 wxSize size
= GetHiconSize(hicon
);
451 icon
->SetSize(size
.x
, size
.y
);
453 // Override the found values with desired values
454 if ( desiredWidth
> -1 && desiredHeight
> -1 )
456 icon
->SetSize(desiredWidth
, desiredHeight
);
459 icon
->SetHICON((WXHICON
)hicon
);
464 // ----------------------------------------------------------------------------
466 // ----------------------------------------------------------------------------
468 static wxSize
GetHiconSize(HICON hicon
)
470 wxSize
size(32, 32); // default
473 // Win32s doesn't have GetIconInfo function...
474 if ( hicon
&& wxGetOsVersion() != wxWIN32S
)
477 if ( !::GetIconInfo(hicon
, &info
) )
479 wxLogLastError("GetIconInfo");
483 HBITMAP hbmp
= info
.hbmMask
;
487 if ( ::GetObject(hbmp
, sizeof(BITMAP
), (LPSTR
) &bm
) )
489 size
= wxSize(bm
.bmWidth
, bm
.bmHeight
);
492 ::DeleteObject(info
.hbmMask
);
495 ::DeleteObject(info
.hbmColor
);