libyui-ncurses  2.44.1
/usr/src/RPM/BUILD/libyui-ncurses-2.44.1/src/position.h
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019    File:       position.h
00020 
00021    Author:     Michael Andres <ma@suse.de>
00022 
00023 /-*/
00024 
00025 #ifndef position_h
00026 #define position_h
00027 
00028 #include <iosfwd>
00029 
00030 
00031 class wpair
00032 {
00033 
00034     friend std::ostream & operator<<( std::ostream & STREAM, const wpair & OBJ );
00035 
00036 protected:
00037 
00038     int A;
00039     int B;
00040 
00041 public:
00042 
00043     wpair( const int v = 0 )          { A = B = v; }
00044 
00045     wpair( const int a, const int b ) { A = a; B = b; }
00046 
00047     wpair( const wpair & Rhs )        { A = Rhs.A; B = Rhs.B; }
00048 
00049     virtual ~wpair() {}
00050 
00051 protected:
00052 
00053     wpair & operator= ( const wpair & Rhs ) { A =  Rhs.A; B =  Rhs.B; return *this; }
00054 
00055     wpair & operator+=( const wpair & Rhs ) { A += Rhs.A; B += Rhs.B; return *this; }
00056 
00057     wpair & operator-=( const wpair & Rhs ) { A -= Rhs.A; B -= Rhs.B; return *this; }
00058 
00059     wpair & operator*=( const wpair & Rhs ) { A *= Rhs.A; B *= Rhs.B; return *this; }
00060 
00061     wpair & operator/=( const wpair & Rhs ) { A /= Rhs.A; B /= Rhs.B; return *this; }
00062 
00063     wpair operator+( const wpair & Rhs ) const { return wpair( A + Rhs.A, B + Rhs.B ); }
00064 
00065     wpair operator-( const wpair & Rhs ) const { return wpair( A - Rhs.A, B - Rhs.B ); }
00066 
00067     wpair operator*( const wpair & Rhs ) const { return wpair( A * Rhs.A, B * Rhs.B ); }
00068 
00069     wpair operator/( const wpair & Rhs ) const { return wpair( A / Rhs.A, B / Rhs.B ); }
00070 
00071 public:
00072 
00073     bool operator==( const wpair & Rhs ) const { return A == Rhs.A && B == Rhs.B; }
00074 
00075     bool operator!=( const wpair & Rhs ) const { return A != Rhs.A || B != Rhs.B; }
00076 
00077     bool operator> ( const wpair & Rhs ) const { return A >  Rhs.A && B >  Rhs.B; }
00078 
00079     bool operator< ( const wpair & Rhs ) const { return A <  Rhs.A && B <  Rhs.B; }
00080 
00081     bool operator>=( const wpair & Rhs ) const { return A >= Rhs.A && B >= Rhs.B; }
00082 
00083     bool operator<=( const wpair & Rhs ) const { return A <= Rhs.A && B <= Rhs.B; }
00084 
00085 
00086     wpair between( const wpair & Min, const wpair & Max ) const
00087         {
00088             return min( max( *this, Min ), Max );
00089         }
00090 
00091     static wpair min( const wpair & Lhs, const wpair & Rhs )
00092         {
00093             return wpair( Lhs.A < Rhs.A ? Lhs.A : Rhs.A,
00094                           Lhs.B < Rhs.B ? Lhs.B : Rhs.B );
00095         }
00096 
00097     static wpair max( const wpair & Lhs, const wpair & Rhs )
00098         {
00099             return wpair( Lhs.A > Rhs.A ? Lhs.A : Rhs.A,
00100                           Lhs.B > Rhs.B ? Lhs.B : Rhs.B );
00101         }
00102 
00103 };
00104 
00105 
00106 
00107 // screen position in (line,col)
00108 
00109 class wpos : public wpair
00110 {
00111 
00112 public:
00113 
00114     int & L;
00115     int & C;
00116 
00117     wpos( const int v = 0 )          : wpair( v ),    L( A ), C( B ) {}
00118 
00119     wpos( const int l, const int c ) : wpair( l, c ), L( A ), C( B ) {}
00120 
00121     wpos( const wpair & Rhs )        : wpair( Rhs ),  L( A ), C( B ) {}
00122 
00123     wpos( const wpos & Rhs )         : wpair( Rhs ),  L( A ), C( B ) {}
00124 
00125     virtual ~wpos() {}
00126 
00127 public:
00128 
00129     wpos & operator= ( const wpos & Rhs )  { wpair::operator= ( Rhs ); return *this; }
00130 
00131     wpos & operator+=( const wpair & Rhs ) { wpair::operator+=( Rhs ); return *this; }
00132 
00133     wpos & operator-=( const wpair & Rhs ) { wpair::operator-=( Rhs ); return *this; }
00134 
00135     wpos & operator*=( const wpair & Rhs ) { wpair::operator*=( Rhs ); return *this; }
00136 
00137     wpos & operator/=( const wpair & Rhs ) { wpair::operator/=( Rhs ); return *this; }
00138 
00139     wpos operator+( const wpair & Rhs ) const { return wpair::operator+( Rhs ); }
00140 
00141     wpos operator-( const wpair & Rhs ) const { return wpair::operator-( Rhs ); }
00142 
00143     wpos operator*( const wpair & Rhs ) const { return wpair::operator*( Rhs ); }
00144 
00145     wpos operator/( const wpair & Rhs ) const { return wpair::operator/( Rhs ); }
00146 };
00147 
00148 extern std::ostream & operator<<( std::ostream & STREAM, const wpos & OBJ );
00149 
00150 
00151 
00152 // screen dimension in (height,width)
00153 
00154 class wsze : public wpair
00155 {
00156 
00157 public:
00158 
00159     int & H;
00160     int & W;
00161 
00162     wsze( const int v = 0 )          : wpair( v ),    H( A ), W( B ) {}
00163 
00164     wsze( const int h, const int w ) : wpair( h, w ), H( A ), W( B ) {}
00165 
00166     wsze( const wpair & Rhs )        : wpair( Rhs ),  H( A ), W( B ) {}
00167 
00168     wsze( const wsze & Rhs )         : wpair( Rhs ),  H( A ), W( B ) {}
00169 
00170     virtual ~wsze() {}
00171 
00172     wsze & operator= ( const wsze & Rhs )  { wpair::operator= ( Rhs ); return *this; }
00173 
00174     wsze & operator+=( const wpair & Rhs ) { wpair::operator+=( Rhs ); return *this; }
00175 
00176     wsze & operator-=( const wpair & Rhs ) { wpair::operator-=( Rhs ); return *this; }
00177 
00178     wsze & operator*=( const wpair & Rhs ) { wpair::operator*=( Rhs ); return *this; }
00179 
00180     wsze & operator/=( const wpair & Rhs ) { wpair::operator/=( Rhs ); return *this; }
00181 
00182     wsze operator+( const wpair & Rhs ) const { return wpair::operator+( Rhs ); }
00183 
00184     wsze operator-( const wpair & Rhs ) const { return wpair::operator-( Rhs ); }
00185 
00186     wsze operator*( const wpair & Rhs ) const { return wpair::operator*( Rhs ); }
00187 
00188     wsze operator/( const wpair & Rhs ) const { return wpair::operator/( Rhs ); }
00189 };
00190 
00191 extern std::ostream & operator<<( std::ostream & STREAM, const wsze & OBJ );
00192 
00193 
00194 
00195 // rectangle {wpos,wsze}
00196 
00197 class wrect
00198 {
00199 
00200 public:
00201 
00202     wpos Pos;
00203     wsze Sze;
00204 
00205     wrect() : Pos( 0 ), Sze( 0 ) {}
00206 
00207     wrect( const wpos & pos, const wsze & sze ) : Pos( pos ), Sze( sze ) {}
00208 
00209     virtual ~wrect() {}
00210 
00211 public:
00212 
00213     bool operator==( const wrect & Rhs ) const
00214     {
00215         return Pos == Rhs.Pos && Sze == Rhs.Sze;
00216     }
00217 
00218     bool operator!=( const wrect & Rhs ) const { return !operator==( Rhs ); }
00219 
00220 
00221     wrect inside() const
00222     {
00223         wpos incpos( 1 );
00224         wsze decsze( 2 );
00225 
00226         if ( Sze.H < 2 )
00227             incpos.L = decsze.H = 0;
00228 
00229         if ( Sze.W < 2 )
00230             incpos.C = decsze.W = 0;
00231 
00232         return wrect( Pos + incpos, Sze - decsze );
00233     }
00234 
00235 
00236     wrect intersectRelTo( const wrect & par ) const
00237     {
00238         // Pos is relative to parent
00239         if ( !( Pos < par.Sze ) )
00240             return wrect(); // UL is right or above par
00241 
00242         wrect ret( *this );
00243 
00244         // expand negative Sze to par limit
00245         if ( ret.Sze.H < 0 )
00246             ret.Sze.H = par.Sze.H - ret.Pos.L;
00247 
00248         if ( ret.Sze.W < 0 )
00249             ret.Sze.W = par.Sze.W - ret.Pos.C;
00250 
00251         if ( !( ret.Pos + ret.Sze >= 0 ) )
00252             return wrect(); // LR is left or below par
00253 
00254         // HERE we know, there's an intersection
00255 
00256         // adjust Pos if it is left or below par
00257         if ( ret.Pos.L < 0 )
00258         {
00259             ret.Sze.H += ret.Pos.L;
00260             ret.Pos.L = 0;
00261         }
00262 
00263         if ( ret.Pos.C < 0 )
00264         {
00265             ret.Sze.W += ret.Pos.C;
00266             ret.Pos.C = 0;
00267         }
00268 
00269         // adjust Sze
00270         ret.Sze = wpair::min( ret.Sze, par.Sze - ret.Pos );
00271 
00272         return ret;
00273     }
00274 
00275 };
00276 
00277 extern std::ostream & operator<<( std::ostream & STREAM, const wrect & OBJ );
00278 
00279 
00280 #endif // wpair_h
 All Classes Functions Variables