region.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/region.h
  3. // Purpose: Base header for wxRegion
  4. // Author: Julian Smart
  5. // Modified by:
  6. // Created:
  7. // Copyright: (c) Julian Smart
  8. // Licence: wxWindows Licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_REGION_H_BASE_
  11. #define _WX_REGION_H_BASE_
  12. #include "wx/gdiobj.h"
  13. #include "wx/gdicmn.h"
  14. class WXDLLIMPEXP_FWD_CORE wxBitmap;
  15. class WXDLLIMPEXP_FWD_CORE wxColour;
  16. class WXDLLIMPEXP_FWD_CORE wxRegion;
  17. // ----------------------------------------------------------------------------
  18. // constants
  19. // ----------------------------------------------------------------------------
  20. // result of wxRegion::Contains() call
  21. enum wxRegionContain
  22. {
  23. wxOutRegion = 0,
  24. wxPartRegion = 1,
  25. wxInRegion = 2
  26. };
  27. // these constants are used with wxRegion::Combine() in the ports which have
  28. // this method
  29. enum wxRegionOp
  30. {
  31. // Creates the intersection of the two combined regions.
  32. wxRGN_AND,
  33. // Creates a copy of the region
  34. wxRGN_COPY,
  35. // Combines the parts of first region that are not in the second one
  36. wxRGN_DIFF,
  37. // Creates the union of two combined regions.
  38. wxRGN_OR,
  39. // Creates the union of two regions except for any overlapping areas.
  40. wxRGN_XOR
  41. };
  42. // ----------------------------------------------------------------------------
  43. // wxRegionBase defines wxRegion API
  44. // ----------------------------------------------------------------------------
  45. class WXDLLIMPEXP_CORE wxRegionBase : public wxGDIObject
  46. {
  47. public:
  48. // ctors
  49. // -----
  50. // none are defined here but the following should be available:
  51. #if 0
  52. wxRegion();
  53. wxRegion(wxCoord x, wxCoord y, wxCoord w, wxCoord h);
  54. wxRegion(const wxPoint& topLeft, const wxPoint& bottomRight);
  55. wxRegion(const wxRect& rect);
  56. wxRegion(size_t n, const wxPoint *points, wxPolygonFillMode fillStyle = wxODDEVEN_RULE);
  57. wxRegion(const wxBitmap& bmp);
  58. wxRegion(const wxBitmap& bmp, const wxColour& transp, int tolerance = 0);
  59. #endif // 0
  60. // operators
  61. // ---------
  62. bool operator==(const wxRegion& region) const { return IsEqual(region); }
  63. bool operator!=(const wxRegion& region) const { return !(*this == region); }
  64. // accessors
  65. // ---------
  66. // Is region empty?
  67. virtual bool IsEmpty() const = 0;
  68. bool Empty() const { return IsEmpty(); }
  69. // Is region equal (i.e. covers the same area as another one)?
  70. bool IsEqual(const wxRegion& region) const;
  71. // Get the bounding box
  72. bool GetBox(wxCoord& x, wxCoord& y, wxCoord& w, wxCoord& h) const
  73. { return DoGetBox(x, y, w, h); }
  74. wxRect GetBox() const
  75. {
  76. wxCoord x, y, w, h;
  77. return DoGetBox(x, y, w, h) ? wxRect(x, y, w, h) : wxRect();
  78. }
  79. // Test if the given point or rectangle is inside this region
  80. wxRegionContain Contains(wxCoord x, wxCoord y) const
  81. { return DoContainsPoint(x, y); }
  82. wxRegionContain Contains(const wxPoint& pt) const
  83. { return DoContainsPoint(pt.x, pt.y); }
  84. wxRegionContain Contains(wxCoord x, wxCoord y, wxCoord w, wxCoord h) const
  85. { return DoContainsRect(wxRect(x, y, w, h)); }
  86. wxRegionContain Contains(const wxRect& rect) const
  87. { return DoContainsRect(rect); }
  88. // operations
  89. // ----------
  90. virtual void Clear() = 0;
  91. // Move the region
  92. bool Offset(wxCoord x, wxCoord y)
  93. { return DoOffset(x, y); }
  94. bool Offset(const wxPoint& pt)
  95. { return DoOffset(pt.x, pt.y); }
  96. // Union rectangle or region with this region.
  97. bool Union(wxCoord x, wxCoord y, wxCoord w, wxCoord h)
  98. { return DoUnionWithRect(wxRect(x, y, w, h)); }
  99. bool Union(const wxRect& rect)
  100. { return DoUnionWithRect(rect); }
  101. bool Union(const wxRegion& region)
  102. { return DoUnionWithRegion(region); }
  103. #if wxUSE_IMAGE
  104. // Use the non-transparent pixels of a wxBitmap for the region to combine
  105. // with this region. First version takes transparency from bitmap's mask,
  106. // second lets the user specify the colour to be treated as transparent
  107. // along with an optional tolerance value.
  108. // NOTE: implemented in common/rgncmn.cpp
  109. bool Union(const wxBitmap& bmp);
  110. bool Union(const wxBitmap& bmp, const wxColour& transp, int tolerance = 0);
  111. #endif // wxUSE_IMAGE
  112. // Intersect rectangle or region with this one.
  113. bool Intersect(wxCoord x, wxCoord y, wxCoord w, wxCoord h);
  114. bool Intersect(const wxRect& rect);
  115. bool Intersect(const wxRegion& region)
  116. { return DoIntersect(region); }
  117. // Subtract rectangle or region from this:
  118. // Combines the parts of 'this' that are not part of the second region.
  119. bool Subtract(wxCoord x, wxCoord y, wxCoord w, wxCoord h);
  120. bool Subtract(const wxRect& rect);
  121. bool Subtract(const wxRegion& region)
  122. { return DoSubtract(region); }
  123. // XOR: the union of two combined regions except for any overlapping areas.
  124. bool Xor(wxCoord x, wxCoord y, wxCoord w, wxCoord h);
  125. bool Xor(const wxRect& rect);
  126. bool Xor(const wxRegion& region)
  127. { return DoXor(region); }
  128. // Convert the region to a B&W bitmap with the white pixels being inside
  129. // the region.
  130. wxBitmap ConvertToBitmap() const;
  131. protected:
  132. virtual bool DoIsEqual(const wxRegion& region) const = 0;
  133. virtual bool DoGetBox(wxCoord& x, wxCoord& y, wxCoord& w, wxCoord& h) const = 0;
  134. virtual wxRegionContain DoContainsPoint(wxCoord x, wxCoord y) const = 0;
  135. virtual wxRegionContain DoContainsRect(const wxRect& rect) const = 0;
  136. virtual bool DoOffset(wxCoord x, wxCoord y) = 0;
  137. virtual bool DoUnionWithRect(const wxRect& rect) = 0;
  138. virtual bool DoUnionWithRegion(const wxRegion& region) = 0;
  139. virtual bool DoIntersect(const wxRegion& region) = 0;
  140. virtual bool DoSubtract(const wxRegion& region) = 0;
  141. virtual bool DoXor(const wxRegion& region) = 0;
  142. };
  143. // some ports implement a generic Combine() function while others only
  144. // implement individual wxRegion operations, factor out the common code for the
  145. // ports with Combine() in this class
  146. #if defined(__WXMSW__) || \
  147. ( defined(__WXMAC__) && wxOSX_USE_COCOA_OR_CARBON ) || \
  148. defined(__WXPM__)
  149. #define wxHAS_REGION_COMBINE
  150. class WXDLLIMPEXP_CORE wxRegionWithCombine : public wxRegionBase
  151. {
  152. public:
  153. // these methods are not part of public API as they're not implemented on
  154. // all ports
  155. bool Combine(wxCoord x, wxCoord y, wxCoord w, wxCoord h, wxRegionOp op);
  156. bool Combine(const wxRect& rect, wxRegionOp op);
  157. bool Combine(const wxRegion& region, wxRegionOp op)
  158. { return DoCombine(region, op); }
  159. protected:
  160. // the real Combine() method, to be defined in the derived class
  161. virtual bool DoCombine(const wxRegion& region, wxRegionOp op) = 0;
  162. // implement some wxRegionBase pure virtuals in terms of Combine()
  163. virtual bool DoUnionWithRect(const wxRect& rect);
  164. virtual bool DoUnionWithRegion(const wxRegion& region);
  165. virtual bool DoIntersect(const wxRegion& region);
  166. virtual bool DoSubtract(const wxRegion& region);
  167. virtual bool DoXor(const wxRegion& region);
  168. };
  169. #endif // ports with wxRegion::Combine()
  170. #if defined(__WXMSW__)
  171. #include "wx/msw/region.h"
  172. #elif defined(__WXGTK20__)
  173. #include "wx/gtk/region.h"
  174. #elif defined(__WXGTK__)
  175. #include "wx/gtk1/region.h"
  176. #elif defined(__WXMOTIF__) || defined(__WXX11__)
  177. #include "wx/x11/region.h"
  178. #elif defined(__WXDFB__)
  179. #include "wx/dfb/region.h"
  180. #elif defined(__WXMAC__)
  181. #include "wx/osx/region.h"
  182. #elif defined(__WXCOCOA__)
  183. #include "wx/cocoa/region.h"
  184. #elif defined(__WXPM__)
  185. #include "wx/os2/region.h"
  186. #endif
  187. // ----------------------------------------------------------------------------
  188. // inline functions implementation
  189. // ----------------------------------------------------------------------------
  190. // NB: these functions couldn't be defined in the class declaration as they use
  191. // wxRegion and so can be only defined after including the header declaring
  192. // the real class
  193. inline bool wxRegionBase::Intersect(const wxRect& rect)
  194. {
  195. return DoIntersect(wxRegion(rect));
  196. }
  197. inline bool wxRegionBase::Subtract(const wxRect& rect)
  198. {
  199. return DoSubtract(wxRegion(rect));
  200. }
  201. inline bool wxRegionBase::Xor(const wxRect& rect)
  202. {
  203. return DoXor(wxRegion(rect));
  204. }
  205. // ...and these functions are here because they call the ones above, and its
  206. // not really proper to call an inline function before its defined inline.
  207. inline bool wxRegionBase::Intersect(wxCoord x, wxCoord y, wxCoord w, wxCoord h)
  208. {
  209. return Intersect(wxRect(x, y, w, h));
  210. }
  211. inline bool wxRegionBase::Subtract(wxCoord x, wxCoord y, wxCoord w, wxCoord h)
  212. {
  213. return Subtract(wxRect(x, y, w, h));
  214. }
  215. inline bool wxRegionBase::Xor(wxCoord x, wxCoord y, wxCoord w, wxCoord h)
  216. {
  217. return Xor(wxRect(x, y, w, h));
  218. }
  219. #ifdef wxHAS_REGION_COMBINE
  220. inline bool wxRegionWithCombine::Combine(wxCoord x,
  221. wxCoord y,
  222. wxCoord w,
  223. wxCoord h,
  224. wxRegionOp op)
  225. {
  226. return DoCombine(wxRegion(x, y, w, h), op);
  227. }
  228. inline bool wxRegionWithCombine::Combine(const wxRect& rect, wxRegionOp op)
  229. {
  230. return DoCombine(wxRegion(rect), op);
  231. }
  232. #endif // wxHAS_REGION_COMBINE
  233. #endif // _WX_REGION_H_BASE_