]>
git.saurik.com Git - wxWidgets.git/blob - src/common/rgncmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/rgncmn.cpp
3 // Purpose: Methods of wxRegion that have a generic implementation
6 // Created: 27-Mar-2003
8 // Copyright: (c) Robin Dunn
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
23 #include "wx/region.h"
26 #include "wx/dcmemory.h"
27 #include "wx/bitmap.h"
31 // ============================================================================
32 // wxRegionBase implementation
33 // ============================================================================
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 bool wxRegionBase::IsEqual(const wxRegion
& region
) const
41 if ( m_refData
== region
.GetRefData() )
43 // regions are identical, hence equal
47 if ( !m_refData
|| !region
.GetRefData() )
49 // one, but not both, of the regions is invalid
53 return DoIsEqual(region
);
56 // ----------------------------------------------------------------------------
57 // region to/from bitmap conversions
58 // ----------------------------------------------------------------------------
60 wxBitmap
wxRegionBase::ConvertToBitmap() const
62 wxRect box
= GetBox();
63 wxBitmap
bmp(box
.GetRight(), box
.GetBottom());
66 dc
.SetBackground(*wxBLACK_BRUSH
);
68 dc
.SetClippingRegion(*wx_static_cast(const wxRegion
*, this));
69 dc
.SetBackground(*wxWHITE_BRUSH
);
71 dc
.SelectObject(wxNullBitmap
);
77 static bool DoRegionUnion(wxRegionBase
& region
,
84 unsigned char hiR
, hiG
, hiB
;
86 hiR
= (unsigned char)wxMin(0xFF, loR
+ tolerance
);
87 hiG
= (unsigned char)wxMin(0xFF, loG
+ tolerance
);
88 hiB
= (unsigned char)wxMin(0xFF, loB
+ tolerance
);
90 // Loop through the image row by row, pixel by pixel, building up
91 // rectangles to add to the region.
92 int width
= image
.GetWidth();
93 int height
= image
.GetHeight();
94 for (int y
=0; y
< height
; y
++)
100 for (int x
=0; x
< width
; x
++)
102 // search for a continuous range of non-transparent pixels
106 unsigned char R
= image
.GetRed(x
,y
);
107 unsigned char G
= image
.GetGreen(x
,y
);
108 unsigned char B
= image
.GetBlue(x
,y
);
109 if (( R
>= loR
&& R
<= hiR
) &&
110 ( G
>= loG
&& G
<= hiG
) &&
111 ( B
>= loB
&& B
<= hiB
)) // It's transparent
116 // Add the run of non-transparent pixels (if any) to the region
129 bool wxRegionBase::Union(const wxBitmap
& bmp
)
133 wxImage image
= bmp
.ConvertToImage();
134 wxASSERT_MSG( image
.HasMask(), _T("wxBitmap::ConvertToImage doesn't preserve mask?") );
135 return DoRegionUnion(*this, image
,
137 image
.GetMaskGreen(),
143 return Union(0, 0, bmp
.GetWidth(), bmp
.GetHeight());
147 bool wxRegionBase::Union(const wxBitmap
& bmp
,
148 const wxColour
& transColour
,
151 wxImage image
= bmp
.ConvertToImage();
152 return DoRegionUnion(*this, image
,
159 #endif // wxUSE_IMAGE
161 #ifdef wxHAS_REGION_COMBINE
162 // ============================================================================
163 // wxRegionWithCombine
164 // ============================================================================
166 // implement some wxRegionBase pure virtuals in terms of Combine()
167 bool wxRegionWithCombine::DoUnionWithRect(const wxRect
& rect
)
169 return Combine(rect
, wxRGN_OR
);
172 bool wxRegionWithCombine::DoUnionWithRegion(const wxRegion
& region
)
174 return DoCombine(region
, wxRGN_OR
);
177 bool wxRegionWithCombine::DoIntersect(const wxRegion
& region
)
179 return DoCombine(region
, wxRGN_AND
);
182 bool wxRegionWithCombine::DoSubtract(const wxRegion
& region
)
184 return DoCombine(region
, wxRGN_DIFF
);
187 bool wxRegionWithCombine::DoXor(const wxRegion
& region
)
189 return DoCombine(region
, wxRGN_XOR
);
192 #endif // wxHAS_REGION_COMBINE