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