1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/iconbndl.cpp
3 // Purpose: wxIconBundle
4 // Author: Mattia Barbon, Vadim Zeitlin
7 // Copyright: (c) Mattia barbon
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #include "wx/iconbndl.h"
21 #include "wx/settings.h"
24 #include "wx/bitmap.h"
26 #include "wx/stream.h"
29 #include "wx/wfstream.h"
31 #include "wx/arrimpl.cpp"
32 WX_DEFINE_OBJARRAY(wxIconArray
)
34 IMPLEMENT_DYNAMIC_CLASS(wxIconBundle
, wxGDIObject
)
36 #define M_ICONBUNDLEDATA static_cast<wxIconBundleRefData*>(m_refData)
38 // ----------------------------------------------------------------------------
39 // wxIconBundleRefData
40 // ----------------------------------------------------------------------------
42 class WXDLLEXPORT wxIconBundleRefData
: public wxGDIRefData
45 wxIconBundleRefData() { }
47 // We need the copy ctor for CloneGDIRefData() but notice that we use the
48 // base class default ctor in it and not the copy one which it doesn't have.
49 wxIconBundleRefData(const wxIconBundleRefData
& other
)
51 m_icons(other
.m_icons
)
55 // default assignment operator and dtor are ok
57 virtual bool IsOk() const { return !m_icons
.empty(); }
62 // ============================================================================
63 // wxIconBundle implementation
64 // ============================================================================
66 wxIconBundle::wxIconBundle()
70 #if wxUSE_STREAMS && wxUSE_IMAGE
72 #if wxUSE_FFILE || wxUSE_FILE
73 wxIconBundle::wxIconBundle(const wxString
& file
, wxBitmapType type
)
78 #endif // wxUSE_FFILE || wxUSE_FILE
80 wxIconBundle::wxIconBundle(wxInputStream
& stream
, wxBitmapType type
)
83 AddIcon(stream
, type
);
85 #endif // wxUSE_STREAMS && wxUSE_IMAGE
87 wxIconBundle::wxIconBundle(const wxIcon
& icon
)
93 wxGDIRefData
*wxIconBundle::CreateGDIRefData() const
95 return new wxIconBundleRefData
;
98 wxGDIRefData
*wxIconBundle::CloneGDIRefData(const wxGDIRefData
*data
) const
100 return new wxIconBundleRefData(*static_cast<const wxIconBundleRefData
*>(data
));
103 void wxIconBundle::DeleteIcons()
108 #if wxUSE_STREAMS && wxUSE_IMAGE
113 // Adds icon from 'input' to the bundle. Shows 'errorMessage' on failure
114 // (it must contain "%d", because it is used to report # of image in the file
115 // that failed to load):
116 void DoAddIcon(wxIconBundle
& bundle
,
117 wxInputStream
& input
,
119 const wxString
& errorMessage
)
123 const wxFileOffset posOrig
= input
.TellI();
125 const size_t count
= wxImage::GetImageCount(input
, type
);
126 for ( size_t i
= 0; i
< count
; ++i
)
130 // the call to LoadFile() for the first sub-image updated the
131 // stream position but we need to start reading the subsequent
132 // sub-image at the image beginning too
133 input
.SeekI(posOrig
);
136 if ( !image
.LoadFile(input
, type
, i
) )
138 wxLogError(errorMessage
, i
);
142 if ( type
== wxBITMAP_TYPE_ANY
)
144 // store the type so that we don't need to try all handlers again
145 // for the subsequent images, they should all be of the same type
146 type
= image
.GetType();
150 tmp
.CopyFromBitmap(wxBitmap(image
));
155 } // anonymous namespace
157 #if wxUSE_FFILE || wxUSE_FILE
159 void wxIconBundle::AddIcon(const wxString
& file
, wxBitmapType type
)
162 // Deal with standard icons
163 if ( type
== wxBITMAP_TYPE_ICON_RESOURCE
)
165 wxIcon
tmp(file
, type
);
175 wxFFileInputStream
stream(file
);
177 wxFileInputStream
stream(file
);
183 wxString::Format(_("Failed to load image %%d from file '%s'."), file
)
187 #endif // wxUSE_FFILE || wxUSE_FILE
189 void wxIconBundle::AddIcon(wxInputStream
& stream
, wxBitmapType type
)
191 DoAddIcon(*this, stream
, type
, _("Failed to load image %d from stream."));
194 #endif // wxUSE_STREAMS && wxUSE_IMAGE
196 wxIcon
wxIconBundle::GetIcon(const wxSize
& size
, int flags
) const
198 wxASSERT( size
== wxDefaultSize
|| (size
.x
>= 0 && size
.y
> 0) );
200 // We need the standard system icon size when using FALLBACK_SYSTEM.
203 if ( flags
& FALLBACK_SYSTEM
)
205 sysX
= wxSystemSettings::GetMetric(wxSYS_ICON_X
);
206 sysY
= wxSystemSettings::GetMetric(wxSYS_ICON_Y
);
209 // If size == wxDefaultSize, we use system default icon size by convention.
210 wxCoord sizeX
= size
.x
;
211 wxCoord sizeY
= size
.y
;
212 if ( size
== wxDefaultSize
)
214 wxASSERT_MSG( flags
== FALLBACK_SYSTEM
,
215 wxS("Must have valid size if not using FALLBACK_SYSTEM") );
221 // Iterate over all icons searching for the exact match or the closest icon
222 // for FALLBACK_NEAREST_LARGER.
225 bool bestIsLarger
= false;
226 bool bestIsSystem
= false;
228 const size_t count
= GetIconCount();
230 const wxIconArray
& iconArray
= M_ICONBUNDLEDATA
->m_icons
;
231 for ( size_t i
= 0; i
< count
; i
++ )
233 const wxIcon
& icon
= iconArray
[i
];
236 wxCoord sx
= icon
.GetWidth(),
237 sy
= icon
.GetHeight();
239 // Exact match ends search immediately in any case.
240 if ( sx
== sizeX
&& sy
== sizeY
)
246 if ( flags
& FALLBACK_SYSTEM
)
248 if ( sx
== sysX
&& sy
== sysY
)
256 if ( !bestIsSystem
&& (flags
& FALLBACK_NEAREST_LARGER
) )
258 bool iconLarger
= (sx
>= sizeX
) && (sy
>= sizeY
);
259 int iconDiff
= abs(sx
- sizeX
) + abs(sy
- sizeY
);
261 // Use current icon as candidate for the best icon, if either:
262 // - we have no candidate yet
263 // - we have no candidate larger than desired size and current icon is
264 // - current icon is closer to desired size than candidate
265 if ( !iconBest
.IsOk() ||
266 (!bestIsLarger
&& iconLarger
) ||
267 (iconLarger
&& (iconDiff
< bestDiff
)) )
270 bestIsLarger
= iconLarger
;
277 #if defined( __WXMAC__ ) && wxOSX_USE_CARBON
278 if (!iconBest
.IsOk())
281 return wxIcon(iconBest
.GetHICON(), size
);
287 wxIcon
wxIconBundle::GetIconOfExactSize(const wxSize
& size
) const
289 return GetIcon(size
, FALLBACK_NONE
);
292 void wxIconBundle::AddIcon(const wxIcon
& icon
)
294 wxCHECK_RET( icon
.IsOk(), wxT("invalid icon") );
298 wxIconArray
& iconArray
= M_ICONBUNDLEDATA
->m_icons
;
300 // replace existing icon with the same size if we already have it
301 const size_t count
= iconArray
.size();
302 for ( size_t i
= 0; i
< count
; ++i
)
304 wxIcon
& tmp
= iconArray
[i
];
306 tmp
.GetWidth() == icon
.GetWidth() &&
307 tmp
.GetHeight() == icon
.GetHeight() )
314 // if we don't, add an icon with new size
318 size_t wxIconBundle::GetIconCount() const
320 return IsOk() ? M_ICONBUNDLEDATA
->m_icons
.size() : 0;
323 wxIcon
wxIconBundle::GetIconByIndex(size_t n
) const
325 wxCHECK_MSG( n
< GetIconCount(), wxNullIcon
, wxT("invalid index") );
327 return M_ICONBUNDLEDATA
->m_icons
[n
];