The error given when loading an image file failed was not very useful because
they didn't specify which file exactly we failed to load and also because the
numeric handler type which means nothing at all to the end user (and not much
to the developer) was used.
Use the description of the file format instead and also always give the name
of the file that we failed to load.
Finally, remove the test for file existence: this is one of many reasons why
opening the file could fail and it doesn't make sense to handle it specially,
just let the underlying stream generate the appropriate error message in all
cases.
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@66251
c3d73ce0-8a6f-49c7-b76d-
6d57e0e08775
#include "wx/colour.h"
#endif
#include "wx/colour.h"
#endif
#include "wx/wfstream.h"
#include "wx/xpmdecod.h"
#include "wx/wfstream.h"
#include "wx/xpmdecod.h"
int WXUNUSED_UNLESS_STREAMS(index) )
{
#if HAS_FILE_STREAMS
int WXUNUSED_UNLESS_STREAMS(index) )
{
#if HAS_FILE_STREAMS
- if (wxFileExists(filename))
+ wxImageFileInputStream stream(filename);
+ if ( stream.IsOk() )
- wxImageFileInputStream stream(filename);
wxBufferedInputStream bstream( stream );
wxBufferedInputStream bstream( stream );
- return LoadFile(bstream, type, index);
+ if ( LoadFile(bstream, type, index) )
+ return true;
- else
- {
- wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() );
- return false;
- }
-#else // !HAS_FILE_STREAMS
- return false;
+ wxLogError(_("Failed to load image from file \"%s\"."), filename);
#endif // HAS_FILE_STREAMS
#endif // HAS_FILE_STREAMS
}
bool wxImage::LoadFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename),
}
bool wxImage::LoadFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename),
int WXUNUSED_UNLESS_STREAMS(index) )
{
#if HAS_FILE_STREAMS
int WXUNUSED_UNLESS_STREAMS(index) )
{
#if HAS_FILE_STREAMS
- if (wxFileExists(filename))
+ wxImageFileInputStream stream(filename);
+ if ( stream.IsOk() )
- wxImageFileInputStream stream(filename);
wxBufferedInputStream bstream( stream );
wxBufferedInputStream bstream( stream );
- return LoadFile(bstream, mimetype, index);
+ if ( LoadFile(bstream, mimetype, index) )
+ return true;
- else
- {
- wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() );
- return false;
- }
-#else // !HAS_FILE_STREAMS
- return false;
+ wxLogError(_("Failed to load image from file \"%s\"."), filename);
#endif // HAS_FILE_STREAMS
#endif // HAS_FILE_STREAMS
if ( type == wxBITMAP_TYPE_ANY )
{
if ( type == wxBITMAP_TYPE_ANY )
{
+ if ( !stream.IsSeekable() )
+ {
+ // The error message about image data format being unknown below
+ // would be misleading in this case as we are not even going to try
+ // any handlers because CanRead() never does anything for not
+ // seekable stream, so try to be more precise here.
+ wxLogError(_("Can't automatically determine the image format "
+ "for non-seekable input."));
+ return false;
+ }
+
const wxList& list = GetHandlers();
for ( wxList::compatibility_iterator node = list.GetFirst();
node;
const wxList& list = GetHandlers();
for ( wxList::compatibility_iterator node = list.GetFirst();
node;
- wxLogWarning( _("No handler found for image type.") );
+ wxLogWarning( _("Unknown image data format.") );
if ( stream.IsSeekable() && !handler->CanRead(stream) )
{
if ( stream.IsSeekable() && !handler->CanRead(stream) )
{
- wxLogError(_("Image file is not of type %d."), type);
+ wxLogError(_("This is not a %s."), handler->GetName());
if ( stream.IsSeekable() && !handler->CanRead(stream) )
{
if ( stream.IsSeekable() && !handler->CanRead(stream) )
{
- wxLogError(_("Image file is not of type %s."), mimetype);
+ wxLogError(_("Image is not of type %s."), mimetype);
bool wxImageHandler::CanRead( const wxString& name )
{
bool wxImageHandler::CanRead( const wxString& name )
{
- if (wxFileExists(name))
+ wxImageFileInputStream stream(name);
+ if ( !stream.IsOk() )
- wxImageFileInputStream stream(name);
- return CanRead(stream);
- }
+ wxLogError(_("Failed to check format of image file \"%s\"."), name);
- wxLogError( _("Can't check image format of file '%s': file does not exist."), name.c_str() );
+ return CanRead(stream);
}
bool wxImageHandler::CallDoCanRead(wxInputStream& stream)
}
bool wxImageHandler::CallDoCanRead(wxInputStream& stream)