]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/rgncmn.cpp
Improved wxDCPen/BrushChangers.
[wxWidgets.git] / src / common / rgncmn.cpp
index c794b59ab9134843232cb49884bb34ed8360e687..a32f24e8cf25a6089db563e2c8bf1e4233a6db0f 100644 (file)
@@ -1,5 +1,5 @@
 /////////////////////////////////////////////////////////////////////////////
-// Name:        rgncmn.cpp
+// Name:        src/common/rgncmn.cpp
 // Purpose:     Methods of wxRegion that have a generic implementation
 // Author:      Robin Dunn
 // Modified by:
@@ -9,21 +9,24 @@
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
-#ifdef __GNUG__
-#pragma implementation "rgncmn.h"
-#endif
-
-
 // For compilers that support precompilation, includes "wx.h".
 #include "wx/wxprec.h"
 
 #ifdef __BORLANDC__
-#pragma hdrstop
+    #pragma hdrstop
 #endif
 
 #include "wx/region.h"
+
+#ifndef WX_PRECOMP
+    #include "wx/dcmemory.h"
+#endif //WX_PRECOMP
+
 #include "wx/bitmap.h"
-#include "wx/dcmemory.h"
+
+#if wxUSE_IMAGE
+    #include "wx/image.h"
+#endif
 
 
 //---------------------------------------------------------------------------
@@ -36,10 +39,10 @@ wxBitmap wxRegion::ConvertToBitmap() const
     wxBitmap bmp(box.GetRight(), box.GetBottom());
     wxMemoryDC dc;
     dc.SelectObject(bmp);
-    dc.SetBackground(*wxWHITE_BRUSH);
+    dc.SetBackground(*wxBLACK_BRUSH);
     dc.Clear();
     dc.SetClippingRegion(*this);
-    dc.SetBackground(*wxBLACK_BRUSH);
+    dc.SetBackground(*wxWHITE_BRUSH);
     dc.Clear();
     dc.SelectObject(wxNullBitmap);
     return bmp;
@@ -47,35 +50,19 @@ wxBitmap wxRegion::ConvertToBitmap() const
 
 //---------------------------------------------------------------------------
 
-bool wxRegion::Union(const wxBitmap& bmp,
-                     const wxColour& transColour,
-                     int   tolerance)
+#if wxUSE_IMAGE
+static bool DoRegionUnion(wxRegion& region,
+                          const wxImage& image,
+                          unsigned char loR,
+                          unsigned char loG,
+                          unsigned char loB,
+                          int tolerance)
 {
-    unsigned char loR, loG, loB;
     unsigned char hiR, hiG, hiB;
 
-    wxCHECK_MSG((bmp.GetMask() != NULL) || transColour.Ok(),
-                FALSE,
-                wxT("Either the bitmap should have a mask or a colour should be given."));
-
-    wxImage image = bmp.ConvertToImage();
-
-    if (image.HasMask())
-    {
-        loR = image.GetMaskRed();
-        loG = image.GetMaskGreen();
-        loB = image.GetMaskBlue();
-    }
-    else
-    {
-        loR = transColour.Red();
-        loG = transColour.Green();
-        loB = transColour.Blue();
-    }
-
-    hiR = wxMin(0xFF, loR + tolerance);
-    hiG = wxMin(0xFF, loG + tolerance);
-    hiB = wxMin(0xFF, loB + tolerance);
+    hiR = (unsigned char)wxMin(0xFF, loR + tolerance);
+    hiG = (unsigned char)wxMin(0xFF, loG + tolerance);
+    hiB = (unsigned char)wxMin(0xFF, loB + tolerance);
 
     // Loop through the image row by row, pixel by pixel, building up
     // rectangles to add to the region.
@@ -107,12 +94,67 @@ bool wxRegion::Union(const wxBitmap& bmp,
             if (x > x0) {
                 rect.x = x0;
                 rect.width = x - x0;
-                Union(rect);
+                region.Union(rect);
             }
         }
     }
 
-    return TRUE;
+    return true;
+}
+
+
+bool wxRegion::Union(const wxBitmap& bmp)
+{
+#if (!defined(__WXMSW__) || wxUSE_WXDIB)
+    if (bmp.GetMask())
+    {
+        wxImage image = bmp.ConvertToImage();
+        wxASSERT_MSG( image.HasMask(), _T("wxBitmap::ConvertToImage doesn't preserve mask?") );
+        return DoRegionUnion(*this, image,
+                             image.GetMaskRed(),
+                             image.GetMaskGreen(),
+                             image.GetMaskBlue(),
+                             0);
+    }
+    else
+#endif
+    {
+        return Union(0, 0, bmp.GetWidth(), bmp.GetHeight());
+    }
+}
+
+bool wxRegion::Union(const wxBitmap& bmp,
+                     const wxColour& transColour,
+                     int   tolerance)
+{
+#if (!defined(__WXMSW__) || wxUSE_WXDIB)
+    wxImage image = bmp.ConvertToImage();
+    return DoRegionUnion(*this, image,
+                         transColour.Red(),
+                         transColour.Green(),
+                         transColour.Blue(),
+                         tolerance);
+#else
+    return false;
+#endif
+}
+
+#else
+
+bool wxRegion::Union(const wxBitmap& WXUNUSED(bmp))
+{
+    // No wxImage support
+    return false;
+}
+
+bool wxRegion::Union(const wxBitmap& WXUNUSED(bmp),
+                     const wxColour& WXUNUSED(transColour),
+                     int   WXUNUSED(tolerance))
+{
+    // No wxImage support
+    return false;
 }
 
+#endif
+
 //---------------------------------------------------------------------------