1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/iconbndl.cpp
3 // Purpose: wxIconBundle
4 // Author: Mattia Barbon, Vadim Zeitlin
7 // Copyright: (c) Mattia barbon
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #include "wx/iconbndl.h"
21 #include "wx/settings.h"
24 #include "wx/bitmap.h"
26 #include "wx/stream.h"
29 #include "wx/wfstream.h"
31 #include "wx/arrimpl.cpp"
32 WX_DEFINE_OBJARRAY(wxIconArray
)
34 IMPLEMENT_DYNAMIC_CLASS(wxIconBundle
, wxGDIObject
)
36 #define M_ICONBUNDLEDATA static_cast<wxIconBundleRefData*>(m_refData)
38 // ----------------------------------------------------------------------------
39 // wxIconBundleRefData
40 // ----------------------------------------------------------------------------
42 class WXDLLEXPORT wxIconBundleRefData
: public wxGDIRefData
45 // default and copy ctors and assignment operators are ok
47 virtual bool IsOk() const { return !m_icons
.empty(); }
52 // ============================================================================
53 // wxIconBundle implementation
54 // ============================================================================
56 wxIconBundle::wxIconBundle()
60 wxIconBundle::wxIconBundle(const wxString
& file
, wxBitmapType type
)
67 wxIconBundle::wxIconBundle(wxInputStream
& stream
, wxBitmapType type
)
70 AddIcon(stream
, type
);
72 #endif // wxUSE_STREAMS
74 wxIconBundle::wxIconBundle(const wxIcon
& icon
)
80 wxGDIRefData
*wxIconBundle::CreateGDIRefData() const
82 return new wxIconBundleRefData
;
85 wxGDIRefData
*wxIconBundle::CloneGDIRefData(const wxGDIRefData
*data
) const
87 return new wxIconBundleRefData(*static_cast<const wxIconBundleRefData
*>(data
));
90 void wxIconBundle::DeleteIcons()
98 // Adds icon from 'input' to the bundle. Shows 'errorMessage' on failure
99 // (it must contain "%d", because it is used to report # of image in the file
100 // that failed to load):
101 void DoAddIcon(wxIconBundle
& bundle
,
102 wxInputStream
& input
,
104 const wxString
& errorMessage
)
108 const wxFileOffset posOrig
= input
.TellI();
110 const size_t count
= wxImage::GetImageCount(input
, type
);
111 for ( size_t i
= 0; i
< count
; ++i
)
115 // the call to LoadFile() for the first sub-image updated the
116 // stream position but we need to start reading the subsequent
117 // sub-image at the image beginning too
118 input
.SeekI(posOrig
);
121 if ( !image
.LoadFile(input
, type
, i
) )
123 wxLogError(errorMessage
, i
);
127 if ( type
== wxBITMAP_TYPE_ANY
)
129 // store the type so that we don't need to try all handlers again
130 // for the subsequent images, they should all be of the same type
131 type
= image
.GetType();
135 tmp
.CopyFromBitmap(wxBitmap(image
));
140 } // anonymous namespace
142 void wxIconBundle::AddIcon(const wxString
& file
, wxBitmapType type
)
145 // Deal with standard icons
146 if ( type
== wxBITMAP_TYPE_ICON_RESOURCE
)
148 wxIcon
tmp(file
, type
);
157 wxFFileInputStream
stream(file
);
162 wxString::Format(_("Failed to load image %%d from file '%s'."), file
)
167 void wxIconBundle::AddIcon(wxInputStream
& stream
, wxBitmapType type
)
169 DoAddIcon(*this, stream
, type
, _("Failed to load image %d from stream."));
171 #endif // wxUSE_STREAMS
173 wxIcon
wxIconBundle::GetIcon(const wxSize
& size
) const
175 const size_t count
= GetIconCount();
177 // optimize for the common case of icon bundles containing one icon only
182 // nothing to do, iconBest is already invalid
186 iconBest
= M_ICONBUNDLEDATA
->m_icons
[0];
190 // there is more than one icon, find the best match:
191 wxCoord sysX
= wxSystemSettings::GetMetric( wxSYS_ICON_X
),
192 sysY
= wxSystemSettings::GetMetric( wxSYS_ICON_Y
);
194 const wxIconArray
& iconArray
= M_ICONBUNDLEDATA
->m_icons
;
195 for ( size_t i
= 0; i
< count
; i
++ )
197 const wxIcon
& icon
= iconArray
[i
];
198 wxCoord sx
= icon
.GetWidth(),
199 sy
= icon
.GetHeight();
201 // if we got an icon of exactly the requested size, we're done
202 if ( sx
== size
.x
&& sy
== size
.y
)
208 // the best icon is by default (arbitrarily) the first one but
209 // if we find a system-sized icon, take it instead
210 if ((sx
== sysX
&& sy
== sysY
) || !iconBest
.IsOk())
215 #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
216 return wxIcon(iconBest
.GetHICON(), size
);
222 wxIcon
wxIconBundle::GetIconOfExactSize(const wxSize
& size
) const
224 wxIcon icon
= GetIcon(size
);
226 (icon
.GetWidth() != size
.x
|| icon
.GetHeight() != size
.y
) )
234 void wxIconBundle::AddIcon(const wxIcon
& icon
)
236 wxCHECK_RET( icon
.IsOk(), _T("invalid icon") );
240 wxIconArray
& iconArray
= M_ICONBUNDLEDATA
->m_icons
;
242 // replace existing icon with the same size if we already have it
243 const size_t count
= iconArray
.size();
244 for ( size_t i
= 0; i
< count
; ++i
)
246 wxIcon
& tmp
= iconArray
[i
];
248 tmp
.GetWidth() == icon
.GetWidth() &&
249 tmp
.GetHeight() == icon
.GetHeight() )
256 // if we don't, add an icon with new size
260 size_t wxIconBundle::GetIconCount() const
262 return IsOk() ? M_ICONBUNDLEDATA
->m_icons
.size() : 0;
265 wxIcon
wxIconBundle::GetIconByIndex(size_t n
) const
267 wxCHECK_MSG( n
< GetIconCount(), wxNullIcon
, _T("invalid index") );
269 return M_ICONBUNDLEDATA
->m_icons
[n
];