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