]> git.saurik.com Git - wxWidgets.git/blame - src/common/iconbndl.cpp
Remove references to joytest.rc that was recently removed.
[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:
45 // default and copy ctors and assignment operators are ok
46
8f884a0d
VZ
47 virtual bool IsOk() const { return !m_icons.empty(); }
48
52734360 49 wxIconArray m_icons;
52734360
VZ
50};
51
52// ============================================================================
53// wxIconBundle implementation
54// ============================================================================
55
56wxIconBundle::wxIconBundle()
52734360 57{
52734360
VZ
58}
59
b5c4f0df 60#if wxUSE_STREAMS && wxUSE_IMAGE
89e1de64
VZ
61
62#if wxUSE_FFILE || wxUSE_FILE
e98e625c 63wxIconBundle::wxIconBundle(const wxString& file, wxBitmapType type)
52734360 64 : wxGDIObject()
f618020a 65{
52734360
VZ
66 AddIcon(file, type);
67}
89e1de64 68#endif // wxUSE_FFILE || wxUSE_FILE
f618020a 69
cee875e3
VS
70wxIconBundle::wxIconBundle(wxInputStream& stream, wxBitmapType type)
71 : wxGDIObject()
72{
73 AddIcon(stream, type);
74}
b5c4f0df 75#endif // wxUSE_STREAMS && wxUSE_IMAGE
cee875e3 76
52734360
VZ
77wxIconBundle::wxIconBundle(const wxIcon& icon)
78 : wxGDIObject()
79{
52734360
VZ
80 AddIcon(icon);
81}
f618020a 82
8f884a0d 83wxGDIRefData *wxIconBundle::CreateGDIRefData() const
52734360
VZ
84{
85 return new wxIconBundleRefData;
86}
87
8f884a0d 88wxGDIRefData *wxIconBundle::CloneGDIRefData(const wxGDIRefData *data) const
52734360 89{
5c33522f 90 return new wxIconBundleRefData(*static_cast<const wxIconBundleRefData *>(data));
f618020a
MB
91}
92
93void wxIconBundle::DeleteIcons()
94{
52734360 95 UnRef();
f618020a
MB
96}
97
b5c4f0df 98#if wxUSE_STREAMS && wxUSE_IMAGE
b85b06e1 99
cee875e3 100namespace
f618020a 101{
52734360 102
cee875e3
VS
103// Adds icon from 'input' to the bundle. Shows 'errorMessage' on failure
104// (it must contain "%d", because it is used to report # of image in the file
105// that failed to load):
cee875e3 106void DoAddIcon(wxIconBundle& bundle,
feaa5f91
VZ
107 wxInputStream& input,
108 wxBitmapType type,
cee875e3
VS
109 const wxString& errorMessage)
110{
f618020a 111 wxImage image;
f618020a 112
feaa5f91
VZ
113 const wxFileOffset posOrig = input.TellI();
114
cee875e3 115 const size_t count = wxImage::GetImageCount(input, type);
52734360 116 for ( size_t i = 0; i < count; ++i )
f618020a 117 {
feaa5f91
VZ
118 if ( i )
119 {
120 // the call to LoadFile() for the first sub-image updated the
121 // stream position but we need to start reading the subsequent
122 // sub-image at the image beginning too
123 input.SeekI(posOrig);
124 }
125
cee875e3 126 if ( !image.LoadFile(input, type, i) )
f618020a 127 {
cee875e3 128 wxLogError(errorMessage, i);
f618020a
MB
129 continue;
130 }
131
feaa5f91
VZ
132 if ( type == wxBITMAP_TYPE_ANY )
133 {
134 // store the type so that we don't need to try all handlers again
135 // for the subsequent images, they should all be of the same type
136 type = image.GetType();
137 }
138
52734360
VZ
139 wxIcon tmp;
140 tmp.CopyFromBitmap(wxBitmap(image));
cee875e3 141 bundle.AddIcon(tmp);
f618020a
MB
142 }
143}
144
cee875e3
VS
145} // anonymous namespace
146
89e1de64
VZ
147#if wxUSE_FFILE || wxUSE_FILE
148
cee875e3
VS
149void wxIconBundle::AddIcon(const wxString& file, wxBitmapType type)
150{
151#ifdef __WXMAC__
152 // Deal with standard icons
153 if ( type == wxBITMAP_TYPE_ICON_RESOURCE )
154 {
155 wxIcon tmp(file, type);
156 if (tmp.Ok())
157 {
158 AddIcon(tmp);
159 return;
160 }
161 }
162#endif // __WXMAC__
163
c7f171ed 164#if wxUSE_FFILE
feaa5f91 165 wxFFileInputStream stream(file);
c7f171ed
VZ
166#elif wxUSE_FILE
167 wxFileInputStream stream(file);
168#endif
cee875e3
VS
169 DoAddIcon
170 (
171 *this,
feaa5f91 172 stream, type,
cee875e3
VS
173 wxString::Format(_("Failed to load image %%d from file '%s'."), file)
174 );
175}
176
89e1de64
VZ
177#endif // wxUSE_FFILE || wxUSE_FILE
178
cee875e3
VS
179void wxIconBundle::AddIcon(wxInputStream& stream, wxBitmapType type)
180{
181 DoAddIcon(*this, stream, type, _("Failed to load image %d from stream."));
182}
b85b06e1 183
b5c4f0df 184#endif // wxUSE_STREAMS && wxUSE_IMAGE
cee875e3 185
52734360 186wxIcon wxIconBundle::GetIcon(const wxSize& size) const
f618020a 187{
9fc3681a 188 const size_t count = GetIconCount();
8f696653 189
52734360
VZ
190 // optimize for the common case of icon bundles containing one icon only
191 wxIcon iconBest;
192 switch ( count )
f618020a 193 {
52734360
VZ
194 case 0:
195 // nothing to do, iconBest is already invalid
196 break;
197
198 case 1:
9fc3681a 199 iconBest = M_ICONBUNDLEDATA->m_icons[0];
52734360
VZ
200 break;
201
202 default:
203 // there is more than one icon, find the best match:
204 wxCoord sysX = wxSystemSettings::GetMetric( wxSYS_ICON_X ),
205 sysY = wxSystemSettings::GetMetric( wxSYS_ICON_Y );
206
9fc3681a 207 const wxIconArray& iconArray = M_ICONBUNDLEDATA->m_icons;
52734360
VZ
208 for ( size_t i = 0; i < count; i++ )
209 {
210 const wxIcon& icon = iconArray[i];
211 wxCoord sx = icon.GetWidth(),
212 sy = icon.GetHeight();
213
214 // if we got an icon of exactly the requested size, we're done
215 if ( sx == size.x && sy == size.y )
216 {
217 iconBest = icon;
218 break;
219 }
220
221 // the best icon is by default (arbitrarily) the first one but
222 // if we find a system-sized icon, take it instead
2a230426 223 if ((sx == sysX && sy == sysY) || !iconBest.IsOk())
52734360
VZ
224 iconBest = icon;
225 }
f618020a
MB
226 }
227
451a00f7 228#if defined( __WXMAC__ ) && wxOSX_USE_CARBON
52734360
VZ
229 return wxIcon(iconBest.GetHICON(), size);
230#else
231 return iconBest;
232#endif
f618020a
MB
233}
234
9b5933bc
VZ
235wxIcon wxIconBundle::GetIconOfExactSize(const wxSize& size) const
236{
237 wxIcon icon = GetIcon(size);
238 if ( icon.Ok() &&
239 (icon.GetWidth() != size.x || icon.GetHeight() != size.y) )
240 {
241 icon = wxNullIcon;
242 }
243
244 return icon;
245}
246
52734360 247void wxIconBundle::AddIcon(const wxIcon& icon)
f618020a 248{
9a83f860 249 wxCHECK_RET( icon.IsOk(), wxT("invalid icon") );
f618020a 250
52734360
VZ
251 AllocExclusive();
252
253 wxIconArray& iconArray = M_ICONBUNDLEDATA->m_icons;
254
255 // replace existing icon with the same size if we already have it
256 const size_t count = iconArray.size();
257 for ( size_t i = 0; i < count; ++i )
f618020a 258 {
52734360
VZ
259 wxIcon& tmp = iconArray[i];
260 if ( tmp.Ok() &&
261 tmp.GetWidth() == icon.GetWidth() &&
262 tmp.GetHeight() == icon.GetHeight() )
f618020a
MB
263 {
264 tmp = icon;
265 return;
266 }
267 }
268
52734360
VZ
269 // if we don't, add an icon with new size
270 iconArray.Add(icon);
f618020a 271}
52734360
VZ
272
273size_t wxIconBundle::GetIconCount() const
274{
9fc3681a 275 return IsOk() ? M_ICONBUNDLEDATA->m_icons.size() : 0;
52734360
VZ
276}
277
278wxIcon wxIconBundle::GetIconByIndex(size_t n) const
279{
9a83f860 280 wxCHECK_MSG( n < GetIconCount(), wxNullIcon, wxT("invalid index") );
9fc3681a 281
52734360
VZ
282 return M_ICONBUNDLEDATA->m_icons[n];
283}