]> git.saurik.com Git - wxWidgets.git/blob - src/common/iconbndl.cpp
Added detection of vsscanf and whether or not a prototype exists in headers.
[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 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #include "wx/iconbndl.h"
27
28 #ifndef WX_PRECOMP
29 #include "wx/settings.h"
30 #include "wx/icon.h"
31 #include "wx/log.h"
32 #include "wx/intl.h"
33 #include "wx/bitmap.h"
34 #include "wx/image.h"
35 #endif
36
37 #include "wx/arrimpl.cpp"
38
39 WX_DEFINE_OBJARRAY(wxIconArray)
40
41 IMPLEMENT_DYNAMIC_CLASS(wxIconBundle, wxGDIObject)
42
43 #define M_ICONBUNDLEDATA ((wxIconBundleRefData *)m_refData)
44
45 // ----------------------------------------------------------------------------
46 // wxIconBundleRefData
47 // ----------------------------------------------------------------------------
48
49 class WXDLLEXPORT wxIconBundleRefData : public wxGDIRefData
50 {
51 public:
52 // default and copy ctors and assignment operators are ok
53
54 virtual bool IsOk() const { return !m_icons.empty(); }
55
56 protected:
57 wxIconArray m_icons;
58
59 friend class wxIconBundle;
60 };
61
62 // ============================================================================
63 // wxIconBundle implementation
64 // ============================================================================
65
66 wxIconBundle::wxIconBundle()
67 : wxGDIObject()
68 {
69 }
70
71 wxIconBundle::wxIconBundle(const wxString& file, long type)
72 : wxGDIObject()
73 {
74 AddIcon(file, type);
75 }
76
77 wxIconBundle::wxIconBundle(const wxIconBundle& icon)
78 : wxGDIObject()
79 {
80 Ref(icon);
81 }
82
83 wxIconBundle::wxIconBundle(const wxIcon& icon)
84 : wxGDIObject()
85 {
86 AddIcon(icon);
87 }
88
89 wxGDIRefData *wxIconBundle::CreateGDIRefData() const
90 {
91 return new wxIconBundleRefData;
92 }
93
94 wxGDIRefData *wxIconBundle::CloneGDIRefData(const wxGDIRefData *data) const
95 {
96 return new wxIconBundleRefData(*wx_static_cast(const wxIconBundleRefData *, data));
97 }
98
99 void wxIconBundle::DeleteIcons()
100 {
101 UnRef();
102 }
103
104 void wxIconBundle::AddIcon(const wxString& file, long type)
105 {
106 #ifdef __WXMAC__
107 // Deal with standard icons
108 if ( type == wxBITMAP_TYPE_ICON_RESOURCE )
109 {
110 wxIcon tmp(file, type);
111 if (tmp.Ok())
112 {
113 AddIcon(tmp);
114 return;
115 }
116 }
117 #endif // __WXMAC__
118
119 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
120 wxImage image;
121
122 const size_t count = wxImage::GetImageCount( file, type );
123 for ( size_t i = 0; i < count; ++i )
124 {
125 if ( !image.LoadFile( file, type, i ) )
126 {
127 wxLogError( _("Failed to load image %d from file '%s'."),
128 i, file.c_str() );
129 continue;
130 }
131
132 wxIcon tmp;
133 tmp.CopyFromBitmap(wxBitmap(image));
134 AddIcon(tmp);
135 }
136 #else // !wxUSE_IMAGE
137 wxUnusedVar(file);
138 wxUnusedVar(type);
139 #endif // wxUSE_IMAGE/!wxUSE_IMAGE
140 }
141
142 wxIcon wxIconBundle::GetIcon(const wxSize& size) const
143 {
144 const size_t count = GetIconCount();
145
146 // optimize for the common case of icon bundles containing one icon only
147 wxIcon iconBest;
148 switch ( count )
149 {
150 case 0:
151 // nothing to do, iconBest is already invalid
152 break;
153
154 case 1:
155 iconBest = M_ICONBUNDLEDATA->m_icons[0];
156 break;
157
158 default:
159 // there is more than one icon, find the best match:
160 wxCoord sysX = wxSystemSettings::GetMetric( wxSYS_ICON_X ),
161 sysY = wxSystemSettings::GetMetric( wxSYS_ICON_Y );
162
163 const wxIconArray& iconArray = M_ICONBUNDLEDATA->m_icons;
164 for ( size_t i = 0; i < count; i++ )
165 {
166 const wxIcon& icon = iconArray[i];
167 wxCoord sx = icon.GetWidth(),
168 sy = icon.GetHeight();
169
170 // if we got an icon of exactly the requested size, we're done
171 if ( sx == size.x && sy == size.y )
172 {
173 iconBest = icon;
174 break;
175 }
176
177 // the best icon is by default (arbitrarily) the first one but
178 // if we find a system-sized icon, take it instead
179 if ( sx == sysX && sy == sysY || !iconBest.IsOk() )
180 iconBest = icon;
181 }
182 }
183
184 #ifdef __WXMAC__
185 return wxIcon(iconBest.GetHICON(), size);
186 #else
187 return iconBest;
188 #endif
189 }
190
191 wxIcon wxIconBundle::GetIconOfExactSize(const wxSize& size) const
192 {
193 wxIcon icon = GetIcon(size);
194 if ( icon.Ok() &&
195 (icon.GetWidth() != size.x || icon.GetHeight() != size.y) )
196 {
197 icon = wxNullIcon;
198 }
199
200 return icon;
201 }
202
203 void wxIconBundle::AddIcon(const wxIcon& icon)
204 {
205 wxCHECK_RET( icon.IsOk(), _T("invalid icon") );
206
207 AllocExclusive();
208
209 wxIconArray& iconArray = M_ICONBUNDLEDATA->m_icons;
210
211 // replace existing icon with the same size if we already have it
212 const size_t count = iconArray.size();
213 for ( size_t i = 0; i < count; ++i )
214 {
215 wxIcon& tmp = iconArray[i];
216 if ( tmp.Ok() &&
217 tmp.GetWidth() == icon.GetWidth() &&
218 tmp.GetHeight() == icon.GetHeight() )
219 {
220 tmp = icon;
221 return;
222 }
223 }
224
225 // if we don't, add an icon with new size
226 iconArray.Add(icon);
227 }
228
229 size_t wxIconBundle::GetIconCount() const
230 {
231 return IsOk() ? M_ICONBUNDLEDATA->m_icons.size() : 0;
232 }
233
234 wxIcon wxIconBundle::GetIconByIndex(size_t n) const
235 {
236 wxCHECK_MSG( n < GetIconCount(), wxNullIcon, _T("invalid index") );
237
238 return M_ICONBUNDLEDATA->m_icons[n];
239 }
240