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/bitmap.h"
41 #include "wx/msw/gdiimage.h"
44 #include "wx/msw/curico.h"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 // all image handlers are declared/defined in this file because the outside
52 // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids)
54 class WXDLLEXPORT wxBMPFileHandler
: public wxBitmapHandler
57 wxBMPFileHandler() : wxBitmapHandler(_T("Windows bitmap file"), _T("bmp"),
62 virtual bool LoadFile(wxBitmap
*bitmap
,
63 const wxString
& name
, long flags
,
64 int desiredWidth
, int desiredHeight
);
65 virtual bool SaveFile(wxBitmap
*bitmap
,
66 const wxString
& name
, int type
,
67 const wxPalette
*palette
= NULL
);
70 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
)
73 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
76 wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"),
78 wxBITMAP_TYPE_BMP_RESOURCE
)
82 virtual bool LoadFile(wxBitmap
*bitmap
,
83 const wxString
& name
, long flags
,
84 int desiredWidth
, int desiredHeight
);
87 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
90 class WXDLLEXPORT wxIconHandler
: public wxGDIImageHandler
93 wxIconHandler(const wxString
& name
, const wxString
& ext
, long type
)
94 : wxGDIImageHandler(name
, ext
, type
)
98 // creating and saving icons is not supported
99 virtual bool Create(wxGDIImage
*WXUNUSED(image
),
100 void *WXUNUSED(data
),
101 long WXUNUSED(flags
),
103 int WXUNUSED(height
),
104 int WXUNUSED(depth
) = 1)
109 virtual bool Save(wxGDIImage
*WXUNUSED(image
),
110 const wxString
& WXUNUSED(name
),
116 virtual bool Load(wxGDIImage
*image
,
117 const wxString
& name
,
119 int desiredWidth
, int desiredHeight
)
121 wxIcon
*icon
= wxDynamicCast(image
, wxIcon
);
122 wxCHECK_MSG( icon
, FALSE
, _T("wxIconHandler only works with icons") );
124 return LoadIcon(icon
, name
, flags
, desiredWidth
, desiredHeight
);
128 virtual bool LoadIcon(wxIcon
*icon
,
129 const wxString
& name
, long flags
,
130 int desiredWidth
= -1, int desiredHeight
= -1) = 0;
133 class WXDLLEXPORT wxICOFileHandler
: public wxIconHandler
136 wxICOFileHandler() : wxIconHandler(_T("ICO icon file"),
142 virtual bool LoadIcon(wxIcon
*icon
,
143 const wxString
& name
, long flags
,
144 int desiredWidth
= -1, int desiredHeight
= -1);
147 DECLARE_DYNAMIC_CLASS(wxICOFileHandler
)
150 class WXDLLEXPORT wxICOResourceHandler
: public wxIconHandler
153 wxICOResourceHandler() : wxIconHandler(_T("ICO resource"),
155 wxBITMAP_TYPE_ICO_RESOURCE
)
159 virtual bool LoadIcon(wxIcon
*icon
,
160 const wxString
& name
, long flags
,
161 int desiredWidth
= -1, int desiredHeight
= -1);
164 DECLARE_DYNAMIC_CLASS(wxICOResourceHandler
)
167 // ----------------------------------------------------------------------------
169 // ----------------------------------------------------------------------------
171 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
172 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
173 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler
, wxGDIImageHandler
)
174 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler
, wxGDIImageHandler
)
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
180 static wxSize
GetHiconSize(HICON hicon
);
182 // ============================================================================
184 // ============================================================================
186 wxList
wxGDIImage::ms_handlers
;
188 // ----------------------------------------------------------------------------
189 // wxGDIImage functions forwarded to wxGDIImageRefData
190 // ----------------------------------------------------------------------------
192 bool wxGDIImage::FreeResource(bool WXUNUSED(force
))
196 GetGDIImageData()->Free();
197 GetGDIImageData()->m_handle
= 0;
203 WXHANDLE
wxGDIImage::GetResourceHandle()
208 // ----------------------------------------------------------------------------
209 // wxGDIImage handler stuff
210 // ----------------------------------------------------------------------------
212 void wxGDIImage::AddHandler(wxGDIImageHandler
*handler
)
214 ms_handlers
.Append(handler
);
217 void wxGDIImage::InsertHandler(wxGDIImageHandler
*handler
)
219 ms_handlers
.Insert(handler
);
222 bool wxGDIImage::RemoveHandler(const wxString
& name
)
224 wxGDIImageHandler
*handler
= FindHandler(name
);
227 ms_handlers
.DeleteObject(handler
);
234 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& name
)
236 wxNode
*node
= ms_handlers
.First();
239 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
240 if ( handler
->GetName() == name
)
248 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& extension
,
251 wxNode
*node
= ms_handlers
.First();
254 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
255 if ( (handler
->GetExtension() = extension
) &&
256 (type
== -1 || handler
->GetType() == type
) )
266 wxGDIImageHandler
*wxGDIImage::FindHandler(long type
)
268 wxNode
*node
= ms_handlers
.First();
271 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
272 if ( handler
->GetType() == type
)
281 void wxGDIImage::CleanUpHandlers()
283 wxNode
*node
= ms_handlers
.First();
286 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
287 wxNode
*next
= node
->Next();
294 void wxGDIImage::InitStandardHandlers()
296 AddHandler(new wxBMPResourceHandler
);
297 AddHandler(new wxBMPFileHandler
);
299 // Not added by default: include xpmhand.h in your app
300 // and call these in your wxApp::OnInit.
301 // AddHandler(new wxXPMFileHandler);
302 // AddHandler(new wxXPMDataHandler);
304 AddHandler(new wxICOResourceHandler
);
305 AddHandler(new wxICOFileHandler
);
308 // ----------------------------------------------------------------------------
310 // ----------------------------------------------------------------------------
312 bool wxBMPResourceHandler::LoadFile(wxBitmap
*bitmap
,
313 const wxString
& name
, long WXUNUSED(flags
),
314 int WXUNUSED(desiredWidth
),
315 int WXUNUSED(desiredHeight
))
317 // TODO: load colourmap.
318 bitmap
->SetHBITMAP((WXHBITMAP
)::LoadBitmap(wxGetInstance(), name
));
320 wxBitmapRefData
*data
= bitmap
->GetBitmapData();
324 if ( !::GetObject(GetHbitmapOf(*bitmap
), sizeof(BITMAP
), (LPSTR
) &bm
) )
326 wxLogLastError("GetObject(HBITMAP)");
329 data
->m_width
= bm
.bmWidth
;
330 data
->m_height
= bm
.bmHeight
;
331 data
->m_depth
= bm
.bmBitsPixel
;
335 // it's probably not found
336 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
343 bool wxBMPFileHandler::LoadFile(wxBitmap
*bitmap
,
344 const wxString
& name
, long WXUNUSED(flags
),
345 int WXUNUSED(desiredWidth
),
346 int WXUNUSED(desiredHeight
))
348 #if wxUSE_IMAGE_LOADING_IN_MSW
349 wxPalette
*palette
= NULL
;
350 bool success
= wxLoadIntoBitmap(WXSTRINGCAST name
, bitmap
, &palette
) != 0;
351 if ( success
&& palette
)
353 bitmap
->SetPalette(*palette
);
356 // it was copied by the bitmap if it was loaded successfully
365 bool wxBMPFileHandler::SaveFile(wxBitmap
*bitmap
,
366 const wxString
& name
,
368 const wxPalette
*pal
)
370 #if wxUSE_IMAGE_LOADING_IN_MSW
371 wxPalette
*actualPalette
= (wxPalette
*)pal
;
372 if ( !actualPalette
)
373 actualPalette
= bitmap
->GetPalette();
374 return wxSaveBitmap(WXSTRINGCAST name
, bitmap
, actualPalette
) != 0;
380 // ----------------------------------------------------------------------------
382 // ----------------------------------------------------------------------------
384 bool wxICOFileHandler::LoadIcon(wxIcon
*icon
,
385 const wxString
& name
,
387 int desiredWidth
, int desiredHeight
)
389 #if wxUSE_RESOURCE_LOADING_IN_MSW
396 HICON hicon
= ::ExtractIcon(wxGetInstance(), name
, 0 /* first */);
399 wxLogSysError(_T("Failed to load icon from the file '%s'"),
405 size
= GetHiconSize(hicon
);
407 HICON hicon
= ReadIconFile((wxChar
*)name
.c_str(),
410 #endif // Win32/Win16
412 if ( (desiredWidth
!= -1 && desiredWidth
!= size
.x
) ||
413 (desiredHeight
!= -1 && desiredHeight
!= size
.y
) )
415 wxLogDebug(_T("Returning FALSE from wxICOFileHandler::Load because "
416 "of the size mismatch: actual (%d, %d), "
417 "requested (%d, %d)"),
419 desiredWidth
, desiredHeight
);
421 ::DestroyIcon(hicon
);
426 icon
->SetHICON((WXHICON
)hicon
);
427 icon
->SetSize(size
.x
, size
.y
);
435 bool wxICOResourceHandler::LoadIcon(wxIcon
*icon
,
436 const wxString
& name
,
438 int desiredWidth
, int desiredHeight
)
442 #if defined(__WIN32__) && !defined(__SC__)
443 if ( desiredWidth
!= -1 && desiredHeight
!= -1 )
445 hicon
= (HICON
)::LoadImage(wxGetInstance(), name
, IMAGE_ICON
,
446 desiredWidth
, desiredHeight
,
452 hicon
= ::LoadIcon(wxGetInstance(), name
);
455 wxSize size
= GetHiconSize(hicon
);
456 icon
->SetSize(size
.x
, size
.y
);
458 // Override the found values with desired values
459 if ( desiredWidth
> -1 && desiredHeight
> -1 )
461 icon
->SetSize(desiredWidth
, desiredHeight
);
464 icon
->SetHICON((WXHICON
)hicon
);
469 // ----------------------------------------------------------------------------
471 // ----------------------------------------------------------------------------
473 static wxSize
GetHiconSize(HICON hicon
)
475 wxSize
size(32, 32); // default
478 // Win32s doesn't have GetIconInfo function...
479 if ( hicon
&& wxGetOsVersion() != wxWIN32S
)
482 if ( !::GetIconInfo(hicon
, &info
) )
484 wxLogLastError("GetIconInfo");
488 HBITMAP hbmp
= info
.hbmMask
;
492 if ( ::GetObject(hbmp
, sizeof(BITMAP
), (LPSTR
) &bm
) )
494 size
= wxSize(bm
.bmWidth
, bm
.bmHeight
);
497 ::DeleteObject(info
.hbmMask
);
500 ::DeleteObject(info
.hbmColor
);