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"
42 #include "wx/bitmap.h"
45 # include "wx/xpmhand.h"
46 #endif // wxUSE_XPM_IN_MSW
49 # include "wx/msw/curico.h"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 // all image handlers are declared/defined in this file because the outside
57 // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids)
59 class WXDLLEXPORT wxBMPFileHandler
: public wxBitmapHandler
62 wxBMPFileHandler() : wxBitmapHandler(_T("Windows bitmap file"), _T("bmp"),
67 virtual bool LoadFile(wxBitmap
*bitmap
,
68 const wxString
& name
, long flags
,
69 int desiredWidth
, int desiredHeight
);
70 virtual bool SaveFile(wxBitmap
*bitmap
,
71 const wxString
& name
, int type
,
72 const wxPalette
*palette
= NULL
);
75 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
)
78 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
81 wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"),
83 wxBITMAP_TYPE_BMP_RESOURCE
)
87 virtual bool LoadFile(wxBitmap
*bitmap
,
88 const wxString
& name
, long flags
,
89 int desiredWidth
, int desiredHeight
);
92 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
95 class WXDLLEXPORT wxIconHandler
: public wxGDIImageHandler
98 wxIconHandler(const wxString
& name
, const wxString
& ext
, long type
)
99 : wxGDIImageHandler(name
, ext
, type
)
103 // creating and saving icons is not supported
104 virtual bool Create(wxGDIImage
*WXUNUSED(image
),
105 void *WXUNUSED(data
),
106 long WXUNUSED(flags
),
108 int WXUNUSED(height
),
109 int WXUNUSED(depth
) = 1)
114 virtual bool Save(wxGDIImage
*WXUNUSED(image
),
115 const wxString
& WXUNUSED(name
),
121 virtual bool Load(wxGDIImage
*image
,
122 const wxString
& name
,
124 int desiredWidth
, int desiredHeight
)
126 wxIcon
*icon
= wxDynamicCast(image
, wxIcon
);
127 wxCHECK_MSG( icon
, FALSE
, _T("wxIconHandler only works with icons") );
129 return LoadIcon(icon
, name
, flags
, desiredWidth
, desiredHeight
);
133 virtual bool LoadIcon(wxIcon
*icon
,
134 const wxString
& name
, long flags
,
135 int desiredWidth
= -1, int desiredHeight
= -1) = 0;
138 class WXDLLEXPORT wxICOFileHandler
: public wxIconHandler
141 wxICOFileHandler() : wxIconHandler(_T("ICO icon file"),
147 virtual bool LoadIcon(wxIcon
*icon
,
148 const wxString
& name
, long flags
,
149 int desiredWidth
= -1, int desiredHeight
= -1);
152 DECLARE_DYNAMIC_CLASS(wxICOFileHandler
)
155 class WXDLLEXPORT wxICOResourceHandler
: public wxIconHandler
158 wxICOResourceHandler() : wxIconHandler(_T("ICO resource"),
160 wxBITMAP_TYPE_ICO_RESOURCE
)
164 virtual bool LoadIcon(wxIcon
*icon
,
165 const wxString
& name
, long flags
,
166 int desiredWidth
= -1, int desiredHeight
= -1);
169 DECLARE_DYNAMIC_CLASS(wxICOResourceHandler
)
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
176 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
177 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
178 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler
, wxGDIImageHandler
)
179 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler
, wxGDIImageHandler
)
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
185 static wxSize
GetHiconSize(HICON hicon
);
187 // ============================================================================
189 // ============================================================================
191 wxList
wxGDIImage::ms_handlers
;
193 // ----------------------------------------------------------------------------
194 // wxGDIImage functions forwarded to wxGDIImageRefData
195 // ----------------------------------------------------------------------------
197 bool wxGDIImage::FreeResource(bool WXUNUSED(force
))
201 GetGDIImageData()->Free();
202 GetGDIImageData()->m_handle
= 0;
208 WXHANDLE
wxGDIImage::GetResourceHandle()
213 // ----------------------------------------------------------------------------
214 // wxGDIImage handler stuff
215 // ----------------------------------------------------------------------------
217 void wxGDIImage::AddHandler(wxGDIImageHandler
*handler
)
219 ms_handlers
.Append(handler
);
222 void wxGDIImage::InsertHandler(wxGDIImageHandler
*handler
)
224 ms_handlers
.Insert(handler
);
227 bool wxGDIImage::RemoveHandler(const wxString
& name
)
229 wxGDIImageHandler
*handler
= FindHandler(name
);
232 ms_handlers
.DeleteObject(handler
);
239 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& name
)
241 wxNode
*node
= ms_handlers
.First();
244 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
245 if ( handler
->GetName() == name
)
253 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& extension
,
256 wxNode
*node
= ms_handlers
.First();
259 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
260 if ( (handler
->GetExtension() = extension
) &&
261 (type
== -1 || handler
->GetType() == type
) )
271 wxGDIImageHandler
*wxGDIImage::FindHandler(long type
)
273 wxNode
*node
= ms_handlers
.First();
276 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
277 if ( handler
->GetType() == type
)
286 void wxGDIImage::CleanUpHandlers()
288 wxNode
*node
= ms_handlers
.First();
291 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
292 wxNode
*next
= node
->Next();
299 void wxGDIImage::InitStandardHandlers()
301 AddHandler(new wxBMPResourceHandler
);
302 AddHandler(new wxBMPFileHandler
);
304 // GRG: Add these handlers by default if XPM support is enabled
307 AddHandler(new wxXPMFileHandler
);
308 AddHandler(new wxXPMDataHandler
);
309 #endif // wxUSE_XPM_IN_MSW
311 AddHandler(new wxICOResourceHandler
);
312 AddHandler(new wxICOFileHandler
);
315 // ----------------------------------------------------------------------------
317 // ----------------------------------------------------------------------------
319 bool wxBMPResourceHandler::LoadFile(wxBitmap
*bitmap
,
320 const wxString
& name
, long WXUNUSED(flags
),
321 int WXUNUSED(desiredWidth
),
322 int WXUNUSED(desiredHeight
))
324 // TODO: load colourmap.
325 bitmap
->SetHBITMAP((WXHBITMAP
)::LoadBitmap(wxGetInstance(), name
));
327 wxBitmapRefData
*data
= bitmap
->GetBitmapData();
331 if ( !::GetObject(GetHbitmapOf(*bitmap
), sizeof(BITMAP
), (LPSTR
) &bm
) )
333 wxLogLastError("GetObject(HBITMAP)");
336 data
->m_width
= bm
.bmWidth
;
337 data
->m_height
= bm
.bmHeight
;
338 data
->m_depth
= bm
.bmBitsPixel
;
342 // it's probably not found
343 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
350 bool wxBMPFileHandler::LoadFile(wxBitmap
*bitmap
,
351 const wxString
& name
, long WXUNUSED(flags
),
352 int WXUNUSED(desiredWidth
),
353 int WXUNUSED(desiredHeight
))
355 #if wxUSE_IMAGE_LOADING_IN_MSW
356 wxPalette
*palette
= NULL
;
357 bool success
= wxLoadIntoBitmap(WXSTRINGCAST name
, bitmap
, &palette
) != 0;
358 if ( success
&& palette
)
360 bitmap
->SetPalette(*palette
);
363 // it was copied by the bitmap if it was loaded successfully
372 bool wxBMPFileHandler::SaveFile(wxBitmap
*bitmap
,
373 const wxString
& name
,
375 const wxPalette
*pal
)
377 #if wxUSE_IMAGE_LOADING_IN_MSW
378 wxPalette
*actualPalette
= (wxPalette
*)pal
;
379 if ( !actualPalette
)
380 actualPalette
= bitmap
->GetPalette();
381 return wxSaveBitmap(WXSTRINGCAST name
, bitmap
, actualPalette
) != 0;
387 // ----------------------------------------------------------------------------
389 // ----------------------------------------------------------------------------
391 bool wxICOFileHandler::LoadIcon(wxIcon
*icon
,
392 const wxString
& name
,
394 int desiredWidth
, int desiredHeight
)
396 #if wxUSE_RESOURCE_LOADING_IN_MSW
403 HICON hicon
= ::ExtractIcon(wxGetInstance(), name
, 0 /* first */);
406 wxLogSysError(_T("Failed to load icon from the file '%s'"),
412 size
= GetHiconSize(hicon
);
414 HICON hicon
= ReadIconFile((wxChar
*)name
.c_str(),
417 #endif // Win32/Win16
419 if ( (desiredWidth
!= -1 && desiredWidth
!= size
.x
) ||
420 (desiredHeight
!= -1 && desiredHeight
!= size
.y
) )
422 wxLogDebug(_T("Returning FALSE from wxICOFileHandler::Load because "
423 "of the size mismatch: actual (%d, %d), "
424 "requested (%d, %d)"),
426 desiredWidth
, desiredHeight
);
428 ::DestroyIcon(hicon
);
433 icon
->SetHICON((WXHICON
)hicon
);
434 icon
->SetSize(size
.x
, size
.y
);
442 bool wxICOResourceHandler::LoadIcon(wxIcon
*icon
,
443 const wxString
& name
,
445 int desiredWidth
, int desiredHeight
)
449 #if defined(__WIN32__) && !defined(__SC__)
450 if ( desiredWidth
!= -1 && desiredHeight
!= -1 )
452 hicon
= (HICON
)::LoadImage(wxGetInstance(), name
, IMAGE_ICON
,
453 desiredWidth
, desiredHeight
,
459 hicon
= ::LoadIcon(wxGetInstance(), name
);
462 wxSize size
= GetHiconSize(hicon
);
463 icon
->SetSize(size
.x
, size
.y
);
465 // Override the found values with desired values
466 if ( desiredWidth
> -1 && desiredHeight
> -1 )
468 icon
->SetSize(desiredWidth
, desiredHeight
);
471 icon
->SetHICON((WXHICON
)hicon
);
476 // ----------------------------------------------------------------------------
478 // ----------------------------------------------------------------------------
480 static wxSize
GetHiconSize(HICON hicon
)
482 wxSize
size(32, 32); // default
485 // Win32s doesn't have GetIconInfo function...
486 if ( hicon
&& wxGetOsVersion() != wxWIN32S
)
489 if ( !::GetIconInfo(hicon
, &info
) )
491 wxLogLastError("GetIconInfo");
495 HBITMAP hbmp
= info
.hbmMask
;
499 if ( ::GetObject(hbmp
, sizeof(BITMAP
), (LPSTR
) &bm
) )
501 size
= wxSize(bm
.bmWidth
, bm
.bmHeight
);
504 ::DeleteObject(info
.hbmMask
);
507 ::DeleteObject(info
.hbmColor
);