]> git.saurik.com Git - wxWidgets.git/blob - src/common/iconbndl.cpp
remove confusing wxDialog::ButtonSizerFlags and extract button styles properly in...
[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 #if wxUSE_STREAMS
61 wxIconBundle::wxIconBundle(const wxString& file, wxBitmapType type)
62 : wxGDIObject()
63 {
64 AddIcon(file, type);
65 }
66
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 #if wxUSE_STREAMS
96
97 namespace
98 {
99
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):
103 void DoAddIcon(wxIconBundle& bundle,
104 wxInputStream& input,
105 wxBitmapType type,
106 const wxString& errorMessage)
107 {
108 wxImage image;
109
110 const wxFileOffset posOrig = input.TellI();
111
112 const size_t count = wxImage::GetImageCount(input, type);
113 for ( size_t i = 0; i < count; ++i )
114 {
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
123 if ( !image.LoadFile(input, type, i) )
124 {
125 wxLogError(errorMessage, i);
126 continue;
127 }
128
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
136 wxIcon tmp;
137 tmp.CopyFromBitmap(wxBitmap(image));
138 bundle.AddIcon(tmp);
139 }
140 }
141
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
159 wxFFileInputStream stream(file);
160 DoAddIcon
161 (
162 *this,
163 stream, type,
164 wxString::Format(_("Failed to load image %%d from file '%s'."), file)
165 );
166 }
167
168 void wxIconBundle::AddIcon(wxInputStream& stream, wxBitmapType type)
169 {
170 DoAddIcon(*this, stream, type, _("Failed to load image %d from stream."));
171 }
172
173 #endif // wxUSE_STREAMS
174
175 wxIcon wxIconBundle::GetIcon(const wxSize& size) const
176 {
177 const size_t count = GetIconCount();
178
179 // optimize for the common case of icon bundles containing one icon only
180 wxIcon iconBest;
181 switch ( count )
182 {
183 case 0:
184 // nothing to do, iconBest is already invalid
185 break;
186
187 case 1:
188 iconBest = M_ICONBUNDLEDATA->m_icons[0];
189 break;
190
191 default:
192 // there is more than one icon, find the best match:
193 wxCoord sysX = wxSystemSettings::GetMetric( wxSYS_ICON_X ),
194 sysY = wxSystemSettings::GetMetric( wxSYS_ICON_Y );
195
196 const wxIconArray& iconArray = M_ICONBUNDLEDATA->m_icons;
197 for ( size_t i = 0; i < count; i++ )
198 {
199 const wxIcon& icon = iconArray[i];
200 wxCoord sx = icon.GetWidth(),
201 sy = icon.GetHeight();
202
203 // if we got an icon of exactly the requested size, we're done
204 if ( sx == size.x && sy == size.y )
205 {
206 iconBest = icon;
207 break;
208 }
209
210 // the best icon is by default (arbitrarily) the first one but
211 // if we find a system-sized icon, take it instead
212 if ((sx == sysX && sy == sysY) || !iconBest.IsOk())
213 iconBest = icon;
214 }
215 }
216
217 #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
218 return wxIcon(iconBest.GetHICON(), size);
219 #else
220 return iconBest;
221 #endif
222 }
223
224 wxIcon wxIconBundle::GetIconOfExactSize(const wxSize& size) const
225 {
226 wxIcon icon = GetIcon(size);
227 if ( icon.Ok() &&
228 (icon.GetWidth() != size.x || icon.GetHeight() != size.y) )
229 {
230 icon = wxNullIcon;
231 }
232
233 return icon;
234 }
235
236 void wxIconBundle::AddIcon(const wxIcon& icon)
237 {
238 wxCHECK_RET( icon.IsOk(), _T("invalid icon") );
239
240 AllocExclusive();
241
242 wxIconArray& iconArray = M_ICONBUNDLEDATA->m_icons;
243
244 // replace existing icon with the same size if we already have it
245 const size_t count = iconArray.size();
246 for ( size_t i = 0; i < count; ++i )
247 {
248 wxIcon& tmp = iconArray[i];
249 if ( tmp.Ok() &&
250 tmp.GetWidth() == icon.GetWidth() &&
251 tmp.GetHeight() == icon.GetHeight() )
252 {
253 tmp = icon;
254 return;
255 }
256 }
257
258 // if we don't, add an icon with new size
259 iconArray.Add(icon);
260 }
261
262 size_t wxIconBundle::GetIconCount() const
263 {
264 return IsOk() ? M_ICONBUNDLEDATA->m_icons.size() : 0;
265 }
266
267 wxIcon wxIconBundle::GetIconByIndex(size_t n) const
268 {
269 wxCHECK_MSG( n < GetIconCount(), wxNullIcon, _T("invalid index") );
270
271 return M_ICONBUNDLEDATA->m_icons[n];
272 }