libyui-ncurses
Loading...
Searching...
No Matches
position.h
1/*
2 Copyright (C) 2000-2012 Novell, Inc
3 This library is free software; you can redistribute it and/or modify
4 it under the terms of the GNU Lesser General Public License as
5 published by the Free Software Foundation; either version 2.1 of the
6 License, or (at your option) version 3.0 of the License. This library
7 is distributed in the hope that it will be useful, but WITHOUT ANY
8 WARRANTY; without even the implied warranty of MERCHANTABILITY or
9 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10 License for more details. You should have received a copy of the GNU
11 Lesser General Public License along with this library; if not, write
12 to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13 Floor, Boston, MA 02110-1301 USA
14*/
15
16
17/*-/
18
19 File: position.h
20
21 Author: Michael Andres <ma@suse.de>
22
23/-*/
24
25#ifndef position_h
26#define position_h
27
28#include <iosfwd>
29
31class wpair
32{
33
34 friend std::ostream & operator<<( std::ostream & str, const wpair & obj );
35
36protected:
37
38 int A;
39 int B;
40
41public:
42
44 wpair( int v = 0 ) { A = B = v; }
45
46 wpair( int a, int b ) { A = a; B = b; }
47
48 wpair( const wpair & Rhs ) { A = Rhs.A; B = Rhs.B; }
49
50 virtual ~wpair() {}
51
52protected:
53
54 wpair & operator= ( const wpair & Rhs ) { A = Rhs.A; B = Rhs.B; return *this; }
55
56 wpair & operator+=( const wpair & Rhs ) { A += Rhs.A; B += Rhs.B; return *this; }
57
58 wpair & operator-=( const wpair & Rhs ) { A -= Rhs.A; B -= Rhs.B; return *this; }
59
60 wpair & operator*=( const wpair & Rhs ) { A *= Rhs.A; B *= Rhs.B; return *this; }
61
62 wpair & operator/=( const wpair & Rhs ) { A /= Rhs.A; B /= Rhs.B; return *this; }
63
64 wpair operator+( const wpair & Rhs ) const { return wpair( A + Rhs.A, B + Rhs.B ); }
65
66 wpair operator-( const wpair & Rhs ) const { return wpair( A - Rhs.A, B - Rhs.B ); }
67
68 wpair operator*( const wpair & Rhs ) const { return wpair( A * Rhs.A, B * Rhs.B ); }
69
70 wpair operator/( const wpair & Rhs ) const { return wpair( A / Rhs.A, B / Rhs.B ); }
71
72public:
73
74 bool operator==( const wpair & Rhs ) const { return A == Rhs.A && B == Rhs.B; }
75
76 bool operator!=( const wpair & Rhs ) const { return A != Rhs.A || B != Rhs.B; }
77
78 bool operator> ( const wpair & Rhs ) const { return A > Rhs.A && B > Rhs.B; }
79
80 bool operator< ( const wpair & Rhs ) const { return A < Rhs.A && B < Rhs.B; }
81
82 bool operator>=( const wpair & Rhs ) const { return A >= Rhs.A && B >= Rhs.B; }
83
84 bool operator<=( const wpair & Rhs ) const { return A <= Rhs.A && B <= Rhs.B; }
85
87 wpair between( const wpair & Min, const wpair & Max ) const
88 {
89 return min( max( *this, Min ), Max );
90 }
91
92 static wpair min( const wpair & Lhs, const wpair & Rhs )
93 {
94 return wpair( Lhs.A < Rhs.A ? Lhs.A : Rhs.A,
95 Lhs.B < Rhs.B ? Lhs.B : Rhs.B );
96 }
97
98 static wpair max( const wpair & Lhs, const wpair & Rhs )
99 {
100 return wpair( Lhs.A > Rhs.A ? Lhs.A : Rhs.A,
101 Lhs.B > Rhs.B ? Lhs.B : Rhs.B );
102 }
103
104};
105
106
107
109class wpos : public wpair
110{
111
112public:
113
114 int & L;
115 int & C;
116
117 wpos( int v = 0 ) : wpair( v ), L( A ), C( B ) {}
118
119 wpos( int l, int c ) : wpair( l, c ), L( A ), C( B ) {}
120
121 wpos( const wpair & Rhs ) : wpair( Rhs ), L( A ), C( B ) {}
122
123 wpos( const wpos & Rhs ) : wpair( Rhs ), L( A ), C( B ) {}
124
125 virtual ~wpos() {}
126
127public:
128
129 wpos & operator= ( const wpos & Rhs ) { wpair::operator= ( Rhs ); return *this; }
130
131 wpos & operator+=( const wpair & Rhs ) { wpair::operator+=( Rhs ); return *this; }
132
133 wpos & operator-=( const wpair & Rhs ) { wpair::operator-=( Rhs ); return *this; }
134
135 wpos & operator*=( const wpair & Rhs ) { wpair::operator*=( Rhs ); return *this; }
136
137 wpos & operator/=( const wpair & Rhs ) { wpair::operator/=( Rhs ); return *this; }
138
139 wpos operator+( const wpair & Rhs ) const { return wpair::operator+( Rhs ); }
140
141 wpos operator-( const wpair & Rhs ) const { return wpair::operator-( Rhs ); }
142
143 wpos operator*( const wpair & Rhs ) const { return wpair::operator*( Rhs ); }
144
145 wpos operator/( const wpair & Rhs ) const { return wpair::operator/( Rhs ); }
146};
147
148extern std::ostream & operator<<( std::ostream & str, const wpos & obj );
149
150
151
153class wsze : public wpair
154{
155
156public:
157
158 int & H;
159 int & W;
160
161 wsze( int v = 0 ) : wpair( v ), H( A ), W( B ) {}
162
163 wsze( int h, int w ) : wpair( h, w ), H( A ), W( B ) {}
164
165 wsze( const wpair & Rhs ) : wpair( Rhs ), H( A ), W( B ) {}
166
167 wsze( const wsze & Rhs ) : wpair( Rhs ), H( A ), W( B ) {}
168
169 virtual ~wsze() {}
170
171 wsze & operator= ( const wsze & Rhs ) { wpair::operator= ( Rhs ); return *this; }
172
173 wsze & operator+=( const wpair & Rhs ) { wpair::operator+=( Rhs ); return *this; }
174
175 wsze & operator-=( const wpair & Rhs ) { wpair::operator-=( Rhs ); return *this; }
176
177 wsze & operator*=( const wpair & Rhs ) { wpair::operator*=( Rhs ); return *this; }
178
179 wsze & operator/=( const wpair & Rhs ) { wpair::operator/=( Rhs ); return *this; }
180
181 wsze operator+( const wpair & Rhs ) const { return wpair::operator+( Rhs ); }
182
183 wsze operator-( const wpair & Rhs ) const { return wpair::operator-( Rhs ); }
184
185 wsze operator*( const wpair & Rhs ) const { return wpair::operator*( Rhs ); }
186
187 wsze operator/( const wpair & Rhs ) const { return wpair::operator/( Rhs ); }
188};
189
190extern std::ostream & operator<<( std::ostream & str, const wsze & obj );
191
193class wrect
194{
195
196public:
197
198 wpos Pos;
199 wsze Sze;
200
201 wrect() : Pos( 0 ), Sze( 0 ) {}
202
203 wrect( const wpos & pos, const wsze & sze ) : Pos( pos ), Sze( sze ) {}
204
205 virtual ~wrect() {}
206
207public:
208
209 bool operator==( const wrect & Rhs ) const
210 {
211 return Pos == Rhs.Pos && Sze == Rhs.Sze;
212 }
213
214 bool operator!=( const wrect & Rhs ) const { return !operator==( Rhs ); }
215
216
217 wrect inside() const
218 {
219 wpos incpos( 1 );
220 wsze decsze( 2 );
221
222 if ( Sze.H < 2 )
223 incpos.L = decsze.H = 0;
224
225 if ( Sze.W < 2 )
226 incpos.C = decsze.W = 0;
227
228 return wrect( Pos + incpos, Sze - decsze );
229 }
230
231
232 wrect intersectRelTo( const wrect & par ) const
233 {
234 // Pos is relative to parent
235 if ( !( Pos < par.Sze ) )
236 return wrect(); // UL is right or above par
237
238 wrect ret( *this );
239
240 // expand negative Sze to par limit
241 if ( ret.Sze.H < 0 )
242 ret.Sze.H = par.Sze.H - ret.Pos.L;
243
244 if ( ret.Sze.W < 0 )
245 ret.Sze.W = par.Sze.W - ret.Pos.C;
246
247 if ( !( ret.Pos + ret.Sze >= 0 ) )
248 return wrect(); // LR is left or below par
249
250 // HERE we know, there's an intersection
251
252 // adjust Pos if it is left or below par
253 if ( ret.Pos.L < 0 )
254 {
255 ret.Sze.H += ret.Pos.L;
256 ret.Pos.L = 0;
257 }
258
259 if ( ret.Pos.C < 0 )
260 {
261 ret.Sze.W += ret.Pos.C;
262 ret.Pos.C = 0;
263 }
264
265 // adjust Sze
266 ret.Sze = wpair::min( ret.Sze, par.Sze - ret.Pos );
267
268 return ret;
269 }
270
271};
272
273extern std::ostream & operator<<( std::ostream & str, const wrect & obj );
274
275
276#endif // wpair_h
A pair of 2 numbers, the base class for wpos and wsze.
Definition position.h:32
wpair between(const wpair &Min, const wpair &Max) const
a copy of this clamped between Min and Max
Definition position.h:87
wpair(int v=0)
Set BOTH members to v
Definition position.h:44
Screen position pair in the order line, column: (L, C)
Definition position.h:110
A rectangle is defined by its position and size: wpos Pos, wsze Sze.
Definition position.h:194
Screen dimension (screen size) in the order height, width: (H, W)
Definition position.h:154