use the directory of the most recently opened file in wxDocManager if we have any
[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 // default and copy ctors and assignment operators are ok
46
47 virtual bool IsOk() const { return !m_icons.empty(); }
48
49 wxIconArray m_icons;
50 };
51
52 // ============================================================================
53 // wxIconBundle implementation
54 // ============================================================================
55
56 wxIconBundle::wxIconBundle()
57 {
58 }
59
60 wxIconBundle::wxIconBundle(const wxString& file, wxBitmapType type)
61 : wxGDIObject()
62 {
63 AddIcon(file, type);
64 }
65
66 #if wxUSE_STREAMS
67 wxIconBundle::wxIconBundle(wxInputStream& stream, wxBitmapType type)
68 : wxGDIObject()
69 {
70 AddIcon(stream, type);
71 }
72 #endif // wxUSE_STREAMS
73
74 wxIconBundle::wxIconBundle(const wxIcon& icon)
75 : wxGDIObject()
76 {
77 AddIcon(icon);
78 }
79
80 wxGDIRefData *wxIconBundle::CreateGDIRefData() const
81 {
82 return new wxIconBundleRefData;
83 }
84
85 wxGDIRefData *wxIconBundle::CloneGDIRefData(const wxGDIRefData *data) const
86 {
87 return new wxIconBundleRefData(*static_cast<const wxIconBundleRefData *>(data));
88 }
89
90 void wxIconBundle::DeleteIcons()
91 {
92 UnRef();
93 }
94
95 namespace
96 {
97
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):
101 void DoAddIcon(wxIconBundle& bundle,
102 wxInputStream& input,
103 wxBitmapType type,
104 const wxString& errorMessage)
105 {
106 wxImage image;
107
108 const wxFileOffset posOrig = input.TellI();
109
110 const size_t count = wxImage::GetImageCount(input, type);
111 for ( size_t i = 0; i < count; ++i )
112 {
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
121 if ( !image.LoadFile(input, type, i) )
122 {
123 wxLogError(errorMessage, i);
124 continue;
125 }
126
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
134 wxIcon tmp;
135 tmp.CopyFromBitmap(wxBitmap(image));
136 bundle.AddIcon(tmp);
137 }
138 }
139
140 } // anonymous namespace
141
142 void 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
157 wxFFileInputStream stream(file);
158 DoAddIcon
159 (
160 *this,
161 stream, type,
162 wxString::Format(_("Failed to load image %%d from file '%s'."), file)
163 );
164 }
165
166 #if wxUSE_STREAMS
167 void 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
173 wxIcon wxIconBundle::GetIcon(const wxSize& size) const
174 {
175 const size_t count = GetIconCount();
176
177 // optimize for the common case of icon bundles containing one icon only
178 wxIcon iconBest;
179 switch ( count )
180 {
181 case 0:
182 // nothing to do, iconBest is already invalid
183 break;
184
185 case 1:
186 iconBest = M_ICONBUNDLEDATA->m_icons[0];
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
194 const wxIconArray& iconArray = M_ICONBUNDLEDATA->m_icons;
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
210 if ((sx == sysX && sy == sysY) || !iconBest.IsOk())
211 iconBest = icon;
212 }
213 }
214
215 #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
216 return wxIcon(iconBest.GetHICON(), size);
217 #else
218 return iconBest;
219 #endif
220 }
221
222 wxIcon 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
234 void wxIconBundle::AddIcon(const wxIcon& icon)
235 {
236 wxCHECK_RET( icon.IsOk(), _T("invalid icon") );
237
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 )
245 {
246 wxIcon& tmp = iconArray[i];
247 if ( tmp.Ok() &&
248 tmp.GetWidth() == icon.GetWidth() &&
249 tmp.GetHeight() == icon.GetHeight() )
250 {
251 tmp = icon;
252 return;
253 }
254 }
255
256 // if we don't, add an icon with new size
257 iconArray.Add(icon);
258 }
259
260 size_t wxIconBundle::GetIconCount() const
261 {
262 return IsOk() ? M_ICONBUNDLEDATA->m_icons.size() : 0;
263 }
264
265 wxIcon wxIconBundle::GetIconByIndex(size_t n) const
266 {
267 wxCHECK_MSG( n < GetIconCount(), wxNullIcon, _T("invalid index") );
268
269 return M_ICONBUNDLEDATA->m_icons[n];
270 }