]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/xpmhand.cpp
Changed order of #ifdefs to get native version on OS/2, even if
[wxWidgets.git] / src / msw / xpmhand.cpp
index da59e8b1509c1bd683176b1f8a98579da79c73bb..7c3a590ffc77672336e8d29ebb1c193c259fac99 100644 (file)
 /////////////////////////////////////////////////////////////////////////////
 
 #ifdef __GNUG__
-#pragma implementation "xpmhand.h"
+    #pragma implementation "xpmhand.h"
 #endif
 
 // For compilers that support precompilation, includes "wx.h".
 #include "wx/wxprec.h"
 
 #ifdef __BORLANDC__
-#pragma hdrstop
+    #pragma hdrstop
 #endif
 
 #ifndef WX_PRECOMP
-#include <stdio.h>
-#include "wx/setup.h"
-#include "wx/list.h"
-#include "wx/utils.h"
-#include "wx/app.h"
-#include "wx/palette.h"
-#include "wx/dcmemory.h"
-#include "wx/bitmap.h"
-#include "wx/icon.h"
+    #include "wx/list.h"
+    #include "wx/utils.h"
+    #include "wx/app.h"
+    #include "wx/palette.h"
+    #include "wx/dcmemory.h"
+    #include "wx/bitmap.h"
+    #include "wx/icon.h"
 #endif
 
 #include "wx/msw/private.h"
 #include "wx/log.h"
 
-#include "assert.h"
-
 #if wxUSE_XPM_IN_MSW
-#define FOR_MSW 1
-#include "../xpm/xpm34.h"
+    #define FOR_MSW 1
+    #include "../xpm/xpm34.h"
 #endif
 
 #include "wx/xpmhand.h"
 #include "wx/msw/dib.h"
 
+#if wxUSE_XPM_IN_MSW
+
+static void XpmToBitmap(wxBitmap *bitmap,
+                        const XImage *ximage,
+                        const XpmAttributes& xpmAttr)
+{
+    wxBitmapRefData *refData = bitmap->GetBitmapData();
+    refData->m_hBitmap = (WXHBITMAP)ximage->bitmap;
+
+    // first set the bitmap width, height, depth...
+    BITMAP bm;
+    if ( !::GetObject(GetHbitmapOf(*bitmap), sizeof(bm), (LPSTR) & bm) )
+    {
+        wxLogLastError("GetObject(bitmap)");
+    }
+
+    refData->m_width = bm.bmWidth;
+    refData->m_height = bm.bmHeight;
+    refData->m_depth = bm.bmPlanes * bm.bmBitsPixel;
+    refData->m_numColors = xpmAttr.npixels;
+
+    // next get the mask, if any
+    if ( xpmAttr.mask_pixel != XpmUndefPixel )
+    {
+        int red, green, blue;
+        const char *clrString = xpmAttr.colorTable[xpmAttr.mask_pixel].c_color;
+        if ( strcmp(clrString, "None") == 0 )
+        {
+            // TODO what to do here??
+            red = green = 0;
+            blue = 1;
+        }
+        else
+        {
+            sscanf(clrString, "#%02x%02x%02x", &red, &green, &blue);
+        }
+
+        wxMask *mask = new wxMask(*bitmap, wxColour(red, green, blue));
+        bitmap->SetMask(mask);
+    }
+}
+
+#endif // wxUSE_XPM_IN_MSW
+
 IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler, wxBitmapHandler)
 
-bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
-    int desiredWidth, int desiredHeight)
+bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap,
+                                const wxString& name,
+                                long flags,
+                                int desiredWidth, int desiredHeight)
 {
 #if wxUSE_XPM_IN_MSW
     XImage *ximage;
     XpmAttributes xpmAttr;
     HDC     dc;
 
-    M_BITMAPHANDLERDATA->m_ok = FALSE;
     dc = CreateCompatibleDC(NULL);
     if (dc)
     {
-      xpmAttr.valuemask = XpmReturnPixels;
-      int errorStatus = XpmReadFileToImage(&dc, WXSTRINGCAST name, &ximage, (XImage **) NULL, &xpmAttr);
-      DeleteDC(dc);
-      if (errorStatus == XpmSuccess)
-      {
-        M_BITMAPHANDLERDATA->m_hBitmap = (WXHBITMAP) ximage->bitmap;
+        xpmAttr.valuemask = XpmReturnPixels | XpmColorTable;
+        int errorStatus = XpmReadFileToImage(&dc,
+                                             wxMBSTRINGCAST name.fn_str(),
+                                             &ximage,
+                                             (XImage **)NULL,
+                                             &xpmAttr);
+        DeleteDC(dc);
+        if (errorStatus == XpmSuccess)
+        {
+            XpmToBitmap(bitmap, ximage, xpmAttr);
 
-        BITMAP  bm;
-        GetObject((HBITMAP)M_BITMAPHANDLERDATA->m_hBitmap, sizeof(bm), (LPSTR) & bm);
+            XpmFree(xpmAttr.pixels);
+            XpmFreeAttributes(&xpmAttr);
+            XImageFree(ximage);
+        }
 
-        M_BITMAPHANDLERDATA->m_width = (bm.bmWidth);
-        M_BITMAPHANDLERDATA->m_height = (bm.bmHeight);
-        M_BITMAPHANDLERDATA->m_depth = (bm.bmPlanes * bm.bmBitsPixel);
-        M_BITMAPHANDLERDATA->m_numColors = xpmAttr.npixels;
-        XpmFreeAttributes(&xpmAttr);
-        XImageFree(ximage);
+#if WXWIN_COMPATIBILITY_2
+        bitmap->SetOk(errorStatus == XpmSuccess);
+#endif // WXWIN_COMPATIBILITY_2
 
-        M_BITMAPHANDLERDATA->m_ok = TRUE;
-        return TRUE;
-      }
-      else
-      {
-        M_BITMAPHANDLERDATA->m_ok = FALSE;
-        return FALSE;
-      }
+        return bitmap->Ok();
     }
-#endif
+#endif // wxUSE_XPM_IN_MSW
 
     return FALSE;
 }
 
-bool wxXPMFileHandler::SaveFile(wxBitmap *bitmap, const wxString& name, int type, const wxPalette *palette)
+bool wxXPMFileHandler::SaveFile(wxBitmap *bitmap,
+                                const wxString& name,
+                                int type,
+                                const wxPalette *palette)
 {
 #if wxUSE_XPM_IN_MSW
-    HDC     dc = NULL;
-    
-    XImage  ximage;
-    
-    dc = CreateCompatibleDC(NULL);
+    XImage ximage;
+
+    HDC dc = CreateCompatibleDC(NULL);
     if (dc)
     {
-        if (SelectObject(dc, (HBITMAP) M_BITMAPHANDLERDATA->m_hBitmap))
+        if ( SelectObject(dc, GetHbitmapOf(*bitmap)) )
         {
             /* for following SetPixel */
             /* fill the XImage struct 'by hand' */
-            ximage.width = M_BITMAPHANDLERDATA->m_width; 
-            ximage.height = M_BITMAPHANDLERDATA->m_height;
-            ximage.depth = M_BITMAPHANDLERDATA->m_depth; 
-            ximage.bitmap = (HBITMAP)M_BITMAPHANDLERDATA->m_hBitmap;
-            int errorStatus = XpmWriteFileFromImage(&dc, WXSTRINGCAST name,
-                &ximage, (XImage *) NULL, (XpmAttributes *) NULL);
-            
+            wxBitmapRefData *refData = bitmap->GetBitmapData();
+            ximage.width = refData->m_width;
+            ximage.height = refData->m_height;
+            ximage.depth = refData->m_depth;
+            ximage.bitmap = (HBITMAP)refData->m_hBitmap;
+            int errorStatus = XpmWriteFileFromImage(&dc, wxMBSTRINGCAST name.fn_str(),
+                                                    &ximage, (XImage *) NULL,
+                                                    (XpmAttributes *) NULL);
+
             if (dc)
                 DeleteDC(dc);
-            
+
             if (errorStatus == XpmSuccess)
                 return TRUE;    /* no error */
-            else
-                return FALSE;
-        } else return FALSE;
-    } else return FALSE;
-#else
+        }
+    }
+#endif // !wxUSE_XPM_IN_MSW
+
     return FALSE;
