home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc / qc20 / chrtsupt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-12-06  |  13.8 KB  |  506 lines

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