X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/1904aa72f0df85f2f02af417eff35639d4f1b93b..439c7eae9b77cc6c0414ccae52e688d626c86851:/src/common/rgncmn.cpp?ds=sidebyside diff --git a/src/common/rgncmn.cpp b/src/common/rgncmn.cpp index 581ecbf68e..bd8281850b 100644 --- a/src/common/rgncmn.cpp +++ b/src/common/rgncmn.cpp @@ -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,31 +9,55 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) -#pragma implementation "rgncmn.h" -#endif - +// ============================================================================ +// declarations +// ============================================================================ // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" #ifdef __BORLANDC__ -#pragma hdrstop + #pragma hdrstop #endif #include "wx/region.h" -#include "wx/bitmap.h" -#if wxUSE_IMAGE -#include "wx/image.h" -#endif -#include "wx/dcmemory.h" +#ifndef WX_PRECOMP + #include "wx/dcmemory.h" + #include "wx/bitmap.h" + #include "wx/image.h" +#endif //WX_PRECOMP -//--------------------------------------------------------------------------- +// ============================================================================ +// wxRegionBase implementation +// ============================================================================ +// ---------------------------------------------------------------------------- +// region comparision +// ---------------------------------------------------------------------------- +bool wxRegionBase::IsEqual(const wxRegion& region) const +{ + if ( m_refData == region.GetRefData() ) + { + // regions are identical, hence equal + return true; + } -wxBitmap wxRegion::ConvertToBitmap() const + if ( !m_refData || !region.GetRefData() ) + { + // one, but not both, of the regions is invalid + return false; + } + + return DoIsEqual(region); +} + +// ---------------------------------------------------------------------------- +// region to/from bitmap conversions +// ---------------------------------------------------------------------------- + +wxBitmap wxRegionBase::ConvertToBitmap() const { wxRect box = GetBox(); wxBitmap bmp(box.GetRight(), box.GetBottom()); @@ -41,51 +65,27 @@ wxBitmap wxRegion::ConvertToBitmap() const dc.SelectObject(bmp); dc.SetBackground(*wxBLACK_BRUSH); dc.Clear(); - dc.SetClippingRegion(*this); + dc.SetDeviceClippingRegion(*static_cast(this)); dc.SetBackground(*wxWHITE_BRUSH); dc.Clear(); dc.SelectObject(wxNullBitmap); return bmp; } -//--------------------------------------------------------------------------- - #if wxUSE_IMAGE -bool wxRegion::Union(const wxBitmap& bmp, - const wxColour& transColour, - int tolerance) -#else -bool wxRegion::Union(const wxBitmap& WXUNUSED(bmp), - const wxColour& WXUNUSED(transColour), - int WXUNUSED(tolerance)) -#endif + +static bool DoRegionUnion(wxRegionBase& region, + const wxImage& image, + unsigned char loR, + unsigned char loG, + unsigned char loB, + int tolerance) { -#if wxUSE_IMAGE - 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. @@ -117,16 +117,76 @@ bool wxRegion::Union(const wxBitmap& WXUNUSED(bmp), if (x > x0) { rect.x = x0; rect.width = x - x0; - Union(rect); + region.Union(rect); } } } return true; -#else - // No wxImage support - return false; -#endif } -//--------------------------------------------------------------------------- + +bool wxRegionBase::Union(const wxBitmap& bmp) +{ + if (bmp.GetMask()) + { + wxImage image = bmp.ConvertToImage(); + wxASSERT_MSG( image.HasMask(), wxT("wxBitmap::ConvertToImage doesn't preserve mask?") ); + return DoRegionUnion(*this, image, + image.GetMaskRed(), + image.GetMaskGreen(), + image.GetMaskBlue(), + 0); + } + else + { + return Union(0, 0, bmp.GetWidth(), bmp.GetHeight()); + } +} + +bool wxRegionBase::Union(const wxBitmap& bmp, + const wxColour& transColour, + int tolerance) +{ + wxImage image = bmp.ConvertToImage(); + return DoRegionUnion(*this, image, + transColour.Red(), + transColour.Green(), + transColour.Blue(), + tolerance); +} + +#endif // wxUSE_IMAGE + +#ifdef wxHAS_REGION_COMBINE +// ============================================================================ +// wxRegionWithCombine +// ============================================================================ + +// implement some wxRegionBase pure virtuals in terms of Combine() +bool wxRegionWithCombine::DoUnionWithRect(const wxRect& rect) +{ + return Combine(rect, wxRGN_OR); +} + +bool wxRegionWithCombine::DoUnionWithRegion(const wxRegion& region) +{ + return DoCombine(region, wxRGN_OR); +} + +bool wxRegionWithCombine::DoIntersect(const wxRegion& region) +{ + return DoCombine(region, wxRGN_AND); +} + +bool wxRegionWithCombine::DoSubtract(const wxRegion& region) +{ + return DoCombine(region, wxRGN_DIFF); +} + +bool wxRegionWithCombine::DoXor(const wxRegion& region) +{ + return DoCombine(region, wxRGN_XOR); +} + +#endif // wxHAS_REGION_COMBINE