MIME support

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@2088 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Václav Slavík
1999-04-11 19:07:17 +00:00
parent 092fdc46a9
commit 9e9ee68e90
3 changed files with 148 additions and 6 deletions

View File

@@ -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{long}{ type = wxBITMAP\_TYPE\_PNG}}
\func{}{wxImage}{\param{const wxString\& }{name}, \param{const wxString\&}{ mimetype}}
Loads an image from a file. Loads an image from a file.
\func{}{wxImage}{\param{wxInputStream\& }{stream}, \param{long}{ type = wxBITMAP\_TYPE\_PNG}} \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. Loads an image from an input stream.
\wxheading{Parameters} \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 a PNG (portable network graphics) file and a JPEG file is supported on all platforms that
implement wxImage.} implement wxImage.}
\docparam{mimetype}{MIME type string (for example 'image/jpeg')}
Note : you must call wxImage::AddHandler(new wxJPEGHandler) during application Note : you must call wxImage::AddHandler(new wxJPEGHandler) during application
initialization in order to work with JPEGs. 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. 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{name}{The handler name.}
\docparam{extension}{The file extension, such as ``bmp".} \docparam{extension}{The file extension, such as ``bmp".}
\docparam{imageType}{The image type, such as wxBITMAP\_TYPE\_BMP.} \docparam{imageType}{The image type, such as wxBITMAP\_TYPE\_BMP.}
\docparam{mimetype}{MIME type.}
\wxheading{Return value} \wxheading{Return value}
A pointer to the handler if found, NULL otherwise. 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{long}{ type}}
\func{bool}{LoadFile}{\param{const wxString\&}{ name}, \param{const wxString\&}{ mimetype}}
Loads an image from a file. Loads an image from a file.
\func{bool}{LoadFile}{\param{wxInputStream\&}{ stream}, \param{long}{ type}} \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. Loads an image from an input stream.
\wxheading{Parameters} \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.} The validity of these flags depends on the platform and wxWindows configuration.}
\docparam{mimetype}{MIME type string (for example 'image/jpeg')}
\wxheading{Return value} \wxheading{Return value}
TRUE if the operation succeeded, FALSE otherwise. 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{int}{ type}}
\func{bool}{SaveFile}{\param{const wxString\& }{name}, \param{const wxString\&}{ mimetype}}
Saves a image in the named file. Saves a image in the named file.
\func{bool}{SaveFile}{\param{wxOutputStream\& }{stream}, \param{int}{ type}} \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. Saves a image in the given stream.
\wxheading{Parameters} \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 The validity of these flags depends on the platform and wxWindows configuration
as well as user-added handlers.} as well as user-added handlers.}
\docparam{mimetype}{MIME type.}
\wxheading{Return value} \wxheading{Return value}
TRUE if the operation succeeded, FALSE otherwise. 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. 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} \membersection{wxImageHandler::LoadFile}\label{wximagehandlerloadfile}
\func{bool}{LoadFile}{\param{wxImage* }{image}, \param{wxInputStream\&}{ stream}} \func{bool}{LoadFile}{\param{wxImage* }{image}, \param{wxInputStream\&}{ stream}}
@@ -594,3 +624,14 @@ Sets the handler type.
\docparam{name}{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.}

View File

