home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR23 / TOUCH2.ZIP / MENU.C < prev    next >
C/C++ Source or Header  |  1993-08-14  |  6KB  |  327 lines

  1. /*
  2.  * menu.c - menu handling function
  3.  *
  4.  * V. Abell
  5.  */
  6.  
  7. /*
  8.  * Copyright 1993 Victor A. Abell, Lafayette, Indiana  47906.  All rights
  9.  * reserved.
  10.  *
  11.  * Written by Victor A. Abell.
  12.  *
  13.  * Permission is granted to anyone to use this software for any purpose on
  14.  * any computer system, and to alter it and redistribute it freely, subject
  15.  * to the following restrictions:
  16.  *
  17.  * 1. Victor A. Abell is not responsible for any consequences of the use of
  18.  * this software.
  19.  *
  20.  * 2. The origin of this software must not be misrepresented, either by
  21.  *    explicit claim or by omission.  Credit to Victor A. Abell must
  22.  *    appear in documentation and sources.
  23.  *
  24.  * 3. Altered versions must be plainly marked as such, and must not be
  25.  *    misrepresented as being the original software.
  26.  *
  27.  * 4. This notice may not be removed or altered.
  28.  */
  29. #ifndef lint
  30. static char copyright[] =
  31. "@(#) Copyright 1993 Victor A. Abell.\nAll rights reserved.\n";
  32. #endif
  33.  
  34. #include "touch2.h"
  35.  
  36. short Cursor;
  37. short Mode = -1;
  38. struct videoconfig Vc;
  39. short Vmode = 0;            /* 0 = text; 1 = graphics */
  40.  
  41.  
  42. /*
  43.  * ClearRow() - clear a text row
  44.  */
  45.  
  46. void
  47. ClearRow(r, c)
  48.  
  49. #if    defined(UNIX)
  50.     int r;                /* row */
  51.     int c;                /* column */
  52. #else
  53.     short r;            /* row */
  54.     short c;            /* column */
  55. #endif
  56.  
  57. {
  58.     while (c <= ((r == Vc.numtextrows) ? Vc.numtextcols - 1
  59.                            : Vc.numtextcols)) {
  60.         _settextposition(r, c++);
  61.         _outtext(" ");
  62.     }
  63. }
  64.  
  65.  
  66. /*
  67.  * DispMenu() - display a menu
  68.  */
  69.  
  70. void
  71. DispMenu(m, b)
  72.     struct menu *m;            /* menu array */
  73.     char *b;                        /* bottom line contents */
  74. {
  75.     int i;
  76.  
  77.  
  78.     _clearscreen(_GCLEARSCREEN);
  79.     for (i = 0; m[i].msg; i++) {
  80.         _settextposition(m[i].row, m[i].col);
  81.         _outtext(m[i].msg);
  82.     }
  83.     PromptMsg((b == NULL) ? "Press ESC to exit." : b);
  84. }
  85.  
  86.  
  87. /*
  88.  * GetInp() - get input
  89.  */
  90.  
  91. int
  92. GetInp(r, c, p, def, ib, ibl)
  93.  
  94. #if    defined(UNIX)
  95.     int c;                /* column */
  96.     int r;                /* row */
  97. #else
  98.     short c;            /* column */
  99.     short r;            /* row */
  100. #endif
  101.     char *p;            /* prompt */
  102.     char *def;            /* default */
  103.     char *ib;            /* input buffer */
  104.     int ibl;            /* input buffer limit */
  105. {
  106.     short cc, sc;
  107.     int ch[2];
  108.     int eoi, i, j, n;
  109.  
  110.     PromptMsg("ESC - exit; RETURN - accept; CTRL-U - erase; BKSP, DELETE, arrow keys - edit.");
  111.     ClearRow(r, 1);
  112.     _settextposition(r, c);
  113.     _outtext(p);
  114.     sc = c + (short)strlen(p)  + 1;
  115.     _settextposition(r, sc);
  116.     if (def != NULL && *def != '\0') {
  117.         i = n = strlen(def);
  118.         if (n > (ibl - 1)) {
  119.             (void) fprintf(stderr,
  120.                 "GetInp: default (\"%s\") too long", def);
  121.             TouchExit(1);
  122.         }
  123.         if (def != ib)
  124.             (void) strcpy(ib, def);
  125.         cc = sc + n;
  126.         _outtext(ib);
  127.     } else {
  128.         i = n = 0;
  129.         ib[0] = '\0';
  130.         cc = sc;
  131.     }
  132.     _displaycursor(_GCURSORON);
  133.     _settextcursor(Cursor);
  134. /*
  135.  * Get the input.
  136.  */
  137.     for (eoi = 0; !eoi;) {
  138.         ch[1] = 0;
  139.         if ( ! kbhit()) {
  140.             reset_buffer();
  141.             continue;
  142.         }
  143.         if ((ch[0] = getch()) == 0) {
  144.     /*
  145.      * Get and process function or cursor keystroke.
  146.      */
  147.             ch[1] = getch();
  148.             switch (ch[1]) {
  149.         /*
  150.          * Delete the character under the cursor.
  151.          */
  152.             case CHDEL:
  153.                 if (i >= n-1)
  154.                     ib[i] = '\0';
  155.                 else {
  156.                     for (j = i; j < n; j++) {
  157.                         ib[j] = ib[j+1];
  158.                     }
  159.                 }
  160.                 n--;
  161.                 ClearRow(r, cc);
  162.                 _settextposition(r, cc);
  163.                 _outtext(&ib[i]);
  164.                 _settextposition(r, cc);
  165.                 continue;
  166.         /*
  167.          * Move the cursor left.
  168.          */
  169.             case LARW:
  170.                 if (cc == sc)
  171.                     putch(BELL);
  172.                 else {
  173.                     cc--;
  174.                     i--;
  175.                     _settextposition(r, cc);
  176.                 }
  177.                 continue;
  178.         /*
  179.          * Move the cursor right.
  180.          */
  181.             case RARW:
  182.                 if (i == n)
  183.                     putch(BELL);
  184.                 else {
  185.                     cc++;
  186.                     i++;
  187.                     _settextposition(r, cc);
  188.                 }
  189.                 continue;
  190.         /*
  191.          * Meaningless keystroke.
  192.          */
  193.             default:
  194.                 putch(BELL);
  195.             }
  196.             continue;
  197.         }
  198.         if (isprint(ch[0])) {
  199.     /*
  200.      * Display and store printable character.
  201.      */
  202.             if (i >= n) {
  203.                 if (i >= ibl - 1) {
  204.                     putch(BELL);
  205.                     continue;
  206.                 }
  207.                 ib[i] = (char)ch[0];
  208.                 ib[i+1] = '\0';
  209.                 n = i+1;
  210.             } else {
  211.                 if (n >= ibl - 1) {
  212.                     putch(BELL);
  213.                     continue;
  214.                 }
  215.                 for (j = ++n; j > i; j--)
  216.                     ib[j] = ib[j-1];
  217.                 ib[i] = (char)ch[0];
  218.             }
  219.             _settextposition(r, cc++);
  220.             _outtext(&ib[i++]);
  221.             _settextposition(r, cc);
  222.             continue;
  223.         }
  224.         if (iscntrl(ch[0])) {
  225.     /*
  226.      * Handle control characters.
  227.      */
  228.             switch (ch[0]) {
  229.         /*
  230.          * ESC - exit
  231.          */
  232.              case ESC:
  233.                 eoi = 1;
  234.                 n = 0;
  235.                 continue;
  236.         /*
  237.          * Backspace -- CTRL-H
  238.          */
  239.             case BS:
  240.                 if (cc == sc)
  241.                     continue;
  242.                 i--;
  243.                 if (i >= n-1)
  244.                     ib[i] = '\0';
  245.                 else {
  246.                     for (j = i; j < n; j++) {
  247.                         ib[j] = ib[j+1];
  248.                     }
  249.                 }
  250.                 n--;
  251.                 cc--;
  252.                 ClearRow(r, cc);
  253.                 _settextposition(r, cc);
  254.                 _outtext(&ib[i]);
  255.                 continue;
  256.         /*
  257.          * RETURN -- end of input
  258.          */
  259.             case CR:
  260.                 eoi = 1;
  261.                 continue;
  262.         /*
  263.          * Line erase -- CTRL-U
  264.          */
  265.             case DC4:
  266.                 cc = sc;
  267.                 i = n = 0;
  268.                 ib[0] = '\0';
  269.                 ClearRow(r, cc);
  270.                 _settextposition(r, cc);
  271.                 continue;
  272.             }
  273.         }
  274.     /*
  275.      * Beep on an unknown.
  276.      */
  277.         putch(BELL);
  278.     }
  279.     _displaycursor(_GCURSOROFF);
  280.     _settextcursor(NOCURSOR);
  281.     return(n);
  282. }
  283.  
  284.  
  285. /*
  286.  * InitMenu() - initialize menu processing
  287.  */
  288.  
  289. void
  290. InitMenu()
  291. {
  292.     struct videoconfig vc;
  293.  
  294.     _getvideoconfig(&vc);
  295.     if ((Mode = vc.mode) != _TEXTC80)
  296.         (void) _setvideomode(_TEXTC80);
  297.     Cursor = _gettextcursor();
  298.     _getvideoconfig(&Vc);
  299.     _displaycursor(_GCURSOROFF);
  300. }
  301.  
  302.  
  303. /*
  304.  * PromptMsg() - issue prompt message
  305.  */
  306.  
  307. void
  308. PromptMsg(m)
  309.     char *m;            /* message text */
  310. {
  311.     long pb;
  312.     short ptc;
  313.  
  314.     ptc = _gettextcolor();
  315.     ClearRow(Vc.numtextrows, 1);
  316.     _settextposition(Vc.numtextrows, 1);
  317.     if ( ! Vmode) {
  318.         pb = _getbkcolor();
  319.         _setbkcolor(PromptBkClrx & 7);
  320.     }
  321.     _settextcolor(PromptTxtClrx);
  322.     _outtext(m);
  323.     if ( ! Vmode)
  324.         _setbkcolor(pb);
  325.     _settextcolor(ptc);
  326. }
  327.