]> git.saurik.com Git - wxWidgets.git/blob - src/common/iconbndl.cpp
remove "item.cchTextMax=0" line from SetItem() as it doesn't seem to make any sense...
[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/arrimpl.cpp"
30 WX_DEFINE_OBJARRAY(wxIconArray)
31
32 IMPLEMENT_DYNAMIC_CLASS(wxIconBundle, wxGDIObject)
33
34 #define M_ICONBUNDLEDATA static_cast<wxIconBundleRefData*>(m_refData)
35
36 // ----------------------------------------------------------------------------
37 // wxIconBundleRefData
38 // ----------------------------------------------------------------------------
39
40 class WXDLLEXPORT wxIconBundleRefData : public wxGDIRefData
41 {
42 public:
43 // default and copy ctors and assignment operators are ok
44
45 virtual bool IsOk() const { return !m_icons.empty(); }
46
47 wxIconArray m_icons;
48 };
49
50 // ============================================================================
51 // wxIconBundle implementation
52 // ============================================================================
53
54 wxIconBundle::wxIconBundle()
55 {
56 }
57
58 wxIconBundle::wxIconBundle(const wxString& file, wxBitmapType type)
59 : wxGDIObject()
60 {
61 AddIcon(file, type);
62 }
63
64 #if wxUSE_STREAMS
65 wxIconBundle::wxIconBundle(wxInputStream& stream, wxBitmapType type)
66 : wxGDIObject()
67 {
68 AddIcon(stream, type);
69 }
70 #endif // wxUSE_STREAMS
71
72 wxIconBundle::wxIconBundle(const wxIcon& icon)
73 : wxGDIObject()
74 {
75 AddIcon(icon);
76 }
77
78 wxGDIRefData *wxIconBundle::CreateGDIRefData() const
79 {
80 return new wxIconBundleRefData;
81 }
82
83 wxGDIRefData *wxIconBundle::CloneGDIRefData(const wxGDIRefData *data) const
84 {
85 return new wxIconBundleRefData(*static_cast<const wxIconBundleRefData *>(data));
86 }
87
88 void wxIconBundle::DeleteIcons()
89 {
90 UnRef();
91 }
92
93 namespace
94 {
95
96 // Adds icon from 'input' to the bundle. Shows 'errorMessage' on failure
97 // (it must contain "%d", because it is used to report # of image in the file
98 // that failed to load):
99 template<typename T>
100 void DoAddIcon(wxIconBundle& bundle,
101 T& input, wxBitmapType type,
102 const wxString& errorMessage)
103 {
104 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
105 wxImage image;
106
107 const size_t count = wxImage::GetImageCount(input, type);
108 for ( size_t i = 0; i < count; ++i )
109 {
110 if ( !image.LoadFile(input, type, i) )
111 {
112 wxLogError(errorMessage, i);
113 continue;
114 }
115
116 wxIcon tmp;
117 tmp.CopyFromBitmap(wxBitmap(image));
118 bundle.AddIcon(tmp);
119 }
120 #else // !wxUSE_IMAGE
121 wxUnusedVar(input);
122 wxUnusedVar(type);
123 #endif // wxUSE_IMAGE/!wxUSE_IMAGE
124 }
125
126 } // anonymous namespace
127
128 void wxIconBundle::AddIcon(const wxString& file, wxBitmapType type)
129 {
130 #ifdef __WXMAC__
131 // Deal with standard icons
132 if ( type == wxBITMAP_TYPE_ICON_RESOURCE )
133 {
134 wxIcon tmp(file, type);
135 if (tmp.Ok())
136 {
137 AddIcon(tmp);
138 return;
139 }
140 }
141 #endif // __WXMAC__
142
143 DoAddIcon
144 (
145 *this,
146 file, type,
147 wxString::Format(_("Failed to load image %%d from file '%s'."), file)
148 );
149 }
150
151 #if wxUSE_STREAMS
152 void wxIconBundle::AddIcon(wxInputStream& stream, wxBitmapType type)
153 {
154 DoAddIcon(*this, stream, type, _("Failed to load image %d from stream."));
155 }
156 #endif // wxUSE_STREAMS
157
158 wxIcon wxIconBundle::GetIcon(const wxSize& size) const
159 {
160 const size_t count = GetIconCount();
161
162 // optimize for the common case of icon bundles containing one icon only
163 wxIcon iconBest;
164 switch ( count )
165 {
166 case 0:
167 // nothing to do, iconBest is already invalid
168 break;
169
170 case 1:
171 iconBest = M_ICONBUNDLEDATA->m_icons[0];
172 break;
173
174 default:
175 // there is more than one icon, find the best match:
176 wxCoord sysX = wxSystemSettings::GetMetric( wxSYS_ICON_X ),
177 sysY = wxSystemSettings::GetMetric( wxSYS_ICON_Y );
178
179 const wxIconArray& iconArray = M_ICONBUNDLEDATA->m_icons;
180 for ( size_t i = 0; i < count; i++ )
181 {
182 const wxIcon& icon = iconArray[i];
183 wxCoord sx = icon.GetWidth(),
184 sy = icon.GetHeight();
185
186 // if we got an icon of exactly the requested size, we're done
187 if ( sx == size.x && sy == size.y )
188 {
189 iconBest = icon;
190 break;
191 }
192
193 // the best icon is by default (arbitrarily) the first one but
194 // if we find a system-sized icon, take it instead
195 if ((sx == sysX && sy == sysY) || !iconBest.IsOk())
196 iconBest = icon;
197 }
198 }
199
200 #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
201 return wxIcon(iconBest.GetHICON(), size);
202 #else
203 return iconBest;
204 #endif
205 }
206
207 wxIcon wxIconBundle::GetIconOfExactSize(const wxSize& size) const
208 {
209 wxIcon icon = GetIcon(size);
210 if ( icon.Ok() &&
211 (icon.GetWidth() != size.x || icon.GetHeight() != size.y) )
212 {
213 icon = wxNullIcon;
214 }
215
216 return icon;
217 }
218
219 void wxIconBundle::AddIcon(const wxIcon& icon)
220 {
221 wxCHECK_RET( icon.IsOk(), _T("invalid icon") );
222
223 AllocExclusive();
224
225 wxIconArray& iconArray = M_ICONBUNDLEDATA->m_icons;
226
227 // replace existing icon with the same size if we already have it
228 const size_t count = iconArray.size();
229 for ( size_t i = 0; i < count; ++i )
230 {
231 wxIcon& tmp = iconArray[i];
232 if ( tmp.Ok() &&
233 tmp.GetWidth() == icon.GetWidth() &&
234 tmp.GetHeight() == icon.GetHeight() )
235 {
236 tmp = icon;
237 return;
238 }
239 }
240
241 // if we don't, add an icon with new size
242 iconArray.Add(icon);
243 }
244
245 size_t wxIconBundle::GetIconCount() const
246 {
247 return IsOk() ? M_ICONBUNDLEDATA->m_icons.size() : 0;
248 }
249
250 wxIcon wxIconBundle::GetIconByIndex(size_t n) const
251 {
252 wxCHECK_MSG( n < GetIconCount(), wxNullIcon, _T("invalid index") );
253
254 return M_ICONBUNDLEDATA->m_icons[n];
255 }