- new GUI lib
[enigma2.git] / 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         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         static eRect emptyRect() { return eRect(0, 0, 0, 0); }
108         static eRect invalidRect() { return eRect(); }
109         
110 private:
111         int x1;
112         int y1;
113         int x2;
114         int y2;
115 };
116
117 bool operator==( const eRect &, const eRect & );
118 bool operator!=( const eRect &, const eRect & );
119
120
121 /*****************************************************************************
122   eRect inline member functions
123  *****************************************************************************/
124
125 inline eRect::eRect( int left, int top, int width, int height )
126 {
127         x1 = left;
128         y1 = top;
129         x2 = left+width;
130         y2 = top+height;
131 }
132
133 inline bool eRect::empty() const
134 { return x1 == x2 && y1 == y2; }
135
136 inline bool eRect::valid() const
137 { return x1 <= x2 && y1 <= y2; }
138
139 inline int eRect::left() const
140 { return x1; }
141
142 inline int eRect::top() const
143 { return y1; }
144
145 inline int eRect::right() const
146 { return x2; }
147
148 inline int eRect::bottom() const
149 { return y2; }
150
151 inline int &eRect::rLeft()
152 { return x1; }
153
154 inline int & eRect::rTop()
155 { return y1; }
156
157 inline int & eRect::rRight()
158 { return x2; }
159
160 inline int & eRect::rBottom()
161 { return y2; }
162
163 inline int eRect::x() const
164 { return x1; }
165
166 inline int eRect::y() const
167 { return y1; }
168
169 inline void eRect::setLeft( int pos )
170 { x1 = pos; }
171
172 inline void eRect::setTop( int pos )
173 { y1 = pos; }
174
175 inline void eRect::setRight( int pos )
176 { x2 = pos; }
177
178 inline void eRect::setBottom( int pos )
179 { y2 = pos; }
180
181 inline void eRect::setX( int x )
182 { x1 = x; }
183
184 inline void eRect::setY( int y )
185 { y1 = y; }
186
187 inline ePoint eRect::topLeft() const
188 { return ePoint(x1, y1); }
189
190 inline ePoint eRect::bottomRight() const
191 { return ePoint(x2, y2); }
192
193 inline ePoint eRect::topRight() const
194 { return ePoint(x2, y1); }
195
196 inline ePoint eRect::bottomLeft() const
197 { return ePoint(x1, y2); }
198
199 inline ePoint eRect::center() const
200 { return ePoint((x1+x2)/2, (y1+y2)/2); }
201
202 inline int eRect::width() const
203 { return  x2 - x1; }
204
205 inline int eRect::height() const
206 { return  y2 - y1; }
207
208 inline eSize eRect::size() const
209 { return eSize(x2-x1, y2-y1); }
210
211 inline bool eRect::contains( int x, int y) const
212 {
213         return (x >= x1) && (x < x2) && (y >= y1) && (y < y2);
214 }
215
216 #endif // eRect_H