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 #ifndef __WXMICROWIN__
41 #include "wx/msw/dib.h"
44 #include "wx/msw/bitmap.h"
45 #include "wx/msw/gdiimage.h"
46 #include "wx/bitmap.h"
49 # include "wx/msw/curico.h"
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 #ifndef __WXMICROWIN__
58 // all image handlers are declared/defined in this file because the outside
59 // world doesn't have to know about them (but only about wxBITMAP_TYPE_XXX ids)
61 class WXDLLEXPORT wxBMPFileHandler
: public wxBitmapHandler
64 wxBMPFileHandler() : wxBitmapHandler(_T("Windows bitmap file"), _T("bmp"),
69 virtual bool LoadFile(wxBitmap
*bitmap
,
70 const wxString
& name
, long flags
,
71 int desiredWidth
, int desiredHeight
);
72 virtual bool SaveFile(wxBitmap
*bitmap
,
73 const wxString
& name
, int type
,
74 const wxPalette
*palette
= NULL
);
77 DECLARE_DYNAMIC_CLASS(wxBMPFileHandler
)
80 class WXDLLEXPORT wxBMPResourceHandler
: public wxBitmapHandler
83 wxBMPResourceHandler() : wxBitmapHandler(_T("Windows bitmap resource"),
85 wxBITMAP_TYPE_BMP_RESOURCE
)
89 virtual bool LoadFile(wxBitmap
*bitmap
,
90 const wxString
& name
, long flags
,
91 int desiredWidth
, int desiredHeight
);
94 DECLARE_DYNAMIC_CLASS(wxBMPResourceHandler
)
97 class WXDLLEXPORT wxIconHandler
: public wxGDIImageHandler
100 wxIconHandler(const wxString
& name
, const wxString
& ext
, long type
)
101 : wxGDIImageHandler(name
, ext
, type
)
105 // creating and saving icons is not supported
106 virtual bool Create(wxGDIImage
*WXUNUSED(image
),
107 void *WXUNUSED(data
),
108 long WXUNUSED(flags
),
110 int WXUNUSED(height
),
111 int WXUNUSED(depth
) = 1)
116 virtual bool Save(wxGDIImage
*WXUNUSED(image
),
117 const wxString
& WXUNUSED(name
),
123 virtual bool Load(wxGDIImage
*image
,
124 const wxString
& name
,
126 int desiredWidth
, int desiredHeight
)
128 wxIcon
*icon
= wxDynamicCast(image
, wxIcon
);
129 wxCHECK_MSG( icon
, FALSE
, _T("wxIconHandler only works with icons") );
131 return LoadIcon(icon
, name
, flags
, desiredWidth
, desiredHeight
);
135 virtual bool LoadIcon(wxIcon
*icon
,
136 const wxString
& name
, long flags
,
137 int desiredWidth
= -1, int desiredHeight
= -1) = 0;
140 class WXDLLEXPORT wxICOFileHandler
: public wxIconHandler
143 wxICOFileHandler() : wxIconHandler(_T("ICO icon file"),
149 virtual bool LoadIcon(wxIcon
*icon
,
150 const wxString
& name
, long flags
,
151 int desiredWidth
= -1, int desiredHeight
= -1);
154 DECLARE_DYNAMIC_CLASS(wxICOFileHandler
)
157 class WXDLLEXPORT wxICOResourceHandler
: public wxIconHandler
160 wxICOResourceHandler() : wxIconHandler(_T("ICO resource"),
162 wxBITMAP_TYPE_ICO_RESOURCE
)
166 virtual bool LoadIcon(wxIcon
*icon
,
167 const wxString
& name
, long flags
,
168 int desiredWidth
= -1, int desiredHeight
= -1);
171 DECLARE_DYNAMIC_CLASS(wxICOResourceHandler
)
174 // ----------------------------------------------------------------------------
176 // ----------------------------------------------------------------------------
178 IMPLEMENT_DYNAMIC_CLASS(wxBMPFileHandler
, wxBitmapHandler
)
179 IMPLEMENT_DYNAMIC_CLASS(wxBMPResourceHandler
, wxBitmapHandler
)
180 IMPLEMENT_DYNAMIC_CLASS(wxICOFileHandler
, wxObject
)
181 IMPLEMENT_DYNAMIC_CLASS(wxICOResourceHandler
, wxObject
)
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
190 // ============================================================================
192 // ============================================================================
194 wxList
wxGDIImage::ms_handlers
;
196 // ----------------------------------------------------------------------------
197 // wxGDIImage functions forwarded to wxGDIImageRefData
198 // ----------------------------------------------------------------------------
200 bool wxGDIImage::FreeResource(bool WXUNUSED(force
))
204 GetGDIImageData()->Free();
205 GetGDIImageData()->m_handle
= 0;
211 WXHANDLE
wxGDIImage::GetResourceHandle() const
216 // ----------------------------------------------------------------------------
217 // wxGDIImage handler stuff
218 // ----------------------------------------------------------------------------
220 void wxGDIImage::AddHandler(wxGDIImageHandler
*handler
)
222 ms_handlers
.Append(handler
);
225 void wxGDIImage::InsertHandler(wxGDIImageHandler
*handler
)
227 ms_handlers
.Insert(handler
);
230 bool wxGDIImage::RemoveHandler(const wxString
& name
)
232 wxGDIImageHandler
*handler
= FindHandler(name
);
235 ms_handlers
.DeleteObject(handler
);
242 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& name
)
244 wxNode
*node
= ms_handlers
.First();
247 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
248 if ( handler
->GetName() == name
)
256 wxGDIImageHandler
*wxGDIImage::FindHandler(const wxString
& extension
,
259 wxNode
*node
= ms_handlers
.First();
262 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
263 if ( (handler
->GetExtension() = extension
) &&
264 (type
== -1 || handler
->GetType() == type
) )
274 wxGDIImageHandler
*wxGDIImage::FindHandler(long type
)
276 wxNode
*node
= ms_handlers
.First();
279 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
280 if ( handler
->GetType() == type
)
289 void wxGDIImage::CleanUpHandlers()
291 wxNode
*node
= ms_handlers
.First();
294 wxGDIImageHandler
*handler
= (wxGDIImageHandler
*)node
->Data();
295 wxNode
*next
= node
->Next();
302 void wxGDIImage::InitStandardHandlers()
304 #ifndef __WXMICROWIN__
305 AddHandler(new wxBMPResourceHandler
);
306 AddHandler(new wxBMPFileHandler
);
307 AddHandler(new wxICOResourceHandler
);
308 AddHandler(new wxICOFileHandler
);
312 #ifndef __WXMICROWIN__
314 // ----------------------------------------------------------------------------
316 // ----------------------------------------------------------------------------
318 bool wxBMPResourceHandler::LoadFile(wxBitmap
*bitmap
,
319 const wxString
& name
, long WXUNUSED(flags
),
320 int WXUNUSED(desiredWidth
),
321 int WXUNUSED(desiredHeight
))
323 // TODO: load colourmap.
324 bitmap
->SetHBITMAP((WXHBITMAP
)::LoadBitmap(wxGetInstance(), name
));
326 wxBitmapRefData
*data
= bitmap
->GetBitmapData();
330 if ( !::GetObject(GetHbitmapOf(*bitmap
), sizeof(BITMAP
), (LPSTR
) &bm
) )
332 wxLogLastError(wxT("GetObject(HBITMAP)"));
335 data
->m_width
= bm
.bmWidth
;
336 data
->m_height
= bm
.bmHeight
;
337 data
->m_depth
= bm
.bmBitsPixel
;
341 // it's probably not found
342 wxLogError(wxT("Can't load bitmap '%s' from resources! Check .rc file."),
349 bool wxBMPFileHandler::LoadFile(wxBitmap
*bitmap
,
350 const wxString
& name
, long WXUNUSED(flags
),
351 int WXUNUSED(desiredWidth
),
352 int WXUNUSED(desiredHeight
))
354 #if wxUSE_IMAGE_LOADING_IN_MSW
355 wxPalette
*palette
= NULL
;
356 bool success
= wxLoadIntoBitmap(WXSTRINGCAST name
, bitmap
, &palette
) != 0;
359 if ( success
&& palette
)
361 bitmap
->SetPalette(*palette
);
364 // it was copied by the bitmap if it was loaded successfully
366 #endif // wxUSE_PALETTE
374 bool wxBMPFileHandler::SaveFile(wxBitmap
*bitmap
,
375 const wxString
& name
,
377 const wxPalette
*pal
)
379 #if wxUSE_IMAGE_LOADING_IN_MSW
382 wxPalette
*actualPalette
= (wxPalette
*)pal
;
383 if ( !actualPalette
)
384 actualPalette
= bitmap
->GetPalette();
386 wxPalette
*actualPalette
= NULL
;
387 #endif // wxUSE_PALETTE
389 return wxSaveBitmap(WXSTRINGCAST name
, bitmap
, actualPalette
) != 0;
395 // ----------------------------------------------------------------------------
397 // ----------------------------------------------------------------------------
399 bool wxICOFileHandler::LoadIcon(wxIcon
*icon
,
400 const wxString
& name
,
401 long WXUNUSED(flags
),
402 int desiredWidth
, int desiredHeight
)
404 #if wxUSE_RESOURCE_LOADING_IN_MSW
413 // Parse the filename: it may be of the form "filename;n" in order to
414 // specify the nth icon in the file.
416 // For the moment, ignore the issue of possible semicolons in the
419 wxString
nameReal(name
);
420 wxString strIconIndex
= name
.AfterLast(wxT(';'));
421 if (strIconIndex
!= name
)
423 iconIndex
= wxAtoi(strIconIndex
);
424 nameReal
= name
.BeforeLast(wxT(';'));
428 // If we don't know what size icon we're looking for,
429 // try to find out what's there.
430 // Unfortunately this doesn't work, because ExtractIconEx
431 // will scale the icon to the 'desired' size, even if that
432 // size of icon isn't explicitly stored. So we would have
433 // to parse the icon file outselves.
434 if ( desiredWidth
== -1 &&
437 // Try loading a large icon first
438 if ( ::ExtractIconEx(nameReal
, iconIndex
, &hicon
, NULL
, 1) == 1)
441 // Then try loading a small icon
442 else if ( ::ExtractIconEx(nameReal
, iconIndex
, NULL
, &hicon
, 1) == 1)
448 // were we asked for a large icon?
449 if ( desiredWidth
== ::GetSystemMetrics(SM_CXICON
) &&
450 desiredHeight
== ::GetSystemMetrics(SM_CYICON
) )
452 // get the specified large icon from file
453 if ( !::ExtractIconEx(nameReal
, iconIndex
, &hicon
, NULL
, 1) )
455 // it is not an error, but it might still be useful to be informed
456 // about it optionally
457 wxLogTrace(_T("iconload"),
458 _T("No large icons found in the file '%s'."),
462 else if ( desiredWidth
== ::GetSystemMetrics(SM_CXSMICON
) &&
463 desiredHeight
== ::GetSystemMetrics(SM_CYSMICON
) )
465 // get the specified small icon from file
466 if ( !::ExtractIconEx(nameReal
, iconIndex
, NULL
, &hicon
, 1) )
468 wxLogTrace(_T("iconload"),
469 _T("No small icons found in the file '%s'."),
473 //else: not standard size, load below
477 // take any size icon from the file by index
478 hicon
= ::ExtractIcon(wxGetInstance(), nameReal
, iconIndex
);
483 wxLogSysError(_T("Failed to load icon from the file '%s'"),
489 size
= wxGetHiconSize(hicon
);
491 HICON hicon
= ReadIconFile((wxChar
*)name
.c_str(),
494 #endif // Win32/Win16
496 if ( (desiredWidth
!= -1 && desiredWidth
!= size
.x
) ||
497 (desiredHeight
!= -1 && desiredHeight
!= size
.y
) )
499 wxLogTrace(_T("iconload"),
500 _T("Returning FALSE from wxICOFileHandler::Load because of the size mismatch: actual (%d, %d), requested (%d, %d)"),
502 desiredWidth
, desiredHeight
);
504 ::DestroyIcon(hicon
);
509 icon
->SetHICON((WXHICON
)hicon
);
510 icon
->SetSize(size
.x
, size
.y
);
518 bool wxICOResourceHandler::LoadIcon(wxIcon
*icon
,
519 const wxString
& name
,
520 long WXUNUSED(flags
),
521 int desiredWidth
, int desiredHeight
)
525 // do we need the icon of the specific size or would any icon do?
526 bool hasSize
= desiredWidth
!= -1 || desiredHeight
!= -1;
528 wxASSERT_MSG( !hasSize
|| (desiredWidth
!= -1 && desiredHeight
!= -1),
529 _T("width and height should be either both -1 or not") );
531 // try to load the icon from this program first to allow overriding the
532 // standard icons (although why one would want to do it considering that
533 // we already have wxApp::GetStdIcon() is unclear)
535 // note that we can't just always call LoadImage() because it seems to do
536 // some icon rescaling internally which results in very ugly 16x16 icons
537 #if defined(__WIN32__) && !defined(__SC__)
540 hicon
= (HICON
)::LoadImage(wxGetInstance(), name
, IMAGE_ICON
,
541 desiredWidth
, desiredHeight
,
545 #endif // Win32/!Win32
547 hicon
= ::LoadIcon(wxGetInstance(), name
);
550 // next check if it's not a standard icon
551 if ( !hicon
&& !hasSize
)
559 { wxT("wxICON_QUESTION"), IDI_QUESTION
},
560 { wxT("wxICON_WARNING"), IDI_EXCLAMATION
},
561 { wxT("wxICON_ERROR"), IDI_HAND
},
562 { wxT("wxICON_INFORMATION"), IDI_ASTERISK
},
565 for ( size_t nIcon
= 0; !hicon
&& nIcon
< WXSIZEOF(stdIcons
); nIcon
++ )
567 if ( name
== stdIcons
[nIcon
].name
)
569 hicon
= ::LoadIcon((HINSTANCE
)NULL
, stdIcons
[nIcon
].id
);
574 wxSize size
= wxGetHiconSize(hicon
);
575 icon
->SetSize(size
.x
, size
.y
);
577 icon
->SetHICON((WXHICON
)hicon
);
582 // ----------------------------------------------------------------------------
584 // ----------------------------------------------------------------------------
586 wxSize
wxGetHiconSize(HICON hicon
)
588 wxSize
size(32, 32); // default
591 // Win32s doesn't have GetIconInfo function...
592 if ( hicon
&& wxGetOsVersion() != wxWIN32S
)
595 if ( !::GetIconInfo(hicon
, &info
) )
597 wxLogLastError(wxT("GetIconInfo"));
601 HBITMAP hbmp
= info
.hbmMask
;
605 if ( ::GetObject(hbmp
, sizeof(BITMAP
), (LPSTR
) &bm
) )
607 size
= wxSize(bm
.bmWidth
, bm
.bmHeight
);
610 ::DeleteObject(info
.hbmMask
);
613 ::DeleteObject(info
.hbmColor
);