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 WX_DECLARE_EXPORTED_OBJARRAY(wxIcon
, wxIconArray
);
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
)
64 wxIconBundle::wxIconBundle(const wxIcon
& icon
)
70 wxGDIRefData
*wxIconBundle::CreateGDIRefData() const
72 return new wxIconBundleRefData
;
75 wxGDIRefData
*wxIconBundle::CloneGDIRefData(const wxGDIRefData
*data
) const
77 return new wxIconBundleRefData(*wx_static_cast(const wxIconBundleRefData
*, data
));
80 void wxIconBundle::DeleteIcons()
85 void wxIconBundle::AddIcon(const wxString
& file
, wxBitmapType type
)
88 // Deal with standard icons
89 if ( type
== wxBITMAP_TYPE_ICON_RESOURCE
)
91 wxIcon
tmp(file
, type
);
100 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
103 const size_t count
= wxImage::GetImageCount( file
, type
);
104 for ( size_t i
= 0; i
< count
; ++i
)
106 if ( !image
.LoadFile( file
, type
, i
) )
108 wxLogError( _("Failed to load image %d from file '%s'."),
114 tmp
.CopyFromBitmap(wxBitmap(image
));
117 #else // !wxUSE_IMAGE
120 #endif // wxUSE_IMAGE/!wxUSE_IMAGE
123 wxIcon
wxIconBundle::GetIcon(const wxSize
& size
) const
125 const size_t count
= GetIconCount();
127 // optimize for the common case of icon bundles containing one icon only
132 // nothing to do, iconBest is already invalid
136 iconBest
= M_ICONBUNDLEDATA
->m_icons
[0];
140 // there is more than one icon, find the best match:
141 wxCoord sysX
= wxSystemSettings::GetMetric( wxSYS_ICON_X
),
142 sysY
= wxSystemSettings::GetMetric( wxSYS_ICON_Y
);
144 const wxIconArray
& iconArray
= M_ICONBUNDLEDATA
->m_icons
;
145 for ( size_t i
= 0; i
< count
; i
++ )
147 const wxIcon
& icon
= iconArray
[i
];
148 wxCoord sx
= icon
.GetWidth(),
149 sy
= icon
.GetHeight();
151 // if we got an icon of exactly the requested size, we're done
152 if ( sx
== size
.x
&& sy
== size
.y
)
158 // the best icon is by default (arbitrarily) the first one but
159 // if we find a system-sized icon, take it instead
160 if ((sx
== sysX
&& sy
== sysY
) || !iconBest
.IsOk())
165 #if defined( __WXMAC__ ) && wxOSX_USE_CARRBON
166 return wxIcon(iconBest
.GetHICON(), size
);
172 wxIcon
wxIconBundle::GetIconOfExactSize(const wxSize
& size
) const
174 wxIcon icon
= GetIcon(size
);
176 (icon
.GetWidth() != size
.x
|| icon
.GetHeight() != size
.y
) )
184 void wxIconBundle::AddIcon(const wxIcon
& icon
)
186 wxCHECK_RET( icon
.IsOk(), _T("invalid icon") );
190 wxIconArray
& iconArray
= M_ICONBUNDLEDATA
->m_icons
;
192 // replace existing icon with the same size if we already have it
193 const size_t count
= iconArray
.size();
194 for ( size_t i
= 0; i
< count
; ++i
)
196 wxIcon
& tmp
= iconArray
[i
];
198 tmp
.GetWidth() == icon
.GetWidth() &&
199 tmp
.GetHeight() == icon
.GetHeight() )
206 // if we don't, add an icon with new size
210 size_t wxIconBundle::GetIconCount() const
212 return IsOk() ? M_ICONBUNDLEDATA
->m_icons
.size() : 0;
215 wxIcon
wxIconBundle::GetIconByIndex(size_t n
) const
217 wxCHECK_MSG( n
< GetIconCount(), wxNullIcon
, _T("invalid index") );
219 return M_ICONBUNDLEDATA
->m_icons
[n
];