1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
const char SET_BACK = '&';
const char SET_FORE = '^';
const int MAX_STRING = 1024;
void wsconout( WINDOW* win, char* strIn )
{
short color_pair, fore, back;
char* str;
// Get current colors
wattr_get( win, 0, &color_pair, 0 );
pair_content( color_pair, &fore, &back );
for ( str = strIn; str - strIn < MAX_STRING; ++str )
{
switch( *str ) {
default: // Output all unrecognized chars literally
waddch( win, *str );
break;
case '\0': // End-of-string, done
return;
case SET_BACK:
case SET_FORE:
{
char mode = *str++;
short newColor;
switch( toupper( *str )) {
case 'C': newColor = COLOR_CYAN; break;
case 'Y': newColor = COLOR_YELLOW; break;
case 'G': newColor = COLOR_GREEN; break;
case 'R': newColor = COLOR_RED; break;
case 'B': newColor = COLOR_BLUE; break;
case 'W': newColor = COLOR_WHITE; break;
case 'K': newColor = COLOR_BLACK; break;
case 'P': newColor = COLOR_MAGENTA; break;
case '\0': // Output lone mode char if end-of-string
waddch( win, mode );
return;
default: // Print something we don't recognize literally
waddch( win, mode );
// Just print one mode char for two in a row
if( *str != mode ) waddch( win, *str );
goto SkipIt; // two-level break
}
newColor += isupper( *str ) ? 8 : 0; // Set brightness
if( mode == SET_BACK ) back = newColor;
else fore = newColor;
wattrset( win, COLOR_PAIR( fore * COLORS + back ));
}
SkipIt: ;
break;
}
}
}
void wvconout( WINDOW* win, char* fmt, va_list args )
{
char str[MAX_STRING];
vsprintf( str, fmt, args );
wsconout( win, str );
}
void conout( char *fmt, ... )
{
va_list args;
va_start( args, fmt );
wvconout( stdscr, fmt, args );
va_end( args );
}
void wconout( WINDOW* win, char* fmt, ... )
{
va_list args;
va_start( args, fmt );
wvconout( win, fmt, args );
va_end( args );
}
void init_color_pairs() {
short f, b;
for( f = 0; f < COLORS; ++f )
for( b = 0; b < COLORS; ++b )
init_pair( f * COLORS + b, f, b );
}
int main( int argc, char *argv[] )
{
initscr();
start_color();
init_color_pairs();
bkgd( COLOR_PAIR( (COLOR_YELLOW + 8) * COLORS + COLOR_BLACK ));
conout( "Hello world\n" );
conout( "&k^wHello, &B^WWorld!\n" );
conout( "&k^cW^Ch^ya^Yt'^gs ^Gu^rp^R?\n" );
conout( "&k^BCOLORS: %d \n&b^RCOLOR_PAIRS: %d\n", COLORS, COLOR_PAIRS );
getch();
endwin();
}
|