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 #if !USE_SHARED_LIBRARIES
167 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
168 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
169 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler
, wxGDIImageHandler
)
170 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler
, wxGDIImageHandler
)
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
177 static wxSize
GetHiconSize(HICON hicon
);
179 // ============================================================================
181 // ============================================================================
183 wxList
wxGDIImage::ms_handlers
;
185 // ----------------------------------------------------------------------------
186 // wxGDIImage functions forwarded to wxGDIImageRefData
187 // ----------------------------------------------------------------------------
189 bool wxGDIImage::FreeResource(bool WXUNUSED(force
))
193 GetGDIImageData()->Free();
194 GetGDIImageData()->m_handle
= 0;
200 WXHANDLE
wxGDIImage::GetResourceHandle()
205 // ----------------------------------------------------------------------------
206 // wxGDIImage handler stuff
207 // ----------------------------------------------------------------------------
209 void wxGDIImage::AddHandler(wxGDIImageHandler
*handler
)
211 ms_handlers
.Append(handler
);
214 void wxGDIImage::InsertHandler(wxGDIImageHandler
*handler
)
216 ms_handlers
.Insert(handler
);
219 bool wxGDIImage::RemoveHandler(const wxString
& name
)
221 wxGDIImageHandler
*handler
= FindHandler(name
);
224 ms_handlers
.DeleteObject(handler
);
231 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& name
)
233 wxNode
*node
= ms_handlers
.First();
236 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
237 if ( handler
->GetName() == name
)
245 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& extension
,
248 wxNode
*node
= ms_handlers
.First();
251 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
252 if ( (handler
->GetExtension() = extension
) &&
253 (type
== -1 || handler
->GetType() == type
) )
263 wxGDIImageHandler
*wxGDIImage::FindHandler(long type
)
265 wxNode
*node
= ms_handlers
.First();
268 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
269 if ( handler
->GetType() == type
)
278 void wxGDIImage::CleanUpHandlers()
280 wxNode
*node
= ms_handlers
.First();
283 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
284 wxNode
*next
= node
->Next();
291 void wxGDIImage::InitStandardHandlers()
293 AddHandler(new wxBMPResourceHandler
);
294 AddHandler(new wxBMPFileHandler
);
296 // Not added by default: include xpmhand.h in your app
297 // and call these in your wxApp::OnInit.
298 // AddHandler(new wxXPMFileHandler);
299 // AddHandler(new wxXPMDataHandler);
301 AddHandler(new wxICOResourceHandler
);
302 AddHandler(new wxICOFileHandler
);
305 // ----------------------------------------------------------------------------
307 // ----------------------------------------------------------------------------
309 bool wxBMPResourceHandler::LoadFile(wxBitmap
*bitmap
,
310 const wxString
& name
, long WXUNUSED(flags
),
311 int WXUNUSED(desiredWidth
),
312 int WXUNUSED(desiredHeight
))
314 // TODO: load colourmap.
315 bitmap
->SetHBITMAP((WXHBITMAP
)::LoadBitmap(wxGetInstance(), name
));
317 wxBitmapRefData
*data
= bitmap
->GetBitmapData();
321 if ( !::GetObject(GetHbitmapOf(*bitmap
), sizeof(BITMAP
), (LPSTR
) &bm
) )
323 wxLogLastError("GetObject(HBITMAP)");
326 data
->m_width
= bm
.bmWidth
;
327 data
->m_height
= bm
.bmHeight
;
328 data
->m_depth
= bm
.bmBitsPixel
;
332 // it's probably not found
333 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
340 bool wxBMPFileHandler::LoadFile(wxBitmap
*bitmap
,
341 const wxString
& name
, long WXUNUSED(flags
),
342 int WXUNUSED(desiredWidth
),
343 int WXUNUSED(desiredHeight
))
345 #if wxUSE_IMAGE_LOADING_IN_MSW
346 wxPalette
*palette
= NULL
;
347 bool success
= wxLoadIntoBitmap(WXSTRINGCAST name
, bitmap
, &palette
) != 0;
348 if ( success
&& palette
)
350 bitmap
->SetPalette(*palette
);
353 // it was copied by the bitmap if it was loaded successfully
362 bool wxBMPFileHandler::SaveFile(wxBitmap
*bitmap
,
363 const wxString
& name
,
365 const wxPalette
*pal
)
367 #if wxUSE_IMAGE_LOADING_IN_MSW
368 wxPalette
*actualPalette
= (wxPalette
*)pal
;
369 if ( !actualPalette
)
370 actualPalette
= bitmap
->GetPalette();
371 return wxSaveBitmap(WXSTRINGCAST name
, bitmap
, actualPalette
) != 0;
377 // ----------------------------------------------------------------------------
379 // ----------------------------------------------------------------------------
381 bool wxICOFileHandler::LoadIcon(wxIcon
*icon
,
382 const wxString
& name
,
384 int desiredWidth
, int desiredHeight
)
386 #if wxUSE_RESOURCE_LOADING_IN_MSW
393 HICON hicon
= ::ExtractIcon(wxGetInstance(), name
, 0 /* first */);
396 wxLogSysError(_T("Failed to load icon from the file '%s'"),
402 size
= GetHiconSize(hicon
);
404 HICON hicon
= ReadIconFile((wxChar
*)name
.c_str(),
407 #endif // Win32/Win16
409 if ( (desiredWidth
!= -1 && desiredWidth
!= size
.x
) ||
410 (desiredHeight
!= -1 && desiredHeight
!= size
.y
) )
412 wxLogDebug(_T("Returning FALSE from wxICOFileHandler::Load because "
413 "of the size mismatch: actual (%d, %d), "
414 "requested (%d, %d)"),
416 desiredWidth
, desiredHeight
);
418 ::DestroyIcon(hicon
);
423 icon
->SetHICON((WXHICON
)hicon
);
424 icon
->SetSize(size
.x
, size
.y
);
432 bool wxICOResourceHandler::LoadIcon(wxIcon
*icon
,
433 const wxString
& name
,
435 int desiredWidth
, int desiredHeight
)
439 #if defined(__WIN32__) && !defined(__SC__)
440 if ( desiredWidth
!= -1 && desiredHeight
!= -1 )
442 hicon
= (HICON
)::LoadImage(wxGetInstance(), name
, IMAGE_ICON
,
443 desiredWidth
, desiredHeight
,
449 hicon
= ::LoadIcon(wxGetInstance(), name
);
452 wxSize size
= GetHiconSize(hicon
);
453 icon
->SetSize(size
.x
, size
.y
);
455 // Override the found values with desired values
456 if ( desiredWidth
> -1 && desiredHeight
> -1 )
458 icon
->SetSize(desiredWidth
, desiredHeight
);
461 icon
->SetHICON((WXHICON
)hicon
);
466 // ----------------------------------------------------------------------------
468 // ----------------------------------------------------------------------------
470 static wxSize
GetHiconSize(HICON hicon
)
472 wxSize
size(32, 32); // default
475 // Win32s doesn't have GetIconInfo function...
476 if ( hicon
&& wxGetOsVersion() != wxWIN32S
)
479 if ( !::GetIconInfo(hicon
, &info
) )
481 wxLogLastError("GetIconInfo");
485 HBITMAP hbmp
= info
.hbmMask
;
489 if ( ::GetObject(hbmp
, sizeof(BITMAP
), (LPSTR
) &bm
) )
491 size
= wxSize(bm
.bmWidth
, bm
.bmHeight
);
494 ::DeleteObject(info
.hbmMask
);
497 ::DeleteObject(info
.hbmColor
);