add support for loading wxIconBundles from streams

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@59929 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Václav Slavík
2009-03-29 21:19:40 +00:00
parent 694cb6c581
commit cee875e329
3 changed files with 88 additions and 28 deletions

View File

@@ -19,6 +19,8 @@
#include "wx/dynarray.h" #include "wx/dynarray.h"
class WXDLLIMPEXP_FWD_BASE wxInputStream;
WX_DECLARE_EXPORTED_OBJARRAY(wxIcon, wxIconArray); WX_DECLARE_EXPORTED_OBJARRAY(wxIcon, wxIconArray);
// this class can't load bitmaps of type wxBITMAP_TYPE_ICO_RESOURCE, // this class can't load bitmaps of type wxBITMAP_TYPE_ICO_RESOURCE,
@@ -31,7 +33,10 @@ public:
wxIconBundle(); wxIconBundle();
// initializes the bundle with the icon(s) found in the file // initializes the bundle with the icon(s) found in the file
wxIconBundle(const wxString& file, wxBitmapType type); wxIconBundle(const wxString& file, wxBitmapType type = wxBITMAP_TYPE_ANY);
#if wxUSE_STREAMS
wxIconBundle(wxInputStream& stream, wxBitmapType type = wxBITMAP_TYPE_ANY);
#endif
// initializes the bundle with a single icon // initializes the bundle with a single icon
wxIconBundle(const wxIcon& icon); wxIconBundle(const wxIcon& icon);
@@ -41,7 +46,10 @@ public:
// adds all the icons contained in the file to the collection, // adds all the icons contained in the file to the collection,
// if the collection already contains icons with the same // if the collection already contains icons with the same
// width and height, they are replaced // width and height, they are replaced
void AddIcon(const wxString& file, wxBitmapType type); void AddIcon(const wxString& file, wxBitmapType type = wxBITMAP_TYPE_ANY);
#if wxUSE_STREAMS
void AddIcon(wxInputStream& stream, wxBitmapType type = wxBITMAP_TYPE_ANY);
#endif
// adds the icon to the collection, if the collection already // adds the icon to the collection, if the collection already
// contains an icon with the same width and height, it is // contains an icon with the same width and height, it is

View File

@@ -29,7 +29,14 @@ public:
/** /**
Initializes the bundle with the icon(s) found in the file. Initializes the bundle with the icon(s) found in the file.
*/ */
wxIconBundle(const wxString& file, wxBitmapType type); wxIconBundle(const wxString& file, wxBitmapType type = wxBITMAP_TYPE_ANY);
/**
Initializes the bundle with the icon(s) found in the stream.
@since 2.9.0
*/
wxIconBundle(wxInputStream& stream, wxBitmapType type = wxBITMAP_TYPE_ANY);
/** /**
Initializes the bundle with a single icon. Initializes the bundle with a single icon.
@@ -47,11 +54,20 @@ public:
virtual ~wxIconBundle(); virtual ~wxIconBundle();
/** /**
Adds all the icons contained in the file to the bundle; if the collection Adds all the icons contained in the file to the bundle; if the
already contains icons with the same width and height, they are replaced collection already contains icons with the same width and height, they
by the new ones. are replaced by the new ones.
*/ */
void AddIcon(const wxString& file, wxBitmapType type); void AddIcon(const wxString& file, wxBitmapType type = wxBITMAP_TYPE_ANY);
/**
Adds all the icons contained in the stream to the bundle; if the
collection already contains icons with the same width and height, they
are replaced by the new ones.
@since 2.9.0
*/
void AddIcon(wxInputStream& stream, wxBitmapType type = wxBITMAP_TYPE_ANY);
/** /**
Adds the icon to the collection; if the collection already Adds the icon to the collection; if the collection already

View File

@@ -23,6 +23,7 @@
#include "wx/intl.h" #include "wx/intl.h"
#include "wx/bitmap.h" #include "wx/bitmap.h"
#include "wx/image.h" #include "wx/image.h"
#include "wx/stream.h"
#endif #endif
#include "wx/arrimpl.cpp" #include "wx/arrimpl.cpp"
@@ -60,6 +61,14 @@ wxIconBundle::wxIconBundle(const wxString& file, wxBitmapType type)
AddIcon(file, type); AddIcon(file, type);
} }
#if wxUSE_STREAMS
wxIconBundle::wxIconBundle(wxInputStream& stream, wxBitmapType type)
: wxGDIObject()
{
AddIcon(stream, type);
}
#endif // wxUSE_STREAMS
wxIconBundle::wxIconBundle(const wxIcon& icon) wxIconBundle::wxIconBundle(const wxIcon& icon)
: wxGDIObject() : wxGDIObject()
{ {
@@ -81,6 +90,41 @@ void wxIconBundle::DeleteIcons()
UnRef(); UnRef();
} }
namespace
{
// Adds icon from 'input' to the bundle. Shows 'errorMessage' on failure
// (it must contain "%d", because it is used to report # of image in the file
// that failed to load):
template<typename T>
void DoAddIcon(wxIconBundle& bundle,
T& input, wxBitmapType type,
const wxString& errorMessage)
{
#if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
wxImage image;
const size_t count = wxImage::GetImageCount(input, type);
for ( size_t i = 0; i < count; ++i )
{
if ( !image.LoadFile(input, type, i) )
{
wxLogError(errorMessage, i);
continue;
}
wxIcon tmp;
tmp.CopyFromBitmap(wxBitmap(image));
bundle.AddIcon(tmp);
}
#else // !wxUSE_IMAGE
wxUnusedVar(input);
wxUnusedVar(type);
#endif // wxUSE_IMAGE/!wxUSE_IMAGE
}
} // anonymous namespace
void wxIconBundle::AddIcon(const wxString& file, wxBitmapType type) void wxIconBundle::AddIcon(const wxString& file, wxBitmapType type)
{ {
#ifdef __WXMAC__ #ifdef __WXMAC__
@@ -96,29 +140,21 @@ void wxIconBundle::AddIcon(const wxString& file, wxBitmapType type)
} }
#endif // __WXMAC__ #endif // __WXMAC__
#if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB) DoAddIcon
wxImage image; (
*this,
const size_t count = wxImage::GetImageCount( file, type ); file, type,
for ( size_t i = 0; i < count; ++i ) wxString::Format(_("Failed to load image %%d from file '%s'."), file)
{ );
if ( !image.LoadFile( file, type, i ) )
{
wxLogError( _("Failed to load image %d from file '%s'."),
i, file.c_str() );
continue;
}
wxIcon tmp;
tmp.CopyFromBitmap(wxBitmap(image));
AddIcon(tmp);
}
#else // !wxUSE_IMAGE
wxUnusedVar(file);
wxUnusedVar(type);
#endif // wxUSE_IMAGE/!wxUSE_IMAGE
} }
#if wxUSE_STREAMS
void wxIconBundle::AddIcon(wxInputStream& stream, wxBitmapType type)
{
DoAddIcon(*this, stream, type, _("Failed to load image %d from stream."));
}
#endif // wxUSE_STREAMS
wxIcon wxIconBundle::GetIcon(const wxSize& size) const wxIcon wxIconBundle::GetIcon(const wxSize& size) const
{ {
const size_t count = GetIconCount(); const size_t count = GetIconCount();