'invalid' eRects (produced from operator& on non-overlapping-rects) are in fact empty...
[vuplus_dvbapp] / lib / gdi / erect.h
1 #ifndef ERECT_H
2 #define ERECT_H
3
4 #include <lib/gdi/esize.h>
5 #include <lib/gdi/epoint.h>
6
7
8 // x2 = x1 + width  (AND NOT, NEVER, NEVER EVER +1 or -1 !!!!)
9
10 class eRect // rectangle class
11 {
12         friend class gRegion;
13 public:
14                         /* eRect() constructs an INVALID rectangle. */
15         eRect() { x1 = y1 = 0; x2 = y2 = -1; }
16         eRect( const ePoint &topleft, const ePoint &bottomright );
17
18         // we use this contructor very often... do it inline...
19         eRect( const ePoint &topleft, const eSize &size )
20         {
21                 x1 = topleft.x();
22                 y1 = topleft.y();
23                 x2 = (x1+size.width());
24                 y2 = (y1+size.height());
25         }
26
27         eRect( int left, int top, int width, int height );
28
29         bool empty()    const;
30         bool valid()    const;
31         eRect normalize()       const;
32
33         int left()      const;
34         int top()       const;
35         int right()     const;
36         int  bottom()   const;
37         int &rLeft();
38         int &rTop();
39         int &rRight();
40         int &rBottom();
41
42         int x() const;
43         int y() const;
44         void setLeft( int pos );
45         void setTop( int pos );
46         void setRight( int pos );
47         void setBottom( int pos );
48         void setX( int x );
49         void setY( int y );
50
51         ePoint topLeft()         const;
52         ePoint bottomRight() const;
53         ePoint topRight()        const;
54         ePoint bottomLeft()      const;
55
56                 /* the sole intention of these functions
57                    is to allow painting frames without 
58                    messing around with the coordinates.
59                    they point to the last pixel included
60                    in the rectangle (which means that 1 is
61                    subtracted from the right and bottom 
62                    coordinates  */
63         ePoint topLeft1()        const;
64         ePoint bottomRight1() const;
65         ePoint topRight1()       const;
66         ePoint bottomLeft1()     const;
67         ePoint center()  const;
68
69         void rect( int *x, int *y, int *w, int *h ) const;
70         void coords( int *x1, int *y1, int *x2, int *y2 ) const;
71
72         void moveTopLeft( const ePoint &p );
73         void moveBottomRight( const ePoint &p );
74         void moveTopRight( const ePoint &p );
75         void moveBottomLeft( const ePoint &p );
76         void moveCenter( const ePoint &p );
77
78         void moveBy( int dx, int dy )
79         {
80                 x1 += dx;
81                 y1 += dy;
82                 x2 += dx;
83                 y2 += dy;
84         }
85
86         void moveBy(ePoint r)
87         {
88                 x1 += r.x();
89                 y1 += r.y();
90                 x2 += r.x();
91                 y2 += r.y();
92         }
93
94         void setRect( int x, int y, int w, int h );
95         void setCoords( int x1, int y1, int x2, int y2 );
96
97         eSize size()    const;
98         int width()     const;
99         int height()    const;
100         void setWidth( int w );
101         void setHeight( int h );
102         void setSize( const eSize &s );
103
104         eRect operator|(const eRect &r) const;
105         eRect operator&(const eRect &r) const;
106         eRect& operator|=(const eRect &r);
107         eRect& operator&=(const eRect &r);
108
109         bool contains( const ePoint &p) const;
110         bool contains( int x, int y) const;
111         bool contains( const eRect &r) const;
112         eRect unite( const eRect &r ) const;
113         eRect intersect( const eRect &r ) const;
114         bool intersects( const eRect &r ) const;
115
116         friend bool operator==( const eRect &, const eRect & );
117         friend bool operator!=( const eRect &, const eRect & );
118         
119         static eRect emptyRect() { return eRect(0, 0, 0, 0); }
120         static eRect invalidRect() { return eRect(); }
121         
122         inline void scale(int x_n, int x_d, int y_n, int y_d) 
123         {
124                 x1 *= x_n; x1 /= x_d; 
125                 x2 *= x_n; x2 /= x_d; 
126                 y1 *= y_n; y1 /= y_d; 
127                 y2 *= y_n; y2 /= y_d; 
128         }
129         
130 private:
131         int x1;
132         int y1;
133         int x2;
134         int y2;
135 };
136
137 bool operator==( const eRect &, const eRect & );
138 bool operator!=( const eRect &, const eRect & );
139
140
141 /*****************************************************************************
142   eRect inline member functions
143  *****************************************************************************/
144
145 inline eRect::eRect( int left, int top, int width, int height )
146 {
147         x1 = left;
148         y1 = top;
149         x2 = left+width;
150         y2 = top+height;
151 }
152
153 inline bool eRect::empty() const
154 { return x1 >= x2 || y1 >= y2; }
155
156 inline bool eRect::valid() const
157 { return x1 <= x2 && y1 <= y2; }
158
159 inline int eRect::left() const
160 { return x1; }
161
162 inline int eRect::top() const
163 { return y1; }
164
165 inline int eRect::right() const
166 { return x2; }
167
168 inline int eRect::bottom() const
169 { return y2; }
170
171 inline int &eRect::rLeft()
172 { return x1; }
173
174 inline int & eRect::rTop()
175 { return y1; }
176
177 inline int & eRect::rRight()
178 { return x2; }
179
180 inline int & eRect::rBottom()
181 { return y2; }
182
183 inline int eRect::x() const
184 { return x1; }
185
186 inline int eRect::y() const
187 { return y1; }
188
189 inline void eRect::setLeft( int pos )
190 { x1 = pos; }
191
192 inline void eRect::setTop( int pos )
193 { y1 = pos; }
194
195 inline void eRect::setRight( int pos )
196 { x2 = pos; }
197
198 inline void eRect::setBottom( int pos )
199 { y2 = pos; }
200
201 inline void eRect::setX( int x )
202 { x1 = x; }
203
204 inline void eRect::setY( int y )
205 { y1 = y; }
206
207 inline ePoint eRect::topLeft() const
208 { return ePoint(x1, y1); }
209
210 inline ePoint eRect::bottomRight() const
211 { return ePoint(x2, y2); }
212
213 inline ePoint eRect::topRight() const
214 { return ePoint(x2, y1); }
215
216 inline ePoint eRect::bottomLeft() const
217 { return ePoint(x1, y2); }
218
219 inline ePoint eRect::topLeft1() const
220 { return ePoint(x1, y1); }
221
222 inline ePoint eRect::bottomRight1() const
223 { return ePoint(x2-1, y2-1); }
224
225 inline ePoint eRect::topRight1() const
226 { return ePoint(x2-1, y1); }
227
228 inline ePoint eRect::bottomLeft1() const
229 { return ePoint(x1, y2-1); }
230
231 inline ePoint eRect::center() const
232 { return ePoint((x1+x2)/2, (y1+y2)/2); }
233
234 inline int eRect::width() const
235 { return  x2 - x1; }
236
237 inline int eRect::height() const
238 { return  y2 - y1; }
239
240 inline eSize eRect::size() const
241 { return eSize(x2-x1, y2-y1); }
242
243 inline bool eRect::contains( int x, int y) const
244 {
245         return (x >= x1) && (x < x2) && (y >= y1) && (y < y2);
246 }
247
248 #endif // eRect_H