/////////////////////////////////////////////////////////////// // // frontend.cpp // // Implementation of a class that can help organize // a curses-based user interface. // // Jeff Ondich, 6/1/03 // /////////////////////////////////////////////////////////////// #include #include #include #include "frontend.h" FrontEnd::FrontEnd() { // Set up the curses environment. initscr(); raw(); noecho(); nonl(); // Draw the initial screen. mUserText[0] = '\0'; DrawScreen(); refresh(); } FrontEnd::~FrontEnd() { clear(); refresh(); mvcur( 0, COLS - 1, LINES - 1, 0 ); endwin(); } // Respond to keyboard input until user types ESC. // I'm just storing the user's string in an array (mUserText), and // DrawScreen puts this string and its upper-case version on // screen. This demonstrates how to deal with keyboard input // one character at a time. void FrontEnd::Run() { char ch; int n; while( true ) { ch = getch(); switch( ch ) { case 0x1B: // the ESC character return; case 0x08: // backspace n = strlen( mUserText ); if( n > 0 ) mUserText[n-1] = '\0'; else flash(); break; default: n = strlen( mUserText ); if( n < kUserTextBufferSize - 1 ) { mUserText[n] = ch; mUserText[n+1] = '\0'; } else flash(); break; } DrawScreen(); refresh(); } } // Put the proper characters in the proper places in the // standard screen (stdscr). These changes won't appear on the // user's monitor until refresh() is called. void FrontEnd::DrawScreen() { int i, halfHeight; char str[kUserTextBufferSize]; // Draw the lines. erase(); box( stdscr, '|', '-' ); halfHeight = LINES / 2; for( i=1; i < COLS - 1; i++ ) mvaddch( halfHeight, i, '-' ); // Draw captions and user strings. mvaddstr( 3, 2, "Type something here (ESC to quit)"); mvaddstr( 5, 4, mUserText ); mvaddstr( halfHeight + 3, 2, "Here's your text, in upper case."); for( i=0; mUserText[i] != '\0'; i++ ) str[i] = toupper( mUserText[i] ); str[i] = '\0'; mvaddstr( halfHeight + 5, 4, str ); // Set the cursor just beyond the input. move( 5, 4 + strlen(mUserText) ); }