@@ -58,13 +58,16 @@ public:
inline void SetName(const wxString& name) { m_name = name; } inline void SetName(const wxString& name) { m_name = name; }
inline void SetExtension(const wxString& ext) { m_extension = ext; } inline void SetExtension(const wxString& ext) { m_extension = ext; }
inline void SetType(long type) { m_type = type; } 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 GetName() const { return m_name; }
inline wxString GetExtension() const { return m_extension; } inline wxString GetExtension() const { return m_extension; }
inline long GetType() const { return m_type; } inline long GetType() const { return m_type; }
inline wxString GetMimeType() const { return m_mime; }
protected: protected:
wxString m_name; wxString m_name;
wxString m_extension; wxString m_extension;
wxString m_mime;
long m_type; long m_type;
}; };
@@ -85,6 +88,7 @@ public:
m_name = "PNG file"; m_name = "PNG file";
m_extension = "png"; m_extension = "png";
m_type = wxBITMAP_TYPE_PNG; m_type = wxBITMAP_TYPE_PNG;
m_mime = "image/png";
}; };
#if wxUSE_STREAMS #if wxUSE_STREAMS
@@ -111,6 +115,7 @@ public:
m_name = "JPEG file"; m_name = "JPEG file";
m_extension = "jpg"; m_extension = "jpg";
m_type = wxBITMAP_TYPE_JPEG; m_type = wxBITMAP_TYPE_JPEG;
m_mime = "image/jpeg";
}; };
virtual bool LoadFile( wxImage *image, wxInputStream& stream ); virtual bool LoadFile( wxImage *image, wxInputStream& stream );
@@ -133,6 +138,7 @@ public:
m_name = "BMP file"; m_name = "BMP file";
m_extension = "bmp"; m_extension = "bmp";
m_type = wxBITMAP_TYPE_BMP; m_type = wxBITMAP_TYPE_BMP;
m_mime = "image/bmp";
}; };
#if wxUSE_STREAMS #if wxUSE_STREAMS
@@ -156,7 +162,9 @@ public:
wxImage( int width, int height ); wxImage( int width, int height );
wxImage( const wxString& name, long type = wxBITMAP_TYPE_PNG ); wxImage( const wxString& name, long type = wxBITMAP_TYPE_PNG );
wxImage( wxInputStream& stream, 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 );
wxImage( const wxImage* image ); wxImage( const wxImage* image );
@@ -176,15 +184,19 @@ public:
unsigned char GetBlue( int x, int y ); unsigned char GetBlue( int x, int y );
virtual bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_PNG ); virtual bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_PNG );
virtual bool LoadFile( const wxString& name, const wxString& mimetype );
#if wxUSE_STREAMS #if wxUSE_STREAMS
virtual bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_PNG ); virtual bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_PNG );
virtual bool LoadFile( wxInputStream& stream, const wxString& mimetype );
#endif #endif
virtual bool SaveFile( const wxString& name, int type ); virtual bool SaveFile( const wxString& name, int type );
virtual bool SaveFile( const wxString& name, const wxString& mimetype );
#if wxUSE_STREAMS #if wxUSE_STREAMS
virtual bool SaveFile( wxOutputStream& stream, int type ); virtual bool SaveFile( wxOutputStream& stream, int type );
virtual bool SaveFile( wxOutputStream& stream, const wxString& mimetype );
#endif #endif
bool Ok() const; bool Ok() const;
@@ -219,7 +231,8 @@ public:
static wxImageHandler *FindHandler( const wxString& name ); static wxImageHandler *FindHandler( const wxString& name );
static wxImageHandler *FindHandler( const wxString& extension, long imageType ); static wxImageHandler *FindHandler( const wxString& extension, long imageType );
static wxImageHandler *FindHandler( long imageType ); static wxImageHandler *FindHandler( long imageType );
static wxImageHandler *FindHandlerMime( const wxString& mimetype );
static void CleanUpHandlers(); static void CleanUpHandlers();
static void InitStandardHandlers(); static void InitStandardHandlers();
@@ -231,3 +244,4 @@ protected:
#endif #endif
// _WX_IMAGE_H_ // _WX_IMAGE_H_

View File

@@ -101,11 +101,21 @@ wxImage::wxImage( const wxString& name, long type )
LoadFile( name, type ); LoadFile( name, type );
} }
wxImage::wxImage( const wxString& name, const wxString& mimetype )
{
LoadFile( name, mimetype );
}
#if wxUSE_STREAMS #if wxUSE_STREAMS
wxImage::wxImage( wxInputStream& stream, long type ) wxImage::wxImage( wxInputStream& stream, long type )
{ {
LoadFile( stream, type ); LoadFile( stream, type );
} }
wxImage::wxImage( wxInputStream& stream, const wxString& mimetype )
{
LoadFile( stream, mimetype );
}
#endif // wxUSE_STREAMS #endif // wxUSE_STREAMS
wxImage::wxImage( const wxImage& image ) wxImage::wxImage( const wxImage& image )
@@ -325,6 +335,25 @@ bool wxImage::LoadFile( const wxString& filename, long type )
wxFileInputStream stream(filename); wxFileInputStream stream(filename);
return LoadFile(stream, type); 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 { else {
wxLogError( "Can't load image from file '%s': file does not exist.", filename.c_str() ); 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 #if wxUSE_STREAMS
wxFileOutputStream stream(filename); wxFileOutputStream stream(filename);
if ( stream.LastError() == wxStream_NOERROR ) if ( stream.LastError() == wxStream_NOERROR )
return SaveFile(stream, type); return SaveFile(stream, type);
else else
@@ -348,6 +377,18 @@ bool wxImage::SaveFile( const wxString& filename, int type )
return FALSE; 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 #if wxUSE_STREAMS
bool wxImage::LoadFile( wxInputStream& stream, long type ) bool wxImage::LoadFile( wxInputStream& stream, long type )
{ {
@@ -367,15 +408,49 @@ bool wxImage::LoadFile( wxInputStream& stream, long type )
return handler->LoadFile( this, stream ); 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 ) bool wxImage::SaveFile( wxOutputStream& stream, int type )
{ {
wxCHECK_MSG( Ok(), FALSE, "invalid image" ); wxCHECK_MSG( Ok(), FALSE, "invalid image" );
wxImageHandler *handler = FindHandler(type); wxImageHandler *handler = FindHandler(type);
if (handler == NULL) if (handler == NULL)
{ {
wxLogWarning( "No image handler for type %d defined.", type ); 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; return FALSE;
} }
@@ -451,6 +526,18 @@ wxImageHandler *wxImage::FindHandler( long bitmapType )
return NULL; 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() void wxImage::InitStandardHandlers()
{ {
AddHandler( new wxBMPHandler ); AddHandler( new wxBMPHandler );