]> git.saurik.com Git - wxWidgets.git/blame - src/common/iconbndl.cpp
Make this header SWIG-safe
[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
f618020a 29#include "wx/arrimpl.cpp"
f618020a
MB
30WX_DEFINE_OBJARRAY(wxIconArray)
31
52734360
VZ
32IMPLEMENT_DYNAMIC_CLASS(wxIconBundle, wxGDIObject)
33
24af522c 34#define M_ICONBUNDLEDATA static_cast<wxIconBundleRefData*>(m_refData)
52734360
VZ
35
36// ----------------------------------------------------------------------------
37// wxIconBundleRefData
38// ----------------------------------------------------------------------------
39
40class WXDLLEXPORT wxIconBundleRefData : public wxGDIRefData
41{
42public:
43 // default and copy ctors and assignment operators are ok
44
8f884a0d
VZ
45 virtual bool IsOk() const { return !m_icons.empty(); }
46
52734360 47 wxIconArray m_icons;
52734360
VZ
48};
49
50// ============================================================================
51// wxIconBundle implementation
52// ============================================================================
53
54wxIconBundle::wxIconBundle()
52734360 55{
52734360
VZ
56}
57
e98e625c 58wxIconBundle::wxIconBundle(const wxString& file, wxBitmapType type)
52734360 59 : wxGDIObject()
f618020a 60{
52734360
VZ
61 AddIcon(file, type);
62}
f618020a 63
cee875e3
VS
64#if wxUSE_STREAMS
65wxIconBundle::wxIconBundle(wxInputStream& stream, wxBitmapType type)
66 : wxGDIObject()
67{
68 AddIcon(stream, type);
69}
70#endif // wxUSE_STREAMS
71
52734360
VZ
72wxIconBundle::wxIconBundle(const wxIcon& icon)
73 : wxGDIObject()
74{
52734360
VZ
75 AddIcon(icon);
76}
f618020a 77
8f884a0d 78wxGDIRefData *wxIconBundle::CreateGDIRefData() const
52734360
VZ
79{
80 return new wxIconBundleRefData;
81}
82
8f884a0d 83wxGDIRefData *wxIconBundle::CloneGDIRefData(const wxGDIRefData *data) const
52734360 84{
5c33522f 85 return new wxIconBundleRefData(*static_cast<const wxIconBundleRefData *>(data));
f618020a
MB
86}
87
88void wxIconBundle::DeleteIcons()
89{
52734360 90 UnRef();
f618020a
MB
91}
92
cee875e3 93namespace
f618020a 94{
52734360 95
cee875e3
VS
96// Adds icon from 'input' to the bundle. Shows 'errorMessage' on failure
97// (it must contain "%d", because it is used to report # of image in the file
98// that failed to load):
99template<typename T>
100void DoAddIcon(wxIconBundle& bundle,
101 T& input, wxBitmapType type,
102 const wxString& errorMessage)
103{
64c288fa 104#if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
f618020a 105 wxImage image;
f618020a 106
cee875e3 107 const size_t count = wxImage::GetImageCount(input, type);
52734360 108 for ( size_t i = 0; i < count; ++i )
f618020a 109 {
cee875e3 110 if ( !image.LoadFile(input, type, i) )
f618020a 111 {
cee875e3 112 wxLogError(errorMessage, i);
f618020a
MB
113 continue;
114 }
115
52734360
VZ
116 wxIcon tmp;
117 tmp.CopyFromBitmap(wxBitmap(image));
cee875e3 118 bundle.AddIcon(tmp);
f618020a 119 }
52734360 120#else // !wxUSE_IMAGE
cee875e3 121 wxUnusedVar(input);
52734360
VZ
122 wxUnusedVar(type);
123#endif // wxUSE_IMAGE/!wxUSE_IMAGE
f618020a
MB
124}
125
cee875e3
VS
126} // anonymous namespace
127
128void wxIconBundle::AddIcon(const wxString& file, wxBitmapType type)
129{
130#ifdef __WXMAC__
131 // Deal with standard icons
132 if ( type == wxBITMAP_TYPE_ICON_RESOURCE )
133 {
134 wxIcon tmp(file, type);
135 if (tmp.Ok())
136 {
137 AddIcon(tmp);
138 return;
139 }
140 }
141#endif // __WXMAC__
142
143 DoAddIcon
144 (
145 *this,
146 file, type,
147 wxString::Format(_("Failed to load image %%d from file '%s'."), file)
148 );
149}
150
151#if wxUSE_STREAMS
152void wxIconBundle::AddIcon(wxInputStream& stream, wxBitmapType type)
153{
154 DoAddIcon(*this, stream, type, _("Failed to load image %d from stream."));
155}
156#endif // wxUSE_STREAMS
157
52734360 158wxIcon wxIconBundle::GetIcon(const wxSize& size) const
f618020a 159{
9fc3681a 160 const size_t count = GetIconCount();
8f696653 161
52734360
VZ
162 // optimize for the common case of icon bundles containing one icon only
163 wxIcon iconBest;
164 switch ( count )
f618020a 165 {
52734360
VZ
166 case 0:
167 // nothing to do, iconBest is already invalid
168 break;
169
170 case 1:
9fc3681a 171 iconBest = M_ICONBUNDLEDATA->m_icons[0];
52734360
VZ
172 break;
173
174 default:
175 // there is more than one icon, find the best match:
176 wxCoord sysX = wxSystemSettings::GetMetric( wxSYS_ICON_X ),
177 sysY = wxSystemSettings::GetMetric( wxSYS_ICON_Y );
178
9fc3681a 179 const wxIconArray& iconArray = M_ICONBUNDLEDATA->m_icons;
52734360
VZ
180 for ( size_t i = 0; i < count; i++ )
181 {
182 const wxIcon& icon = iconArray[i];
183 wxCoord sx = icon.GetWidth(),
184 sy = icon.GetHeight();
185
186 // if we got an icon of exactly the requested size, we're done
187 if ( sx == size.x && sy == size.y )
188 {
189 iconBest = icon;
190 break;
191 }
192
193 // the best icon is by default (arbitrarily) the first one but
194 // if we find a system-sized icon, take it instead
2a230426 195 if ((sx == sysX && sy == sysY) || !iconBest.IsOk())
52734360
VZ
196 iconBest = icon;
197 }
f618020a
MB
198 }
199
451a00f7 200#if defined( __WXMAC__ ) && wxOSX_USE_CARBON
52734360
VZ
201 return wxIcon(iconBest.GetHICON(), size);
202#else
203 return iconBest;
204#endif
f618020a
MB
205}
206
9b5933bc
VZ
207wxIcon wxIconBundle::GetIconOfExactSize(const wxSize& size) const
208{
209 wxIcon icon = GetIcon(size);
210 if ( icon.Ok() &&
211 (icon.GetWidth() != size.x || icon.GetHeight() != size.y) )
212 {
213 icon = wxNullIcon;
214 }
215
216 return icon;
217}
218
52734360 219void wxIconBundle::AddIcon(const wxIcon& icon)
f618020a 220{
52734360 221 wxCHECK_RET( icon.IsOk(), _T("invalid icon") );
f618020a 222
52734360
VZ
223 AllocExclusive();
224
225 wxIconArray& iconArray = M_ICONBUNDLEDATA->m_icons;
226
227 // replace existing icon with the same size if we already have it
228 const size_t count = iconArray.size();
229 for ( size_t i = 0; i < count; ++i )
f618020a 230 {
52734360
VZ
231 wxIcon& tmp = iconArray[i];
232 if ( tmp.Ok() &&
233 tmp.GetWidth() == icon.GetWidth() &&
234 tmp.GetHeight() == icon.GetHeight() )
f618020a
MB
235 {
236 tmp = icon;
237 return;
238 }
239 }
240
52734360
VZ
241 // if we don't, add an icon with new size
242 iconArray.Add(icon);
f618020a 243}
52734360
VZ
244
245size_t wxIconBundle::GetIconCount() const
246{
9fc3681a 247 return IsOk() ? M_ICONBUNDLEDATA->m_icons.size() : 0;
52734360
VZ
248}
249
250wxIcon wxIconBundle::GetIconByIndex(size_t n) const
251{
9fc3681a
VZ
252 wxCHECK_MSG( n < GetIconCount(), wxNullIcon, _T("invalid index") );
253
52734360
VZ
254 return M_ICONBUNDLEDATA->m_icons[n];
255}