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"
26 #include "wx/stream.h"
29 #include "wx/wfstream.h"
31 #include "wx/arrimpl.cpp"
32 WX_DEFINE_OBJARRAY(wxIconArray
)
34 IMPLEMENT_DYNAMIC_CLASS(wxIconBundle
, wxGDIObject
)
36 #define M_ICONBUNDLEDATA static_cast<wxIconBundleRefData*>(m_refData)
38 // ----------------------------------------------------------------------------
39 // wxIconBundleRefData
40 // ----------------------------------------------------------------------------
42 class WXDLLEXPORT wxIconBundleRefData
: public wxGDIRefData
45 // default and copy ctors and assignment operators are ok
47 virtual bool IsOk() const { return !m_icons
.empty(); }
52 // ============================================================================
53 // wxIconBundle implementation
54 // ============================================================================
56 wxIconBundle::wxIconBundle()
61 wxIconBundle::wxIconBundle(const wxString
& file
, wxBitmapType type
)
67 wxIconBundle::wxIconBundle(wxInputStream
& stream
, wxBitmapType type
)
70 AddIcon(stream
, type
);
72 #endif // wxUSE_STREAMS
74 wxIconBundle::wxIconBundle(const wxIcon
& icon
)
80 wxGDIRefData
*wxIconBundle::CreateGDIRefData() const
82 return new wxIconBundleRefData
;
85 wxGDIRefData
*wxIconBundle::CloneGDIRefData(const wxGDIRefData
*data
) const
87 return new wxIconBundleRefData(*static_cast<const wxIconBundleRefData
*>(data
));
90 void wxIconBundle::DeleteIcons()
100 // Adds icon from 'input' to the bundle. Shows 'errorMessage' on failure
101 // (it must contain "%d", because it is used to report # of image in the file
102 // that failed to load):
103 void DoAddIcon(wxIconBundle
& bundle
,
104 wxInputStream
& input
,
106 const wxString
& errorMessage
)
110 const wxFileOffset posOrig
= input
.TellI();
112 const size_t count
= wxImage::GetImageCount(input
, type
);
113 for ( size_t i
= 0; i
< count
; ++i
)
117 // the call to LoadFile() for the first sub-image updated the
118 // stream position but we need to start reading the subsequent
119 // sub-image at the image beginning too
120 input
.SeekI(posOrig
);
123 if ( !image
.LoadFile(input
, type
, i
) )
125 wxLogError(errorMessage
, i
);
129 if ( type
== wxBITMAP_TYPE_ANY
)
131 // store the type so that we don't need to try all handlers again
132 // for the subsequent images, they should all be of the same type
133 type
= image
.GetType();
137 tmp
.CopyFromBitmap(wxBitmap(image
));
142 } // anonymous namespace
144 void wxIconBundle::AddIcon(const wxString
& file
, wxBitmapType type
)
147 // Deal with standard icons
148 if ( type
== wxBITMAP_TYPE_ICON_RESOURCE
)
150 wxIcon
tmp(file
, type
);
159 wxFFileInputStream
stream(file
);
164 wxString::Format(_("Failed to load image %%d from file '%s'."), file
)
168 void wxIconBundle::AddIcon(wxInputStream
& stream
, wxBitmapType type
)
170 DoAddIcon(*this, stream
, type
, _("Failed to load image %d from stream."));
173 #endif // wxUSE_STREAMS
175 wxIcon
wxIconBundle::GetIcon(const wxSize
& size
) const
177 const size_t count
= GetIconCount();
179 // optimize for the common case of icon bundles containing one icon only
184 // nothing to do, iconBest is already invalid
188 iconBest
= M_ICONBUNDLEDATA
->m_icons
[0];
192 // there is more than one icon, find the best match:
193 wxCoord sysX
= wxSystemSettings::GetMetric( wxSYS_ICON_X
),
194 sysY
= wxSystemSettings::GetMetric( wxSYS_ICON_Y
);
196 const wxIconArray
& iconArray
= M_ICONBUNDLEDATA
->m_icons
;
197 for ( size_t i
= 0; i
< count
; i
++ )
199 const wxIcon
& icon
= iconArray
[i
];
200 wxCoord sx
= icon
.GetWidth(),
201 sy
= icon
.GetHeight();
203 // if we got an icon of exactly the requested size, we're done
204 if ( sx
== size
.x
&& sy
== size
.y
)
210 // the best icon is by default (arbitrarily) the first one but
211 // if we find a system-sized icon, take it instead
212 if ((sx
== sysX
&& sy
== sysY
) || !iconBest
.IsOk())
217 #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
218 return wxIcon(iconBest
.GetHICON(), size
);
224 wxIcon
wxIconBundle::GetIconOfExactSize(const wxSize
& size
) const
226 wxIcon icon
= GetIcon(size
);
228 (icon
.GetWidth() != size
.x
|| icon
.GetHeight() != size
.y
) )
236 void wxIconBundle::AddIcon(const wxIcon
& icon
)
238 wxCHECK_RET( icon
.IsOk(), _T("invalid icon") );
242 wxIconArray
& iconArray
= M_ICONBUNDLEDATA
->m_icons
;
244 // replace existing icon with the same size if we already have it
245 const size_t count
= iconArray
.size();
246 for ( size_t i
= 0; i
< count
; ++i
)
248 wxIcon
& tmp
= iconArray
[i
];
250 tmp
.GetWidth() == icon
.GetWidth() &&
251 tmp
.GetHeight() == icon
.GetHeight() )
258 // if we don't, add an icon with new size
262 size_t wxIconBundle::GetIconCount() const
264 return IsOk() ? M_ICONBUNDLEDATA
->m_icons
.size() : 0;
267 wxIcon
wxIconBundle::GetIconByIndex(size_t n
) const
269 wxCHECK_MSG( n
< GetIconCount(), wxNullIcon
, _T("invalid index") );
271 return M_ICONBUNDLEDATA
->m_icons
[n
];