+// ============================================================================
+// Bitmap handlers
+// ============================================================================
+
+IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxBitmapHandlerBase)
+
+#define M_BMPHANDLERDATA ((wxBitmapRefData *)bitmap->GetRefData())
+
+#if wxUSE_XPM
+
+#if wxHAVE_LIB_XPM || wxUSE_STREAMS
+
+// ----------------------------------------------------------------------------
+// wxXPMFileHandler
+// ----------------------------------------------------------------------------
+
+class wxXPMFileHandler : public wxBitmapHandler
+{
+ DECLARE_DYNAMIC_CLASS(wxXPMFileHandler)
+public:
+ wxXPMFileHandler()
+ {
+ SetName( wxT("XPM file") );
+ SetExtension( wxT("xpm") );
+ SetType( wxBITMAP_TYPE_XPM );
+ };
+
+ virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
+ int desiredWidth, int desiredHeight);
+
+ virtual bool SaveFile(const wxBitmap *bitmap, const wxString& name,
+ int type, const wxPalette *palette = NULL);
+
+ virtual bool Create(wxBitmap *WXUNUSED(bitmap), const void* WXUNUSED(data), long WXUNUSED(flags),
+ int WXUNUSED(width), int WXUNUSED(height), int WXUNUSED(depth) = 1)
+ { return false; }
+};
+
+IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler, wxBitmapHandler)
+
+bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name,
+ long WXUNUSED(flags), int WXUNUSED(desiredWidth),
+ int WXUNUSED(desiredHeight))
+{
+#if wxHAVE_LIB_XPM
+ if (!bitmap->GetRefData())
+ bitmap->SetRefData( new wxBitmapRefData() );
+
+ M_BMPHANDLERDATA->m_display = wxGlobalDisplay();
+
+ Display *xdisplay = (Display*) M_BMPHANDLERDATA->m_display;
+
+ int xscreen = DefaultScreen( xdisplay );
+ Window xroot = RootWindow( xdisplay, xscreen );
+
+ int bpp = DefaultDepth( xdisplay, xscreen );
+
+ XpmAttributes xpmAttr;
+ xpmAttr.valuemask = XpmReturnInfos; // nothing yet, but get infos back
+
+ Pixmap pixmap;
+ Pixmap mask = 0;
+
+ int ErrorStatus = XpmReadFileToPixmap( xdisplay, xroot,
+ (char*) ((const char*) name.c_str()),
+ &pixmap, &mask, &xpmAttr);
+
+ if (ErrorStatus == XpmSuccess)
+ {
+ M_BMPHANDLERDATA->m_width = xpmAttr.width;
+ M_BMPHANDLERDATA->m_height = xpmAttr.height;
+
+ M_BMPHANDLERDATA->m_bpp = bpp; // mono as well?
+
+ XpmFreeAttributes(&xpmAttr);
+
+ M_BMPHANDLERDATA->m_bitmap = (Pixmap) pixmap;
+
+ if (mask)
+ {
+ M_BMPHANDLERDATA->m_mask = new wxMask;
+ M_BMPHANDLERDATA->m_mask->SetBitmap( (WXPixmap) mask );
+ M_BMPHANDLERDATA->m_mask->SetDisplay( xdisplay );
+ }
+ }
+ else
+ {
+ UnRef();
+
+ return false;
+ }
+
+ return true;
+#elif wxUSE_STREAMS
+ wxXPMDecoder decoder;
+ wxFileInputStream stream(name);
+ if (stream.Ok())
+ {
+ wxImage image(decoder.ReadFile(stream));
+ return image.Ok() && bitmap->CreateFromImage(image);
+ }
+
+ return false;
+#else // !wxHAVE_LIB_XPM && !wxUSE_STREAMS
+ return false;
+#endif // wxHAVE_LIB_XPM / wxUSE_STREAMS
+}
+
+bool wxXPMFileHandler::SaveFile(const wxBitmap *bitmap, const wxString& name,
+ int type,
+ const wxPalette *WXUNUSED(palette))
+{
+ wxImage image(bitmap->ConvertToImage());
+ if (image.Ok()) return image.SaveFile( name, (wxBitmapType)type );
+
+ return false;
+}
+
+#endif // wxHAVE_LIB_XPM || wxUSE_STREAMS
+
+// ----------------------------------------------------------------------------
+// wxXPMDataHandler
+// ----------------------------------------------------------------------------
+
+class wxXPMDataHandler : public wxBitmapHandler
+{
+ DECLARE_DYNAMIC_CLASS(wxXPMDataHandler)
+public:
+ wxXPMDataHandler()
+ {
+ SetName( wxT("XPM data") );
+ SetExtension( wxT("xpm") );
+ SetType( wxBITMAP_TYPE_XPM_DATA );
+ };
+
+ virtual bool LoadFile(wxBitmap *WXUNUSED(bitmap),
+ const wxString& WXUNUSED(name),
+ long WXUNUSED(flags),
+ int WXUNUSED(desiredWidth),
+ int WXUNUSED(desiredHeight))
+ { return false; }
+
+ virtual bool SaveFile(const wxBitmap *WXUNUSED(bitmap),
+ const wxString& WXUNUSED(name),
+ int WXUNUSED(type),
+ const wxPalette *WXUNUSED(palette) = NULL)
+ { return false; }
+
+ virtual bool Create(wxBitmap *bitmap, const void* data, long flags,
+ int width, int height, int depth = 1);
+};
+
+IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler)
+
+bool wxXPMDataHandler::Create(wxBitmap *bitmap, const void* bits,
+ long WXUNUSED(flags),
+ int WXUNUSED(width), int WXUNUSED(height), int WXUNUSED(depth))
+{
+#if wxHAVE_LIB_XPM
+ wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") );
+
+ if (!bitmap->GetRefData())
+ bitmap->SetRefData( new wxBitmapRefData() );
+
+ M_BMPHANDLERDATA->m_display = wxGlobalDisplay();
+
+ Display *xdisplay = (Display*) M_BMPHANDLERDATA->m_display;
+
+ int xscreen = DefaultScreen( xdisplay );
+ Window xroot = RootWindow( xdisplay, xscreen );
+
+ int bpp = DefaultDepth( xdisplay, xscreen );
+
+ XpmAttributes xpmAttr;
+ xpmAttr.valuemask = XpmReturnInfos; // nothing yet, but get infos back
+
+ Pixmap pixmap = 0;
+ Pixmap mask = 0;
+
+ int ErrorStatus = XpmCreatePixmapFromData( xdisplay, xroot, (char**) bits,
+ &pixmap, &mask, &xpmAttr );
+
+ if (ErrorStatus == XpmSuccess)
+ {
+ M_BMPHANDLERDATA->m_width = xpmAttr.width;
+ M_BMPHANDLERDATA->m_height = xpmAttr.height;
+
+ M_BMPHANDLERDATA->m_bpp = bpp; // mono as well?
+
+#if __WXDEBUG__
+ unsigned int depthRet;
+ int xRet, yRet;
+ unsigned int widthRet, heightRet, borderWidthRet;
+ XGetGeometry( xdisplay, pixmap, &xroot, &xRet, &yRet,
+ &widthRet, &heightRet, &borderWidthRet, &depthRet);
+
+ wxASSERT_MSG( bpp == (int)depthRet, wxT("colour depth mismatch") );
+#endif
+
+ XpmFreeAttributes(&xpmAttr);
+
+ M_BMPHANDLERDATA->m_pixmap = (Pixmap) pixmap;
+
+ if (mask)
+ {
+ M_BMPHANDLERDATA->m_mask = new wxMask;
+ M_BMPHANDLERDATA->m_mask->SetBitmap( (WXPixmap) mask );
+ M_BMPHANDLERDATA->m_mask->SetDisplay( xdisplay );
+ }
+ return true;
+ }
+ else
+ {
+ bitmap->UnRef();
+
+ return false;
+ }
+#else // !wxHAVE_LIB_XPM
+ wxXPMDecoder decoder;
+ wxImage image(decoder.ReadData((const char **)bits));
+ return image.Ok() && bitmap->CreateFromImage(image);
+#endif // wxHAVE_LIB_XPM/!wxHAVE_LIB_XPM
+}
+
+#endif // wxUSE_XPM
+
+// ----------------------------------------------------------------------------
+// wxXBMDataHandler
+// ----------------------------------------------------------------------------
+
+class WXDLLEXPORT wxXBMDataHandler: public wxBitmapHandler
+{
+ DECLARE_DYNAMIC_CLASS(wxXBMDataHandler)
+public:
+ inline wxXBMDataHandler()
+ {
+ SetName( wxT("XBM data") );
+ SetExtension( wxT("xbm") );
+ SetType( wxBITMAP_TYPE_XBM_DATA );
+ };
+
+ virtual bool LoadFile(wxBitmap *WXUNUSED(bitmap),
+ const wxString& WXUNUSED(name),
+ long WXUNUSED(flags),
+ int WXUNUSED(desiredWidth),
+ int WXUNUSED(desiredHeight))
+ { return false; }
+
+ virtual bool SaveFile(const wxBitmap *WXUNUSED(bitmap),
+ const wxString& WXUNUSED(name),
+ int WXUNUSED(type),
+ const wxPalette *WXUNUSED(palette) = NULL)
+ { return false; }
+
+ virtual bool Create(wxBitmap *bitmap, const void* data, long flags,
+ int width, int height, int depth = 1);
+};
+
+IMPLEMENT_DYNAMIC_CLASS(wxXBMDataHandler, wxBitmapHandler)
+
+bool wxXBMDataHandler::Create( wxBitmap *bitmap, const void* bits,
+ long WXUNUSED(flags),
+ int width, int height, int WXUNUSED(depth))
+{
+#if !wxUSE_NANOX
+ if (!bitmap->GetRefData())
+ bitmap->SetRefData( new wxBitmapRefData() );
+
+ M_BMPHANDLERDATA->m_display = wxGlobalDisplay();
+
+ Display *xdisplay = (Display*) M_BMPHANDLERDATA->m_display;
+
+ int xscreen = DefaultScreen( xdisplay );
+ Window xroot = RootWindow( xdisplay, xscreen );
+
+ M_BMPHANDLERDATA->m_mask = (wxMask *) NULL;
+ M_BMPHANDLERDATA->m_bitmap =
+ XCreateBitmapFromData(xdisplay, xroot,
+ (char *) bits, width, height );
+ M_BMPHANDLERDATA->m_width = width;
+ M_BMPHANDLERDATA->m_height = height;
+ M_BMPHANDLERDATA->m_bpp = 1;
+
+ return true;
+#else
+ wxCHECK_MSG( M_BMPHANDLERDATA->m_bitmap, false,
+ wxT("couldn't create bitmap") );
+#endif
+}
+
+void wxBitmap::InitStandardHandlers()
+{
+ AddHandler(new wxXBMDataHandler);
+#if wxUSE_XPM
+#if wxHAVE_LIB_XPM || wxUSE_STREAMS
+ AddHandler(new wxXPMFileHandler);
+#endif
+ AddHandler(new wxXPMDataHandler);
+#endif
+}