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"
43 #include "wx/msw/curico.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // all image handlers are declared/defined in this file because the outside
51 // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids)
53 class WXDLLEXPORT wxBMPFileHandler
: public wxBitmapHandler
56 wxBMPFileHandler() : wxBitmapHandler(_T("Windows bitmap file"), _T("bmp"),
61 virtual bool LoadFile(wxBitmap
*bitmap
,
62 const wxString
& name
, long flags
,
63 int desiredWidth
, int desiredHeight
);
64 virtual bool SaveFile(wxBitmap
*bitmap
,
65 const wxString
& name
, int type
,
66 const wxPalette
*palette
= NULL
);
69 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
)
72 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
75 wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"),
77 wxBITMAP_TYPE_BMP_RESOURCE
)
81 virtual bool LoadFile(wxBitmap
*bitmap
,
82 const wxString
& name
, long flags
,
83 int desiredWidth
, int desiredHeight
);
86 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
89 class WXDLLEXPORT wxIconHandler
: public wxGDIImageHandler
92 wxIconHandler(const wxString
& name
, const wxString
& ext
, long type
)
93 : wxGDIImageHandler(name
, ext
, type
)
97 // creating and saving icons is not supported
98 virtual bool Create(wxGDIImage
*WXUNUSED(image
),
100 long WXUNUSED(flags
),
102 int WXUNUSED(height
),
103 int WXUNUSED(depth
) = 1)
108 virtual bool Save(wxGDIImage
*WXUNUSED(image
),
109 const wxString
& WXUNUSED(name
),
115 virtual bool Load(wxGDIImage
*image
,
116 const wxString
& name
,
118 int desiredWidth
, int desiredHeight
)
120 wxIcon
*icon
= wxDynamicCast(image
, wxIcon
);
121 wxCHECK_MSG( icon
, FALSE
, _T("wxIconHandler only works with icons") );
123 return LoadIcon(icon
, name
, flags
, desiredWidth
, desiredHeight
);
127 virtual bool LoadIcon(wxIcon
*icon
,
128 const wxString
& name
, long flags
,
129 int desiredWidth
= -1, int desiredHeight
= -1) = 0;
132 class WXDLLEXPORT wxICOFileHandler
: public wxIconHandler
135 wxICOFileHandler() : wxIconHandler(_T("ICO icon file"),
141 virtual bool LoadIcon(wxIcon
*icon
,
142 const wxString
& name
, long flags
,
143 int desiredWidth
= -1, int desiredHeight
= -1);
146 DECLARE_DYNAMIC_CLASS(wxICOFileHandler
)
149 class WXDLLEXPORT wxICOResourceHandler
: public wxIconHandler
152 wxICOResourceHandler() : wxIconHandler(_T("ICO resource"),
154 wxBITMAP_TYPE_ICO_RESOURCE
)
158 virtual bool LoadIcon(wxIcon
*icon
,
159 const wxString
& name
, long flags
,
160 int desiredWidth
= -1, int desiredHeight
= -1);
163 DECLARE_DYNAMIC_CLASS(wxICOResourceHandler
)
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
170 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
171 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
172 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler
, wxGDIImageHandler
)
173 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler
, wxGDIImageHandler
)
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
179 static wxSize
GetHiconSize(HICON hicon
);
181 // ============================================================================
183 // ============================================================================
185 wxList
wxGDIImage::ms_handlers
;
187 // ----------------------------------------------------------------------------
188 // wxGDIImage functions forwarded to wxGDIImageRefData
189 // ----------------------------------------------------------------------------
191 bool wxGDIImage::FreeResource(bool WXUNUSED(force
))
195 GetGDIImageData()->Free();
196 GetGDIImageData()->m_handle
= 0;
202 WXHANDLE
wxGDIImage::GetResourceHandle()
207 // ----------------------------------------------------------------------------
208 // wxGDIImage handler stuff
209 // ----------------------------------------------------------------------------
211 void wxGDIImage::AddHandler(wxGDIImageHandler
*handler
)
213 ms_handlers
.Append(handler
);
216 void wxGDIImage::InsertHandler(wxGDIImageHandler
*handler
)
218 ms_handlers
.Insert(handler
);
221 bool wxGDIImage::RemoveHandler(const wxString
& name
)
223 wxGDIImageHandler
*handler
= FindHandler(name
);
226 ms_handlers
.DeleteObject(handler
);
233 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& name
)
235 wxNode
*node
= ms_handlers
.First();
238 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
239 if ( handler
->GetName() == name
)
247 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& extension
,
250 wxNode
*node
= ms_handlers
.First();
253 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
254 if ( (handler
->GetExtension() = extension
) &&
255 (type
== -1 || handler
->GetType() == type
) )
265 wxGDIImageHandler
*wxGDIImage::FindHandler(long type
)
267 wxNode
*node
= ms_handlers
.First();
270 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
271 if ( handler
->GetType() == type
)
280 void wxGDIImage::CleanUpHandlers()
282 wxNode
*node
= ms_handlers
.First();
285 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
286 wxNode
*next
= node
->Next();
293 void wxGDIImage::InitStandardHandlers()
295 AddHandler(new wxBMPResourceHandler
);
296 AddHandler(new wxBMPFileHandler
);
298 // Not added by default: include xpmhand.h in your app
299 // and call these in your wxApp::OnInit.
300 // AddHandler(new wxXPMFileHandler);
301 // AddHandler(new wxXPMDataHandler);
303 AddHandler(new wxICOResourceHandler
);
304 AddHandler(new wxICOFileHandler
);
307 // ----------------------------------------------------------------------------
309 // ----------------------------------------------------------------------------
311 bool wxBMPResourceHandler::LoadFile(wxBitmap
*bitmap
,
312 const wxString
& name
, long WXUNUSED(flags
),
313 int WXUNUSED(desiredWidth
),
314 int WXUNUSED(desiredHeight
))
316 // TODO: load colourmap.
317 bitmap
->SetHBITMAP((WXHBITMAP
)::LoadBitmap(wxGetInstance(), name
));
319 wxBitmapRefData
*data
= bitmap
->GetBitmapData();
323 if ( !::GetObject(GetHbitmapOf(*bitmap
), sizeof(BITMAP
), (LPSTR
) &bm
) )
325 wxLogLastError("GetObject(HBITMAP)");
328 data
->m_width
= bm
.bmWidth
;
329 data
->m_height
= bm
.bmHeight
;
330 data
->m_depth
= bm
.bmBitsPixel
;
334 // it's probably not found
335 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
342 bool wxBMPFileHandler::LoadFile(wxBitmap
*bitmap
,
343 const wxString
& name
, long WXUNUSED(flags
),
344 int WXUNUSED(desiredWidth
),
345 int WXUNUSED(desiredHeight
))
347 #if wxUSE_IMAGE_LOADING_IN_MSW
348 wxPalette
*palette
= NULL
;
349 bool success
= wxLoadIntoBitmap(WXSTRINGCAST name
, bitmap
, &palette
) != 0;
350 if ( success
&& palette
)
352 bitmap
->SetPalette(*palette
);
355 // it was copied by the bitmap if it was loaded successfully
364 bool wxBMPFileHandler::SaveFile(wxBitmap
*bitmap
,
365 const wxString
& name
,
367 const wxPalette
*pal
)
369 #if wxUSE_IMAGE_LOADING_IN_MSW
370 wxPalette
*actualPalette
= (wxPalette
*)pal
;
371 if ( !actualPalette
)
372 actualPalette
= bitmap
->GetPalette();
373 return wxSaveBitmap(WXSTRINGCAST name
, bitmap
, actualPalette
) != 0;
379 // ----------------------------------------------------------------------------
381 // ----------------------------------------------------------------------------
383 bool wxICOFileHandler::LoadIcon(wxIcon
*icon
,
384 const wxString
& name
,
386 int desiredWidth
, int desiredHeight
)
388 #if wxUSE_RESOURCE_LOADING_IN_MSW
395 HICON hicon
= ::ExtractIcon(wxGetInstance(), name
, 0 /* first */);
398 wxLogSysError(_T("Failed to load icon from the file '%s'"),
404 size
= GetHiconSize(hicon
);
406 HICON hicon
= ReadIconFile((wxChar
*)name
.c_str(),
409 #endif // Win32/Win16
411 if ( (desiredWidth
!= -1 && desiredWidth
!= size
.x
) ||
412 (desiredHeight
!= -1 && desiredHeight
!= size
.y
) )
414 wxLogDebug(_T("Returning FALSE from wxICOFileHandler::Load because "
415 "of the size mismatch: actual (%d, %d), "
416 "requested (%d, %d)"),
418 desiredWidth
, desiredHeight
);
420 ::DestroyIcon(hicon
);
425 icon
->SetHICON((WXHICON
)hicon
);
426 icon
->SetSize(size
.x
, size
.y
);
434 bool wxICOResourceHandler::LoadIcon(wxIcon
*icon
,
435 const wxString
& name
,
437 int desiredWidth
, int desiredHeight
)
441 #if defined(__WIN32__) && !defined(__SC__)
442 if ( desiredWidth
!= -1 && desiredHeight
!= -1 )
444 hicon
= (HICON
)::LoadImage(wxGetInstance(), name
, IMAGE_ICON
,
445 desiredWidth
, desiredHeight
,
451 hicon
= ::LoadIcon(wxGetInstance(), name
);
454 wxSize size
= GetHiconSize(hicon
);
455 icon
->SetSize(size
.x
, size
.y
);
457 // Override the found values with desired values
458 if ( desiredWidth
> -1 && desiredHeight
> -1 )
460 icon
->SetSize(desiredWidth
, desiredHeight
);
463 icon
->SetHICON((WXHICON
)hicon
);
468 // ----------------------------------------------------------------------------
470 // ----------------------------------------------------------------------------
472 static wxSize
GetHiconSize(HICON hicon
)
474 wxSize
size(32, 32); // default
477 // Win32s doesn't have GetIconInfo function...
478 if ( hicon
&& wxGetOsVersion() != wxWIN32S
)
481 if ( !::GetIconInfo(hicon
, &info
) )
483 wxLogLastError("GetIconInfo");
487 HBITMAP hbmp
= info
.hbmMask
;
491 if ( ::GetObject(hbmp
, sizeof(BITMAP
), (LPSTR
) &bm
) )
493 size
= wxSize(bm
.bmWidth
, bm
.bmHeight
);
496 ::DeleteObject(info
.hbmMask
);
499 ::DeleteObject(info
.hbmColor
);