- disabled gui for a moment
[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() { x1 = y1 = x2 = y2 = 0; }
15         eRect( const ePoint &topleft, const ePoint &bottomright );
16
17         // we use this contructor very often... do it inline...
18         eRect( const ePoint &topleft, const eSize &size )
19         {
20                 x1 = topleft.x();
21                 y1 = topleft.y();
22                 x2 = (x1+size.width());
23                 y2 = (y1+size.height());
24         }
25
26         eRect( int left, int top, int width, int height );
27
28         bool isNull()   const;
29         bool isEmpty()  const;
30         bool isValid()  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         ePoint center()  const;
56
57         void rect( int *x, int *y, int *w, int *h ) const;
58         void coords( int *x1, int *y1, int *x2, int *y2 ) const;
59
60         void moveTopLeft( const ePoint &p );
61         void moveBottomRight( const ePoint &p );
62         void moveTopRight( const ePoint &p );
63         void moveBottomLeft( const ePoint &p );
64         void moveCenter( const ePoint &p );
65
66         void moveBy( int dx, int dy )
67         {
68                 x1 += dx;
69                 y1 += dy;
70                 x2 += dx;
71                 y2 += dy;
72         }
73
74         void moveBy(ePoint r)
75         {
76                 x1 += r.x();
77                 y1 += r.y();
78                 x2 += r.x();
79                 y2 += r.y();
80         }
81
82         void setRect( int x, int y, int w, int h );
83         void setCoords( int x1, int y1, int x2, int y2 );
84
85         eSize size()    const;
86         int width()     const;
87         int height()    const;
88         void setWidth( int w );
89         void setHeight( int h );
90         void setSize( const eSize &s );
91
92         eRect operator|(const eRect &r) const;
93         eRect operator&(const eRect &r) const;
94         eRect& operator|=(const eRect &r);
95         eRect& operator&=(const eRect &r);
96
97         bool contains( const ePoint &p) const;
98         bool contains( int x, int y) const;
99         bool contains( const eRect &r) const;
100         eRect unite( const eRect &r ) const;
101         eRect intersect( const eRect &r ) const;
102         bool intersects( const eRect &r ) const;
103
104         friend bool operator==( const eRect &, const eRect & );
105         friend bool operator!=( const eRect &, const eRect & );
106
107 private:
108         int x1;
109         int y1;
110         int x2;
111         int y2;
112 };
113
114 bool operator==( const eRect &, const eRect & );
115 bool operator!=( const eRect &, const eRect & );
116
117
118 /*****************************************************************************
119   eRect stream functions
120  *****************************************************************************/
121 namespace std
122 {
123         inline ostream &operator<<( ostream & s, const eRect & r )
124         {
125                 s << r.left()  << r.top()
126                   << r.right() << r.bottom();
127
128                 return s;
129         }
130
131         inline istream &operator>>( istream & s, eRect & r )
132         {
133                 int x1, y1, x2, y2;
134                 s >> x1 >> y1 >> x2 >> y2;
135                 r.setCoords( x1, y1, x2, y2 );
136                 return s;
137         }
138 }
139
140 /*****************************************************************************
141   eRect inline member functions
142  *****************************************************************************/
143
144 inline eRect::eRect( int left, int top, int width, int height )
145 {
146         x1 = left;
147         y1 = top;
148         x2 = left+width;
149         y2 = top+height;
150 }
151
152 inline bool eRect::isNull() const
153 { return x2 == x1 && y2 == y1; }
154
155 inline bool eRect::isEmpty() const
156 { return x1 >= x2 || y1 >= y2; }
157
158 inline bool eRect::isValid() const
159 { return x1 <= x2 && y1 <= y2; }
160
161 inline int eRect::left() const
162 { return x1; }
163
164 inline int eRect::top() const
165 { return y1; }
166
167 inline int eRect::right() const
168 { return x2; }
169
170 inline int eRect::bottom() const
171 { return y2; }
172
173 inline int &eRect::rLeft()
174 { return x1; }
175
176 inline int & eRect::rTop()
177 { return y1; }
178
179 inline int & eRect::rRight()
180 { return x2; }
181
182 inline int & eRect::rBottom()
183 { return y2; }
184
185 inline int eRect::x() const
186 { return x1; }
187
188 inline int eRect::y() const
189 { return y1; }
190
191 inline void eRect::setLeft( int pos )
192 { x1 = pos; }
193
194 inline void eRect::setTop( int pos )
195 { y1 = pos; }
196
197 inline void eRect::setRight( int pos )
198 { x2 = pos; }
199
200 inline void eRect::setBottom( int pos )
201 { y2 = pos; }
202
203 inline void eRect::setX( int x )
204 { x1 = x; }
205
206 inline void eRect::setY( int y )
207 { y1 = y; }
208
209 inline ePoint eRect::topLeft() const
210 { return ePoint(x1, y1); }
211
212 inline ePoint eRect::bottomRight() const
213 { return ePoint(x2, y2); }
214
215 inline ePoint eRect::topRight() const
216 { return ePoint(x2, y1); }
217
218 inline ePoint eRect::bottomLeft() const
219 { return ePoint(x1, y2); }
220
221 inline ePoint eRect::center() const
222 { return ePoint((x1+x2)/2, (y1+y2)/2); }
223
224 inline int eRect::width() const
225 { return  x2 - x1; }
226
227 inline int eRect::height() const
228 { return  y2 - y1; }
229
230 inline eSize eRect::size() const
231 { return eSize(x2-x1, y2-y1); }
232
233 inline bool eRect::contains( int x, int y) const
234 {
235         return (x >= x1) && (x < x2) && (y >= y1) && (y < y2);
236 }
237
238 #endif // eRect_H