libyui-ncurses  2.44.1
/usr/src/RPM/BUILD/libyui-ncurses-2.44.1/src/NCstyle.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:       NCstyle.h
00020 
00021    Author:     Michael Andres <ma@suse.de>
00022 
00023 /-*/
00024 
00025 #ifndef NCstyle_h
00026 #define NCstyle_h
00027 
00028 #include <ncursesw/ncurses.h>
00029 
00030 #include <iosfwd>
00031 #include <string>
00032 #include <vector>
00033 
00034 #include "NCtypes.h"
00035 
00036 
00037 struct NCattribute
00038 {
00039     //
00040     // available colors and color pairs
00041     //
00042     static int _colors;
00043     static int _pairs;
00044 
00045     //if we have color support, return number of available colors
00046     //(at most 8 though)
00047     //will be initialized by init_color() function
00048     inline static int colors()      { return _colors ? _colors : ::COLORS; }
00049 
00050     // do the same with color pairs
00051     inline static int color_pairs() { return _pairs ? _pairs : ::COLOR_PAIRS; }
00052 
00053     //
00054     //
00055     // color pair to chtype
00056     //
00057     inline static chtype color_pair( short fg, short bg ) { return colors() ? COLOR_PAIR( bg * colors() + COLOR_WHITE - fg ) : A_NORMAL; }
00058 
00059     inline static chtype color_pair( int i )              { return colors() ? COLOR_PAIR( i ) : A_NORMAL; }
00060 
00061     inline static short  fg_color_pair( int i )           { return colors() ? ( COLOR_WHITE - ( i % colors() ) ) : -1; }
00062 
00063     inline static short  bg_color_pair( int i )           { return colors() ? ( i / colors() ) : -1; }
00064 
00065     //
00066     // chtype to color pair
00067     //
00068     inline static int    color_pair_of( chtype ch )       { return PAIR_NUMBER( ch ); }
00069 
00070     inline static short  fg_color_of( chtype ch )         { return fg_color_pair( color_pair_of( ch ) ); }
00071 
00072     inline static short  bg_color_of( chtype ch )         { return bg_color_pair( color_pair_of( ch ) ); }
00073 
00074     //
00075     // chtype manipualtion
00076     //
00077     static const chtype style_mask = A_ATTRIBUTES & ~A_COLOR & ~A_ALTCHARSET;
00078     static const chtype color_mask = A_COLOR;
00079     static const chtype char_mask  = A_CHARTEXT | A_ALTCHARSET;
00080     //
00081     inline static chtype getStyle( chtype a )             { return a & style_mask; }
00082 
00083     inline static chtype getColor( chtype a )             { return a & color_mask; }
00084 
00085     inline static chtype getChar( chtype a )              { return a & char_mask;  }
00086 
00087     inline static chtype getNonChar( chtype a )           { return a & ~char_mask;  }
00088 
00089     inline static void   setStyle( chtype & a, chtype ch ) { a = ( a & ~style_mask ) | ( ch & style_mask ); }
00090 
00091     inline static void   setColor( chtype & a, chtype ch ) { if ( colors() ) a = ( a & ~color_mask ) | ( ch & color_mask ); }
00092 
00093     inline static void   setChar( chtype & a, chtype ch ) { a = ( a & ~char_mask ) | ( ch & char_mask );  }
00094 
00095     inline static void   addStyle( chtype & a, chtype ch ) { a = a | ( ch & style_mask ); }
00096 
00097     inline static void   delStyle( chtype & a, chtype ch ) { a = a & ~( ch & style_mask ); }
00098 
00099     inline static void   toggleStyle( chtype & a, chtype ch ) { setStyle( a, ( a & ~ch ) | (( a ^ ch ) & ch ) ); }
00100 
00101     inline static void   addAlt( chtype & a ) { a |= A_ALTCHARSET; }
00102 
00103     inline static void   delAlt( chtype & a ) { a &= ~A_ALTCHARSET; }
00104 
00105     //
00106     inline static short  getFg( chtype a )              { return fg_color_of( a ); }
00107 
00108     inline static short  getBg( chtype a )              { return bg_color_of( a ); }
00109 
00110     inline static void   setFg( chtype & a, short c )   { if ( colors() ) setColor( a, color_pair(( colors() + c ) % colors(), getBg( a ) ) ); }
00111 
00112     inline static void   setBg( chtype & a, short c )   { if ( colors() ) setColor( a, color_pair( getFg( a ), ( colors() + c ) % colors() ) ); }
00113 
00114     // debug aid
00115 
00116     /** Color name */
00117     inline static std::string colorAsString( short i )
00118     {
00119         switch ( i )
00120         {
00121             #define OUTS(X)     case COLOR_##X: return #X
00122             OUTS( BLACK );
00123             OUTS( RED );
00124             OUTS( GREEN );
00125             OUTS( YELLOW );
00126             OUTS( BLUE );
00127             OUTS( MAGENTA );
00128             OUTS( CYAN );
00129             OUTS( WHITE );
00130             #undef OUTS
00131         }
00132         return "COLOR?";
00133     }
00134 
00135     /** String representation of color_pair "[fg,bg]" */
00136     inline static std::string color_pairAsString( int i )
00137     {
00138         return std::string( "[" ) + colorAsString( fg_color_pair( i ) )
00139                 + "," + colorAsString( bg_color_pair( i ) ) + "]";
00140     }
00141 
00142 private:
00143 
00144     friend class NCurses;
00145 
00146     static void init_colors()
00147     {
00148 
00149         //get number of available colors (property of the terminal)
00150         //the same with color pairs
00151 
00152         _colors = ::COLORS;
00153         _pairs = ::COLOR_PAIRS;
00154 
00155         //if we have more than 8 colors available, use only 8 anyway
00156         //in order to preserve the same color palette even for
00157         //e.g. 256color terminal
00158 
00159         if ( _colors > COLOR_WHITE + 1 )
00160             //_colors = 8 at all times
00161             _colors = COLOR_WHITE + 1;
00162 
00163         if ( _pairs > _colors * _colors )
00164             //_pairs == 64 at all times
00165             _pairs = _colors * _colors;
00166 
00167         for ( short i = 1; i < color_pairs(); ++i )
00168             ::init_pair( i, fg_color_pair( i ), bg_color_pair( i ) );
00169     }
00170 };
00171 
00172 
00173 
00174 class NCattrset
00175 {
00176 
00177 private:
00178 
00179     std::vector<chtype> attr;
00180 
00181 public:
00182 
00183     NCattrset( unsigned num ) : attr( num, A_NORMAL ) {}
00184 
00185     virtual ~NCattrset() {}
00186 
00187 public:
00188 
00189     const chtype & operator[]( unsigned a ) const { return attr[a]; }
00190 
00191     chtype getAttr( unsigned a ) const { return attr[a]; }
00192 
00193     chtype getStyle( unsigned a ) const { return NCattribute::getStyle( attr[a] ); }
00194 
00195     chtype getColor( unsigned a ) const { return NCattribute::getColor( attr[a] ); }
00196 
00197     chtype getChar( unsigned a ) const { return NCattribute::getChar( attr[a] ); }
00198 
00199     chtype getNonChar( unsigned a ) const { return NCattribute::getNonChar( attr[a] ); }
00200 
00201     void setAttr( unsigned a, chtype ch ) { attr[a] = ch; }
00202 
00203     void setStyle( unsigned a, chtype ch ) { NCattribute::setStyle( attr[a], ch ); }
00204 
00205     void setColor( unsigned a, chtype ch ) { NCattribute::setColor( attr[a], ch ); }
00206 
00207     void setChar( unsigned a, chtype ch ) { NCattribute::setChar( attr[a], ch ); }
00208 
00209     void addStyle( unsigned a, chtype ch ) { NCattribute::addStyle( attr[a], ch ); }
00210 
00211     void delStyle( unsigned a, chtype ch ) { NCattribute::delStyle( attr[a], ch ); }
00212 
00213     void toggleStyle( unsigned a, chtype ch ) { NCattribute::toggleStyle( attr[a], ch ); }
00214 
00215     void addAlt( unsigned a ) { NCattribute::addAlt( attr[a] ); }
00216 
00217     void delAlt( unsigned a ) { NCattribute::delAlt( attr[a] ); }
00218 
00219 public:
00220 
00221     short getFg( unsigned a ) const { return NCattribute::getFg( attr[a] ); }
00222 
00223     short getBg( unsigned a ) const { return NCattribute::getBg( attr[a] ); }
00224 
00225     void setFg( unsigned a, short c ) { NCattribute::setFg( attr[a], c ); }
00226 
00227     void setBg( unsigned a, short c ) { NCattribute::setBg( attr[a], c ); }
00228 };
00229 
00230 
00231 
00232 class NCstyle
00233 {
00234 
00235     friend class NCStyleDef;
00236 
00237 public:
00238 
00239     enum STglobal
00240     {
00241         AppTitle,
00242         AppText,
00243         // last entry
00244         MaxSTglobal
00245     };
00246 
00247     enum STlocal
00248     {
00249         DialogBorder,
00250         DialogTitle,
00251         DialogActiveBorder,
00252         DialogActiveTitle,
00253         //
00254         DialogText,
00255         DialogHeadline,
00256         //
00257         DialogDisabled,
00258         //
00259         DialogPlain,
00260         DialogLabel,
00261         DialogData,
00262         DialogHint,
00263         DialogScrl,
00264         DialogActivePlain,
00265         DialogActiveLabel,
00266         DialogActiveData,
00267         DialogActiveHint,
00268         DialogActiveScrl,
00269         //
00270         DialogFramePlain,
00271         DialogFrameLabel,
00272         DialogFrameData,
00273         DialogFrameHint,
00274         DialogFrameScrl,
00275         DialogActiveFramePlain,
00276         DialogActiveFrameLabel,
00277         DialogActiveFrameData,
00278         DialogActiveFrameHint,
00279         DialogActiveFrameScrl,
00280         //
00281         ListTitle,
00282         ListPlain,
00283         ListLabel,
00284         ListData,
00285         ListHint,
00286         ListSelPlain,
00287         ListSelLabel,
00288         ListSelData,
00289         ListSelHint,
00290         //
00291         ListActiveTitle,
00292         ListActivePlain,
00293         ListActiveLabel,
00294         ListActiveData,
00295         ListActiveHint,
00296         ListActiveSelPlain,
00297         ListActiveSelLabel,
00298         ListActiveSelData,
00299         ListActiveSelHint,
00300         //
00301         RichTextPlain,
00302         RichTextTitle,
00303         RichTextLink,
00304         RichTextArmedlink,
00305         RichTextActiveArmedlink,
00306         RichTextVisitedLink,
00307         RichTextB,
00308         RichTextI,
00309         RichTextT,
00310         RichTextBI,
00311         RichTextBT,
00312         RichTextIT,
00313         RichTextBIT,
00314         //
00315         ProgbarCh,
00316         ProgbarBgch,
00317         //
00318         TextCursor,
00319         // last entry
00320         MaxSTlocal
00321     };
00322 
00323 public:
00324 
00325     struct StBase
00326     {
00327         const chtype & title;
00328         const chtype & text;
00329         StBase( const chtype & ti, const chtype & te )
00330                 : title( ti ), text( te )
00331         {}
00332     };
00333 
00334     struct STChar
00335     {
00336         const chtype & chattr;
00337         chtype getChar()    const { return NCattribute::getChar( chattr ); }
00338 
00339         chtype getNonChar() const { return NCattribute::getNonChar( chattr ); }
00340 
00341         STChar( const chtype & cha )
00342                 : chattr( cha )
00343         {}
00344     };
00345 
00346     struct StItem
00347     {
00348         const chtype & plain;
00349         const chtype & label;
00350         const chtype & data;
00351         const chtype & hint;
00352         StItem( const chtype & p, const chtype & l, const chtype & d, const chtype & h )
00353                 : plain( p ), label( l ), data( d ), hint( h )
00354         {}
00355     };
00356 
00357     struct StWidget : public StItem
00358     {
00359         const chtype & scrl;
00360         StWidget( const chtype & p, const chtype & l, const chtype & d, const chtype & h,
00361                   const chtype & s )
00362                 : StItem( p, l, d, h ), scrl( s )
00363         {}
00364     };
00365 
00366     struct StList
00367     {
00368         const chtype & title;
00369         const StItem   item;
00370         const StItem   selected;
00371         StList( const chtype & t, const StItem & i, const StItem & s )
00372                 : title( t ), item( i ), selected( s )
00373         {}
00374 
00375         const StItem & getItem( bool sel ) const { return sel ? selected : item; }
00376     };
00377 
00378     struct StProgbar
00379     {
00380         const STChar bar;
00381         const STChar nonbar;
00382         StProgbar( const chtype & b, const chtype & nb )
00383                 : bar( b ), nonbar( nb )
00384         {}
00385     };
00386 
00387     struct StRichtext
00388     {
00389         const chtype & plain;
00390         const chtype & title;
00391         const chtype & link;
00392         const chtype & armedlink;
00393         const chtype & activearmedlink;
00394         const chtype & visitedlink;
00395         const chtype & B;
00396         const chtype & I;
00397         const chtype & T;
00398         const chtype & BI;
00399         const chtype & BT;
00400         const chtype & IT;
00401         const chtype & BIT;
00402 
00403         StRichtext( const chtype & p, const chtype & tit,
00404                     const chtype & l, const chtype & al, const chtype & aal, const chtype & vl,
00405                     const chtype & b, const chtype & i, const chtype & t,
00406                     const chtype & bi, const chtype & bt, const chtype & it,
00407                     const chtype & bit )
00408                 : plain( p ), title( tit ),
00409                 link( l ), armedlink( al ), activearmedlink( aal ), visitedlink( vl ),
00410                 B( b ), I( i ), T( t ),
00411                 BI( bi ), BT( bt ), IT( it ),
00412                 BIT( bit )
00413         {}
00414 
00415         const chtype & getArmed( NC::WState s ) const
00416         {
00417             return ( s == NC::WSactive ) ? activearmedlink : armedlink;
00418         }
00419     };
00420 
00421     struct StDialog
00422     {
00423         StBase    border;
00424         StBase    activeBorder;
00425         StBase    dumb;
00426         StWidget  disabled;
00427         StWidget  normal;
00428         StWidget  active;
00429         StWidget  frame;
00430         StWidget  activeFrame;
00431         StList    list;
00432         StList    activeList;
00433         StList    disabledList;
00434         StProgbar  progbar;
00435         StRichtext richtext;
00436         const chtype & cursor;
00437         //
00438         StDialog( const StBase & b, const StBase & ab, const StBase & d, const StWidget & dis,
00439                   const StWidget & n, const StWidget & a,
00440                   const StWidget & f, const StWidget & af,
00441                   const StList & l, const StList & al, const StList & dl,
00442                   const StProgbar & pbar,
00443                   const StRichtext & rtext,
00444                   const chtype & curs )
00445                 : border( b ), activeBorder( ab ), dumb( d ), disabled( dis ),
00446                 normal( n ), active( a ),
00447                 frame( f ), activeFrame( af ),
00448                 list( l ), activeList( al ), disabledList( dl ),
00449                 progbar( pbar ),
00450                 richtext( rtext ),
00451                 cursor( curs )
00452         {}
00453 
00454 public:
00455 
00456         const StBase & getDlgBorder( bool active ) const { return active ? activeBorder : border; }
00457 
00458         const StBase & getDumb()                   const { return dumb; }
00459 
00460         const StWidget & getWidget( NC::WState s, bool nonactive = false ) const
00461         {
00462             switch ( s )
00463             {
00464 
00465                 case NC::WSdisabeled:
00466                     return disabled;
00467 
00468                 case NC::WSactive:
00469                     return nonactive ? normal : active;
00470 
00471                 case NC::WSnormal:
00472 
00473                 case NC::WSdumb:
00474                     break;
00475             }
00476 
00477             return normal;
00478         }
00479 
00480         const StWidget & getFrame( NC::WState s ) const
00481         {
00482             switch ( s )
00483             {
00484 
00485                 case NC::WSdisabeled:
00486                     return disabled;
00487 
00488                 case NC::WSactive:
00489                     return activeFrame;
00490 
00491                 case NC::WSnormal:
00492 
00493                 case NC::WSdumb:
00494                     break;
00495             }
00496 
00497             return frame;
00498         }
00499 
00500         const StList & getList( NC::WState s ) const
00501         {
00502             switch ( s )
00503             {
00504 
00505                 case NC::WSdisabeled:
00506                     return disabledList;
00507 
00508                 case NC::WSactive:
00509                     return activeList;
00510 
00511                 case NC::WSnormal:
00512 
00513                 case NC::WSdumb:
00514                     break;
00515             }
00516 
00517             return list;
00518         }
00519     };
00520 
00521 public:
00522 
00523 
00524     class Style : private NCattrset, public StDialog
00525     {
00526 
00527         friend class NCstyle;
00528 
00529         Style & operator=( const Style & ); // no assignment
00530 
00531     private:
00532 
00533         static unsigned sanitycheck();
00534         static NCattrset attrGlobal;
00535 
00536     public:
00537 
00538         NCattrset & getAttrGlobal() { return attrGlobal; }
00539 
00540         NCattrset & getAttrLocal() { return *this; }
00541 
00542     private:
00543 
00544         StDialog initDialog(); // use this to initialize StDialog
00545 
00546     public:
00547 
00548         Style();
00549         Style( const Style & rhs );
00550         virtual ~Style();
00551 
00552     public:
00553 
00554         const chtype & attr( STglobal a ) const { return attrGlobal[a]; }
00555 
00556         const chtype & attr( STlocal a )  const { return NCattrset::operator[]( a ); }
00557 
00558         const chtype & operator()( STglobal a ) const { return attr( a ); }
00559 
00560         const chtype & operator()( STlocal a )  const { return attr( a ); }
00561     };
00562 
00563 public:
00564 
00565     enum StyleSet
00566     {
00567         DefaultStyle,
00568         InfoStyle,
00569         WarnStyle,
00570         PopupStyle,
00571         // last entry
00572         MaxStyleSet
00573     };
00574 
00575 private:
00576 
00577     std::string         styleName;
00578     std::string         term;
00579     std::vector<Style>  styleSet;
00580 
00581     StyleSet fakestyle_e;
00582     void     fakestyle( StyleSet f );
00583     Style &  getStyle( StyleSet a ) { return styleSet[a]; }
00584 
00585 public:
00586 
00587     NCstyle( std::string term_t );
00588     ~NCstyle();
00589 
00590     const chtype & operator()( STglobal a ) const { return Style::attrGlobal[a]; }
00591 
00592     const Style &  operator[]( StyleSet a ) const
00593     {
00594         if ( fakestyle_e != MaxStyleSet )
00595             return styleSet[fakestyle_e];
00596 
00597         return styleSet[a];
00598     }
00599 
00600     void changeSyle();
00601     void nextStyle();
00602 
00603     static std::string dumpName( StyleSet a );
00604     static std::string dumpName( STglobal a );
00605     static std::string dumpName( STlocal a );
00606 };
00607 
00608 
00609 #endif // NCstyle_h
 All Classes Functions Variables