From 9e9ee68e90212f1d9ba9f1cfa05e291463f6ae92 Mon Sep 17 00:00:00 2001
From: =?utf8?q?V=C3=A1clav=20Slav=C3=ADk?= <vslavik@fastmail.fm>
Date: Sun, 11 Apr 1999 19:07:17 +0000
Subject: [PATCH] MIME support

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@2088 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/image.tex | 41 ++++++++++++++++++
 include/wx/image.h      | 20 +++++++--
 src/common/image.cpp    | 93 +++++++++++++++++++++++++++++++++++++++--
 3 files changed, 148 insertions(+), 6 deletions(-)

diff --git a/docs/latex/wx/image.tex b/docs/latex/wx/image.tex
index 7a9351b804..8325f4be63 100644
--- a/docs/latex/wx/image.tex
+++ b/docs/latex/wx/image.tex
@@ -46,10 +46,14 @@ Creates an image with the given width and height.
 
 \func{}{wxImage}{\param{const wxString\& }{name}, \param{long}{ type = wxBITMAP\_TYPE\_PNG}}
 
+\func{}{wxImage}{\param{const wxString\& }{name}, \param{const wxString\&}{ mimetype}}
+
 Loads an image from a file.
 
 \func{}{wxImage}{\param{wxInputStream\& }{stream}, \param{long}{ type = wxBITMAP\_TYPE\_PNG}}
 
+\func{}{wxImage}{\param{wxInputStream\& }{stream}, \param{const wxString\&}{ mimetype}}
+
 Loads an image from an input stream.
 
 \wxheading{Parameters}
@@ -76,6 +80,8 @@ If all possible wxWindows settings are used, the loading a BMP (Windows bitmap)
 a PNG (portable network graphics) file and a JPEG file is supported on all platforms that
 implement wxImage.}
 
+\docparam{mimetype}{MIME type string (for example 'image/jpeg')}
+
 Note : you must call wxImage::AddHandler(new wxJPEGHandler) during application 
 initialization in order to work with JPEGs.
 
@@ -157,12 +163,18 @@ Finds the handler associated with the given extension and type.
 
 Finds the handler associated with the given image type.
 
+\func{static wxImageHandler*}{FindHandlerMime}{\param{const wxString\& }{mimetype}}
+
+Finds the handler associated with the given MIME type.
+
 \docparam{name}{The handler name.}
 
 \docparam{extension}{The file extension, such as ``bmp".}
 
 \docparam{imageType}{The image type, such as wxBITMAP\_TYPE\_BMP.}
 
+\docparam{mimetype}{MIME type.}
+
 \wxheading{Return value}
 
 A pointer to the handler if found, NULL otherwise.
@@ -278,10 +290,14 @@ of a given handler class in an application session.}
 
 \func{bool}{LoadFile}{\param{const wxString\&}{ name}, \param{long}{ type}}
 
+\func{bool}{LoadFile}{\param{const wxString\&}{ name}, \param{const wxString\&}{ mimetype}}
+
 Loads an image from a file.
 
 \func{bool}{LoadFile}{\param{wxInputStream\&}{ stream}, \param{long}{ type}}
 
+\func{bool}{LoadFile}{\param{wxInputStream\&}{ stream}, \param{const wxString\&}{ mimetype}}
+
 Loads an image from an input stream.
 
 \wxheading{Parameters}
@@ -303,6 +319,8 @@ The meaning of {\it stream} data is determined by the {\it type} parameter.}
 
 The validity of these flags depends on the platform and wxWindows configuration.}
 
+\docparam{mimetype}{MIME type string (for example 'image/jpeg')}
+
 \wxheading{Return value}
 
 TRUE if the operation succeeded, FALSE otherwise.
@@ -338,10 +356,14 @@ TRUE if the handler was found and removed, FALSE otherwise.
 
 \func{bool}{SaveFile}{\param{const wxString\& }{name}, \param{int}{ type}}
 
+\func{bool}{SaveFile}{\param{const wxString\& }{name}, \param{const wxString\&}{ mimetype}}
+
 Saves a image in the named file.
 
 \func{bool}{SaveFile}{\param{wxOutputStream\& }{stream}, \param{int}{ type}}
 
+\func{bool}{SaveFile}{\param{wxOutputStream\& }{stream}, \param{const wxString\&}{ mimetype}}
+
 Saves a image in the given stream.
 
 \wxheading{Parameters}
@@ -361,6 +383,8 @@ Saves a image in the given stream.
 The validity of these flags depends on the platform and wxWindows configuration
 as well as user-added handlers.}
 
