Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

position.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                                                                      |
00003 |                      __   __    ____ _____ ____                      |
00004 |                      \ \ / /_ _/ ___|_   _|___ \                     |
00005 |                       \ V / _` \___ \ | |   __) |                    |
00006 |                        | | (_| |___) || |  / __/                     |
00007 |                        |_|\__,_|____/ |_| |_____|                    |
00008 |                                                                      |
00009 |                               core system                            |
00010 |                                                        (C) SuSE GmbH |
00011 \----------------------------------------------------------------------/
00012 
00013    File:       position.h
00014 
00015    Author:     Michael Andres <ma@suse.de>
00016    Maintainer: Michael Andres <ma@suse.de>
00017 
00018 /-*/
00019 #ifndef position_h
00020 #define position_h
00021 
00022 #include <iosfwd>
00023 
00024 
00026 //
00027 //   CLASS NAME : wpair
00028 //
00029 class wpair {
00030 
00031   friend std::ostream & operator<<( std::ostream & STREAM, const wpair & OBJ );
00032 
00033   protected:
00034 
00035   int A;
00036   int B;
00037 
00038   public:
00039 
00040     wpair( const int v = 0 )          { A = B = v; }
00041     wpair( const int a, const int b ) { A = a; B = b; }
00042     wpair( const wpair & Rhs )        { A = Rhs.A; B = Rhs.B; }
00043 
00044     virtual ~wpair() {}
00045 
00046   protected:
00047 
00048     wpair & operator= ( const wpair & Rhs ) { A =  Rhs.A; B =  Rhs.B; return *this; }
00049     wpair & operator+=( const wpair & Rhs ) { A += Rhs.A; B += Rhs.B; return *this; }
00050     wpair & operator-=( const wpair & Rhs ) { A -= Rhs.A; B -= Rhs.B; return *this; }
00051     wpair & operator*=( const wpair & Rhs ) { A *= Rhs.A; B *= Rhs.B; return *this; }
00052     wpair & operator/=( const wpair & Rhs ) { A /= Rhs.A; B /= Rhs.B; return *this; }
00053 
00054     wpair operator+( const wpair & Rhs ) const { return wpair( A + Rhs.A, B + Rhs.B ); }
00055     wpair operator-( const wpair & Rhs ) const { return wpair( A - Rhs.A, B - Rhs.B ); }
00056     wpair operator*( const wpair & Rhs ) const { return wpair( A * Rhs.A, B * Rhs.B ); }
00057     wpair operator/( const wpair & Rhs ) const { return wpair( A / Rhs.A, B / Rhs.B ); }
00058 
00059   public:
00060 
00061     bool operator==( const wpair & Rhs ) const { return A == Rhs.A && B == Rhs.B; }
00062     bool operator!=( const wpair & Rhs ) const { return A != Rhs.A || B != Rhs.B; }
00063 
00064     bool operator> ( const wpair & Rhs ) const { return A >  Rhs.A && B >  Rhs.B; }
00065     bool operator< ( const wpair & Rhs ) const { return A <  Rhs.A && B <  Rhs.B; }
00066     bool operator>=( const wpair & Rhs ) const { return A >= Rhs.A && B >= Rhs.B; }
00067     bool operator<=( const wpair & Rhs ) const { return A <= Rhs.A && B <= Rhs.B; }
00068 
00069 
00070     wpair between( const wpair & Min, const wpair & Max ) const {
00071       return min( max( *this, Min ), Max );
00072     }
00073 
00074     static wpair min( const wpair & Lhs, const wpair & Rhs ) {
00075       return wpair( Lhs.A < Rhs.A ? Lhs.A : Rhs.A,
00076                     Lhs.B < Rhs.B ? Lhs.B : Rhs.B );
00077     }
00078     static wpair max( const wpair & Lhs, const wpair & Rhs ) {
00079       return wpair( Lhs.A > Rhs.A ? Lhs.A : Rhs.A,
00080                     Lhs.B > Rhs.B ? Lhs.B : Rhs.B );
00081     }
00082 
00083 };
00084 
00086 
00088 //
00089 //   CLASS NAME : wpos
00090 //
00091 //   DESCRIPTION : screen position in (line,col)
00092 //
00093 class wpos : public wpair {
00094 
00095   public:
00096 
00097     int & L;
00098     int & C;
00099 
00100     wpos( const int v = 0 )          : wpair( v ),    L( A ), C( B ) {}
00101     wpos( const int l, const int c ) : wpair( l, c ), L( A ), C( B ) {}
00102     wpos( const wpair & Rhs )        : wpair( Rhs ),  L( A ), C( B ) {}
00103     wpos( const wpos & Rhs )         : wpair( Rhs ),  L( A ), C( B ) {}
00104 
00105     virtual ~wpos() {}
00106 
00107   public:
00108 
00109     wpos & operator= ( const wpos & Rhs )  { wpair::operator= ( Rhs ); return *this; }
00110     wpos & operator+=( const wpair & Rhs ) { wpair::operator+=( Rhs ); return *this; }
00111     wpos & operator-=( const wpair & Rhs ) { wpair::operator-=( Rhs ); return *this; }
00112     wpos & operator*=( const wpair & Rhs ) { wpair::operator*=( Rhs ); return *this; }
00113     wpos & operator/=( const wpair & Rhs ) { wpair::operator/=( Rhs ); return *this; }
00114 
00115     wpos operator+( const wpair & Rhs ) const { return wpair::operator+( Rhs ); }
00116     wpos operator-( const wpair & Rhs ) const { return wpair::operator-( Rhs ); }
00117     wpos operator*( const wpair & Rhs ) const { return wpair::operator*( Rhs ); }
00118     wpos operator/( const wpair & Rhs ) const { return wpair::operator/( Rhs ); }
00119 };
00120 
00121 extern std::ostream & operator<<( std::ostream & STREAM, const wpos & OBJ );
00122 
00124 
00126 //
00127 //   CLASS NAME : wsze
00128 //
00129 //   DESCRIPTION : screen dimension in (height,width)
00130 //
00131 class wsze : public wpair {
00132 
00133   public:
00134 
00135     int & H;
00136     int & W;
00137 
00138     wsze( const int v = 0 )          : wpair( v ),    H( A ), W( B ) {}
00139     wsze( const int h, const int w ) : wpair( h, w ), H( A ), W( B ) {}
00140     wsze( const wpair & Rhs )        : wpair( Rhs ),  H( A ), W( B ) {}
00141     wsze( const wsze & Rhs )         : wpair( Rhs ),  H( A ), W( B ) {}
00142 
00143     virtual ~wsze() {}
00144 
00145     wsze & operator= ( const wsze & Rhs )  { wpair::operator= ( Rhs ); return *this; }
00146     wsze & operator+=( const wpair & Rhs ) { wpair::operator+=( Rhs ); return *this; }
00147     wsze & operator-=( const wpair & Rhs ) { wpair::operator-=( Rhs ); return *this; }
00148     wsze & operator*=( const wpair & Rhs ) { wpair::operator*=( Rhs ); return *this; }
00149     wsze & operator/=( const wpair & Rhs ) { wpair::operator/=( Rhs ); return *this; }
00150 
00151     wsze operator+( const wpair & Rhs ) const { return wpair::operator+( Rhs ); }
00152     wsze operator-( const wpair & Rhs ) const { return wpair::operator-( Rhs ); }
00153     wsze operator*( const wpair & Rhs ) const { return wpair::operator*( Rhs ); }
00154     wsze operator/( const wpair & Rhs ) const { return wpair::operator/( Rhs ); }
00155 };
00156 
00157 extern std::ostream & operator<<( std::ostream & STREAM, const wsze & OBJ );
00158 
00160 
00162 //
00163 //   CLASS NAME : wrect
00164 //
00165 //   DESCRIPTION : rectangle {wpos,wsze}
00166 //
00167 class wrect {
00168 
00169   public:
00170 
00171     wpos Pos;
00172     wsze Sze;
00173 
00174     wrect() : Pos( 0 ), Sze( 0 ) {}
00175     wrect( const wpos & pos, const wsze & sze ) : Pos( pos ), Sze( sze ) {}
00176 
00177     virtual ~wrect() {}
00178 
00179   public:
00180 
00181     bool operator==( const wrect & Rhs ) const {
00182       return Pos == Rhs.Pos && Sze == Rhs.Sze;
00183     }
00184     bool operator!=( const wrect & Rhs ) const { return !operator==( Rhs ); }
00185 
00186     wrect inside() const {
00187       wpos incpos( 1 );
00188       wsze decsze( 2 );
00189       if ( Sze.H < 2 )
00190         incpos.L = decsze.H = 0;
00191       if ( Sze.W < 2 )
00192         incpos.C = decsze.W = 0;
00193       return wrect( Pos+incpos, Sze-decsze );
00194     }
00195 
00196     wrect intersectRelTo( const wrect & par ) const {
00197       // Pos is relative to parent
00198       if ( ! (Pos < par.Sze) )
00199         return wrect(); // UL is right or above par
00200 
00201       wrect ret( *this );
00202       // expand negative Sze to par limit
00203       if ( ret.Sze.H < 0 )
00204         ret.Sze.H = par.Sze.H - ret.Pos.L;
00205       if ( ret.Sze.W < 0 )
00206         ret.Sze.W = par.Sze.W - ret.Pos.C;
00207 
00208       if ( ! (ret.Pos+ret.Sze >= 0) )
00209         return wrect(); // LR is left or below par
00210 
00211       // HERE we know, there's an intersection
00212 
00213       // adjust Pos if it is left or below par
00214       if ( ret.Pos.L < 0 ) {
00215         ret.Sze.H += ret.Pos.L;
00216         ret.Pos.L = 0;
00217       }
00218       if ( ret.Pos.C < 0 ) {
00219         ret.Sze.W += ret.Pos.C;
00220         ret.Pos.C = 0;
00221       }
00222       // adjust Sze
00223       ret.Sze = wpair::min( ret.Sze, par.Sze-ret.Pos );
00224       return ret;
00225     }
00226 
00227 };
00228 
00229 extern std::ostream & operator<<( std::ostream & STREAM, const wrect & OBJ );
00230 
00232 
00233 #endif // wpair_h

Generated on Wed Sep 5 17:18:55 2007 for yast2-ncurses by doxygen 1.3.6