-#endif
 }
 
 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler)
 
-bool wxXPMDataHandler::Create(wxBitmap *bitmap, void *data, long flags, int width, int height, int depth)
+bool wxXPMDataHandler::Create(wxBitmap *bitmap,
+                              void *data,
+                              long flags,
+                              int width,
+                              int height,
+                              int depth)
 {
 #if wxUSE_XPM_IN_MSW
   XImage *ximage;
-  int     ErrorStatus;
   XpmAttributes xpmAttr;
-  HDC     dc;
-
-  M_BITMAPHANDLERDATA->m_ok = FALSE;
-  M_BITMAPHANDLERDATA->m_numColors = 0;
 
-  dc = CreateCompatibleDC(NULL);  /* memory DC */
+  HDC dc = CreateCompatibleDC(NULL);  /* memory DC */
 
   if (dc)
   {
-    xpmAttr.valuemask = XpmReturnInfos; /* get infos back */
-    ErrorStatus = XpmCreateImageFromData(&dc, (char **)data,
-         &ximage, (XImage **) NULL, &xpmAttr);
-
-    if (ErrorStatus == XpmSuccess)
-    {
-      /* ximage is malloced and contains bitmap and attributes */
-      M_BITMAPHANDLERDATA->m_hBitmap = (WXHBITMAP) ximage->bitmap;
+      xpmAttr.valuemask = XpmReturnInfos | XpmColorTable;
+      int errorStatus = XpmCreateImageFromData(&dc, (char **)data,
+                                               &ximage,
+                                               (XImage **) NULL,
+                                               &xpmAttr);
+      DeleteDC(dc);
 
-      BITMAP  bm;
-      GetObject((HBITMAP) M_BITMAPHANDLERDATA->m_hBitmap, sizeof(bm), (LPSTR) & bm);
+      if ( errorStatus == XpmSuccess )
+      {
+          XpmToBitmap(bitmap, ximage, xpmAttr);
 
-      M_BITMAPHANDLERDATA->m_width = (bm.bmWidth);
-      M_BITMAPHANDLERDATA->m_height = (bm.bmHeight);
-      M_BITMAPHANDLERDATA->m_depth = (bm.bmPlanes * bm.bmBitsPixel);
-      M_BITMAPHANDLERDATA->m_numColors = xpmAttr.npixels;
-      XpmFreeAttributes(&xpmAttr);
+          XpmFree(xpmAttr.pixels);
+          XpmFreeAttributes(&xpmAttr);
+          XImageFree(ximage); // releases the malloc, but does not destroy bitmap
+      }
 
-      XImageFree(ximage); // releases the malloc, but does not detroy
-                          // the bitmap
-      M_BITMAPHANDLERDATA->m_ok = TRUE;
-      DeleteDC(dc);
+#if WXWIN_COMPATIBILITY_2
+      bitmap->SetOk(errorStatus == XpmSuccess);
+#endif // WXWIN_COMPATIBILITY_2
 
-      return TRUE;
-    } 
-    else
-    {
-      M_BITMAPHANDLERDATA->m_ok = FALSE;
-//    XpmDebugError(ErrorStatus, NULL);
-      DeleteDC(dc);
-      return FALSE;
-    }
+      return bitmap->Ok();
   }
-#endif
+#endif // wxUSE_XPM_IN_MSW
 
   return FALSE;
 }