import of enigma2
[vuplus_dvbapp] / lib / gdi / esize.h
1 #ifndef ESIZE_H
2 #define ESIZE_H
3
4 #include <iostream>
5
6 #define MIN(a,b) (a < b ? a : b)
7 #define MAX(a,b) (a > b ? a : b)
8
9 class eSize
10 {
11 public:
12         eSize();
13         eSize( int w, int h );
14
15         bool isNull()   const;
16         bool isEmpty()  const;
17         bool isValid()  const;
18
19         int width()     const;
20         int height()    const;
21         void setWidth( int w );
22         void setHeight( int h );
23         void transpose();
24
25         eSize expandedTo( const eSize & ) const;
26         eSize boundedTo( const eSize & ) const;
27
28         int &rwidth();
29         int &rheight();
30
31         eSize &operator+=( const eSize & );
32         eSize &operator-=( const eSize & );
33         eSize &operator*=( int c );
34         eSize &operator*=( double c );
35         eSize &operator/=( int c );
36         eSize &operator/=( double c );
37
38         friend inline bool      operator==( const eSize &, const eSize & );
39         friend inline bool      operator!=( const eSize &, const eSize & );
40         friend inline eSize operator+( const eSize &, const eSize & );
41         friend inline eSize operator-( const eSize &, const eSize & );
42         friend inline eSize operator*( const eSize &, int );
43         friend inline eSize operator*( int, const eSize & );
44         friend inline eSize operator*( const eSize &, double );
45         friend inline eSize operator*( double, const eSize & );
46         friend inline eSize operator/( const eSize &, int );
47         friend inline eSize operator/( const eSize &, double );
48
49 private:
50     int wd;
51     int ht;
52 };
53
54
55 /*****************************************************************************
56   eSize stream functions
57  *****************************************************************************/
58
59 namespace std
60 {
61         inline ostream &operator<<( ostream &s, const eSize &sz )
62         {
63                 s << sz.width() << sz.height();
64                 return s;
65         }
66
67         inline istream &operator>>( istream &s, eSize &sz )
68         {
69                 s >> sz.rwidth() >> sz.rheight();
70           return s;
71         }
72 }
73
74
75 /*****************************************************************************
76   eSize inline functions
77  *****************************************************************************/
78
79 inline eSize::eSize()
80 { wd = ht = -1; }
81
82 inline eSize::eSize( int w, int h )
83 { wd=w; ht=h; }
84
85 inline bool eSize::isNull() const
86 { return wd==0 && ht==0; }
87
88 inline bool eSize::isEmpty() const
89 { return wd<1 || ht<1; }
90
91 inline bool eSize::isValid() const
92 { return wd>=0 && ht>=0; }
93
94 inline int eSize::width() const
95 { return wd; }
96
97 inline int eSize::height() const
98 { return ht; }
99
100 inline void eSize::setWidth( int w )
101 { wd=w; }
102
103 inline void eSize::setHeight( int h )
104 { ht=h; }
105
106 inline int &eSize::rwidth()
107 { return wd; }
108
109 inline int &eSize::rheight()
110 { return ht; }
111
112 inline eSize &eSize::operator+=( const eSize &s )
113 { wd+=s.wd; ht+=s.ht; return *this; }
114
115 inline eSize &eSize::operator-=( const eSize &s )
116 { wd-=s.wd; ht-=s.ht; return *this; }
117
118 inline eSize &eSize::operator*=( int c )
119 { wd*=c; ht*=c; return *this; }
120
121 inline eSize &eSize::operator*=( double c )
122 { wd=(int)(wd*c); ht=(int)(ht*c); return *this; }
123
124 inline bool operator==( const eSize &s1, const eSize &s2 )
125 { return s1.wd == s2.wd && s1.ht == s2.ht; }
126
127 inline bool operator!=( const eSize &s1, const eSize &s2 )
128 { return s1.wd != s2.wd || s1.ht != s2.ht; }
129
130 inline eSize operator+( const eSize & s1, const eSize & s2 )
131 { return eSize(s1.wd+s2.wd, s1.ht+s2.ht); }
132
133 inline eSize operator-( const eSize &s1, const eSize &s2 )
134 { return eSize(s1.wd-s2.wd, s1.ht-s2.ht); }
135
136 inline eSize operator*( const eSize &s, int c )
137 { return eSize(s.wd*c, s.ht*c); }
138
139 inline eSize operator*( int c, const eSize &s )
140 {  return eSize(s.wd*c, s.ht*c); }
141
142 inline eSize operator*( const eSize &s, double c )
143 { return eSize((int)(s.wd*c), (int)(s.ht*c)); }
144
145 inline eSize operator*( double c, const eSize &s )
146 { return eSize((int)(s.wd*c), (int)(s.ht*c)); }
147
148 inline eSize &eSize::operator/=( int c )
149 {
150         wd/=c; ht/=c;
151         return *this;
152 }
153
154 inline eSize &eSize::operator/=( double c )
155 {
156         wd=(int)(wd/c); ht=(int)(ht/c);
157         return *this;
158 }
159
160 inline eSize operator/( const eSize &s, int c )
161 {
162         return eSize(s.wd/c, s.ht/c);
163 }
164
165 inline eSize operator/( const eSize &s, double c )
166 {
167         return eSize((int)(s.wd/c), (int)(s.ht/c));
168 }
169
170 inline eSize eSize::expandedTo( const eSize & otherSize ) const
171 {
172         return eSize( MAX(wd,otherSize.wd), MAX(ht,otherSize.ht) );
173 }
174
175 inline eSize eSize::boundedTo( const eSize & otherSize ) const
176 {
177         return eSize( MIN(wd,otherSize.wd), MIN(ht,otherSize.ht) );
178 }
179
180 inline void eSize::transpose()
181 {
182         int tmp = wd;
183         wd = ht;
184         ht = tmp;
185 }
186
187 #endif // ESIZE_H