]>
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
7 // Copyright: (c) Robin Dunn
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
22 #include "wx/region.h"
25 #include "wx/dcmemory.h"
26 #include "wx/bitmap.h"
30 // ============================================================================
31 // wxRegionBase implementation
32 // ============================================================================
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 bool wxRegionBase::IsEqual(const wxRegion
& region
) const
40 if ( m_refData
== region
.GetRefData() )
42 // regions are identical, hence equal
46 if ( !m_refData
|| !region
.GetRefData() )
48 // one, but not both, of the regions is invalid
52 return DoIsEqual(region
);
55 // ----------------------------------------------------------------------------
56 // region to/from bitmap conversions
57 // ----------------------------------------------------------------------------
59 wxBitmap
wxRegionBase::ConvertToBitmap() const
61 wxRect box
= GetBox();
62 wxBitmap
bmp(box
.GetRight() + 1, box
.GetBottom() + 1);
65 dc
.SetBackground(*wxBLACK_BRUSH
);
67 dc
.SetDeviceClippingRegion(*static_cast<const wxRegion
*>(this));
68 dc
.SetBackground(*wxWHITE_BRUSH
);
70 dc
.SelectObject(wxNullBitmap
);
76 static bool DoRegionUnion(wxRegionBase
& region
,
83 unsigned char hiR
, hiG
, hiB
;
85 hiR
= (unsigned char)wxMin(0xFF, loR
+ tolerance
);
86 hiG
= (unsigned char)wxMin(0xFF, loG
+ tolerance
);
87 hiB
= (unsigned char)wxMin(0xFF, loB
+ tolerance
);
89 // Loop through the image row by row, pixel by pixel, building up
90 // rectangles to add to the region.
91 int width
= image
.GetWidth();
92 int height
= image
.GetHeight();
93 for (int y
=0; y
< height
; y
++)
99 for (int x
=0; x
< width
; x
++)
101 // search for a continuous range of non-transparent pixels
105 unsigned char R
= image
.GetRed(x
,y
);
106 unsigned char G
= image
.GetGreen(x
,y
);
107 unsigned char B
= image
.GetBlue(x
,y
);
108 if (( R
>= loR
&& R
<= hiR
) &&
109 ( G
>= loG
&& G
<= hiG
) &&
110 ( B
>= loB
&& B
<= hiB
)) // It's transparent
115 // Add the run of non-transparent pixels (if any) to the region
128 bool wxRegionBase::Union(const wxBitmap
& bmp
)
132 wxImage image
= bmp
.ConvertToImage();
133 wxASSERT_MSG( image
.HasMask(), wxT("wxBitmap::ConvertToImage doesn't preserve mask?") );
134 return DoRegionUnion(*this, image
,
136 image
.GetMaskGreen(),
142 return Union(0, 0, bmp
.GetWidth(), bmp
.GetHeight());
146 bool wxRegionBase::Union(const wxBitmap
& bmp
,
147 const wxColour
& transColour
,
150 wxImage image
= bmp
.ConvertToImage();
151 return DoRegionUnion(*this, image
,
158 #endif // wxUSE_IMAGE
160 #ifdef wxHAS_REGION_COMBINE
161 // ============================================================================
162 // wxRegionWithCombine
163 // ============================================================================
165 // implement some wxRegionBase pure virtuals in terms of Combine()
166 bool wxRegionWithCombine::DoUnionWithRect(const wxRect
& rect
)
168 return Combine(rect
, wxRGN_OR
);
171 bool wxRegionWithCombine::DoUnionWithRegion(const wxRegion
& region
)
173 return DoCombine(region
, wxRGN_OR
);
176 bool wxRegionWithCombine::DoIntersect(const wxRegion
& region
)
178 return DoCombine(region
, wxRGN_AND
);
181 bool wxRegionWithCombine::DoSubtract(const wxRegion
& region
)
183 return DoCombine(region
, wxRGN_DIFF
);
186 bool wxRegionWithCombine::DoXor(const wxRegion
& region
)
188 return DoCombine(region
, wxRGN_XOR
);
191 #endif // wxHAS_REGION_COMBINE