00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef NCstyle_h
00020 #define NCstyle_h
00021
00022 extern "C" {
00023 #include <ncurses.h>
00024 }
00025
00026 #include <iosfwd>
00027 #include <string>
00028 #include <vector>
00029
00030 #include "NCtypes.h"
00031
00033
00034
00035
00036
00037
00038 struct NCattribute {
00039
00040
00041
00042 inline static int colors() { return ::COLORS; }
00043 inline static int color_pairs() { return ::COLOR_PAIRS; }
00044
00045
00046
00047 inline static chtype color_pair ( short fg, short bg ) { return colors() ? COLOR_PAIR( bg * colors() + COLOR_WHITE - fg ) : A_NORMAL; }
00048 inline static chtype color_pair ( int i ) { return colors() ? COLOR_PAIR( i ) : A_NORMAL; }
00049 inline static short fg_color_pair( int i ) { return colors() ? (COLOR_WHITE-( i % colors() )) : -1; }
00050 inline static short bg_color_pair( int i ) { return colors() ? (i / colors()) : -1; }
00051
00052
00053
00054 inline static int color_pair_of( chtype ch ) { return PAIR_NUMBER(ch); }
00055 inline static short fg_color_of ( chtype ch ) { return fg_color_pair( color_pair_of( ch ) ); }
00056 inline static short bg_color_of ( chtype ch ) { return bg_color_pair( color_pair_of( ch ) ); }
00057
00058
00059
00060 static const chtype style_mask = A_ATTRIBUTES & ~A_COLOR & ~A_ALTCHARSET;
00061 static const chtype color_mask = A_COLOR;
00062 static const chtype char_mask = A_CHARTEXT | A_ALTCHARSET;
00063
00064 inline static chtype getStyle ( chtype a ) { return a & style_mask; }
00065 inline static chtype getColor ( chtype a ) { return a & color_mask; }
00066 inline static chtype getChar ( chtype a ) { return a & char_mask; }
00067 inline static chtype getNonChar ( chtype a ) { return a & ~char_mask; }
00068
00069 inline static void setStyle ( chtype & a, chtype ch ) { a = a & ~style_mask | ch & style_mask; }
00070 inline static void setColor ( chtype & a, chtype ch ) { if ( colors() ) a = a & ~color_mask | ch & color_mask; }
00071 inline static void setChar ( chtype & a, chtype ch ) { a = a & ~char_mask | ch & char_mask; }
00072
00073 inline static void addStyle ( chtype & a, chtype ch ) { a = a | ch & style_mask; }
00074 inline static void delStyle ( chtype & a, chtype ch ) { a = a & ~(ch & style_mask); }
00075 inline static void toggleStyle( chtype & a, chtype ch ) { setStyle( a, a & ~ch | (a ^ ch) & ch ); }
00076
00077 inline static void addAlt ( chtype & a ) { a |= A_ALTCHARSET; }
00078 inline static void delAlt ( chtype & a ) { a &= ~A_ALTCHARSET; }
00079
00080 inline static short getFg ( chtype a ) { return fg_color_of( a ); }
00081 inline static short getBg ( chtype a ) { return bg_color_of( a ); }
00082
00083 inline static void setFg ( chtype & a, short c ) { if ( colors() ) setColor( a, color_pair( (colors()+c) % colors(), getBg(a) ) ); }
00084 inline static void setBg ( chtype & a, short c ) { if ( colors() ) setColor( a, color_pair( getFg(a), (colors()+c) % colors() ) ); }
00085
00086 private:
00087
00088 friend class NCurses;
00089
00090 static void init_colors() {
00091 for ( short i = 1; i < color_pairs(); ++i )
00092 ::init_pair( i, fg_color_pair( i ), bg_color_pair( i ) );
00093 }
00094 };
00095
00097
00099
00100
00101
00102
00103
00104 class NCattrset {
00105
00106 private:
00107
00108 vector<chtype> attr;
00109
00110 public:
00111
00112 NCattrset( unsigned num ) : attr( num, A_NORMAL ) {}
00113 virtual ~NCattrset() {}
00114
00115 public:
00116
00117 const chtype & operator[]( unsigned a ) const { return attr[a]; }
00118
00119 chtype getAttr ( unsigned a ) const { return attr[a]; }
00120 chtype getStyle ( unsigned a ) const { return NCattribute::getStyle( attr[a] ); }
00121 chtype getColor ( unsigned a ) const { return NCattribute::getColor( attr[a] ); }
00122 chtype getChar ( unsigned a ) const { return NCattribute::getChar( attr[a] ); }
00123 chtype getNonChar ( unsigned a ) const { return NCattribute::getNonChar( attr[a] ); }
00124
00125 void setAttr ( unsigned a, chtype ch ) { attr[a] = ch; }
00126 void setStyle( unsigned a, chtype ch ) { NCattribute::setStyle( attr[a], ch ); }
00127 void setColor( unsigned a, chtype ch ) { NCattribute::setColor( attr[a], ch ); }
00128 void setChar ( unsigned a, chtype ch ) { NCattribute::setChar( attr[a], ch ); }
00129
00130 void addStyle ( unsigned a, chtype ch ) { NCattribute::addStyle( attr[a], ch ); }
00131 void delStyle ( unsigned a, chtype ch ) { NCattribute::delStyle( attr[a], ch ); }
00132 void toggleStyle( unsigned a, chtype ch ) { NCattribute::toggleStyle( attr[a], ch ); }
00133
00134 void addAlt( unsigned a ) { NCattribute::addAlt( attr[a] ); }
00135 void delAlt( unsigned a ) { NCattribute::delAlt( attr[a] ); }
00136
00137 public:
00138
00139 short getFg( unsigned a ) const { return NCattribute::getFg( attr[a] ); }
00140 short getBg( unsigned a ) const { return NCattribute::getBg( attr[a] ); }
00141
00142 void setFg( unsigned a, short c ) { NCattribute::setFg( attr[a], c ); }
00143 void setBg( unsigned a, short c ) { NCattribute::setBg( attr[a], c ); }
00144 };
00145
00147
00149
00150
00151
00152
00153
00154 class NCstyle {
00155
00156 friend class NCStyleDef;
00157
00158 public:
00159
00160 enum STglobal {
00161 AppTitle,
00162 AppText,
00163
00164 MaxSTglobal
00165 };
00166
00167 enum STlocal {
00168 DialogBorder,
00169 DialogTitle,
00170 DialogActiveBorder,
00171 DialogActiveTitle,
00172
00173 DialogText,
00174 DialogHeadline,
00175
00176 DialogDisabled,
00177
00178 DialogPlain,
00179 DialogLabel,
00180 DialogData,
00181 DialogHint,
00182 DialogScrl,
00183 DialogActivePlain,
00184 DialogActiveLabel,
00185 DialogActiveData,
00186 DialogActiveHint,
00187 DialogActiveScrl,
00188
00189 DialogFramePlain,
00190 DialogFrameLabel,
00191 DialogFrameData,
00192 DialogFrameHint,
00193 DialogFrameScrl,
00194 DialogActiveFramePlain,
00195 DialogActiveFrameLabel,
00196 DialogActiveFrameData,
00197 DialogActiveFrameHint,
00198 DialogActiveFrameScrl,
00199
00200 ListTitle,
00201 ListPlain,
00202 ListLabel,
00203 ListData,
00204 ListHint,
00205 ListSelPlain,
00206 ListSelLabel,
00207 ListSelData,
00208 ListSelHint,
00209
00210 ListActiveTitle,
00211 ListActivePlain,
00212 ListActiveLabel,
00213 ListActiveData,
00214 ListActiveHint,
00215 ListActiveSelPlain,
00216 ListActiveSelLabel,
00217 ListActiveSelData,
00218 ListActiveSelHint,
00219
00220 RichTextPlain,
00221 RichTextTitle,
00222 RichTextLink,
00223 RichTextArmedlink,
00224 RichTextActiveArmedlink,
00225 RichTextB,
00226 RichTextI,
00227 RichTextT,
00228 RichTextBI,
00229 RichTextBT,
00230 RichTextIT,
00231 RichTextBIT,
00232
00233 ProgbarCh,
00234 ProgbarBgch,
00235
00236 TextCursor,
00237
00238 MaxSTlocal
00239 };
00240
00241 public:
00242
00243 struct StBase {
00244 const chtype & title;
00245 const chtype & text;
00246 StBase( const chtype & ti, const chtype & te )
00247 : title( ti ), text( te )
00248 {}
00249 };
00250
00251 struct STChar {
00252 const chtype & chattr;
00253 chtype getChar() const { return NCattribute::getChar( chattr ); }
00254 chtype getNonChar() const { return NCattribute::getNonChar( chattr ); }
00255 STChar( const chtype & cha )
00256 : chattr( cha )
00257 {}
00258 };
00259
00260 struct StItem {
00261 const chtype & plain;
00262 const chtype & label;
00263 const chtype & data;
00264 const chtype & hint;
00265 StItem( const chtype & p, const chtype & l, const chtype & d, const chtype & h )
00266 : plain( p ), label( l ), data( d ), hint( h )
00267 {}
00268 };
00269
00270 struct StWidget : public StItem {
00271 const chtype & scrl;
00272 StWidget( const chtype & p, const chtype & l, const chtype & d, const chtype & h,
00273 const chtype & s )
00274 : StItem( p, l, d, h ), scrl( s )
00275 {}
00276 };
00277
00278 struct StList {
00279 const chtype & title;
00280 const StItem item;
00281 const StItem selected;
00282 StList( const chtype & t, const StItem & i, const StItem & s )
00283 : title( t ), item( i ), selected( s )
00284 {}
00285 const StItem & getItem( bool sel ) const { return sel ? selected : item; }
00286 };
00287
00288 struct StProgbar {
00289 const STChar bar;
00290 const STChar nonbar;
00291 StProgbar( const chtype & b, const chtype & nb )
00292 : bar( b ), nonbar( nb )
00293 {}
00294 };
00295
00296 struct StRichtext {
00297 const chtype & plain;
00298 const chtype & title;
00299 const chtype & link;
00300 const chtype & armedlink;
00301 const chtype & activearmedlink;
00302 const chtype & B;
00303 const chtype & I;
00304 const chtype & T;
00305 const chtype & BI;
00306 const chtype & BT;
00307 const chtype & IT;
00308 const chtype & BIT;
00309
00310 StRichtext( const chtype & p, const chtype & tit,
00311 const chtype & l, const chtype & al, const chtype & aal,
00312 const chtype & b, const chtype & i, const chtype & t,
00313 const chtype & bi, const chtype & bt, const chtype & it,
00314 const chtype & bit )
00315 : plain( p ), title( tit ),
00316 link( l ), armedlink( al ), activearmedlink( aal ),
00317 B( b ), I( i ), T( t ),
00318 BI( bi ), BT( bt ), IT( it ),
00319 BIT( bit )
00320 {}
00321 const chtype & getArmed( NC::WState s ) const {
00322 return ( s == NC::WSactive ) ? activearmedlink : armedlink;
00323 }
00324 };
00325
00326 struct StDialog {
00327 StBase border;
00328 StBase activeBorder;
00329 StBase dumb;
00330 StWidget disabled;
00331 StWidget normal;
00332 StWidget active;
00333 StWidget frame;
00334 StWidget activeFrame;
00335 StList list;
00336 StList activeList;
00337 StList disabledList;
00338 StProgbar progbar;
00339 StRichtext richtext;
00340 const chtype & cursor;
00341
00342 StDialog( const StBase & b, const StBase & ab, const StBase & d, const StWidget & dis,
00343 const StWidget & n, const StWidget & a,
00344 const StWidget & f, const StWidget & af,
00345 const StList & l, const StList & al, const StList & dl,
00346 const StProgbar & pbar,
00347 const StRichtext & rtext,
00348 const chtype & curs )
00349 : border( b ), activeBorder( ab ), dumb( d ), disabled( dis ),
00350 normal( n ), active( a ),
00351 frame( f ), activeFrame( af ),
00352 list( l ), activeList( al ), disabledList( dl ),
00353 progbar( pbar ),
00354 richtext( rtext ),
00355 cursor( curs )
00356 {}
00357
00358 public:
00359
00360 const StBase & getDlgBorder( bool active ) const { return active ? activeBorder : border; }
00361 const StBase & getDumb() const { return dumb; }
00362
00363 const StWidget & getWidget( NC::WState s, bool nonactive = false ) const {
00364 switch ( s ) {
00365 case NC::WSdisabeled: return disabled;
00366 case NC::WSactive: return nonactive ? normal : active;
00367 case NC::WSnormal:
00368 case NC::WSdumb:
00369 break;
00370 }
00371 return normal;
00372 }
00373
00374 const StWidget & getFrame( NC::WState s ) const {
00375 switch ( s ) {
00376 case NC::WSdisabeled: return disabled;
00377 case NC::WSactive: return activeFrame;
00378 case NC::WSnormal:
00379 case NC::WSdumb:
00380 break;
00381 }
00382 return frame;
00383 }
00384
00385 const StList & getList( NC::WState s ) const {
00386 switch ( s ) {
00387 case NC::WSdisabeled: return disabledList;
00388 case NC::WSactive: return activeList;
00389 case NC::WSnormal:
00390 case NC::WSdumb:
00391 break;
00392 }
00393 return list;
00394 }
00395 };
00396
00397 public:
00398
00400
00401
00402
00403 class Style : private NCattrset, public StDialog {
00404
00405 friend class NCstyle;
00406
00407 Style & operator=( const Style & );
00408
00409 private:
00410
00411 static unsigned sanitycheck();
00412 static NCattrset attrGlobal;
00413
00414 public:
00415
00416 NCattrset & getAttrGlobal() { return attrGlobal; }
00417 NCattrset & getAttrLocal () { return *this; }
00418
00419 private:
00420
00421 StDialog initDialog();
00422
00423 public:
00424
00425 Style();
00426 Style( const Style & rhs );
00427 virtual ~Style();
00428
00429 public:
00430
00431 const chtype & attr( STglobal a ) const { return attrGlobal[a]; }
00432 const chtype & attr( STlocal a ) const { return NCattrset::operator[]( a ); }
00433
00434 const chtype & operator()( STglobal a ) const { return attr( a ); }
00435 const chtype & operator()( STlocal a ) const { return attr( a ); }
00436 };
00437
00439
00440 public:
00441
00442 enum StyleSet {
00443 DefaultStyle,
00444 InfoStyle,
00445 WarnStyle,
00446 PopupStyle,
00447
00448 MaxStyleSet
00449 };
00450
00451 private:
00452
00453 string styleName;
00454 vector<Style> styleSet;
00455
00456 StyleSet fakestyle_e;
00457 void fakestyle( StyleSet f );
00458 Style & getStyle( StyleSet a ) { return styleSet[a]; }
00459
00460 public:
00461
00462 NCstyle( string term_t );
00463 ~NCstyle();
00464
00465 public:
00466
00467 const chtype & operator()( STglobal a ) const { return Style::attrGlobal[a]; }
00468 const Style & operator[]( StyleSet a ) const {
00469 if ( fakestyle_e != MaxStyleSet )
00470 return styleSet[fakestyle_e];
00471 return styleSet[a];
00472 }
00473
00474 public:
00475
00476 void changeSyle();
00477
00478 static string dumpName( StyleSet a );
00479 static string dumpName( STglobal a );
00480 static string dumpName( STlocal a );
00481 };
00482
00484
00485 #endif // NCstyle_h