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
54 virtual bool IsOk() const { return !m_icons
.empty(); }
59 friend class wxIconBundle
;
62 // ============================================================================
63 // wxIconBundle implementation
64 // ============================================================================
66 wxIconBundle::wxIconBundle()
71 wxIconBundle::wxIconBundle(const wxString
& file
, long type
)
77 wxIconBundle::wxIconBundle(const wxIconBundle
& icon
)
83 wxIconBundle::wxIconBundle(const wxIcon
& icon
)
89 wxGDIRefData
*wxIconBundle::CreateGDIRefData() const
91 return new wxIconBundleRefData
;
94 wxGDIRefData
*wxIconBundle::CloneGDIRefData(const wxGDIRefData
*data
) const
96 return new wxIconBundleRefData(*wx_static_cast(const wxIconBundleRefData
*, data
));
99 void wxIconBundle::DeleteIcons()
104 void wxIconBundle::AddIcon(const wxString
& file
, long type
)
107 // Deal with standard icons
108 if ( type
== wxBITMAP_TYPE_ICON_RESOURCE
)
110 wxIcon
tmp(file
, type
);
119 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
122 const size_t count
= wxImage::GetImageCount( file
, type
);
123 for ( size_t i
= 0; i
< count
; ++i
)
125 if ( !image
.LoadFile( file
, type
, i
) )
127 wxLogError( _("Failed to load image %d from file '%s'."),
133 tmp
.CopyFromBitmap(wxBitmap(image
));
136 #else // !wxUSE_IMAGE
139 #endif // wxUSE_IMAGE/!wxUSE_IMAGE
142 wxIcon
wxIconBundle::GetIcon(const wxSize
& size
) const
144 const size_t count
= GetIconCount();
146 // optimize for the common case of icon bundles containing one icon only
151 // nothing to do, iconBest is already invalid
155 iconBest
= M_ICONBUNDLEDATA
->m_icons
[0];
159 // there is more than one icon, find the best match:
160 wxCoord sysX
= wxSystemSettings::GetMetric( wxSYS_ICON_X
),
161 sysY
= wxSystemSettings::GetMetric( wxSYS_ICON_Y
);
163 const wxIconArray
& iconArray
= M_ICONBUNDLEDATA
->m_icons
;
164 for ( size_t i
= 0; i
< count
; i
++ )
166 const wxIcon
& icon
= iconArray
[i
];
167 wxCoord sx
= icon
.GetWidth(),
168 sy
= icon
.GetHeight();
170 // if we got an icon of exactly the requested size, we're done
171 if ( sx
== size
.x
&& sy
== size
.y
)
177 // the best icon is by default (arbitrarily) the first one but
178 // if we find a system-sized icon, take it instead
179 if ((sx
== sysX
&& sy
== sysY
) || !iconBest
.IsOk())
185 return wxIcon(iconBest
.GetHICON(), size
);
191 wxIcon
wxIconBundle::GetIconOfExactSize(const wxSize
& size
) const
193 wxIcon icon
= GetIcon(size
);
195 (icon
.GetWidth() != size
.x
|| icon
.GetHeight() != size
.y
) )
203 void wxIconBundle::AddIcon(const wxIcon
& icon
)
205 wxCHECK_RET( icon
.IsOk(), _T("invalid icon") );
209 wxIconArray
& iconArray
= M_ICONBUNDLEDATA
->m_icons
;
211 // replace existing icon with the same size if we already have it
212 const size_t count
= iconArray
.size();
213 for ( size_t i
= 0; i
< count
; ++i
)
215 wxIcon
& tmp
= iconArray
[i
];
217 tmp
.GetWidth() == icon
.GetWidth() &&
218 tmp
.GetHeight() == icon
.GetHeight() )
225 // if we don't, add an icon with new size
229 size_t wxIconBundle::GetIconCount() const
231 return IsOk() ? M_ICONBUNDLEDATA
->m_icons
.size() : 0;
234 wxIcon
wxIconBundle::GetIconByIndex(size_t n
) const
236 wxCHECK_MSG( n
< GetIconCount(), wxNullIcon
, _T("invalid index") );
238 return M_ICONBUNDLEDATA
->m_icons
[n
];