home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c034 / 4.ddi / SOURCE / CHRTSUPT.C$ / CHRTSUPT.bin
Encoding:
Text File  |  1990-01-19  |  12.6 KB  |  482 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdarg.h>
  4. #include <string.h>
  5. #include <conio.h>
  6. #include <graph.h>
  7. #include <pgchart.h>
  8. #include "chrtdemo.h"
  9.  
  10. /* Variables to manage menus.  */
  11. int cMenuLevel = 0;                 /* Current menu level   */
  12. char *szMenuTitles[10];             /* Stack of menu titles */
  13.  
  14. char *pszBlankMenu[4];
  15.  
  16. /* Variables used to track control and screen position.  */
  17. extern struct SCREENINFO si;
  18.  
  19. /* Colors of menus and prompts. */
  20. extern struct tagColor co;
  21.  
  22. /*  BlankMenu - Gets responses to two specified choices.
  23.  *
  24.  *  Params: pchTitle - Menu title string
  25.  *          pchChoice1 - Selection 1 string
  26.  *          pchChoice2 - Selection 2 string
  27.  *
  28.  *  Return: Number of choice, or ESCAPE
  29.  */
  30. int BlankMenu( char *pchTitle, char *pchChoice1, char *pchChoice2 )
  31. {
  32.     int iChoice;
  33.  
  34.     /* Initialize title and selections.  */
  35.     pszBlankMenu[0] = pchTitle;
  36.     pszBlankMenu[1] = pchChoice1;
  37.     pszBlankMenu[2] = pchChoice2;
  38.     pszBlankMenu[3] = "\0";
  39.     PushTitle( pszBlankMenu[0]);
  40.  
  41.     while( TRUE )
  42.     {
  43.         /* Accept only first letter of either selection, or ESC.  */
  44.         iChoice = Menu( pszBlankMenu );
  45.         switch( iChoice )
  46.         {
  47.             case 1:
  48.             case 2:
  49.             case ESCAPE:
  50.                 return iChoice;
  51.         }
  52.     }
  53. }
  54.  
  55. /*  ClrForm - Clears the center of the screen form.
  56.  *
  57.  *  Params: None
  58.  */
  59. void ClrForm()
  60. {
  61.  
  62.     /* Set partial screen window and clear it, then reset full screen.  */
  63.     _settextwindow( si.top, 1, si.bot, 80 );
  64.     _clearscreen( _GWINDOW );
  65.     _settextwindow( 1, 1, 25, 80 );
  66.  
  67. }
  68.  
  69. /*  ClrHelp - Clears the current help line.
  70.  *
  71.  *  Params: None
  72.  */
  73. void ClrHelp()
  74. {
  75.     /* Decrement the help line counter and clear the line.  */
  76.     _settextwindow( --si.help, 1, si.help, 80 );
  77.     _clearscreen( _GWINDOW );
  78.     _settextwindow( 1, 1, 25, 80 );
  79. }
  80.  
  81. /*  ErrorMsg - Displays an error message.
  82.  *
  83.  *  Params: pchMsg - error message string
  84.  */
  85. void ErrorMsg( char *pchMsg )
  86. {
  87.  
  88.     /* Beep, set error color, and display error message and continue prompt.  */
  89.     putch( BEEP );
  90.     Help( pchMsg, co.ErrorColor );
  91.     Help( "Press any key to continue.", co.ErrorColor );
  92.  
  93.     /* Wait for keypress and clear help lines.  */
  94.     getch();
  95.     ClrHelp();
  96.     ClrHelp();
  97.  
  98. }
  99.  
  100. /*  Help - Displays a help line on the screen.
  101.  *
  102.  *  Params: pchMsg - error message string
  103.  *          sColor - color for message
  104.  */
  105. void Help( char *pchMsg, short sColor )
  106. {
  107.  
  108.     struct rccoord rcCursor;
  109.  
  110.     /* Save current cursor position.  */
  111.     rcCursor = _gettextposition();
  112.  
  113.     /* Print out help line and increment Helpline position variable.  */
  114.     PrintAt( si.help++, 5, pchMsg, sColor );
  115.  
  116.     /* Restore cursor position.  */
  117.     _settextposition( rcCursor.row, rcCursor.col );
  118.  
  119. }
  120.  
  121. /*  InputCh - Prompts for and returns a character of input.
  122.  *
  123.  *  Params: pchPrompt - Prompt string
  124.  *          pchAccept - String of acceptable characters (case insensitive)
  125.  *
  126.  *  Return: Character entered
  127.  */
  128. int InputCh( char *pchPrompt, char *pchAccept )
  129. {
  130.     int chResponse;
  131.  
  132.     /* Display prompt.  */
  133.     PrintAt( si.mid, 10, pchPrompt, co.InputColor );
  134.  
  135.     /* Loop until response is valid.  */
  136.     while( TRUE )
  137.     {
  138.         chResponse = toupper( getch() );
  139.  
  140.         /* Display and return if acceptable character, or beep if not.  */
  141.         if( *strchr( pchAccept, chResponse) )
  142.         {
  143.             _settextcolor( co.InfoColor );
  144.             putch( chResponse );
  145.             return chResponse;
  146.         }
  147.         else
  148.             putch( BEEP );
  149.     }
  150. }
  151.  
  152. /*  InputInt - Prompts for and returns an integer value within a
  153.  *  specified range.
  154.  *
  155.  *  Params: pchPrompt - Prompt string
  156.  *          iOld - Previous value
  157.  *          iMin - Minimum value of range
  158.  *          iMax - Maximum value of range
  159.  *
  160.  *  Return: integer input by user
  161.  */
  162. int InputInt( char *pchPrompt, int iOld, int iMin, int iMax )
  163. {
  164.     int i;
  165.     char szTmp[70];
  166.  
  167.     /* Prompt for a string input and convert to an integer until a
  168.      * value in the specified range is given. Then return the value.
  169.      */
  170.     do
  171.     {
  172.         InputStr( pchPrompt, itoa( iOld, szTmp, 10) );
  173.         i = atoi( szTmp );
  174.     } while( !InRange( i, iMin, iMax) );
  175.     return i;
  176. }
  177.  
  178. /*  InputFloat - Prompts for and returns a float value.
  179.  *
  180.  *  Params: pchPrompt - Prompt string
  181.  *          fOld - Previous value
  182.  *
  183.  *  Return: float input by user
  184.  */
  185. float InputFloat( char *pchPrompt, float fOld )
  186. {
  187.     char szTmp[70];
  188.  
  189.     /* Prompt for a string input and convert to a float. */
  190.     sprintf( szTmp, "%f", fOld );
  191.     InputStr( pchPrompt, szTmp );
  192.     return (float)atof( szTmp );
  193. }
  194.  
  195. /*  InputStr - Prompts for a string. Displays the previous string
  196.  *  until the first character is given. Then replaces it with new
  197.  *  entry.
  198.  *
  199.  *  Params: pchPrompt - Prompt string
  200.  *          pchOld - Charater buffer containing previous string; it
  201.  *            must be long enough to hold new string
  202.  *
  203.  *  Return: pointer to pchOld, which now contains new string
  204.  */
  205. char *InputStr( char *pchPrompt, char *pchOld )
  206. {
  207.     char szTmp[81];
  208.     int x = 5, y = si.mid, ch;
  209.  
  210.     /* Display prompt in center of form.  */
  211.     ClrForm();
  212.     PrintAt( y, x, pchPrompt, co.InputColor );
  213.     x += strlen( pchPrompt );
  214.  
  215.     /* Print the old value for reference.  */
  216.     _outtext( pchOld );
  217.     _settextposition( y, x );
  218.  
  219.     /* Wait for input. When received, clear old string.  */
  220.     while( !(ch = kbhit()) )
  221.         ;
  222.     memset( szTmp, ' ', 80 );
  223.     szTmp[80] = '\0';
  224.     PrintAt( y, x, szTmp, -1 );
  225.  
  226.     /* Get new string. If string entered, return it. If null string
  227.      * (ENTER key pressed), return old value.
  228.      */
  229.     _settextcolor( co.InfoColor );
  230.     _settextposition( y, x );
  231.     szTmp[0] = 70;             /* Maximum length to be read */
  232.  
  233.     cgets( szTmp );
  234.     if( szTmp[1] > 0 )         /* Are any characters read?  */
  235.     {
  236.         strcpy( pchOld, &szTmp[2] );
  237.         return &szTmp[2];
  238.     }
  239.     else
  240.     {
  241.         _settextposition( y, x );
  242.         return pchOld;
  243.     }
  244. }
  245.  
  246. /*  InRange - Checks an integer to see if it is in a specified range.
  247.  *
  248.  *  Params: iValue - Integer to check
  249.  *          iMin - Minimum value of range
  250.  *          iMax - Maximim value of range
  251.  *
  252.  *  Return: TRUE if in range, FALSE if not
  253.  */
  254. BOOL InRange( int Value, int iMin, int iMax )
  255. {
  256.     /* Check range and return true if valid, false if not. Note that
  257.      * (iMin >= iMax) is taken as a signal to check only the minimum
  258.      * value; there is no maximum.
  259.      */
  260.     if( Value >= iMin )
  261.         if( (Value <= iMax) || (iMin >= iMax) )
  262.             return TRUE;
  263.     else
  264.     {
  265.         ErrorMsg( "Invalid value." );
  266.         return FALSE;
  267.     }
  268. }
  269.  
  270. /*  Menu - Draws menu on screen and returns choice number.
  271.  *
  272.  *  Params: array of menu strings
  273.  *
  274.  *  Return: number corresponding to the choice made from the menu
  275.  */
  276. int Menu( char *pszMenuList[] )
  277. {
  278.     int iItem, cItem, yItem, x = 10;
  279.     int chResponse;
  280.  
  281.     /* Count menu items.  */
  282.     for( cItem = 1; *pszMenuList[cItem]; cItem++ )
  283.         ;
  284.     --cItem;
  285.  
  286.  
  287.     /* Clear the form and print the items in the menu.  */
  288.     WrtForm( 10 + cItem );
  289.     for( iItem = 1, yItem = 8; iItem <= cItem; iItem++, yItem++ )
  290.     {
  291.         PrintAt( yItem, x, pszMenuList[iItem], co.InputColor );
  292.         PrintChar( yItem, x, pszMenuList[iItem][0], co.HiliteColor );
  293.     }
  294.     ++yItem;
  295.  
  296.     /* Display prompt and help.  */
  297.     if( strcmpi( pszMenuList[0], "main menu" ) )    /* If not the main menu */
  298.         Help( "Type the first letter of your selection or ESC to back up.",
  299.                   co.InputColor );
  300.     else
  301.         Help( "Type the first letter of your selection or \"Q\" to quit.",
  302.                   co.InputColor );
  303.  
  304.     PrintAt( yItem, x += 5, "Choice? ", co.InfoColor );
  305.     x += 8;
  306.  
  307.     /* Loop until a valid choice is made. Beep at invalid choices.  */
  308.     while( TRUE )
  309.     {
  310.         _settextposition( yItem, x );
  311.         chResponse = toupper( getch() );
  312.  
  313.         /* Back up for ESC.  */
  314.         if( chResponse == 27 )
  315.         {
  316.             ClrHelp();
  317.             return ESCAPE;
  318.         }
  319.  
  320.         /* Search first letters of choices for a match. If found, return
  321.          * choice and clear help line.
  322.          */
  323.         for( iItem = 1; iItem <= cItem; iItem++ )
  324.         {
  325.             if( chResponse == toupper( pszMenuList[iItem][0]) )
  326.             {
  327.                 putch( chResponse );
  328.                 ClrHelp();
  329.                 return iItem;
  330.             }
  331.         }
  332.  
  333.         /* If we get here, no valid choice was found, so beep and repeat.  */
  334.         putch( BEEP );
  335.     }
  336. }
  337.  
  338. /*  PopTitle - Pops a menu title from the menu stack.
  339.  *
  340.  *  Params: None
  341.  */
  342. void PopTitle()
  343. {
  344.     szMenuTitles[--cMenuLevel] = "";
  345. }
  346.  
  347. /*  PrintAt - Prints a string at the row/column coordinates
  348.  *            specified, in the specified color.
  349.  *
  350.  *  Params: row        - row at which to begin output of string
  351.  *          col        - column at which to begin output of string
  352.  *          lpszString - zero (null) terminated string
  353.  *          sColor     - color in which to output string (-1 if
  354.  *                       PrintAt should leave color alone)
  355.  */
  356. void PrintAt( int row, int column, char _far *lpszString, short sColor )
  357. {
  358.     if( sColor != -1 )
  359.         _settextcolor( sColor );
  360.     _settextposition( row, column );
  361.     _outtext( lpszString );
  362. }
  363.  
  364. /*  PrintChar - Prints a character at the row/column coordinates
  365.  *              specified, in the specified color.
  366.  *
  367.  *  Params: row        - row at which to begin output of string
  368.  *          col        - column at which to begin output of string
  369.  *          cChar      - character to print
  370.  *          sColor     - color in which to output string (-1 if
  371.  *                       PrintChar should leave color alone)
  372.  */
  373. void PrintChar(int row, int column, char cChar, short sColor)
  374. {
  375.     char szTiny[2];
  376.  
  377.     szTiny[0] = cChar;
  378.     szTiny[1] = '\0';
  379.     PrintAt( row, column, szTiny, sColor );
  380. }
  381.  
  382. /*  PushTitle - Pushes a menu title on to the menu stack.
  383.  *
  384.  *  Params: pchTitle - title string to push
  385.  */
  386. void PushTitle( char *pchTitle )
  387. {
  388.     szMenuTitles[cMenuLevel++] = pchTitle;
  389. }
  390.  
  391. /*  SetDisplayColors - Set the colors to values appropriate to the display
  392.  *                     adaptor being used.
  393.  *
  394.  * Parms: None
  395.  */
  396. void SetDisplayColors()
  397. {
  398.     if( ismono( si.mode ) )
  399.     {
  400.         co.InputColor  = M_INPUTCOLOR;
  401.         co.HiliteColor = M_HILITECOLOR;
  402.         co.FormColor   = M_FORMCOLOR;
  403.         co.TitleColor  = M_TITLECOLOR;
  404.         co.ErrorColor  = M_ERRORCOLOR;
  405.         co.InfoColor   = M_INFOCOLOR;
  406.     }
  407.     else
  408.     {
  409.         co.InputColor  = C_INPUTCOLOR;
  410.         co.HiliteColor = C_HILITECOLOR;
  411.         co.FormColor   = C_FORMCOLOR;
  412.         co.TitleColor  = C_TITLECOLOR;
  413.         co.ErrorColor  = C_ERRORCOLOR;
  414.         co.InfoColor   = C_INFOCOLOR;
  415.     }
  416. }
  417.  
  418. /*  SprintAt - Format a string, using sprintf() and output to screen
  419.  *             using PrintAt.
  420.  *
  421.  *  Parms: iRow  - Row at which to begin display
  422.  *         iCol  - Column at which to begin display
  423.  *         szFmt - Format string (see run-time library documentation for
  424.  *                 correct formation of a format string)
  425.  *         ...   - Variables to output
  426.  */
  427. void SprintAt( int iRow, int iCol, char * szFmt, ... )
  428. {
  429.     char szTmp[81];
  430.     va_list Marker;
  431.     va_list saveMarker;
  432.  
  433.     va_start( Marker, szFmt );
  434.     saveMarker = Marker;
  435.     vsprintf( szTmp, szFmt, Marker );
  436.     va_end( Marker );
  437.  
  438.     PrintAt( iRow, iCol, szTmp, -1 );
  439. }
  440.  
  441. /*  WrtForm - Displays screen form.
  442.  *
  443.  *  Params: yBot - Row number of the bottom row
  444.  */
  445. void WrtForm( int yBot )
  446. {
  447.     int i;
  448.     char szTmp[81];
  449.  
  450.     /* Print message in upper right.  */
  451.     _clearscreen( _GCLEARSCREEN );
  452.     PrintAt( 1, 55, "Presentation Graphics Demo", co.TitleColor );
  453.  
  454.     /* Clear the top separator line.  */
  455.     memset( szTmp, ' ', 79 );
  456.     szTmp[79] = 0;
  457.  
  458.     /* Display each level of the menu title.  */
  459.     _settextposition( 5, 5 );
  460.     for( i = 0; i < cMenuLevel; i++ )
  461.     {
  462.         if( i )
  463.             _outtext( " - " );
  464.         _outtext( szMenuTitles[i] );
  465.     }
  466.  
  467.     /* Display the top separator line.  */
  468.     memset( szTmp, 196, 80 );
  469.     szTmp[80] = 0;
  470.     PrintAt( 6, 1, szTmp, co.FormColor );
  471.  
  472.     /* Display the bottom separator line.  */
  473.     PrintAt( yBot, 1, szTmp, co.FormColor );
  474.  
  475.     /* Set the global screen variables.  */
  476.  
  477.     si.help = yBot + 1;
  478.     si.top = 7;
  479.     si.bot = yBot - 1;
  480.     si.mid = (si.top + si.bot) / 2;
  481. }
  482.