home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <ctype.h>
- #include <strings.h>
- #include "term.h"
-
- #define TRUE 1
- #define FALSE 0
- #define BASE 3
-
- int MenuPick(Prompt, ChoiceList, ChoiceCount)
- char *Prompt;
- char **ChoiceList;
- int ChoiceCount;
-
- /*
- ---------------------------------------------------------------------------
-
- Last revision -
- 23 October 1984 - GWS
-
-
- NAME
- MenuPick - "crash-proof" routine for picking one of a list of strings
-
- SYNOPSIS
- int MenuPick(Prompt, ChoiceList, ChoiceCount)
- char *Prompt;
- char **ChoiceList;
- int ChoiceCount;
-
- DESCRIPTION
- This routine prompts and nudges the user through selection of a
- string from a table of strings - useful for choosing an item
- from a menu for instance. The options are displayed in a menu
- format with one at a time highlighted. The current item is
- selected by pressing RETURN. The space bar advances, the
- up-arrow backs up. The return value of the function is the
- index of the chosen string (0 indexed).
-
- SEE ALSO
-
-
- DIAGNOSTICS
- none
-
- BUGS
- none known
-
- AUTHOR
- George W. Sherouse
- 22 October 1984
-
- ---------------------------------------------------------------------------
- */
-
- {
- int c,
- start,
- stop,
- select;
-
- /*
- In the silly case where there is only one choice, just return the index 0.
- */
- if (ChoiceCount == 1)
- return(0);
-
- select = 0;
- paint_screen(Prompt, ChoiceList, ChoiceCount, &start, &stop, select, TRUE);
- while (1)
- {
- switch (c = (getchar() & 0x7f))
- {
- case '\015':
- TermSetUp();
- return(select);
- case ' ':
- if (++select == ChoiceCount)
- select = 0;
- paint_screen(Prompt, ChoiceList, ChoiceCount, &start, &stop,
- select, FALSE);
- break;
- case '^':
- if (--select < 0)
- select = ChoiceCount - 1;
- paint_screen(Prompt, ChoiceList, ChoiceCount, &start, &stop,
- select, FALSE);
- break;
- case '>':
- select = stop + 1;
- if (select == ChoiceCount)
- select = 0;
- paint_screen(Prompt, ChoiceList, ChoiceCount, &start, &stop,
- select, FALSE);
- break;
- case '\021':
- case '\023':
- break;
- default:
- printf("%c", '\007');
- }
- }
- }
-
- paint_screen(Prompt, ChoiceList, ChoiceCount, start, stop, select, first)
- char *Prompt;
- char **ChoiceList;
- int ChoiceCount;
- int *start;
- int *stop;
- int select;
- int first;
-
- {
- int tgetnum();
-
- static int too_many = FALSE,
- current,
- previous,
- old_start,
- cookie;
-
- int loop,
- loop1,
- line,
- temp;
-
- char erase;
-
- if (first)
- {
-
- /*
- Find out if we need to cope with magic cookies.
- */
- if ((cookie = tgetnum("ug")) < 0)
- cookie = 0;
-
- if (ChoiceCount > 15)
- too_many = TRUE;
-
- current = old_start = -1;
- previous = *start = 0;
- if (too_many)
- *stop = 14;
- else
- *stop = ChoiceCount - 1;
-
- erase = TermSetUp(); /* set no echo, single char input */
- /* get erase character */
-
- first = FALSE;
- }
- else
- {
- if (select < *start)
- *start -= 15;
- if (select > *stop)
- *start += 15;
- if (*start >= ChoiceCount)
- *start = 0;
- if (*start < 0)
- *start = ((ChoiceCount - 1) / 15) * 15;
-
- *stop = *start + 14;
- if (*stop >= ChoiceCount)
- *stop = ChoiceCount - 1;
- }
-
- /*
- Clear the stage for the action
- */
- if (*start == old_start)
- {
- gotoxy(1, BASE + previous);
- ClearEOL();
- gotoxy(1, BASE + previous);
- for (loop = 0; loop < cookie; loop++)
- printf(" ");
- printf("%s", ChoiceList[*start + previous]);
-
- current = select - *start;
- gotoxy(1, BASE + current);
- ClearEOL();
- gotoxy(1, BASE + current);
- underline(TRUE);
- printf("%s", ChoiceList[*start + current]);
- underline(FALSE);
-
- previous = current;
- }
- else
- {
- page();
- center(1, Prompt, 1);
- line = BASE;
-
- for (loop = *start; loop <= *stop; loop++)
- {
- gotoxy(1, line++);
- if (loop == select)
- {
- underline(TRUE);
- printf("%s", ChoiceList[loop]);
- underline(FALSE);
- }
- else
- {
- for (loop1 = 0; loop1 < cookie; loop1++)
- printf(" ");
- printf("%s", ChoiceList[loop]);
- }
- }
- previous = select - *start;
- old_start = *start;
- }
-
- gotoxy(1, 23);
- printf("Press RETURN to accept, SPACE to step forward, ^ to back up");
- if (too_many)
- printf(", > for next page");
- }
-