Added customizable wxDocManager::OnMRUFileNotExist() virtual method.
[wxWidgets.git] / src / common / iconbndl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/iconbndl.cpp
3 // Purpose: wxIconBundle
4 // Author: Mattia Barbon, Vadim Zeitlin
5 // Created: 23.03.2002
6 // RCS-ID: $Id$
7 // Copyright: (c) Mattia barbon
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #include "wx/iconbndl.h"
19
20 #ifndef WX_PRECOMP
21 #include "wx/settings.h"
22 #include "wx/log.h"
23 #include "wx/intl.h"
24 #include "wx/bitmap.h"
25 #include "wx/image.h"
26 #include "wx/stream.h"
27 #endif
28
29 #include "wx/wfstream.h"
30
31 #include "wx/arrimpl.cpp"
32 WX_DEFINE_OBJARRAY(wxIconArray)
33
34 IMPLEMENT_DYNAMIC_CLASS(wxIconBundle, wxGDIObject)
35
36 #define M_ICONBUNDLEDATA static_cast<wxIconBundleRefData*>(m_refData)
37
38 // ----------------------------------------------------------------------------
39 // wxIconBundleRefData
40 // ----------------------------------------------------------------------------
41
42 class WXDLLEXPORT wxIconBundleRefData : public wxGDIRefData
43 {
44 public:
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
56
57 virtual bool IsOk() const { return !m_icons.empty(); }
58
59 wxIconArray m_icons;
60 };
61
62 // ============================================================================
63 // wxIconBundle implementation
64 // ============================================================================
65
66 wxIconBundle::wxIconBundle()
67 {
68 }
69
70 #if wxUSE_STREAMS && wxUSE_IMAGE
71
72 #if wxUSE_FFILE || wxUSE_FILE
73 wxIconBundle::wxIconBundle(const wxString& file, wxBitmapType type)
74 : wxGDIObject()
75 {
76 AddIcon(file, type);
77 }
78 #endif // wxUSE_FFILE || wxUSE_FILE
79
80 wxIconBundle::wxIconBundle(wxInputStream& stream, wxBitmapType type)
81 : wxGDIObject()
82 {
83 AddIcon(stream, type);
84 }
85 #endif // wxUSE_STREAMS && wxUSE_IMAGE
86
87 wxIconBundle::wxIconBundle(const wxIcon& icon)
88 : wxGDIObject()
89 {
90 AddIcon(icon);
91 }
92
93 wxGDIRefData *wxIconBundle::CreateGDIRefData() const
94 {
95 return new wxIconBundleRefData;
96 }
97
98 wxGDIRefData *wxIconBundle::CloneGDIRefData(const wxGDIRefData *data) const
99 {
100 return new wxIconBundleRefData(*static_cast<const wxIconBundleRefData *>(data));
101 }
102
103 void wxIconBundle::DeleteIcons()
104 {
105 UnRef();
106 }
107
108 #if wxUSE_STREAMS && wxUSE_IMAGE
109
110 namespace
111 {
112
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):
116 void DoAddIcon(wxIconBundle& bundle,
117 wxInputStream& input,
118 wxBitmapType type,
119 const wxString& errorMessage)
120 {
121 wxImage image;
122
123 const wxFileOffset posOrig = input.TellI();
124
125 const size_t count = wxImage::GetImageCount(input, type);
126 for ( size_t i = 0; i < count; ++i )
127 {
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
136 if ( !image.LoadFile(input, type, i) )
137 {
138 wxLogError(errorMessage, i);
139 continue;
140 }
141
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
149 wxIcon tmp;
150 tmp.CopyFromBitmap(wxBitmap(image));
151 bundle.AddIcon(tmp);
152 }
153 }
154
155 } // anonymous namespace
156
157 #if wxUSE_FFILE || wxUSE_FILE
158
159 void 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);
166 if (tmp.IsOk())
167 {
168 AddIcon(tmp);
169 return;
170 }
171 }
172 #endif // __WXMAC__
173
174 #if wxUSE_FFILE
175 wxFFileInputStream stream(file);
176 #elif wxUSE_FILE
177 wxFileInputStream stream(file);
178 #endif
179 DoAddIcon
180 (
181 *this,
182 stream, type,
183 wxString::Format(_("Failed to load image %%d from file '%s'."), file)
184 );
185 }
186
187 #endif // wxUSE_FFILE || wxUSE_FILE
188
189 void wxIconBundle::AddIcon(wxInputStream& stream, wxBitmapType type)
190 {
191 DoAddIcon(*this, stream, type, _("Failed to load image %d from stream."));
192 }
193
194 #endif // wxUSE_STREAMS && wxUSE_IMAGE
195
196 wxIcon wxIconBundle::GetIcon(const wxSize& size) const
197 {
198 const size_t count = GetIconCount();
199
200 // optimize for the common case of icon bundles containing one icon only
201 wxIcon iconBest;
202 switch ( count )
203 {
204 case 0:
205 // nothing to do, iconBest is already invalid
206 break;
207
208 case 1:
209 iconBest = M_ICONBUNDLEDATA->m_icons[0];
210 break;
211
212 default:
213 // there is more than one icon, find the best match:
214 wxCoord sysX = wxSystemSettings::GetMetric( wxSYS_ICON_X ),
215 sysY = wxSystemSettings::GetMetric( wxSYS_ICON_Y );
216
217 const wxIconArray& iconArray = M_ICONBUNDLEDATA->m_icons;
218 for ( size_t i = 0; i < count; i++ )
219 {
220 const wxIcon& icon = iconArray[i];
221 wxCoord sx = icon.GetWidth(),
222 sy = icon.GetHeight();
223
224 // if we got an icon of exactly the requested size, we're done
225 if ( sx == size.x && sy == size.y )
226 {
227 iconBest = icon;
228 break;
229 }
230
231 // the best icon is by default (arbitrarily) the first one but
232 // if we find a system-sized icon, take it instead
233 if ((sx == sysX && sy == sysY) || !iconBest.IsOk())
234 iconBest = icon;
235 }
236 }
237
238 #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
239 return wxIcon(iconBest.GetHICON(), size);
240 #else
241 return iconBest;
242 #endif
243 }
244
245 wxIcon wxIconBundle::GetIconOfExactSize(const wxSize& size) const
246 {
247 wxIcon icon = GetIcon(size);
248 if ( icon.IsOk() &&
249 (icon.GetWidth() != size.x || icon.GetHeight() != size.y) )
250 {
251 icon = wxNullIcon;
252 }
253
254 return icon;
255 }
256
257 void wxIconBundle::AddIcon(const wxIcon& icon)
258 {
259 wxCHECK_RET( icon.IsOk(), wxT("invalid icon") );
260
261 AllocExclusive();
262
263 wxIconArray& iconArray = M_ICONBUNDLEDATA->m_icons;
264
265 // replace existing icon with the same size if we already have it
266 const size_t count = iconArray.size();
267 for ( size_t i = 0; i < count; ++i )
268 {
269 wxIcon& tmp = iconArray[i];
270 if ( tmp.IsOk() &&
271 tmp.GetWidth() == icon.GetWidth() &&
272 tmp.GetHeight() == icon.GetHeight() )
273 {
274 tmp = icon;
275 return;
276 }
277 }
278
279 // if we don't, add an icon with new size
280 iconArray.Add(icon);
281 }
282
283 size_t wxIconBundle::GetIconCount() const
284 {
285 return IsOk() ? M_ICONBUNDLEDATA->m_icons.size() : 0;
286 }
287
288 wxIcon wxIconBundle::GetIconByIndex(size_t n) const
289 {
290 wxCHECK_MSG( n < GetIconCount(), wxNullIcon, wxT("invalid index") );
291
292 return M_ICONBUNDLEDATA->m_icons[n];
293 }