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()
67 m_refData
= new wxIconBundleRefData
;
70 wxIconBundle::wxIconBundle(const wxString
& file
, long type
)
73 m_refData
= new wxIconBundleRefData
;
77 wxIconBundle::wxIconBundle(const wxIconBundle
& icon
)
83 wxIconBundle::wxIconBundle(const wxIcon
& icon
)
86 m_refData
= new wxIconBundleRefData
;
90 wxObjectRefData
*wxIconBundle::CreateRefData() const
92 return new wxIconBundleRefData
;
95 wxObjectRefData
*wxIconBundle::CloneRefData(const wxObjectRefData
*data
) const
97 return new wxIconBundleRefData(*wx_static_cast(const wxIconBundleRefData
*, data
));
100 void wxIconBundle::DeleteIcons()
102 wxIconBundleRefData
* ref
= new wxIconBundleRefData();
107 bool wxIconBundle::IsOk() const
109 return M_ICONBUNDLEDATA
&& !M_ICONBUNDLEDATA
->m_icons
.IsEmpty();
112 void wxIconBundle::AddIcon(const wxString
& file
, long type
)
115 // Deal with standard icons
116 if ( type
== wxBITMAP_TYPE_ICON_RESOURCE
)
118 wxIcon
tmp(file
, type
);
127 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
130 const size_t count
= wxImage::GetImageCount( file
, type
);
131 for ( size_t i
= 0; i
< count
; ++i
)
133 if ( !image
.LoadFile( file
, type
, i
) )
135 wxLogError( _("Failed to load image %d from file '%s'."),
141 tmp
.CopyFromBitmap(wxBitmap(image
));
144 #else // !wxUSE_IMAGE
147 #endif // wxUSE_IMAGE/!wxUSE_IMAGE
150 wxIcon
wxIconBundle::GetIcon(const wxSize
& size
) const
152 const size_t count
= GetIconCount();
154 // optimize for the common case of icon bundles containing one icon only
159 // nothing to do, iconBest is already invalid
163 iconBest
= M_ICONBUNDLEDATA
->m_icons
[0];
167 // there is more than one icon, find the best match:
168 wxCoord sysX
= wxSystemSettings::GetMetric( wxSYS_ICON_X
),
169 sysY
= wxSystemSettings::GetMetric( wxSYS_ICON_Y
);
171 const wxIconArray
& iconArray
= M_ICONBUNDLEDATA
->m_icons
;
172 for ( size_t i
= 0; i
< count
; i
++ )
174 const wxIcon
& icon
= iconArray
[i
];
175 wxCoord sx
= icon
.GetWidth(),
176 sy
= icon
.GetHeight();
178 // if we got an icon of exactly the requested size, we're done
179 if ( sx
== size
.x
&& sy
== size
.y
)
185 // the best icon is by default (arbitrarily) the first one but
186 // if we find a system-sized icon, take it instead
187 if ( sx
== sysX
&& sy
== sysY
|| !iconBest
.IsOk() )
193 return wxIcon(iconBest
.GetHICON(), size
);
199 void wxIconBundle::AddIcon(const wxIcon
& icon
)
201 wxCHECK_RET( icon
.IsOk(), _T("invalid icon") );
205 wxIconArray
& iconArray
= M_ICONBUNDLEDATA
->m_icons
;
207 // replace existing icon with the same size if we already have it
208 const size_t count
= iconArray
.size();
209 for ( size_t i
= 0; i
< count
; ++i
)
211 wxIcon
& tmp
= iconArray
[i
];
213 tmp
.GetWidth() == icon
.GetWidth() &&
214 tmp
.GetHeight() == icon
.GetHeight() )
221 // if we don't, add an icon with new size
225 size_t wxIconBundle::GetIconCount() const
227 return IsOk() ? M_ICONBUNDLEDATA
->m_icons
.size() : 0;
230 wxIcon
wxIconBundle::GetIconByIndex(size_t n
) const
232 wxCHECK_MSG( n
< GetIconCount(), wxNullIcon
, _T("invalid index") );
234 return M_ICONBUNDLEDATA
->m_icons
[n
];