]> git.saurik.com Git - wxWidgets.git/blame - src/common/iconbndl.cpp
remove run-time check for now-required GTK 2.4
[wxWidgets.git] / src / common / iconbndl.cpp
CommitLineData
f618020a 1/////////////////////////////////////////////////////////////////////////////
923d28da 2// Name: src/common/iconbndl.cpp
f618020a 3// Purpose: wxIconBundle
52734360 4// Author: Mattia Barbon, Vadim Zeitlin
f618020a
MB
5// Created: 23.03.2002
6// RCS-ID: $Id$
7// Copyright: (c) Mattia barbon
65571936 8// Licence: wxWindows licence
f618020a
MB
9/////////////////////////////////////////////////////////////////////////////
10
f618020a
MB
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
923d28da
WS
18#include "wx/iconbndl.h"
19
f618020a
MB
20#ifndef WX_PRECOMP
21 #include "wx/settings.h"
f618020a
MB
22 #include "wx/log.h"
23 #include "wx/intl.h"
ed39ff57 24 #include "wx/bitmap.h"
d5309f58 25 #include "wx/image.h"
cee875e3 26 #include "wx/stream.h"
d5309f58
SC
27#endif
28
feaa5f91
VZ
29#include "wx/wfstream.h"
30
f618020a 31#include "wx/arrimpl.cpp"
f618020a
MB
32WX_DEFINE_OBJARRAY(wxIconArray)
33
52734360
VZ
34IMPLEMENT_DYNAMIC_CLASS(wxIconBundle, wxGDIObject)
35
24af522c 36#define M_ICONBUNDLEDATA static_cast<wxIconBundleRefData*>(m_refData)
52734360
VZ
37
38// ----------------------------------------------------------------------------
39// wxIconBundleRefData
40// ----------------------------------------------------------------------------
41
42class WXDLLEXPORT wxIconBundleRefData : public wxGDIRefData
43{
44public:
fd08ccd2
VZ
45 wxIconBundleRefData() { }
46
47 // We need the copy ctor for CloneGDIRefData() but notice that we use the
48 // base class default ctor in it and not the copy one which it doesn't have.
49 wxIconBundleRefData(const wxIconBundleRefData& other)
50 : wxGDIRefData(),
51 m_icons(other.m_icons)
52 {
53 }
54
55 // default assignment operator and dtor are ok
52734360 56
8f884a0d
VZ
57 virtual bool IsOk() const { return !m_icons.empty(); }
58
52734360 59 wxIconArray m_icons;
52734360
VZ
60};
61
62// ============================================================================
63// wxIconBundle implementation
64// ============================================================================
65
66wxIconBundle::wxIconBundle()
52734360 67{
52734360
VZ
68}
69
b5c4f0df 70#if wxUSE_STREAMS && wxUSE_IMAGE
89e1de64
VZ
71
72#if wxUSE_FFILE || wxUSE_FILE
e98e625c 73wxIconBundle::wxIconBundle(const wxString& file, wxBitmapType type)
52734360 74 : wxGDIObject()
f618020a 75{
52734360
VZ
76 AddIcon(file, type);
77}
89e1de64 78#endif // wxUSE_FFILE || wxUSE_FILE
f618020a 79
cee875e3
VS
80wxIconBundle::wxIconBundle(wxInputStream& stream, wxBitmapType type)
81 : wxGDIObject()
82{
83 AddIcon(stream, type);
84}
b5c4f0df 85#endif // wxUSE_STREAMS && wxUSE_IMAGE
cee875e3 86
52734360
VZ
87wxIconBundle::wxIconBundle(const wxIcon& icon)
88 : wxGDIObject()
89{
52734360
VZ
90 AddIcon(icon);
91}
f618020a 92
8f884a0d 93wxGDIRefData *wxIconBundle::CreateGDIRefData() const
52734360
VZ
94{
95 return new wxIconBundleRefData;
96}
97
8f884a0d 98wxGDIRefData *wxIconBundle::CloneGDIRefData(const wxGDIRefData *data) const
52734360 99{
5c33522f 100 return new wxIconBundleRefData(*static_cast<const wxIconBundleRefData *>(data));
f618020a
MB
101}
102
103void wxIconBundle::DeleteIcons()
104{
52734360 105 UnRef();
f618020a
MB
106}
107
b5c4f0df 108#if wxUSE_STREAMS && wxUSE_IMAGE
b85b06e1 109
cee875e3 110namespace
f618020a 111{
52734360 112
cee875e3
VS
113// Adds icon from 'input' to the bundle. Shows 'errorMessage' on failure
114// (it must contain "%d", because it is used to report # of image in the file
115// that failed to load):
cee875e3 116void DoAddIcon(wxIconBundle& bundle,
feaa5f91
VZ
117 wxInputStream& input,
118 wxBitmapType type,
cee875e3
VS
119 const wxString& errorMessage)
120{
f618020a 121 wxImage image;
f618020a 122
feaa5f91
VZ
123 const wxFileOffset posOrig = input.TellI();
124
cee875e3 125 const size_t count = wxImage::GetImageCount(input, type);
52734360 126 for ( size_t i = 0; i < count; ++i )
f618020a 127 {
feaa5f91
VZ
128 if ( i )
129 {
130 // the call to LoadFile() for the first sub-image updated the
131 // stream position but we need to start reading the subsequent
132 // sub-image at the image beginning too
133 input.SeekI(posOrig);
134 }
135
cee875e3 136 if ( !image.LoadFile(input, type, i) )
f618020a 137 {
cee875e3 138 wxLogError(errorMessage, i);
f618020a
MB
139 continue;
140 }
141
feaa5f91
VZ
142 if ( type == wxBITMAP_TYPE_ANY )
143 {
144 // store the type so that we don't need to try all handlers again
145 // for the subsequent images, they should all be of the same type
146 type = image.GetType();
147 }
148
52734360
VZ
149 wxIcon tmp;
150 tmp.CopyFromBitmap(wxBitmap(image));
cee875e3 151 bundle.AddIcon(tmp);
f618020a
MB
152 }
153}
154
cee875e3
VS
155} // anonymous namespace
156
89e1de64
VZ
157#if wxUSE_FFILE || wxUSE_FILE
158
cee875e3
VS
159void wxIconBundle::AddIcon(const wxString& file, wxBitmapType type)
160{
161#ifdef __WXMAC__
162 // Deal with standard icons
163 if ( type == wxBITMAP_TYPE_ICON_RESOURCE )
164 {
165 wxIcon tmp(file, type);
a1b806b9 166 if (tmp.IsOk())
cee875e3
VS
167 {
168 AddIcon(tmp);
169 return;
170 }
171 }
172#endif // __WXMAC__
173
c7f171ed 174#if wxUSE_FFILE
feaa5f91 175 wxFFileInputStream stream(file);
c7f171ed
VZ
176#elif wxUSE_FILE
177 wxFileInputStream stream(file);
178#endif
cee875e3
VS
179 DoAddIcon
180 (
181 *this,
feaa5f91 182 stream, type,
cee875e3
VS
183 wxString::Format(_("Failed to load image %%d from file '%s'."), file)
184 );
185}
186
89e1de64
VZ
187#endif // wxUSE_FFILE || wxUSE_FILE
188
cee875e3
VS
189void wxIconBundle::AddIcon(wxInputStream& stream, wxBitmapType type)
190{
191 DoAddIcon(*this, stream, type, _("Failed to load image %d from stream."));
192}
b85b06e1 193
b5c4f0df 194#endif // wxUSE_STREAMS && wxUSE_IMAGE
cee875e3 195
d928f019 196wxIcon wxIconBundle::GetIcon(const wxSize& size, int flags) const
f618020a 197{
d928f019
VZ
198 wxASSERT( size == wxDefaultSize || (size.x >= 0 && size.y > 0) );
199
200 // We need the standard system icon size when using FALLBACK_SYSTEM.
201 wxCoord sysX = 0,
202 sysY = 0;
203 if ( flags & FALLBACK_SYSTEM )
204 {
205 sysX = wxSystemSettings::GetMetric(wxSYS_ICON_X);
206 sysY = wxSystemSettings::GetMetric(wxSYS_ICON_Y);
207 }
8f696653 208
d928f019
VZ
209 // If size == wxDefaultSize, we use system default icon size by convention.
210 wxCoord sizeX = size.x;
211 wxCoord sizeY = size.y;
212 if ( size == wxDefaultSize )
213 {
214 wxASSERT_MSG( flags == FALLBACK_SYSTEM,
215 wxS("Must have valid size if not using FALLBACK_SYSTEM") );
216
217 sizeX = sysX;
218 sizeY = sysY;
219 }
220
221 // Iterate over all icons searching for the exact match or the closest icon
222 // for FALLBACK_NEAREST_LARGER.
52734360 223 wxIcon iconBest;
d928f019
VZ
224 int bestDiff = 0;
225 bool bestIsLarger = false;
226 bool bestIsSystem = false;
227
228 const size_t count = GetIconCount();
229
230 const wxIconArray& iconArray = M_ICONBUNDLEDATA->m_icons;
231 for ( size_t i = 0; i < count; i++ )
f618020a 232 {
d928f019
VZ
233 const wxIcon& icon = iconArray[i];
234 if ( !icon.IsOk() )
235 continue;
236 wxCoord sx = icon.GetWidth(),
237 sy = icon.GetHeight();
52734360 238
d928f019
VZ
239 // Exact match ends search immediately in any case.
240 if ( sx == sizeX && sy == sizeY )
241 {
242 iconBest = icon;
52734360 243 break;
d928f019 244 }
52734360 245
d928f019
VZ
246 if ( flags & FALLBACK_SYSTEM )
247 {
248 if ( sx == sysX && sy == sysY )
249 {
250 iconBest = icon;
251 bestIsSystem = true;
252 continue;
253 }
254 }
52734360 255
d928f019
VZ
256 if ( !bestIsSystem && (flags & FALLBACK_NEAREST_LARGER) )
257 {
258 bool iconLarger = (sx >= sizeX) && (sy >= sizeY);
259 int iconDiff = abs(sx - sizeX) + abs(sy - sizeY);
260
261 // Use current icon as candidate for the best icon, if either:
262 // - we have no candidate yet
263 // - we have no candidate larger than desired size and current icon is
264 // - current icon is closer to desired size than candidate
265 if ( !iconBest.IsOk() ||
266 (!bestIsLarger && iconLarger) ||
267 (iconLarger && (iconDiff < bestDiff)) )
52734360 268 {
d928f019
VZ
269 iconBest = icon;
270 bestIsLarger = iconLarger;
271 bestDiff = iconDiff;
272 continue;
52734360 273 }
d928f019 274 }
f618020a
MB
275 }
276
451a00f7 277#if defined( __WXMAC__ ) && wxOSX_USE_CARBON
d928f019
VZ
278 if (!iconBest.IsOk())
279 return wxNullIcon;
280
52734360
VZ
281 return wxIcon(iconBest.GetHICON(), size);
282#else
283 return iconBest;
284#endif
f618020a
MB
285}
286
9b5933bc
VZ
287wxIcon wxIconBundle::GetIconOfExactSize(const wxSize& size) const
288{
d928f019 289 return GetIcon(size, FALLBACK_NONE);
9b5933bc
VZ
290}
291
52734360 292void wxIconBundle::AddIcon(const wxIcon& icon)
f618020a 293{
9a83f860 294 wxCHECK_RET( icon.IsOk(), wxT("invalid icon") );
f618020a 295
52734360
VZ
296 AllocExclusive();
297
298 wxIconArray& iconArray = M_ICONBUNDLEDATA->m_icons;
299
300 // replace existing icon with the same size if we already have it
301 const size_t count = iconArray.size();
302 for ( size_t i = 0; i < count; ++i )
f618020a 303 {
52734360 304 wxIcon& tmp = iconArray[i];
a1b806b9 305 if ( tmp.IsOk() &&
52734360
VZ
306 tmp.GetWidth() == icon.GetWidth() &&
307 tmp.GetHeight() == icon.GetHeight() )
f618020a
MB
308 {
309 tmp = icon;
310 return;
311 }
312 }
313
52734360
VZ
314 // if we don't, add an icon with new size
315 iconArray.Add(icon);
f618020a 316}
52734360
VZ
317
318size_t wxIconBundle::GetIconCount() const
319{
9fc3681a 320 return IsOk() ? M_ICONBUNDLEDATA->m_icons.size() : 0;
52734360
VZ
321}
322
323wxIcon wxIconBundle::GetIconByIndex(size_t n) const
324{
9a83f860 325 wxCHECK_MSG( n < GetIconCount(), wxNullIcon, wxT("invalid index") );
9fc3681a 326
52734360
VZ
327 return M_ICONBUNDLEDATA->m_icons[n];
328}