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 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #include "wx/iconbndl.h"
29 #include "wx/settings.h"
33 #include "wx/bitmap.h"
37 #include "wx/arrimpl.cpp"
39 WX_DEFINE_OBJARRAY(wxIconArray
)
41 IMPLEMENT_DYNAMIC_CLASS(wxIconBundle
, wxGDIObject
)
43 #define M_ICONBUNDLEDATA ((wxIconBundleRefData *)m_refData)
45 // ----------------------------------------------------------------------------
46 // wxIconBundleRefData
47 // ----------------------------------------------------------------------------
49 class WXDLLEXPORT wxIconBundleRefData
: public wxGDIRefData
52 // default and copy ctors and assignment operators are ok
57 friend class WXDLLEXPORT wxIconBundle
;
60 // ============================================================================
61 // wxIconBundle implementation
62 // ============================================================================
64 wxIconBundle::wxIconBundle()
69 wxIconBundle::wxIconBundle(const wxString
& file
, long type
)
75 wxIconBundle::wxIconBundle(const wxIconBundle
& icon
)
81 wxIconBundle::wxIconBundle(const wxIcon
& icon
)
87 wxObjectRefData
*wxIconBundle::CreateRefData() const
89 return new wxIconBundleRefData
;
92 wxObjectRefData
*wxIconBundle::CloneRefData(const wxObjectRefData
*data
) const
94 return new wxIconBundleRefData(*wx_static_cast(const wxIconBundleRefData
*, data
));
97 void wxIconBundle::DeleteIcons()
102 bool wxIconBundle::IsOk() const
104 return M_ICONBUNDLEDATA
&& !M_ICONBUNDLEDATA
->m_icons
.IsEmpty();
107 void wxIconBundle::AddIcon(const wxString
& file
, long type
)
110 // Deal with standard icons
111 if ( type
== wxBITMAP_TYPE_ICON_RESOURCE
)
113 wxIcon
tmp(file
, type
);
122 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
125 const size_t count
= wxImage::GetImageCount( file
, type
);
126 for ( size_t i
= 0; i
< count
; ++i
)
128 if ( !image
.LoadFile( file
, type
, i
) )
130 wxLogError( _("Failed to load image %d from file '%s'."),
136 tmp
.CopyFromBitmap(wxBitmap(image
));
139 #else // !wxUSE_IMAGE
142 #endif // wxUSE_IMAGE/!wxUSE_IMAGE
145 wxIcon
wxIconBundle::GetIcon(const wxSize
& size
) const
147 const size_t count
= GetIconCount();
149 // optimize for the common case of icon bundles containing one icon only
154 // nothing to do, iconBest is already invalid
158 iconBest
= M_ICONBUNDLEDATA
->m_icons
[0];
162 // there is more than one icon, find the best match:
163 wxCoord sysX
= wxSystemSettings::GetMetric( wxSYS_ICON_X
),
164 sysY
= wxSystemSettings::GetMetric( wxSYS_ICON_Y
);
166 const wxIconArray
& iconArray
= M_ICONBUNDLEDATA
->m_icons
;
167 for ( size_t i
= 0; i
< count
; i
++ )
169 const wxIcon
& icon
= iconArray
[i
];
170 wxCoord sx
= icon
.GetWidth(),
171 sy
= icon
.GetHeight();
173 // if we got an icon of exactly the requested size, we're done
174 if ( sx
== size
.x
&& sy
== size
.y
)
180 // the best icon is by default (arbitrarily) the first one but
181 // if we find a system-sized icon, take it instead
182 if ( sx
== sysX
&& sy
== sysY
|| !iconBest
.IsOk() )
188 return wxIcon(iconBest
.GetHICON(), size
);
194 wxIcon
wxIconBundle::GetIconOfExactSize(const wxSize
& size
) const
196 wxIcon icon
= GetIcon(size
);
198 (icon
.GetWidth() != size
.x
|| icon
.GetHeight() != size
.y
) )
206 void wxIconBundle::AddIcon(const wxIcon
& icon
)
208 wxCHECK_RET( icon
.IsOk(), _T("invalid icon") );
212 wxIconArray
& iconArray
= M_ICONBUNDLEDATA
->m_icons
;
214 // replace existing icon with the same size if we already have it
215 const size_t count
= iconArray
.size();
216 for ( size_t i
= 0; i
< count
; ++i
)
218 wxIcon
& tmp
= iconArray
[i
];
220 tmp
.GetWidth() == icon
.GetWidth() &&
221 tmp
.GetHeight() == icon
.GetHeight() )
228 // if we don't, add an icon with new size
232 size_t wxIconBundle::GetIconCount() const
234 return IsOk() ? M_ICONBUNDLEDATA
->m_icons
.size() : 0;
237 wxIcon
wxIconBundle::GetIconByIndex(size_t n
) const
239 wxCHECK_MSG( n
< GetIconCount(), wxNullIcon
, _T("invalid index") );
241 return M_ICONBUNDLEDATA
->m_icons
[n
];