initialize self.list
[enigma2.git] / lib / gdi / erect.cpp
1 #include <lib/gdi/erect.h>
2
3 /*****************************************************************************
4   eRect member functions
5  *****************************************************************************/
6
7 eRect::eRect( const ePoint &topLeft, const ePoint &bottomRight )
8 {
9         x1 = topLeft.x();
10         y1 = topLeft.y();
11         x2 = bottomRight.x();
12         y2 = bottomRight.y();
13 }
14
15 eRect eRect::normalize() const
16 {
17         eRect r;
18         if ( x2 < x1 ) {                                // swap bad x values
19         r.x1 = x2;
20         r.x2 = x1;
21         } else {
22         r.x1 = x1;
23         r.x2 = x2;
24         }
25         if ( y2 < y1 ) {                                // swap bad y values
26         r.y1 = y2;
27         r.y2 = y1;
28         } else {
29         r.y1 = y1;
30         r.y2 = y2;
31         }
32         return r;
33 }
34
35 void eRect::rect( int *x, int *y, int *w, int *h ) const
36 {
37         *x = x1;
38         *y = y1;
39         *w = x2-x1;
40         *h = y2-y1;
41 }
42
43 void eRect::coords( int *xp1, int *yp1, int *xp2, int *yp2 ) const
44 {
45         *xp1 = x1;
46         *yp1 = y1;
47         *xp2 = x2;
48         *yp2 = y2;
49 }
50
51 void eRect::moveTopLeft( const ePoint &p )
52 {
53         x2 += (p.x() - x1);
54         y2 += (p.y() - y1);
55         x1 = p.x();
56         y1 = p.y();
57 }
58
59 void eRect::moveBottomRight( const ePoint &p )
60 {
61         x1 += (p.x() - x2);
62         y1 += (p.y() - y2);
63         x2 = p.x();
64         y2 = p.y();
65 }
66
67 void eRect::moveTopRight( const ePoint &p )
68 {
69         x1 += (p.x() - x2);
70         y2 += (p.y() - y1);
71         x2 = p.x();
72         y1 = p.y();
73 }
74
75 void eRect::moveBottomLeft( const ePoint &p )
76 {
77         x2 += (p.x() - x1);
78         y1 += (p.y() - y2);
79         x1 = p.x();
80         y2 = p.y();
81 }
82
83 void eRect::moveCenter( const ePoint &p )
84 {
85         int w = x2 - x1;
86         int h = y2 - y1;
87         x1 = (p.x() - w/2);
88         y1 = (p.y() - h/2);
89         x2 = x1 + w;
90         y2 = y1 + h;
91 }
92
93 void eRect::setRect( int x, int y, int w, int h )
94 {
95         x1 = x;
96         y1 = y;
97         x2 = (x+w);
98         y2 = (y+h);
99 }
100
101 void eRect::setCoords( int xp1, int yp1, int xp2, int yp2 )
102 {
103         x1 = xp1;
104         y1 = yp1;
105         x2 = xp2;
106         y2 = yp2;
107 }
108
109 void eRect::setWidth( int w )
110 {
111         x2 = x1 + w;
112 }
113
114 void eRect::setHeight( int h )
115 {
116         y2 = y1 + h;
117 }
118
119 void eRect::setSize( const eSize &s )
120 {
121         x2 = s.width() +x1;
122         y2 = s.height()+y1;
123 }
124
125 bool eRect::contains( const ePoint &p) const
126 {
127         return p.x() >= x1 && p.x() < x2 &&
128                    p.y() >= y1 && p.y() < y2;
129 }
130
131 bool eRect::contains( const eRect &r) const
132 {
133         return r.x1 >= x1 &&
134                                  r.x2 <= x2 &&
135                                  r.y1 >= y1 &&
136                                  r.y2 <= y2;
137 }
138
139 eRect& eRect::operator|=(const eRect &r)
140 {
141         *this = *this | r;
142         return *this;
143 }
144
145 eRect& eRect::operator&=(const eRect &r)
146 {
147         *this = *this & r;
148         return *this;
149 }
150
151 eRect eRect::operator|(const eRect &r) const
152 {
153         if ( valid() ) {
154         if ( r.valid() ) {
155                 eRect tmp;
156                 tmp.setLeft(   MIN( x1, r.x1 ) );
157                 tmp.setRight(  MAX( x2, r.x2 ) );
158                 tmp.setTop(        MIN( y1, r.y1 ) );
159                 tmp.setBottom( MAX( y2, r.y2 ) );
160                 return tmp;
161         } else {
162                 return *this;
163         }
164         } else {
165         return r;
166         }
167 }
168
169 eRect eRect::unite( const eRect &r ) const
170 {
171         return *this | r;
172 }
173
174 eRect eRect::operator&( const eRect &r ) const
175 {
176         eRect tmp;
177         tmp.x1 = MAX( x1, r.x1 );
178         tmp.x2 = MIN( x2, r.x2 );
179         tmp.y1 = MAX( y1, r.y1 );
180         tmp.y2 = MIN( y2, r.y2 );
181         return tmp;
182 }
183
184 eRect eRect::intersect( const eRect &r ) const
185 {
186         return *this & r;
187 }
188
189 bool eRect::intersects( const eRect &r ) const
190 {
191         return ( MAX( x1, r.x1 ) < MIN( x2, r.x2 ) &&
192                  MAX( y1, r.y1 ) < MIN( y2, r.y2 ) );
193 }
194
195 bool operator==( const eRect &r1, const eRect &r2 )
196 {
197         return r1.x1==r2.x1 && r1.x2==r2.x2 && r1.y1==r2.y1 && r1.y2==r2.y2;
198 }
199
200 bool operator!=( const eRect &r1, const eRect &r2 )
201 {
202         return r1.x1!=r2.x1 || r1.x2!=r2.x2 || r1.y1!=r2.y1 || r1.y2!=r2.y2;
203 }