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