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
);
160 wxFFileInputStream
stream(file
);
162 wxFileInputStream
stream(file
);
168 wxString::Format(_("Failed to load image %%d from file '%s'."), file
)
172 void wxIconBundle::AddIcon(wxInputStream
& stream
, wxBitmapType type
)
174 DoAddIcon(*this, stream
, type
, _("Failed to load image %d from stream."));
177 #endif // wxUSE_STREAMS
179 wxIcon
wxIconBundle::GetIcon(const wxSize
& size
) const
181 const size_t count
= GetIconCount();
183 // optimize for the common case of icon bundles containing one icon only
188 // nothing to do, iconBest is already invalid
192 iconBest
= M_ICONBUNDLEDATA
->m_icons
[0];
196 // there is more than one icon, find the best match:
197 wxCoord sysX
= wxSystemSettings::GetMetric( wxSYS_ICON_X
),
198 sysY
= wxSystemSettings::GetMetric( wxSYS_ICON_Y
);
200 const wxIconArray
& iconArray
= M_ICONBUNDLEDATA
->m_icons
;
201 for ( size_t i
= 0; i
< count
; i
++ )
203 const wxIcon
& icon
= iconArray
[i
];
204 wxCoord sx
= icon
.GetWidth(),
205 sy
= icon
.GetHeight();
207 // if we got an icon of exactly the requested size, we're done
208 if ( sx
== size
.x
&& sy
== size
.y
)
214 // the best icon is by default (arbitrarily) the first one but
215 // if we find a system-sized icon, take it instead
216 if ((sx
== sysX
&& sy
== sysY
) || !iconBest
.IsOk())
221 #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
222 return wxIcon(iconBest
.GetHICON(), size
);
228 wxIcon
wxIconBundle::GetIconOfExactSize(const wxSize
& size
) const
230 wxIcon icon
= GetIcon(size
);
232 (icon
.GetWidth() != size
.x
|| icon
.GetHeight() != size
.y
) )
240 void wxIconBundle::AddIcon(const wxIcon
& icon
)
242 wxCHECK_RET( icon
.IsOk(), wxT("invalid icon") );
246 wxIconArray
& iconArray
= M_ICONBUNDLEDATA
->m_icons
;
248 // replace existing icon with the same size if we already have it
249 const size_t count
= iconArray
.size();
250 for ( size_t i
= 0; i
< count
; ++i
)
252 wxIcon
& tmp
= iconArray
[i
];
254 tmp
.GetWidth() == icon
.GetWidth() &&
255 tmp
.GetHeight() == icon
.GetHeight() )
262 // if we don't, add an icon with new size
266 size_t wxIconBundle::GetIconCount() const
268 return IsOk() ? M_ICONBUNDLEDATA
->m_icons
.size() : 0;
271 wxIcon
wxIconBundle::GetIconByIndex(size_t n
) const
273 wxCHECK_MSG( n
< GetIconCount(), wxNullIcon
, wxT("invalid index") );
275 return M_ICONBUNDLEDATA
->m_icons
[n
];