+\docparam{mimetype}{MIME type.}
+
 \wxheading{Return value}
 
 TRUE if the operation succeeded, FALSE otherwise.
@@ -519,6 +543,12 @@ Gets the file extension associated with this handler.
 
 Gets the image type associated with this handler.
 
+\membersection{wxImageHandler::GetMimeType}
+
+\constfunc{wxString}{GetMimeType}{\void}
+
+Gets the MIME type associated with this handler.
+
 \membersection{wxImageHandler::LoadFile}\label{wximagehandlerloadfile}
 
 \func{bool}{LoadFile}{\param{wxImage* }{image}, \param{wxInputStream\&}{ stream}}
@@ -594,3 +624,14 @@ Sets the handler type.
 
 \docparam{name}{Handler type.}
 
+
+\membersection{wxImageHandler::SetMimeType}
+
+\func{void}{SetMimeType}{\param{const wxString\& }{mimetype}}
+
+Sets the handler MIME type.
+
+\wxheading{Parameters}
+
+\docparam{mimename}{Handler MIME type.}
+
diff --git a/include/wx/image.h b/include/wx/image.h
index 1962bd3f87..ffdcf30a2f 100644
--- a/include/wx/image.h
+++ b/include/wx/image.h
@@ -58,13 +58,16 @@ public:
   inline void SetName(const wxString& name) { m_name = name; }
   inline void SetExtension(const wxString& ext) { m_extension = ext; }
   inline void SetType(long type) { m_type = type; }
+  inline void SetMimeType(const wxString& type) { m_mime = type; }
   inline wxString GetName() const { return m_name; }
   inline wxString GetExtension() const { return m_extension; }
   inline long GetType() const { return m_type; }
-  
+  inline wxString GetMimeType() const { return m_mime; }
+
 protected:
   wxString  m_name;
   wxString  m_extension;
+  wxString  m_mime;
   long      m_type;
   
 };
@@ -85,6 +88,7 @@ public:
       m_name = "PNG file";
       m_extension = "png";
       m_type = wxBITMAP_TYPE_PNG;
+      m_mime = "image/png";
   };
 
 #if wxUSE_STREAMS
@@ -111,6 +115,7 @@ public:
       m_name = "JPEG file";
       m_extension = "jpg";
       m_type = wxBITMAP_TYPE_JPEG;
+      m_mime = "image/jpeg";
   };
 
   virtual bool LoadFile( wxImage *image, wxInputStream& stream );
@@ -133,6 +138,7 @@ public:
       m_name = "BMP file";
       m_extension = "bmp";
       m_type = wxBITMAP_TYPE_BMP;
+      m_mime = "image/bmp";
   };
 
 #if wxUSE_STREAMS
@@ -156,7 +162,9 @@ public:
   wxImage( int width, int height );
   wxImage( const wxString& name, long type = wxBITMAP_TYPE_PNG );
   wxImage( wxInputStream& stream, long type = wxBITMAP_TYPE_PNG );
-  
+  wxImage( const wxString& name, const wxString& mimetype );
+  wxImage( wxInputStream& stream, const wxString& mimetype );
+
   wxImage( const wxImage& image );
   wxImage( const wxImage* image );
   
@@ -176,15 +184,19 @@ public:
   unsigned char GetBlue( int x, int y );
   
   virtual bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_PNG );
+  virtual bool LoadFile( const wxString& name, const wxString& mimetype );
 
 #if wxUSE_STREAMS
   virtual bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_PNG );
+  virtual bool LoadFile( wxInputStream& stream, const wxString& mimetype );
 #endif
 
   virtual bool SaveFile( const wxString& name, int type );
+  virtual bool SaveFile( const wxString& name, const wxString& mimetype );
 
 #if wxUSE_STREAMS
   virtual bool SaveFile( wxOutputStream& stream, int type );
+  virtual bool SaveFile( wxOutputStream& stream, const wxString& mimetype );
 #endif
 
   bool Ok() const;
@@ -219,7 +231,8 @@ public:
   static wxImageHandler *FindHandler( const wxString& name );
   static wxImageHandler *FindHandler( const wxString& extension, long imageType );
   static wxImageHandler *FindHandler( long imageType );
-  
+  static wxImageHandler *FindHandlerMime( const wxString& mimetype );
+
   static void CleanUpHandlers();
   static void InitStandardHandlers();
 
@@ -231,3 +244,4 @@ protected:
 
 #endif
   // _WX_IMAGE_H_
