]> git.saurik.com Git - wxWidgets.git/commitdiff
Improve error messages from wxImage::LoadFile().
authorVadim Zeitlin <vadim@wxwidgets.org>
Wed, 24 Nov 2010 00:42:35 +0000 (00:42 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Wed, 24 Nov 2010 00:42:35 +0000 (00:42 +0000)
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

src/common/image.cpp

index 28e43c26d3560a5a47acc53accf8277d367f0041..f17d1cbf2511f2d136cfd4bdaa6baf05fb864052 100644 (file)
@@ -29,7 +29,6 @@
     #include "wx/colour.h"
 #endif
 
-#include "wx/filefn.h"
 #include "wx/wfstream.h"
 #include "wx/xpmdecod.h"
 
@@ -2198,21 +2197,18 @@ bool wxImage::LoadFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename),
                         int WXUNUSED_UNLESS_STREAMS(index) )
 {
 #if HAS_FILE_STREAMS
-    if (wxFileExists(filename))
+    wxImageFileInputStream stream(filename);
+    if ( stream.IsOk() )
     {
-        wxImageFileInputStream stream(filename);
         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
+
+    return false;
 }
 
 bool wxImage::LoadFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename),
@@ -2220,21 +2216,18 @@ bool wxImage::LoadFile( const wxString& WXUNUSED_UNLESS_STREAMS(filename),
                         int WXUNUSED_UNLESS_STREAMS(index) )
 {
 #if HAS_FILE_STREAMS
-    if (wxFileExists(filename))
+    wxImageFileInputStream stream(filename);
+    if ( stream.IsOk() )
     {
-        wxImageFileInputStream stream(filename);
         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
+
+    return false;
 }
 
 
@@ -2420,6 +2413,17 @@ bool wxImage::LoadFile( wxInputStream& stream, wxBitmapType type, int index )
 
     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;
@@ -2430,7 +2434,7 @@ bool wxImage::LoadFile( wxInputStream& stream, wxBitmapType type, int index )
                  return true;
         }
 
-        wxLogWarning( _("No handler found for image type.") );
+        wxLogWarning( _("Unknown image data format.") );
 
         return false;
     }
@@ -2445,7 +2449,7 @@ bool wxImage::LoadFile( wxInputStream& stream, wxBitmapType type, int index )
 
     if ( stream.IsSeekable() && !handler->CanRead(stream) )
     {
-        wxLogError(_("Image file is not of type %d."), type);
+        wxLogError(_("This is not a %s."), handler->GetName());
         return false;
     }
 
@@ -2468,7 +2472,7 @@ bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype, int ind
 
     if ( stream.IsSeekable() && !handler->CanRead(stream) )
     {
-        wxLogError(_("Image file is not of type %s."), mimetype);
+        wxLogError(_("Image is not of type %s."), mimetype);
         return false;
     }
 
@@ -2868,15 +2872,15 @@ int wxImageHandler::GetImageCount( wxInputStream& stream )
 
 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 false;
+    }
 
-    return false;
+    return CanRead(stream);
 }
 
 bool wxImageHandler::CallDoCanRead(wxInputStream& stream)