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/arrimpl.cpp"
30 WX_DEFINE_OBJARRAY(wxIconArray
)
32 IMPLEMENT_DYNAMIC_CLASS(wxIconBundle
, wxGDIObject
)
34 #define M_ICONBUNDLEDATA static_cast<wxIconBundleRefData*>(m_refData)
36 // ----------------------------------------------------------------------------
37 // wxIconBundleRefData
38 // ----------------------------------------------------------------------------
40 class WXDLLEXPORT wxIconBundleRefData
: public wxGDIRefData
43 // default and copy ctors and assignment operators are ok
45 virtual bool IsOk() const { return !m_icons
.empty(); }
50 // ============================================================================
51 // wxIconBundle implementation
52 // ============================================================================
54 wxIconBundle::wxIconBundle()
58 wxIconBundle::wxIconBundle(const wxString
& file
, wxBitmapType type
)
65 wxIconBundle::wxIconBundle(wxInputStream
& stream
, wxBitmapType type
)
68 AddIcon(stream
, type
);
70 #endif // wxUSE_STREAMS
72 wxIconBundle::wxIconBundle(const wxIcon
& icon
)
78 wxGDIRefData
*wxIconBundle::CreateGDIRefData() const
80 return new wxIconBundleRefData
;
83 wxGDIRefData
*wxIconBundle::CloneGDIRefData(const wxGDIRefData
*data
) const
85 return new wxIconBundleRefData(*static_cast<const wxIconBundleRefData
*>(data
));
88 void wxIconBundle::DeleteIcons()
96 // Adds icon from 'input' to the bundle. Shows 'errorMessage' on failure
97 // (it must contain "%d", because it is used to report # of image in the file
98 // that failed to load):
100 void DoAddIcon(wxIconBundle
& bundle
,
101 T
& input
, wxBitmapType type
,
102 const wxString
& errorMessage
)
104 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
107 const size_t count
= wxImage::GetImageCount(input
, type
);
108 for ( size_t i
= 0; i
< count
; ++i
)
110 if ( !image
.LoadFile(input
, type
, i
) )
112 wxLogError(errorMessage
, i
);
117 tmp
.CopyFromBitmap(wxBitmap(image
));
120 #else // !wxUSE_IMAGE
123 #endif // wxUSE_IMAGE/!wxUSE_IMAGE
126 } // anonymous namespace
128 void wxIconBundle::AddIcon(const wxString
& file
, wxBitmapType type
)
131 // Deal with standard icons
132 if ( type
== wxBITMAP_TYPE_ICON_RESOURCE
)
134 wxIcon
tmp(file
, type
);
147 wxString::Format(_("Failed to load image %%d from file '%s'."), file
)
152 void wxIconBundle::AddIcon(wxInputStream
& stream
, wxBitmapType type
)
154 DoAddIcon(*this, stream
, type
, _("Failed to load image %d from stream."));
156 #endif // wxUSE_STREAMS
158 wxIcon
wxIconBundle::GetIcon(const wxSize
& size
) const
160 const size_t count
= GetIconCount();
162 // optimize for the common case of icon bundles containing one icon only
167 // nothing to do, iconBest is already invalid
171 iconBest
= M_ICONBUNDLEDATA
->m_icons
[0];
175 // there is more than one icon, find the best match:
176 wxCoord sysX
= wxSystemSettings::GetMetric( wxSYS_ICON_X
),
177 sysY
= wxSystemSettings::GetMetric( wxSYS_ICON_Y
);
179 const wxIconArray
& iconArray
= M_ICONBUNDLEDATA
->m_icons
;
180 for ( size_t i
= 0; i
< count
; i
++ )
182 const wxIcon
& icon
= iconArray
[i
];
183 wxCoord sx
= icon
.GetWidth(),
184 sy
= icon
.GetHeight();
186 // if we got an icon of exactly the requested size, we're done
187 if ( sx
== size
.x
&& sy
== size
.y
)
193 // the best icon is by default (arbitrarily) the first one but
194 // if we find a system-sized icon, take it instead
195 if ((sx
== sysX
&& sy
== sysY
) || !iconBest
.IsOk())
200 #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
201 return wxIcon(iconBest
.GetHICON(), size
);
207 wxIcon
wxIconBundle::GetIconOfExactSize(const wxSize
& size
) const
209 wxIcon icon
= GetIcon(size
);
211 (icon
.GetWidth() != size
.x
|| icon
.GetHeight() != size
.y
) )
219 void wxIconBundle::AddIcon(const wxIcon
& icon
)
221 wxCHECK_RET( icon
.IsOk(), _T("invalid icon") );
225 wxIconArray
& iconArray
= M_ICONBUNDLEDATA
->m_icons
;
227 // replace existing icon with the same size if we already have it
228 const size_t count
= iconArray
.size();
229 for ( size_t i
= 0; i
< count
; ++i
)
231 wxIcon
& tmp
= iconArray
[i
];
233 tmp
.GetWidth() == icon
.GetWidth() &&
234 tmp
.GetHeight() == icon
.GetHeight() )
241 // if we don't, add an icon with new size
245 size_t wxIconBundle::GetIconCount() const
247 return IsOk() ? M_ICONBUNDLEDATA
->m_icons
.size() : 0;
250 wxIcon
wxIconBundle::GetIconByIndex(size_t n
) const
252 wxCHECK_MSG( n
< GetIconCount(), wxNullIcon
, _T("invalid index") );
254 return M_ICONBUNDLEDATA
->m_icons
[n
];