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