OpenCPN Partial API docs
OCPNRegion.h
1 /***************************************************************************
2  *
3  * Project: OpenCPN
4  *
5  ***************************************************************************
6  * Portions Copyright (C) 2010 by David S. Register *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
22  ***************************************************************************
23  */
24 
26 // Author: Robert Roebling
27 // Copyright: (c) 1998 Robert Roebling
28 // Licence: wxWindows licence
30 
31 #ifndef _OCPN_REGION_H_
32 #define _OCPN_REGION_H_
33 
34 #include <wx/wxprec.h>
35 
36 #ifndef WX_PRECOMP
37 #include <wx/wx.h>
38 #endif // precompiled headers
39 
40 //#if defined(__WXOSX__)
41 #define USE_NEW_REGION
42 //#endif
43 
44 // ----------------------------------------------------------------------------
45 // OCPNRegion
46 // ----------------------------------------------------------------------------
47 
48 class OCPNRegion : public
49 #ifdef USE_NEW_REGION
50  wxObject
51 #else
52  wxRegion
53 #endif
54 {
55 public:
56  OCPNRegion() {}
57 
58  OCPNRegion(wxCoord x, wxCoord y, wxCoord w, wxCoord h);
59  OCPNRegion(const wxPoint& topLeft, const wxPoint& bottomRight);
60  OCPNRegion(const wxRect& rect);
61  OCPNRegion(const wxRegion& region);
62  OCPNRegion(size_t n, const wxPoint* points, int fillStyle = wxODDEVEN_RULE);
63 
64  virtual ~OCPNRegion();
65 
66  wxRegion* GetNew_wxRegion() const;
67 
68 #ifdef USE_NEW_REGION
69 
70  // common part of ctors for a rectangle region
71  void InitRect(wxCoord x, wxCoord y, wxCoord w, wxCoord h);
72 
73  // operators
74  // ---------
75  bool operator==(const OCPNRegion& region) const { return ODoIsEqual(region); }
76  bool operator!=(const OCPNRegion& region) const { return !(*this == region); }
77 
78  bool IsOk() const { return m_refData != NULL; }
79  bool Ok() const { return IsOk(); }
80 
81  // Get the bounding box
82  bool GetBox(wxCoord& x, wxCoord& y, wxCoord& w, wxCoord& h) const {
83  return ODoGetBox(x, y, w, h);
84  }
85  wxRect GetBox() const {
86  wxCoord x, y, w, h;
87  return ODoGetBox(x, y, w, h) ? wxRect(x, y, w, h) : wxRect();
88  }
89 
90  // Test if the given point or rectangle is inside this region
91  wxRegionContain Contains(wxCoord x, wxCoord y) const {
92  return ODoContainsPoint(x, y);
93  }
94  wxRegionContain Contains(const wxPoint& pt) const {
95  return ODoContainsPoint(pt.x, pt.y);
96  }
97  wxRegionContain Contains(wxCoord x, wxCoord y, wxCoord w, wxCoord h) const {
98  return ODoContainsRect(wxRect(x, y, w, h));
99  }
100  wxRegionContain Contains(const wxRect& rect) const {
101  return ODoContainsRect(rect);
102  }
103 
104  // Is region equal (i.e. covers the same area as another one)?
105  bool IsEqual(const OCPNRegion& region) const;
106 
107  // OCPNRegionBase methods
108  virtual void Clear();
109  virtual bool IsEmpty() const;
110  bool Empty() const { return IsEmpty(); }
111 
112 public:
113  // OCPNRegion( OGdkRegion *region );
114 
115  void* GetRegion() const;
116 
117  bool Offset(wxCoord x, wxCoord y) { return ODoOffset(x, y); }
118  bool Offset(const wxPoint& pt) { return ODoOffset(pt.x, pt.y); }
119  bool Intersect(const OCPNRegion& region) { return ODoIntersect(region); }
120  bool Union(const OCPNRegion& region) { return ODoUnionWithRegion(region); }
121  bool Union(wxCoord x, wxCoord y, wxCoord w, wxCoord h) {
122  return ODoUnionWithRect(wxRect(x, y, w, h));
123  }
124  bool Union(const wxRect& rect) { return ODoUnionWithRect(rect); }
125  bool Subtract(const OCPNRegion& region) { return ODoSubtract(region); }
126 
127 protected:
128  // ref counting code
129  virtual wxObjectRefData* CreateRefData() const;
130  virtual wxObjectRefData* CloneRefData(const wxObjectRefData* data) const;
131 
132  // wxRegionBase pure virtuals
133  virtual bool ODoIsEqual(const OCPNRegion& region) const;
134  virtual bool ODoGetBox(wxCoord& x, wxCoord& y, wxCoord& w, wxCoord& h) const;
135  virtual wxRegionContain ODoContainsPoint(wxCoord x, wxCoord y) const;
136  virtual wxRegionContain ODoContainsRect(const wxRect& rect) const;
137 
138  virtual bool ODoOffset(wxCoord x, wxCoord y);
139  virtual bool ODoUnionWithRect(const wxRect& rect);
140  virtual bool ODoUnionWithRegion(const OCPNRegion& region);
141  virtual bool ODoIntersect(const OCPNRegion& region);
142  virtual bool ODoSubtract(const OCPNRegion& region);
143  // virtual bool DoXor(const OCPNRegion& region);
144 
145 #endif
146 
147 private:
148  DECLARE_DYNAMIC_CLASS(OCPNRegion)
149 };
150 
151 // ----------------------------------------------------------------------------
152 // OCPNRegionIterator: decomposes a region into rectangles
153 // ----------------------------------------------------------------------------
154 
156 public:
158  OCPNRegionIterator(const OCPNRegion& region);
159  virtual ~OCPNRegionIterator();
160 
161  void Reset();
162  void Reset(const OCPNRegion& region);
163 
164  bool HaveRects() const;
165  void NextRect(void);
166  wxRect GetRect() const;
167 
168 private:
169 #ifdef USE_NEW_REGION
170  void Init();
171  void CreateRects(const OCPNRegion& r);
172 
173  size_t m_current;
174  OCPNRegion m_region;
175 
176  wxRect* m_rects;
177  size_t m_numRects;
178 #else
179  wxRegionIterator* m_ri;
180 #endif
181 };
182 
183 #endif
184 // _OCPN_REGION_H_