// // Module: display.c // Version: V1.0 // Created: 7/1/97 // Programmer: Ralph Stickley // Environment: Borland 3.1 BGI graphics // // DISPlay interface functions for simple text based screen output // // Module Usage // Use display functions to setup screen for a simple text display. // Provides functions for popup dialog box messages and reseting // display to original configuration. // // Module Interface functions // Display_Init - initialize screen and display module // Display_Frame - displays a graphic '2 line' box and sets window // to area inside box // Display_Frame_Line - display a graphic '1 line' line within a box // Display_Message - draw dialog box with message and OK button // Display_Reset - reset display to original configuration // Display_Get_Key - get a specific key // // Internal Functions // // Display_Button - display a button and label it // Display_Save - store screen image to buffer // Display_Restore - restore screen image from buffer // //-------------------------------------------------------------------------- // Include Files //-------------------------------------------------------------------------- #include "conio.h" #include "string.h" #include "stdio.h" #include "viewer.h" #define BIOS_Get_Key() getch() //-------------------------------------------------------------------------- // Local constants //-------------------------------------------------------------------------- #define FRAME_MARGIN 4 #define BTN_HIGHLIGHT 1 #define BTN_NORMAL 2 //-------------------------------------------------------------------------- // Variables Global to this module //-------------------------------------------------------------------------- static int disp_buf[ 80 * 25 * 2 ]; // display buffer for dialog boxes struct text_info ti; //-------------------------------------------------------------------------- // Internal Functions //-------------------------------------------------------------------------- static void Display_Button( int x, int y, char *label, int status ) { gotoxy( x, y ); textbackground( WHITE ); textcolor( BLUE ); switch( status ) { case BTN_HIGHLIGHT: break; case BTN_NORMAL: break; } cprintf( "%s", label ); textbackground( BLACK ); textcolor( LIGHTGRAY ); } static void Display_Save( void ) { gettext(1, 1, 80, 25, disp_buf ); gettextinfo(&ti); } static void Display_Restore( void ) { puttext(1, 1, 80, 25, disp_buf ); window( ti.winleft, ti.wintop, ti.winright, ti.winbottom ); gotoxy( ti.curx, ti.cury ); } static void msg_size( char *str, int *max_len, int *num_strs ) { int max_so_far = 0, count = 0; int i = 0, slen; char *pstr = str; do { slen = strcspn( &pstr[ i ], "\r" ); if ( slen > max_so_far ) max_so_far = slen; count++; i += slen + 1; // only matched one character - skip it } while ( i < strlen( str ) ); *num_strs = count; *max_len = max_so_far; } //-------------------------------------------------------------------------- // Interface Functions //-------------------------------------------------------------------------- // // Display_Frame - displays a graphic '2 line' box and sets window // to area inside box // // Parameters - x, y - screen coordinates for upper left of frame // width - width of frame // height - height of frame // // returns - nothing // void STDCALL Display_Frame( int x, int y, int width, int height ) { int i; window( 1, 1, 80, 25 ); // draw top line gotoxy( x,y ); cprintf( "É" ); for ( i = 0; i < (width - 2); i++ ) cprintf( "Í" ); cprintf( "»" ); // draw sides for ( i = 1; i < height; i++ ) { gotoxy( x, y + i ); cprintf( "º" ); gotoxy( x + (width - 1 ), y + i ); cprintf( "º" ); } // draw bottom gotoxy( x,y + height ); cprintf( "È" ); for ( i = 0; i < (width - 2); i++ ) cprintf( "Í" ); cprintf( "¼" ); } void STDCALL Window_Frame( int x, int y, int width, int height ) { // set window within box given window( x + 1, y + 1, x + width - 2, y + height - 1 ); clrscr(); } // // Display_Frame_Line - display a graphic '1 line' line within a box // // parameters - none // // returns - nothing // void STDCALL Display_Frame_Line( void ) { struct text_info ti; int i; gettextinfo(&ti); gotoxy( 1, ti.cury ); // start at left edge of window for ( i = 0; i <= (ti.winright - ti.winleft); i++ ) cprintf( "Ä" ); } // // Display_Message - draw dialog box with message and OK button // // parameters - msg - message to be displayed 'centered' in dialog box // // returns - nothing // void STDCALL Display_Message( char *msg ) { int width; int height = 0; int msg_x = 5; _setcursortype( _NOCURSOR ); Display_Save( ); width = strlen( msg ) + 10; if ( width > 65 ) { // display multi-line message msg_size( msg, &width, &height ); width += 2; // add for frame msg_x = 1; // use format from given string (margins, etc) if ( width > 79 ) strcpy( msg, "Msg String too Long" ); } // draw frame and output message Display_Frame( 40 - (width / 2) - (FRAME_MARGIN / 2), 8, width, 7 + height ); Window_Frame ( 40 - (width / 2) - (FRAME_MARGIN / 2), 8, width, 7 + height ); gotoxy( msg_x, 3 ); cprintf( "%s", msg ); Display_Button( (width / 2) - FRAME_MARGIN, 5 + height, " OK ", BTN_NORMAL ); BIOS_Get_Key(); Display_Restore( ); _setcursortype( _NORMALCURSOR ); } // // Display_Init - initialize screen and display module // // parameters - none // // returns - 0 for success // int STDCALL Display_Init( void ) { // setup screen for display in text mode window( 1,1, 80, 25 ); _setcursortype( _NOCURSOR ); clrscr(); return( 0 ); } // // Display_Reset - reset display to original configuration // // parameters - none // // returns - nothing // void STDCALL Display_Reset( void ) { window( 1, 1, 80, 25 ); gotoxy ( 1, 25 ); _setcursortype(_NORMALCURSOR); } // // Display_String - puts a message string at the given row and column // // parameters - dataStr - text to be displayed // row - row number (1-25) // column - column number (1-80) // // returns - nothing // void Display_String( char *dataStr, int row, int col ) { gotoxy( col, row ); cprintf( "%s", dataStr ); } // // Display_Data - writes an integer in hex // at the given row and column // // parameters - dataIn - integer to be displayed // row - row number (1-25) // column - column number (1-80) // // returns - nothing // void Display_Data( int dataIn, int length, int row, int col ) { gotoxy( col, row ); if ( length == 2 ) cprintf( "%04X", dataIn ); else cprintf( "%02X", dataIn ); // display only LSB of data } // // Display_Get_Key - gets a key that matches the given key string // // parameters - key_str - list of characters that are valid // // returns - key pressed that matches one of the key_str characters // or ESC if escape key pressed // int STDCALL Display_Get_Key( char *key_str ) { int c; do { c = (BIOS_Get_Key() & 0xFF); if ( c == ESC ) return( ESC ); if ( c == ENTER ) return( ENTER ); if ( c == 0 ) { c = getch(); if ( c == PAGE_DN ) return( PAGE_DN ); if ( c == PAGE_UP ) return( PAGE_UP ); if ( c == LEFT_ARROW ) return( LEFT_ARROW ); if ( c == RIGHT_ARROW ) return( RIGHT_ARROW ); if ( c == DOWN_ARROW ) return( DOWN_ARROW ); if ( c == UP_ARROW ) return( UP_ARROW ); if ( c == KEY_F1 ) return( KEY_F1 ); } if (key_str[0] == '\0' ) return( c ); // else wait until valid key is pressed } while ( strchr( key_str, c ) == NULL ); return( c ); }