+
diff --git a/src/common/image.cpp b/src/common/image.cpp
index 800e61fa59..a95fe7784a 100644
--- a/src/common/image.cpp
+++ b/src/common/image.cpp
@@ -101,11 +101,21 @@ wxImage::wxImage( const wxString& name, long type )
     LoadFile( name, type );
 }
 
+wxImage::wxImage( const wxString& name, const wxString& mimetype )
+{
+    LoadFile( name, mimetype );
+}
+
 #if wxUSE_STREAMS
 wxImage::wxImage( wxInputStream& stream, long type )
 {
     LoadFile( stream, type );
 }
+
+wxImage::wxImage( wxInputStream& stream, const wxString& mimetype )
+{
+    LoadFile( stream, mimetype );
+}
 #endif // wxUSE_STREAMS
 
 wxImage::wxImage( const wxImage& image )
@@ -325,6 +335,25 @@ bool wxImage::LoadFile( const wxString& filename, long type )
         wxFileInputStream stream(filename);
         return LoadFile(stream, type);
     }
+
+    else {
+        wxLogError( "Can't load image from file '%s': file does not exist.", filename.c_str() );
+
+        return FALSE;
+    }
+#else // !wxUSE_STREAMS
+    return FALSE;
+#endif // wxUSE_STREAMS
+}
+
+bool wxImage::LoadFile( const wxString& filename, const wxString& mimetype )
+{
+#if wxUSE_STREAMS
+    if (wxFileExists(filename))
+    {
+        wxFileInputStream stream(filename);
+        return LoadFile(stream, mimetype);
+    }
     
     else {
         wxLogError( "Can't load image from file '%s': file does not exist.", filename.c_str() );
@@ -340,7 +369,7 @@ bool wxImage::SaveFile( const wxString& filename, int type )
 {
 #if wxUSE_STREAMS
     wxFileOutputStream stream(filename);
-    
+
     if ( stream.LastError() == wxStream_NOERROR )
         return SaveFile(stream, type);
     else
@@ -348,6 +377,18 @@ bool wxImage::SaveFile( const wxString& filename, int type )
         return FALSE;
 }
 
+bool wxImage::SaveFile( const wxString& filename, const wxString& mimetype )
+{
+#if wxUSE_STREAMS
+    wxFileOutputStream stream(filename);
+    
+    if ( stream.LastError() == wxStream_NOERROR )
+        return SaveFile(stream, mimetype);
+    else
+#endif // wxUSE_STREAMS
+        return FALSE;
+}
+
 #if wxUSE_STREAMS
 bool wxImage::LoadFile( wxInputStream& stream, long type )
 {
@@ -367,15 +408,49 @@ bool wxImage::LoadFile( wxInputStream& stream, long type )
     return handler->LoadFile( this, stream );
 }
 
+bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype )
+{
+    UnRef();
+
+    m_refData = new wxImageRefData;
+
+    wxImageHandler *handler = FindHandlerMime(mimetype);
+
+    if (handler == NULL)
+    {
+        wxLogWarning( "No image handler for type %s defined.", mimetype.GetData() );
+
+        return FALSE;
+    }
+
+    return handler->LoadFile( this, stream );
+}
+
 bool wxImage::SaveFile( wxOutputStream& stream, int type )
 {
     wxCHECK_MSG( Ok(), FALSE, "invalid image" );
-    
+
     wxImageHandler *handler = FindHandler(type);
-    
+
     if (handler == NULL)
     {
         wxLogWarning( "No image handler for type %d defined.", type );
+
+        return FALSE;
+    }
+
+    return handler->SaveFile( this, stream );
+}
+
+bool wxImage::SaveFile( wxOutputStream& stream, const wxString& mimetype )
+{
+    wxCHECK_MSG( Ok(), FALSE, "invalid image" );
+    
+    wxImageHandler *handler = FindHandlerMime(mimetype);
+    
+    if (handler == NULL)
+    {
+        wxLogWarning( "No image handler for type %s defined.", mimetype.GetData() );
         
         return FALSE;
     }
@@ -451,6 +526,18 @@ wxImageHandler *wxImage::FindHandler( long bitmapType )
     return NULL;
 }
 
+wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype )
+{
+    wxNode *node = sm_handlers.First();
+    while (node)
+    {
+        wxImageHandler *handler = (wxImageHandler *)node->Data();
+        if (handler->GetMimeType().IsSameAs(mimetype, FALSE)) return handler;
+        node = node->Next();
+    }
+    return NULL;
+}
+
 void wxImage::InitStandardHandlers()
 {
     AddHandler( new wxBMPHandler );
-- 
2.50.0

