home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume3 / libc_term / pick_one.c < prev    next >
C/C++ Source or Header  |  1986-11-30  |  3KB  |  133 lines

  1. #include <curses.h>
  2. #include <ctype.h>
  3. #include <strings.h>
  4. #include "c_term.h"
  5.  
  6. int pick_one(ChoiceList, ChoiceCount)
  7. char **ChoiceList;
  8. int ChoiceCount;
  9.  
  10. /*
  11.  ---------------------------------------------------------------------------
  12.  
  13.    Last revision - 
  14.     6 January 1985 - GWS
  15.     Change to use curses
  16.  
  17.     16 November 1984 - GWS
  18.     Ignore XON, XOFF
  19.  
  20.      12 April 1984 - GWS
  21.  
  22.  
  23.    NAME
  24.      pick_one - "crash-proof" routine for picking one of a list of
  25.           list of strings
  26.  
  27.    SYNOPSIS
  28.     int pick_one(ChoiceList, ChoiceCount)
  29.     char **ChoiceList;
  30.     int ChoiceCount;
  31.  
  32.    DESCRIPTION
  33.     This routine prompts and nudges the user through selection of a
  34.     string from a table of strings - useful for choosing an item
  35.     from a menu for instance.  The options are displayed one at a
  36.     time.  The current item is selected by pressing return.  The
  37.     space bar advances, the up-arrow backs up.  The return
  38.     value of the function is the index of the chosen string.
  39.  
  40.    SEE ALSO
  41.  
  42.  
  43.    DIAGNOSTICS
  44.     none 
  45.  
  46.    BUGS
  47.     none known
  48.  
  49.    AUTHOR
  50.      George W. Sherouse
  51.      11 April 1984
  52.  
  53.  ---------------------------------------------------------------------------
  54. */
  55.  
  56. {
  57.     int c;
  58.     int val;
  59.     int biggest;
  60.     int temp;
  61.     int loop;
  62.     int    where_y,
  63.     where_x;
  64.     char Format[80];
  65.  
  66.     int strlen();
  67.  
  68. /*
  69. In the silly case where there is only one choice, just print it
  70. and return the index 0.
  71. */
  72.     if (ChoiceCount == 1)
  73.     {
  74.     printw("%s", ChoiceList[0]);
  75.     return(0);
  76.     }
  77. /*
  78. Find the longest string in the bunch
  79. */
  80.     biggest = strlen(ChoiceList[0]);
  81.     for (loop = 1; loop < ChoiceCount; loop++)
  82.     if ((temp = strlen(ChoiceList[loop])) > biggest)
  83.         biggest = temp;
  84.  
  85.     getyx(stdscr, where_y, where_x);
  86.     standout();
  87.     for (loop = 0; loop < biggest; loop++)
  88.     printw(" ");
  89.  
  90.     sprintf(Format, "%%-%ds", biggest);
  91.  
  92.     val = 0;
  93.     while (1)
  94.     {
  95.     mvprintw(where_y, where_x, Format, ChoiceList[val]);
  96.     refresh();
  97.  
  98.     switch (c = (getch() & 0177))
  99.     {
  100. #ifdef ABORT_CHAR
  101.     case ABORT_CHAR:
  102.         clear();
  103.         standend();
  104.         mvprintw(0, 0, "Program aborted at your request...");
  105.         move(LINES - 1, 0);
  106.         refresh();
  107.         endwin();
  108.         exit(0);
  109.         break;
  110. #endif ABORT_CHAR
  111.  
  112.     case '\015':
  113.         standend();
  114.         mvprintw(where_y, where_x, Format, ChoiceList[val]);
  115.         refresh();
  116.         return(val);
  117.     case ' ':
  118.         if (++val == ChoiceCount)
  119.         val = 0;
  120.         break;
  121.     case '^':
  122.         if (--val < 0)
  123.         val = ChoiceCount - 1;
  124.         break;
  125.     case '\021':
  126.     case '\023':
  127.         break;
  128.     default:
  129.         fprintf(stderr, "%c", '\007');
  130.     }
  131.     }
  132. }
  133.