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"
28 #include "wx/arrimpl.cpp"
29 WX_DEFINE_OBJARRAY(wxIconArray
)
31 IMPLEMENT_DYNAMIC_CLASS(wxIconBundle
, wxGDIObject
)
33 #define M_ICONBUNDLEDATA static_cast<wxIconBundleRefData*>(m_refData)
35 // ----------------------------------------------------------------------------
36 // wxIconBundleRefData
37 // ----------------------------------------------------------------------------
39 class WXDLLEXPORT wxIconBundleRefData
: public wxGDIRefData
42 // default and copy ctors and assignment operators are ok
44 virtual bool IsOk() const { return !m_icons
.empty(); }
49 // ============================================================================
50 // wxIconBundle implementation
51 // ============================================================================
53 wxIconBundle::wxIconBundle()
57 wxIconBundle::wxIconBundle(const wxString
& file
, wxBitmapType type
)
63 wxIconBundle::wxIconBundle(const wxIcon
& icon
)
69 wxGDIRefData
*wxIconBundle::CreateGDIRefData() const
71 return new wxIconBundleRefData
;
74 wxGDIRefData
*wxIconBundle::CloneGDIRefData(const wxGDIRefData
*data
) const
76 return new wxIconBundleRefData(*wx_static_cast(const wxIconBundleRefData
*, data
));
79 void wxIconBundle::DeleteIcons()
84 void wxIconBundle::AddIcon(const wxString
& file
, wxBitmapType type
)
87 // Deal with standard icons
88 if ( type
== wxBITMAP_TYPE_ICON_RESOURCE
)
90 wxIcon
tmp(file
, type
);
99 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
102 const size_t count
= wxImage::GetImageCount( file
, type
);
103 for ( size_t i
= 0; i
< count
; ++i
)
105 if ( !image
.LoadFile( file
, type
, i
) )
107 wxLogError( _("Failed to load image %d from file '%s'."),
113 tmp
.CopyFromBitmap(wxBitmap(image
));
116 #else // !wxUSE_IMAGE
119 #endif // wxUSE_IMAGE/!wxUSE_IMAGE
122 wxIcon
wxIconBundle::GetIcon(const wxSize
& size
) const
124 const size_t count
= GetIconCount();
126 // optimize for the common case of icon bundles containing one icon only
131 // nothing to do, iconBest is already invalid
135 iconBest
= M_ICONBUNDLEDATA
->m_icons
[0];
139 // there is more than one icon, find the best match:
140 wxCoord sysX
= wxSystemSettings::GetMetric( wxSYS_ICON_X
),
141 sysY
= wxSystemSettings::GetMetric( wxSYS_ICON_Y
);
143 const wxIconArray
& iconArray
= M_ICONBUNDLEDATA
->m_icons
;
144 for ( size_t i
= 0; i
< count
; i
++ )
146 const wxIcon
& icon
= iconArray
[i
];
147 wxCoord sx
= icon
.GetWidth(),
148 sy
= icon
.GetHeight();
150 // if we got an icon of exactly the requested size, we're done
151 if ( sx
== size
.x
&& sy
== size
.y
)
157 // the best icon is by default (arbitrarily) the first one but
158 // if we find a system-sized icon, take it instead
159 if ((sx
== sysX
&& sy
== sysY
) || !iconBest
.IsOk())
164 #if defined( __WXMAC__ ) && wxOSX_USE_CARRBON
165 return wxIcon(iconBest
.GetHICON(), size
);
171 wxIcon
wxIconBundle::GetIconOfExactSize(const wxSize
& size
) const
173 wxIcon icon
= GetIcon(size
);
175 (icon
.GetWidth() != size
.x
|| icon
.GetHeight() != size
.y
) )
183 void wxIconBundle::AddIcon(const wxIcon
& icon
)
185 wxCHECK_RET( icon
.IsOk(), _T("invalid icon") );
189 wxIconArray
& iconArray
= M_ICONBUNDLEDATA
->m_icons
;
191 // replace existing icon with the same size if we already have it
192 const size_t count
= iconArray
.size();
193 for ( size_t i
= 0; i
< count
; ++i
)
195 wxIcon
& tmp
= iconArray
[i
];
197 tmp
.GetWidth() == icon
.GetWidth() &&
198 tmp
.GetHeight() == icon
.GetHeight() )
205 // if we don't, add an icon with new size
209 size_t wxIconBundle::GetIconCount() const
211 return IsOk() ? M_ICONBUNDLEDATA
->m_icons
.size() : 0;
214 wxIcon
wxIconBundle::GetIconByIndex(size_t n
) const
216 wxCHECK_MSG( n
< GetIconCount(), wxNullIcon
, _T("invalid index") );
218 return M_ICONBUNDLEDATA
->m_icons
[n
];