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