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"
36 #include "wx/msw/private.h"
40 #include "wx/msw/dib.h"
41 #include "wx/msw/bitmap.h"
42 #include "wx/msw/gdiimage.h"
43 #include "wx/bitmap.h"
46 # include "wx/msw/curico.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 // all image handlers are declared/defined in this file because the outside
54 // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids)
56 class WXDLLEXPORT wxBMPFileHandler
: public wxBitmapHandler
59 wxBMPFileHandler() : wxBitmapHandler(_T("Windows bitmap file"), _T("bmp"),
64 virtual bool LoadFile(wxBitmap
*bitmap
,
65 const wxString
& name
, long flags
,
66 int desiredWidth
, int desiredHeight
);
67 virtual bool SaveFile(wxBitmap
*bitmap
,
68 const wxString
& name
, int type
,
69 const wxPalette
*palette
= NULL
);
72 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
)
75 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
78 wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"),
80 wxBITMAP_TYPE_BMP_RESOURCE
)
84 virtual bool LoadFile(wxBitmap
*bitmap
,
85 const wxString
& name
, long flags
,
86 int desiredWidth
, int desiredHeight
);
89 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
92 class WXDLLEXPORT wxIconHandler
: public wxGDIImageHandler
95 wxIconHandler(const wxString
& name
, const wxString
& ext
, long type
)
96 : wxGDIImageHandler(name
, ext
, type
)
100 // creating and saving icons is not supported
101 virtual bool Create(wxGDIImage
*WXUNUSED(image
),
102 void *WXUNUSED(data
),
103 long WXUNUSED(flags
),
105 int WXUNUSED(height
),
106 int WXUNUSED(depth
) = 1)
111 virtual bool Save(wxGDIImage
*WXUNUSED(image
),
112 const wxString
& WXUNUSED(name
),
118 virtual bool Load(wxGDIImage
*image
,
119 const wxString
& name
,
121 int desiredWidth
, int desiredHeight
)
123 wxIcon
*icon
= wxDynamicCast(image
, wxIcon
);
124 wxCHECK_MSG( icon
, FALSE
, _T("wxIconHandler only works with icons") );
126 return LoadIcon(icon
, name
, flags
, desiredWidth
, desiredHeight
);
130 virtual bool LoadIcon(wxIcon
*icon
,
131 const wxString
& name
, long flags
,
132 int desiredWidth
= -1, int desiredHeight
= -1) = 0;
135 class WXDLLEXPORT wxICOFileHandler
: public wxIconHandler
138 wxICOFileHandler() : wxIconHandler(_T("ICO icon file"),
144 virtual bool LoadIcon(wxIcon
*icon
,
145 const wxString
& name
, long flags
,
146 int desiredWidth
= -1, int desiredHeight
= -1);
149 DECLARE_DYNAMIC_CLASS(wxICOFileHandler
)
152 class WXDLLEXPORT wxICOResourceHandler
: public wxIconHandler
155 wxICOResourceHandler() : wxIconHandler(_T("ICO resource"),
157 wxBITMAP_TYPE_ICO_RESOURCE
)
161 virtual bool LoadIcon(wxIcon
*icon
,
162 const wxString
& name
, long flags
,
163 int desiredWidth
= -1, int desiredHeight
= -1);
166 DECLARE_DYNAMIC_CLASS(wxICOResourceHandler
)
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
173 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
174 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
175 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler
, wxGDIImageHandler
)
176 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler
, wxGDIImageHandler
)
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
182 static wxSize
GetHiconSize(HICON hicon
);
184 // ============================================================================
186 // ============================================================================
188 wxList
wxGDIImage::ms_handlers
;
190 // ----------------------------------------------------------------------------
191 // wxGDIImage functions forwarded to wxGDIImageRefData
192 // ----------------------------------------------------------------------------
194 bool wxGDIImage::FreeResource(bool WXUNUSED(force
))
198 GetGDIImageData()->Free();
199 GetGDIImageData()->m_handle
= 0;
205 WXHANDLE
wxGDIImage::GetResourceHandle()
210 // ----------------------------------------------------------------------------
211 // wxGDIImage handler stuff
212 // ----------------------------------------------------------------------------
214 void wxGDIImage::AddHandler(wxGDIImageHandler
*handler
)
216 ms_handlers
.Append(handler
);
219 void wxGDIImage::InsertHandler(wxGDIImageHandler
*handler
)
221 ms_handlers
.Insert(handler
);
224 bool wxGDIImage::RemoveHandler(const wxString
& name
)
226 wxGDIImageHandler
*handler
= FindHandler(name
);
229 ms_handlers
.DeleteObject(handler
);
236 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& name
)
238 wxNode
*node
= ms_handlers
.First();
241 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
242 if ( handler
->GetName() == name
)
250 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& extension
,
253 wxNode
*node
= ms_handlers
.First();
256 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
257 if ( (handler
->GetExtension() = extension
) &&
258 (type
== -1 || handler
->GetType() == type
) )
268 wxGDIImageHandler
*wxGDIImage::FindHandler(long type
)
270 wxNode
*node
= ms_handlers
.First();
273 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
274 if ( handler
->GetType() == type
)
283 void wxGDIImage::CleanUpHandlers()
285 wxNode
*node
= ms_handlers
.First();
288 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
289 wxNode
*next
= node
->Next();
296 void wxGDIImage::InitStandardHandlers()
298 AddHandler(new wxBMPResourceHandler
);
299 AddHandler(new wxBMPFileHandler
);
300 AddHandler(new wxICOResourceHandler
);
301 AddHandler(new wxICOFileHandler
);
304 // ----------------------------------------------------------------------------
306 // ----------------------------------------------------------------------------
308 bool wxBMPResourceHandler::LoadFile(wxBitmap
*bitmap
,
309 const wxString
& name
, long WXUNUSED(flags
),
310 int WXUNUSED(desiredWidth
),
311 int WXUNUSED(desiredHeight
))
313 // TODO: load colourmap.
314 bitmap
->SetHBITMAP((WXHBITMAP
)::LoadBitmap(wxGetInstance(), name
));
316 wxBitmapRefData
*data
= bitmap
->GetBitmapData();
320 if ( !::GetObject(GetHbitmapOf(*bitmap
), sizeof(BITMAP
), (LPSTR
) &bm
) )
322 wxLogLastError(wxT("GetObject(HBITMAP)"));
325 data
->m_width
= bm
.bmWidth
;
326 data
->m_height
= bm
.bmHeight
;
327 data
->m_depth
= bm
.bmBitsPixel
;
331 // it's probably not found
332 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
339 bool wxBMPFileHandler::LoadFile(wxBitmap
*bitmap
,
340 const wxString
& name
, long WXUNUSED(flags
),
341 int WXUNUSED(desiredWidth
),
342 int WXUNUSED(desiredHeight
))
344 #if wxUSE_IMAGE_LOADING_IN_MSW
345 wxPalette
*palette
= NULL
;
346 bool success
= wxLoadIntoBitmap(WXSTRINGCAST name
, bitmap
, &palette
) != 0;
347 if ( success
&& palette
)
349 bitmap
->SetPalette(*palette
);
352 // it was copied by the bitmap if it was loaded successfully
361 bool wxBMPFileHandler::SaveFile(wxBitmap
*bitmap
,
362 const wxString
& name
,
364 const wxPalette
*pal
)
366 #if wxUSE_IMAGE_LOADING_IN_MSW
367 wxPalette
*actualPalette
= (wxPalette
*)pal
;
368 if ( !actualPalette
)
369 actualPalette
= bitmap
->GetPalette();
370 return wxSaveBitmap(WXSTRINGCAST name
, bitmap
, actualPalette
) != 0;
376 // ----------------------------------------------------------------------------
378 // ----------------------------------------------------------------------------
380 bool wxICOFileHandler::LoadIcon(wxIcon
*icon
,
381 const wxString
& name
,
382 long WXUNUSED(flags
),
383 int desiredWidth
, int desiredHeight
)
385 #if wxUSE_RESOURCE_LOADING_IN_MSW
394 // were we asked for a large icon?
395 if ( desiredWidth
== ::GetSystemMetrics(SM_CXICON
) &&
396 desiredHeight
== ::GetSystemMetrics(SM_CYICON
) )
398 // get the first large icon from file
399 if ( !::ExtractIconEx(name
, 0, &hicon
, NULL
, 1) )
401 // it is not an error, but it might still be useful to be informed
402 // about it optionally
403 wxLogTrace(_T("iconload"),
404 _T("No large icons found in the file '%s'."),
408 else if ( desiredWidth
== ::GetSystemMetrics(SM_CXSMICON
) &&
409 desiredHeight
== ::GetSystemMetrics(SM_CYSMICON
) )
411 // get the first small icon from file
412 if ( !::ExtractIconEx(name
, 0, NULL
, &hicon
, 1) )
414 wxLogTrace(_T("iconload"),
415 _T("No small icons found in the file '%s'."),
419 //else: not standard size, load below
423 // take any (the first one) icon from the file by default
424 hicon
= ::ExtractIcon(wxGetInstance(), name
, 0 /* first */);
429 wxLogSysError(_T("Failed to load icon from the file '%s'"),
435 size
= GetHiconSize(hicon
);
437 HICON hicon
= ReadIconFile((wxChar
*)name
.c_str(),
440 #endif // Win32/Win16
442 if ( (desiredWidth
!= -1 && desiredWidth
!= size
.x
) ||
443 (desiredHeight
!= -1 && desiredHeight
!= size
.y
) )
445 wxLogTrace(_T("iconload"),
446 _T("Returning FALSE from wxICOFileHandler::Load because of the size mismatch: actual (%d, %d), requested (%d, %d)"),
448 desiredWidth
, desiredHeight
);
450 ::DestroyIcon(hicon
);
455 icon
->SetHICON((WXHICON
)hicon
);
456 icon
->SetSize(size
.x
, size
.y
);
464 bool wxICOResourceHandler::LoadIcon(wxIcon
*icon
,
465 const wxString
& name
,
466 long WXUNUSED(flags
),
467 int desiredWidth
, int desiredHeight
)
471 #if defined(__WIN32__) && !defined(__SC__)
472 if ( desiredWidth
!= -1 && desiredHeight
!= -1 )
474 hicon
= (HICON
)::LoadImage(wxGetInstance(), name
, IMAGE_ICON
,
475 desiredWidth
, desiredHeight
,
481 hicon
= ::LoadIcon(wxGetInstance(), name
);
484 wxSize size
= GetHiconSize(hicon
);
485 icon
->SetSize(size
.x
, size
.y
);
487 // Override the found values with desired values
488 if ( desiredWidth
> -1 && desiredHeight
> -1 )
490 icon
->SetSize(desiredWidth
, desiredHeight
);
493 icon
->SetHICON((WXHICON
)hicon
);
498 // ----------------------------------------------------------------------------
500 // ----------------------------------------------------------------------------
502 static wxSize
GetHiconSize(HICON hicon
)
504 wxSize
size(32, 32); // default
507 // Win32s doesn't have GetIconInfo function...
508 if ( hicon
&& wxGetOsVersion() != wxWIN32S
)
511 if ( !::GetIconInfo(hicon
, &info
) )
513 wxLogLastError(wxT("GetIconInfo"));
517 HBITMAP hbmp
= info
.hbmMask
;
521 if ( ::GetObject(hbmp
, sizeof(BITMAP
), (LPSTR
) &bm
) )
523 size
= wxSize(bm
.bmWidth
, bm
.bmHeight
);
526 ::DeleteObject(info
.hbmMask
);
529 ::DeleteObject(info
.hbmColor
);