libyui-ncurses  2.44.1
 All Classes Functions Variables
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 
30 
31 class wpair
32 {
33 
34  friend std::ostream & operator<<( std::ostream & STREAM, const wpair & OBJ );
35 
36 protected:
37 
38  int A;
39  int B;
40 
41 public:
42 
43  wpair( const int v = 0 ) { A = B = v; }
44 
45  wpair( const int a, const int b ) { A = a; B = b; }
46 
47  wpair( const wpair & Rhs ) { A = Rhs.A; B = Rhs.B; }
48 
49  virtual ~wpair() {}
50 
51 protected:
52 
53  wpair & operator= ( const wpair & Rhs ) { A = Rhs.A; B = Rhs.B; return *this; }
54 
55  wpair & operator+=( const wpair & Rhs ) { A += Rhs.A; B += Rhs.B; return *this; }
56 
57  wpair & operator-=( const wpair & Rhs ) { A -= Rhs.A; B -= Rhs.B; return *this; }
58 
59  wpair & operator*=( const wpair & Rhs ) { A *= Rhs.A; B *= Rhs.B; return *this; }
60 
61  wpair & operator/=( const wpair & Rhs ) { A /= Rhs.A; B /= Rhs.B; return *this; }
62 
63  wpair operator+( const wpair & Rhs ) const { return wpair( A + Rhs.A, B + Rhs.B ); }
64 
65  wpair operator-( const wpair & Rhs ) const { return wpair( A - Rhs.A, B - Rhs.B ); }
66 
67  wpair operator*( const wpair & Rhs ) const { return wpair( A * Rhs.A, B * Rhs.B ); }
68 
69  wpair operator/( const wpair & Rhs ) const { return wpair( A / Rhs.A, B / Rhs.B ); }
70 
71 public:
72 
73  bool operator==( const wpair & Rhs ) const { return A == Rhs.A && B == Rhs.B; }
74 
75  bool operator!=( const wpair & Rhs ) const { return A != Rhs.A || B != Rhs.B; }
76 
77  bool operator> ( const wpair & Rhs ) const { return A > Rhs.A && B > Rhs.B; }
78 
79  bool operator< ( const wpair & Rhs ) const { return A < Rhs.A && B < Rhs.B; }
80 
81  bool operator>=( const wpair & Rhs ) const { return A >= Rhs.A && B >= Rhs.B; }
82 
83  bool operator<=( const wpair & Rhs ) const { return A <= Rhs.A && B <= Rhs.B; }
84 
85 
86  wpair between( const wpair & Min, const wpair & Max ) const
87  {
88  return min( max( *this, Min ), Max );
89  }
90 
91  static wpair min( const wpair & Lhs, const wpair & Rhs )
92  {
93  return wpair( Lhs.A < Rhs.A ? Lhs.A : Rhs.A,
94  Lhs.B < Rhs.B ? Lhs.B : Rhs.B );
95  }
96 
97  static wpair max( const wpair & Lhs, const wpair & Rhs )
98  {
99  return wpair( Lhs.A > Rhs.A ? Lhs.A : Rhs.A,
100  Lhs.B > Rhs.B ? Lhs.B : Rhs.B );
101  }
102 
103 };
104 
105 
106 
107 // screen position in (line,col)
108 
109 class wpos : public wpair
110 {
111 
112 public:
113 
114  int & L;
115  int & C;
116 
117  wpos( const int v = 0 ) : wpair( v ), L( A ), C( B ) {}
118 
119  wpos( const int l, const 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 
127 public:
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 
148 extern std::ostream & operator<<( std::ostream & STREAM, const wpos & OBJ );
149 
150 
151 
152 // screen dimension in (height,width)
153 
154 class wsze : public wpair
155 {
156 
157 public:
158 
159  int & H;
160  int & W;
161 
162  wsze( const int v = 0 ) : wpair( v ), H( A ), W( B ) {}
163 
164  wsze( const int h, const int w ) : wpair( h, w ), H( A ), W( B ) {}
165 
166  wsze( const wpair & Rhs ) : wpair( Rhs ), H( A ), W( B ) {}
167 
168  wsze( const wsze & Rhs ) : wpair( Rhs ), H( A ), W( B ) {}
169 
170  virtual ~wsze() {}
171 
172  wsze & operator= ( const wsze & Rhs ) { wpair::operator= ( Rhs ); return *this; }
173 
174  wsze & operator+=( const wpair & Rhs ) { wpair::operator+=( Rhs ); return *this; }
175 
176  wsze & operator-=( const wpair & Rhs ) { wpair::operator-=( Rhs ); return *this; }
177 
178  wsze & operator*=( const wpair & Rhs ) { wpair::operator*=( Rhs ); return *this; }
179 
180  wsze & operator/=( const wpair & Rhs ) { wpair::operator/=( Rhs ); return *this; }
181 
182  wsze operator+( const wpair & Rhs ) const { return wpair::operator+( Rhs ); }
183 
184  wsze operator-( const wpair & Rhs ) const { return wpair::operator-( Rhs ); }
185 
186  wsze operator*( const wpair & Rhs ) const { return wpair::operator*( Rhs ); }
187 
188  wsze operator/( const wpair & Rhs ) const { return wpair::operator/( Rhs ); }
189 };
190 
191 extern std::ostream & operator<<( std::ostream & STREAM, const wsze & OBJ );
192 
193 
194 
195 // rectangle {wpos,wsze}
196 
197 class wrect
198 {
199 
200 public:
201 
202  wpos Pos;
203  wsze Sze;
204 
205  wrect() : Pos( 0 ), Sze( 0 ) {}
206 
207  wrect( const wpos & pos, const wsze & sze ) : Pos( pos ), Sze( sze ) {}
208 
209  virtual ~wrect() {}
210 
211 public:
212 
213  bool operator==( const wrect & Rhs ) const
214  {
215  return Pos == Rhs.Pos && Sze == Rhs.Sze;
216  }
217 
218  bool operator!=( const wrect & Rhs ) const { return !operator==( Rhs ); }
219 
220 
221  wrect inside() const
222  {
223  wpos incpos( 1 );
224  wsze decsze( 2 );
225 
226  if ( Sze.H < 2 )
227  incpos.L = decsze.H = 0;
228 
229  if ( Sze.W < 2 )
230  incpos.C = decsze.W = 0;
231 
232  return wrect( Pos + incpos, Sze - decsze );
233  }
234 
235 
236  wrect intersectRelTo( const wrect & par ) const
237  {
238  // Pos is relative to parent
239  if ( !( Pos < par.Sze ) )
240  return wrect(); // UL is right or above par
241 
242  wrect ret( *this );
243 
244  // expand negative Sze to par limit
245  if ( ret.Sze.H < 0 )
246  ret.Sze.H = par.Sze.H - ret.Pos.L;
247 
248  if ( ret.Sze.W < 0 )
249  ret.Sze.W = par.Sze.W - ret.Pos.C;
250 
251  if ( !( ret.Pos + ret.Sze >= 0 ) )
252  return wrect(); // LR is left or below par
253 
254  // HERE we know, there's an intersection
255 
256  // adjust Pos if it is left or below par
257  if ( ret.Pos.L < 0 )
258  {
259  ret.Sze.H += ret.Pos.L;
260  ret.Pos.L = 0;
261  }
262 
263  if ( ret.Pos.C < 0 )
264  {
265  ret.Sze.W += ret.Pos.C;
266  ret.Pos.C = 0;
267  }
268 
269  // adjust Sze
270  ret.Sze = wpair::min( ret.Sze, par.Sze - ret.Pos );
271 
272  return ret;
273  }
274 
275 };
276 
277 extern std::ostream & operator<<( std::ostream & STREAM, const wrect & OBJ );
278 
279 
280 #endif // wpair_h
Definition: position.h:109
Definition: position.h:31
Definition: position.h:154