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