]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/xpmhand.cpp
missing event.Skip() added (it was impossible to use mouse without it...)
[wxWidgets.git] / src / msw / xpmhand.cpp
index 5a1b6ab7426af7cf113a1e357627d466ca94dc69..064e9fc34a6bf1c16e12d1efbf870f1c739af864 100644 (file)
@@ -35,7 +35,7 @@
 
 #if wxUSE_XPM_IN_MSW
     #define FOR_MSW 1
-    #include "../xpm/xpm34.h"
+    #include "../xpm/xpm.h"
 #endif
 
 #include "wx/xpmhand.h"
 
 static void XpmToBitmap(wxBitmap *bitmap,
                         const XImage *ximage,
+                        const XImage *xmask,
                         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_width     = bm.bmWidth;
+    refData->m_height    = bm.bmHeight;
+    refData->m_depth     = bm.bmPlanes * bm.bmBitsPixel;
     refData->m_numColors = xpmAttr.npixels;
+
+    // GRG Jan/2000, mask support
+    if (xmask)
+    {
+        wxMask *mask = new wxMask();
+        mask->SetMaskBitmap((WXHBITMAP) wxInvertMask(xmask->bitmap));
+        bitmap->SetMask(mask);
+    }
 }
 
 #endif // wxUSE_XPM_IN_MSW
@@ -72,31 +82,39 @@ bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap,
                                 int desiredWidth, int desiredHeight)
 {
 #if wxUSE_XPM_IN_MSW
-    XImage *ximage;
+    XImage *ximage = NULL;
+    XImage *xmask = NULL;
     XpmAttributes xpmAttr;
     HDC     dc;
 
     dc = CreateCompatibleDC(NULL);
     if (dc)
     {
-      xpmAttr.valuemask = XpmReturnPixels;
-      int errorStatus = XpmReadFileToImage(&dc, wxMBSTRINGCAST name.fn_str(), &ximage, (XImage **) NULL, &xpmAttr);
-      DeleteDC(dc);
-      if (errorStatus == XpmSuccess)
-      {
-        XpmToBitmap(bitmap, ximage, xpmAttr);
+        xpmAttr.valuemask = XpmReturnPixels | XpmColorTable;
+        int errorStatus = XpmReadFileToImage(&dc,
+                                             wxMBSTRINGCAST name.fn_str(),
+                                             &ximage,
+                                             &xmask,
+                                             &xpmAttr);
+        DeleteDC(dc);
+        if (errorStatus == XpmSuccess)
+        {
+            XpmToBitmap(bitmap, ximage, xmask, xpmAttr);
 
-        XpmFreeAttributes(&xpmAttr);
-        XImageFree(ximage);
-      }
+            XpmFree(xpmAttr.pixels);
+            XpmFreeAttributes(&xpmAttr);
+            XImageFree(ximage);
+            if (xmask)
+                XDestroyImage(xmask);
+        }
 
 #if WXWIN_COMPATIBILITY_2
-      bitmap->SetOk(errorStatus == XpmSuccess);
+        bitmap->SetOk(errorStatus == XpmSuccess);
 #endif // WXWIN_COMPATIBILITY_2
 
-      return bitmap->Ok();
+        return bitmap->Ok();
     }
-#endif
+#endif // wxUSE_XPM_IN_MSW
 
     return FALSE;
 }
@@ -107,38 +125,49 @@ bool wxXPMFileHandler::SaveFile(wxBitmap *bitmap,
                                 const wxPalette *palette)
 {
 #if wxUSE_XPM_IN_MSW
-    HDC     dc = NULL;
+    XImage ximage;
+    XImage xmask;
+    bool hasmask = FALSE;
 
-    XImage  ximage;
-
-    dc = CreateCompatibleDC(NULL);
+    HDC dc = CreateCompatibleDC(NULL);
     if (dc)
     {
-        if ( SelectObject(dc, GetHbitmapOf(*bitmap)) )
+        /* fill the XImage struct 'by hand' */
+        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;
+
+        // GRG Jan/2000, mask support
+        hasmask = (refData->m_bitmapMask != NULL);
+        if (hasmask)
         {
-            /* for following SetPixel */
-            /* fill the XImage struct 'by hand' */
-            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
+            /* Strangely enough, the MSW version of xpmlib is not
+             * coherent with itself regarding masks; we have to invert
+             * the mask we get when loading, but we still must pass it
+             * 'as is' when saving...
+             */
+            xmask.bitmap = (HBITMAP) refData->m_bitmapMask->GetMaskBitmap();
+            xmask.width  = refData->m_width;
+            xmask.height = refData->m_height;
+            xmask.depth  = 1;
+        }
+
+        int errorStatus = XpmWriteFileFromImage(
+            &dc,
+            wxMBSTRINGCAST name.fn_str(),
+            &ximage,
+            (hasmask? &xmask : (XImage *)NULL),
+            (XpmAttributes *) NULL);
+
+        DeleteDC(dc);
+
+        return (errorStatus == XpmSuccess);
+    }
+#endif // !wxUSE_XPM_IN_MSW
+
     return FALSE;
-#endif
 }
 
 IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler)
@@ -151,42 +180,39 @@ bool wxXPMDataHandler::Create(wxBitmap *bitmap,
                               int depth)
 {
 #if wxUSE_XPM_IN_MSW
-  XImage *ximage;
-  int     ErrorStatus;
+  XImage *ximage = NULL;
+  XImage *xmask = NULL;
   XpmAttributes xpmAttr;
-  HDC     dc;
 
-  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)
-    {
-        XpmToBitmap(bitmap, ximage, xpmAttr);
-
-      XpmFreeAttributes(&xpmAttr);
+      xpmAttr.valuemask = XpmReturnInfos | XpmColorTable;
+      int errorStatus = XpmCreateImageFromData(&dc, (char **)data,
+                                               &ximage,
+                                               &xmask,
+                                               &xpmAttr);
+      DeleteDC(dc);
 
-      XImageFree(ximage); // releases the malloc, but does not detroy
-                          // the bitmap
-    }
-    else
-    {
-      //    XpmDebugError(ErrorStatus, NULL);
-    }
+      if ( errorStatus == XpmSuccess )
+      {
+          XpmToBitmap(bitmap, ximage, xmask, xpmAttr);
 
-    DeleteDC(dc);
+          XpmFree(xpmAttr.pixels);
+          XpmFreeAttributes(&xpmAttr);
+          XImageFree(ximage); // releases the malloc, but does not destroy bitmap
+          if (xmask)
+              XDestroyImage(xmask);
+      }
 
 #if WXWIN_COMPATIBILITY_2
       bitmap->SetOk(errorStatus == XpmSuccess);
 #endif // WXWIN_COMPATIBILITY_2
 
-    return bitmap->Ok();
+      return bitmap->Ok();
   }
-#endif
+#endif // wxUSE_XPM_IN_MSW
 
   return FALSE;
 }