home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume3 / lib_term / MenuPick.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  4.1 KB  |  223 lines

  1. #include <stdio.h>
  2. #include <ctype.h>
  3. #include <strings.h>
  4. #include "term.h"
  5.  
  6. #define TRUE 1
  7. #define FALSE 0
  8. #define BASE 3
  9.  
  10. int MenuPick(Prompt, ChoiceList, ChoiceCount)
  11. char *Prompt;
  12. char **ChoiceList;
  13. int ChoiceCount;
  14.  
  15. /*
  16.  ---------------------------------------------------------------------------
  17.  
  18.    Last revision - 
  19.      23 October 1984 - GWS
  20.  
  21.  
  22.    NAME
  23.      MenuPick - "crash-proof" routine for picking one of a list of strings
  24.  
  25.    SYNOPSIS
  26.     int MenuPick(Prompt, ChoiceList, ChoiceCount)
  27.     char *Prompt;
  28.     char **ChoiceList;
  29.     int ChoiceCount;
  30.  
  31.    DESCRIPTION
  32.     This routine prompts and nudges the user through selection of a
  33.     string from a table of strings - useful for choosing an item
  34.     from a menu for instance.  The options are displayed in a menu
  35.     format with one at a time highlighted.  The current item is
  36.     selected by pressing RETURN.  The space bar advances, the 
  37.     up-arrow backs up.  The return value of the function is the
  38.     index of the chosen string (0 indexed).
  39.  
  40.    SEE ALSO
  41.  
  42.  
  43.    DIAGNOSTICS
  44.     none 
  45.  
  46.    BUGS
  47.     none known
  48.  
  49.    AUTHOR
  50.      George W. Sherouse
  51.      22 October 1984
  52.  
  53.  ---------------------------------------------------------------------------
  54. */
  55.  
  56. {
  57.     int    c,
  58.         start,
  59.         stop,
  60.         select;
  61.  
  62. /*
  63. In the silly case where there is only one choice, just return the index 0.
  64. */
  65.     if (ChoiceCount == 1)
  66.     return(0);
  67.  
  68.     select = 0;
  69.     paint_screen(Prompt, ChoiceList, ChoiceCount, &start, &stop, select, TRUE);
  70.     while (1)
  71.     {
  72.     switch (c = (getchar() & 0x7f))
  73.     {
  74.     case '\015':
  75.         TermSetUp();
  76.         return(select);
  77.     case ' ':
  78.         if (++select == ChoiceCount)
  79.         select = 0;
  80.         paint_screen(Prompt, ChoiceList, ChoiceCount, &start, &stop,
  81.             select, FALSE);
  82.         break;
  83.     case '^':
  84.         if (--select < 0)
  85.         select = ChoiceCount - 1;
  86.         paint_screen(Prompt, ChoiceList, ChoiceCount, &start, &stop,
  87.             select, FALSE);
  88.         break;
  89.     case '>':
  90.         select = stop + 1;
  91.         if (select == ChoiceCount)
  92.         select = 0;
  93.         paint_screen(Prompt, ChoiceList, ChoiceCount, &start, &stop,
  94.             select, FALSE);
  95.         break;
  96.     case '\021':
  97.     case '\023':
  98.         break;
  99.     default:
  100.         printf("%c", '\007');
  101.     }
  102.     }
  103. }
  104.  
  105. paint_screen(Prompt, ChoiceList, ChoiceCount, start, stop, select, first)
  106. char *Prompt;
  107. char **ChoiceList;
  108. int ChoiceCount;
  109. int *start;
  110. int *stop;
  111. int select;
  112. int first;
  113.  
  114. {
  115.     int        tgetnum();
  116.  
  117.     static int    too_many = FALSE,
  118.         current,
  119.         previous,
  120.         old_start,
  121.         cookie;
  122.  
  123.     int        loop,
  124.         loop1,
  125.         line,
  126.         temp;
  127.  
  128.     char    erase;
  129.  
  130.     if (first)
  131.     {
  132.  
  133. /*
  134. Find out if we need to cope with magic cookies.
  135. */
  136.     if ((cookie = tgetnum("ug")) < 0)
  137.         cookie = 0;
  138.  
  139.     if (ChoiceCount > 15)
  140.         too_many = TRUE;
  141.  
  142.     current = old_start = -1;
  143.     previous = *start = 0;
  144.     if (too_many)
  145.         *stop = 14;
  146.     else
  147.         *stop = ChoiceCount - 1;
  148.  
  149.     erase = TermSetUp();    /* set no echo, single char input */
  150.                     /* get erase character */
  151.  
  152.     first = FALSE;
  153.     }
  154.     else
  155.     {
  156.     if (select < *start)
  157.         *start -= 15;
  158.     if (select > *stop)
  159.         *start += 15;
  160.     if (*start >= ChoiceCount)
  161.         *start = 0;
  162.     if (*start < 0)
  163.         *start = ((ChoiceCount - 1) / 15) * 15;
  164.  
  165.     *stop = *start + 14;
  166.     if (*stop >= ChoiceCount)
  167.         *stop = ChoiceCount - 1;
  168.     }
  169.  
  170. /*
  171. Clear the stage for the action
  172. */
  173.     if (*start == old_start)
  174.     {
  175.     gotoxy(1, BASE + previous);
  176.     ClearEOL();
  177.     gotoxy(1, BASE + previous);
  178.     for (loop = 0; loop < cookie; loop++)
  179.         printf(" ");
  180.     printf("%s", ChoiceList[*start + previous]);
  181.  
  182.     current = select - *start;
  183.     gotoxy(1, BASE + current);
  184.     ClearEOL();
  185.     gotoxy(1, BASE + current);
  186.     underline(TRUE);
  187.     printf("%s", ChoiceList[*start + current]);
  188.     underline(FALSE);
  189.  
  190.     previous = current;
  191.     }
  192.     else
  193.     {
  194.     page();
  195.     center(1, Prompt, 1);
  196.     line = BASE;
  197.  
  198.     for (loop = *start; loop <= *stop; loop++)
  199.     {
  200.         gotoxy(1, line++);
  201.         if (loop == select)
  202.         {
  203.         underline(TRUE);
  204.         printf("%s", ChoiceList[loop]);
  205.         underline(FALSE);
  206.         }
  207.         else
  208.         {
  209.         for (loop1 = 0; loop1 < cookie; loop1++)
  210.             printf(" ");
  211.         printf("%s", ChoiceList[loop]);
  212.         }
  213.     }
  214.     previous = select - *start;
  215.     old_start = *start;
  216.     }
  217.  
  218.     gotoxy(1, 23);
  219.     printf("Press RETURN to accept, SPACE to step forward, ^ to back up");
  220.     if (too_many)
  221.     printf(", > for next page");
  222. }
  223.