home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c040 / 1.ddi / SAMPLES / CHRTSUPT.C$ / CHRTSUPT.bin
Encoding:
Text File  |  1990-02-27  |  12.7 KB  |  486 lines

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