home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 414_02 / demos / tui.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-17  |  17.1 KB  |  698 lines

  1. /********************************* tui.c ************************************/
  2. /*
  3.  * File   : tui.c      'textual user interface'
  4.  * Author : P.J. Kunst  (kunst@prl.philips.nl)
  5.  * Date   : 25-02-93
  6.  * Version: 1.02
  7.  */
  8.  
  9. #include <ctype.h>
  10. #include <curses.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <time.h>
  15. #include "tui.h"
  16.  
  17. #ifdef PDCDEBUG
  18. char *rcsid_tui = "$Header: C:\CURSES\demos\RCS\tui.c 2.1 1993/06/18 20:24:13 MH Rel MH $";
  19. #endif
  20.  
  21. #if defined(__unix) && !defined(GO32)
  22. #define CPUACCOUNT                /* necessary if CPU cycles are to be paid! */
  23. #endif
  24.  
  25. #define TITLECOLOR         1                   /* color pair indices */
  26. #define MAINMENUCOLOR      (2 | A_BOLD)
  27. #define MAINMENUREVCOLOR   (3 | A_BOLD | A_REVERSE)
  28. #define SUBMENUCOLOR       (4 | A_BOLD)
  29. #define SUBMENUREVCOLOR    (5 | A_BOLD | A_REVERSE)
  30. #define BODYCOLOR          6
  31. #define STATUSCOLOR        (7 | A_BOLD)
  32. #define INPUTBOXCOLOR      8 
  33. #define EDITBOXCOLOR       (9 | A_BOLD | A_REVERSE)
  34.  
  35.  
  36. #define  th   1                   /* title window height */
  37. #define  mh   1                   /* main menu height */
  38. #define  sh   2                   /* status window height */
  39. #define  bh   (LINES-th-mh-sh-1)  /* body window height */
  40. #define  bw   COLS                /* body window width */
  41.  
  42.  
  43. /******************************* STATIC ************************************/
  44.  
  45. static WINDOW *wtitl, *wmain, *wbody, *wstat; /* title, menu, body, status win*/
  46. static int nexty, nextx;
  47. static chtype key = ERR, ch = ERR;
  48. static bool quit = FALSE;
  49. static bool incurses = FALSE;
  50.  
  51. #ifndef PDCURSES
  52. static char wordchar (void) { return 0x17; }  /* ^W */ 
  53. #endif
  54.  
  55. static char *padstr (char *s, int length)
  56. {
  57.   static char buf[MAXSTRLEN];
  58.   char fmt[10];
  59.  
  60.   sprintf (fmt, strlen(s)>length ? "%%.%ds" : "%%-%ds", length);
  61.   sprintf (buf, fmt, s);
  62.   return buf;
  63. }
  64.  
  65. static char *prepad (char *s, int length)
  66. {
  67.   int i;
  68.   char *p = s;
  69.  
  70.   if (length > 0)
  71.   {
  72.     memmove ((void *)(s+length), (const void *)s, strlen(s)+1);
  73.     for (i=0; i<length; i++) *p++ = ' ';
  74.   }
  75.   return s;
  76. }
  77.  
  78. static void rmline (WINDOW *win, int nr)   /* keeps box lines intact */
  79. {
  80.   mvwaddstr (win, nr, 1, padstr(" ", bw-2));
  81.   wrefresh (win);
  82. }
  83.  
  84. static void initcolor (void)
  85. {
  86. #ifdef A_COLOR
  87.   if (has_colors()) start_color();     /* foreground, background */
  88.   init_pair (TITLECOLOR       & ~A_ATTR, COLOR_BLACK, COLOR_CYAN);      
  89.   init_pair (MAINMENUCOLOR    & ~A_ATTR, COLOR_WHITE, COLOR_CYAN);    
  90.   init_pair (MAINMENUREVCOLOR & ~A_ATTR, COLOR_WHITE, COLOR_BLACK);
  91.   init_pair (SUBMENUCOLOR     & ~A_ATTR, COLOR_WHITE, COLOR_CYAN);    
  92.   init_pair (SUBMENUREVCOLOR  & ~A_ATTR, COLOR_WHITE, COLOR_BLACK);   
  93.   init_pair (BODYCOLOR        & ~A_ATTR, COLOR_WHITE, COLOR_BLUE);      
  94.   init_pair (STATUSCOLOR      & ~A_ATTR, COLOR_WHITE, COLOR_CYAN);   
  95.   init_pair (INPUTBOXCOLOR    & ~A_ATTR, COLOR_BLACK, COLOR_CYAN);
  96.   init_pair (EDITBOXCOLOR     & ~A_ATTR, COLOR_WHITE, COLOR_BLACK);
  97. #endif
  98. }
  99.  
  100. static void setcolor (WINDOW *win, int color)
  101. {
  102.   int attr = color & A_ATTR;  /* extract Bold, Reverse, Blink bits */
  103.  
  104. #ifdef A_COLOR
  105.   attr &= ~A_REVERSE;  /* ignore reverse, use colors instead! */
  106.   wattrset (win, COLOR_PAIR(color & A_CHARTEXT) | attr);
  107. #else
  108.   attr &= ~A_BOLD;     /* ignore bold, gives messy display on HP-UX */
  109.   wattrset (win, attr);
  110. #endif
  111. }
  112.  
  113. static void colorbox (WINDOW *win, int color, int hasbox)
  114. {
  115.   setcolor (win, color);
  116.   werase (win); 
  117.   if (hasbox && win->_maxy > 2) box (win, 0, 0);
  118.   touchwin (win); wrefresh (win);
  119. }
  120.  
  121. static void idle (void)
  122. {
  123.   char buf[MAXSTRLEN];
  124.   time_t t;
  125.   struct tm *tp;
  126.  
  127.   if (time (&t) == -1) return;  /* time not available */
  128.   tp = localtime(&t);
  129.   sprintf (buf, " %.2d-%.2d-%.2d  %.2d:%.2d:%.2d", 
  130.     tp->tm_mday, tp->tm_mon + 1, tp->tm_year,
  131.     tp->tm_hour, tp->tm_min, tp->tm_sec);
  132.   mvwaddstr (wtitl, 0, bw-strlen(buf)-2, buf);
  133.   wrefresh (wtitl); 
  134. #ifdef CPUACCOUNT
  135.   sleep (1);    /* necessary if CPU cycles are to be paid ! */
  136. #endif
  137. }
  138.  
  139. static void menudim (menu *mp, int *lines, int *columns)
  140. {
  141.   int n, l, max = 0;
  142.  
  143.   for (n=0; mp->func; n++, mp++)
  144.     if ((l = strlen(mp->name)) > max) max = l;
  145.   *lines = n;
  146.   *columns = max + 2;
  147. }
  148.  
  149. static void setmenupos (int y, int x)
  150. {
  151.   nexty = y; nextx = x;
  152. }
  153.  
  154. static void getmenupos (int *y, int *x)
  155. {
  156.   *y = nexty; *x = nextx;
  157. }
  158.  
  159. static chtype hotkey (char *s)
  160. {
  161.   int c, c0 = *s;  /* if no upper case found, return first char */
  162.  
  163.   for (c = *s; c; s++) if (isupper(c)) break;
  164.   return c ? c : c0;
  165. }
  166.  
  167. static void repaintmenu (WINDOW *wmenu, menu *mp)
  168. {
  169.   int i;
  170.   menu *p = mp;
  171.  
  172.   for (i=0; p->func; i++, p++)
  173.     mvwaddstr (wmenu, i+1, 2, p->name);
  174.   touchwin (wmenu); wrefresh (wmenu);
  175. }
  176.  
  177. static void repaintmainmenu (int width, menu *mp)
  178. {
  179.   int i;
  180.   menu *p = mp;
  181.  
  182.   for (i=0; p->func; i++, p++)
  183.     mvwaddstr (wmain, 0, i*width, prepad (padstr(p->name, width-1), 1));
  184.   touchwin (wmain); wrefresh (wmain);
  185. }
  186.  
  187. static void mainhelp (void)
  188. {
  189. #ifdef ALT_X
  190.   statusmsg ("Use arrow keys and Enter to select (Alt-X to quit)");
  191. #else
  192.   statusmsg ("Use arrow keys and Enter to select");
  193. #endif
  194. }
  195.  
  196. static void hidecursor (void)
  197. {
  198. #if defined(PDCURSES) || defined(SYSV)
  199.   curs_set (0);
  200. #endif
  201. }
  202.  
  203. static void normalcursor (void)
  204. {
  205. #if defined(PDCURSES) || defined(SYSV)
  206.   curs_set (1);
  207. #endif
  208. }
  209.  
  210. static void insertcursor (void)
  211. {
  212. #if defined(PDCURSES) || defined(SYSV)
  213.   curs_set (2);
  214. #endif
  215. }
  216.  
  217. static void mainmenu (menu *mp)
  218. {
  219.   int nitems, barlen, old = -1, cur = 0, c, cur0;
  220.  
  221.   menudim (mp, &nitems, &barlen);
  222.   repaintmainmenu (barlen, mp);
  223.  
  224.   while (!quit)
  225.   {
  226.     if (cur != old)
  227.     {
  228.       if (old != -1)
  229.       {
  230.         mvwaddstr (wmain, 0, old*barlen, 
  231.           prepad (padstr(mp[old].name, barlen-1), 1));
  232.         statusmsg (mp[cur].desc);
  233.       }
  234.       else mainhelp ();
  235.       setcolor (wmain, MAINMENUREVCOLOR);
  236.       mvwaddstr (wmain, 0, cur*barlen, 
  237.         prepad (padstr(mp[cur].name, barlen-1), 1));
  238.       setcolor (wmain, MAINMENUCOLOR);
  239.       old = cur;
  240.       wrefresh (wmain);
  241.     }
  242.     switch (c = (key != ERR ? key : waitforkey()))
  243.     {
  244.       case KEY_DOWN:
  245.       case '\n':                              /* menu item selected ! */
  246.         touchwin (wbody); wrefresh (wbody);
  247.         rmerror ();
  248.         setmenupos (th+mh, cur*barlen);
  249.         normalcursor ();
  250.         (mp[cur].func)();                     /* perform function */
  251.         hidecursor ();
  252.         switch (key)
  253.         {
  254.           case KEY_LEFT:
  255.             cur = (cur+nitems-1) % nitems;
  256.             key = '\n';
  257.             break;
  258.  
  259.           case KEY_RIGHT:
  260.             cur = (cur+1) % nitems;
  261.             key = '\n';
  262.             break;
  263.  
  264.           default:
  265.             key = ERR;
  266.             break;
  267.         }
  268.         repaintmainmenu (barlen, mp);
  269.         old = -1;
  270.         break;
  271.  
  272.       case KEY_LEFT:
  273.         cur = (cur+nitems-1) % nitems;
  274.         break;
  275.  
  276.       case KEY_RIGHT:
  277.         cur = (cur+1) % nitems;
  278.         break;
  279.  
  280.       case KEY_ESC:
  281.         mainhelp ();
  282.         break;
  283.  
  284.       default:
  285.         cur0 = cur;
  286.         do { cur = (cur+1) % nitems;
  287.         } while ((cur != cur0) && (hotkey(mp[cur].name) != toupper(c)));
  288.         if (hotkey(mp[cur].name) == toupper(c)) key = '\n';
  289.         break;
  290.     }
  291.   }
  292.   rmerror ();
  293.   touchwin (wbody); wrefresh (wbody);
  294. }
  295.  
  296. static void cleanup (void) /* cleanup curses settings */
  297. {
  298.   if (incurses)
  299.   {
  300.     delwin (wtitl);
  301.     delwin (wmain);
  302.     delwin (wbody);
  303.     delwin (wstat);
  304.     normalcursor ();
  305.     endwin (); 
  306.     incurses = FALSE;
  307.   }
  308. }
  309.  
  310.  
  311. /******************************* EXTERNAL **********************************/
  312.  
  313. void clsbody (void)
  314. {
  315.   werase (wbody); wmove (wbody, 0, 0);
  316. }
  317.  
  318. int bodylen (void)
  319. {
  320.   return wbody->_maxy;
  321. }
  322.  
  323. WINDOW *bodywin (void)
  324. {
  325.   return wbody;
  326. }
  327.  
  328. void rmerror (void)
  329. {
  330.   rmline (wstat, 0);
  331. }
  332.  
  333. void rmstatus (void)
  334. {
  335.   rmline (wstat, 1);
  336. }
  337.  
  338. void titlemsg (char *msg)
  339. {
  340.   mvwaddstr (wtitl, 0, 2, padstr(msg, bw-3));
  341.   wrefresh (wtitl);
  342. }
  343.  
  344. void bodymsg (char *msg)
  345. {
  346.   waddstr (wbody, msg);
  347.   wrefresh (wbody);
  348. }
  349.  
  350. void errormsg (char *msg)
  351. {
  352.   beep ();
  353.   mvwaddstr (wstat, 0, 2, padstr(msg, bw-3));
  354.   wrefresh (wstat);
  355. }
  356.  
  357. void statusmsg (char *msg)
  358. {
  359.   mvwaddstr (wstat, 1, 2, padstr(msg, bw-3));
  360.   wrefresh (wstat);
  361. }
  362.  
  363. bool keypressed (void)
  364. {
  365.   if (ch == ERR) ch = wgetch(wbody);
  366.   return ch != ERR;
  367. }
  368.  
  369. chtype getkey (void)
  370. {
  371.   chtype c = ch;
  372.  
  373.   ch = ERR;
  374. #ifdef ALT_X
  375.   quit = (c == ALT_X);     /* PC only ! */
  376. #endif
  377.   return c;
  378. }
  379.  
  380. void flushkeys (void)
  381. {
  382.   while (keypressed()) getkey();
  383. }
  384.  
  385. chtype waitforkey (void)
  386. {
  387.   flushkeys ();
  388.   while (!keypressed()) idle ();
  389.   return getkey ();
  390. }
  391.  
  392. void DoExit (void)   /* terminate program */
  393. {
  394.   quit = TRUE;
  395. }
  396.  
  397. void domenu (menu *mp)
  398. {
  399.   int y, x, nitems, barlen, mheight, mw, old = -1, cur = 0, cur0;
  400.   bool stop = FALSE;
  401.   WINDOW *wmenu;
  402.  
  403.   hidecursor ();
  404.   getmenupos (&y, &x);
  405.   menudim (mp, &nitems, &barlen);
  406.   mheight = nitems+2; mw = barlen+2;
  407.   wmenu = newwin (mheight, mw, y, x);
  408.   colorbox (wmenu, SUBMENUCOLOR, 1);
  409.   repaintmenu (wmenu, mp);
  410.  
  411.   key = ERR;
  412.   while (!stop && !quit)
  413.   {
  414.     if (cur != old)
  415.     {
  416.       if (old != -1)
  417.         mvwaddstr (wmenu, old+1, 1, prepad (padstr(mp[old].name, barlen-1), 1));
  418.       setcolor (wmenu, SUBMENUREVCOLOR);
  419.       mvwaddstr (wmenu, cur+1, 1, prepad (padstr(mp[cur].name, barlen-1), 1));
  420.       setcolor (wmenu, SUBMENUCOLOR);
  421.       statusmsg (mp[cur].desc);
  422.       old = cur;
  423.       wrefresh (wmenu);
  424.     }
  425.     switch (key = ((key != ERR) ? key : waitforkey()))
  426.     {
  427.       case '\n':                              /* menu item selected ! */
  428.         touchwin (wbody); wrefresh (wbody);
  429.         setmenupos (y+1, x+1);
  430.         rmerror ();
  431.         key = ERR;
  432.         normalcursor ();
  433.         (mp[cur].func)();                     /* perform function */
  434.         hidecursor ();
  435.         repaintmenu (wmenu, mp);
  436.         old = -1;
  437.         break;
  438.  
  439.       case KEY_UP:
  440.         cur = (cur+nitems-1) % nitems;
  441.         key = ERR;
  442.         break;
  443.  
  444.       case KEY_DOWN:
  445.         cur = (cur+1) % nitems;
  446.         key = ERR;
  447.         break;
  448.  
  449.       case KEY_ESC:
  450.       case KEY_LEFT:
  451.       case KEY_RIGHT:
  452.         if (key == KEY_ESC) key = ERR;  /* return to previous submenu */
  453.         stop = TRUE;
  454.         break;
  455.  
  456.       default:
  457.         cur0 = cur;
  458.         do { cur = (cur+1) % nitems;
  459.         } while ((cur != cur0) && (hotkey(mp[cur].name) != toupper((int)key)));
  460.         key = (hotkey(mp[cur].name) == toupper((int)key)) ? '\n' : ERR;
  461.         break;
  462.     }
  463.   }
  464.   rmerror ();
  465.   delwin (wmenu);
  466.   touchwin (wbody); wrefresh (wbody);
  467. }
  468.  
  469. void startmenu (menu *mp, char *title)
  470. {
  471.   initscr ();
  472.   incurses = TRUE;
  473.   initcolor ();
  474. /*  atexit (cleanup); */
  475.  
  476.   wtitl = subwin (stdscr, th,  bw,    0, 0);
  477.   wmain = subwin (stdscr, mh,  bw,   th, 0);
  478.   wbody = subwin (stdscr, bh,  bw,  th+mh, 0);
  479.   wstat = subwin (stdscr, sh,  bw, th+mh+bh, 0);
  480.  
  481.   colorbox (wtitl, TITLECOLOR, 0);
  482.   colorbox (wmain, MAINMENUCOLOR, 0);
  483.   colorbox (wbody, BODYCOLOR, 0);
  484.   colorbox (wstat, STATUSCOLOR, 0);
  485.  
  486.   if (title) titlemsg (title);
  487.  
  488.   cbreak ();                /* direct input (no newline required)... */
  489.   noecho ();                /* ... without echoing */
  490.   hidecursor ();            /* hide cursor (if possible) */
  491.   nodelay (wbody, TRUE);    /* don't wait for input */
  492.   keypad (wbody, TRUE);     /* enable cursor keys */
  493.   scrollok (wbody, TRUE);   /* enable scrolling in main window */
  494.   leaveok (stdscr, TRUE);
  495.   leaveok (wtitl, TRUE);
  496.   leaveok (wmain, TRUE);
  497.   leaveok (wstat, TRUE);
  498.  
  499.   mainmenu (mp);
  500.  
  501.   cleanup ();
  502. }
  503.  
  504.  
  505. /*man-start*********************************************************************
  506.  
  507.   weditstr()     - edit string
  508.  
  509.   PDCurses Description:
  510.         The initial value of 'str' with a maximum length of 'field'-1,
  511.         which is supplied by the calling routine, is editted. The user's 
  512.         erase (^H), kill (^U) and delete word (^W) chars are interpreted. 
  513.         The PC insert or Tab keys toggle between insert and edit mode.
  514.         Escape aborts the edit session, leaving 'str' unchanged.
  515.         Enter, Up or Down Arrow are used to accept the changes to 'str'.
  516.  
  517.         NOTE: editstr(), mveditstr(), and mvweditstr() are macros.
  518.  
  519.   PDCurses Return Value:
  520.         These functions return the input terminating character on 
  521.         success (Escape, Enter, Up or Down Arrow) and ERR on error.
  522.  
  523.   PDCurses Errors:
  524.         It is an error to call this function with a NULL window pointer.
  525.         The length of the initial 'str' must not exceed 'field'-1.
  526.  
  527.   Portability:
  528.         PDCurses        chtype weditstr( WINDOW* win, char* str, int field );
  529.         X/Open Dec '88 
  530.         BSD Curses    
  531.         SYS V Curses 
  532.  
  533. **man-end**********************************************************************/
  534.  
  535.  
  536. static void repainteditbox (WINDOW *win, int x, char *buf)
  537. {
  538.   wmove (win, 0, 0); wclrtoeol (win); wprintw (win, "%s", buf); 
  539.   wmove (win, 0, x); wrefresh (win); 
  540. }
  541.  
  542. chtype weditstr (WINDOW *win, char *buf, int field)
  543. {
  544.   char org[MAXSTRLEN], *tp, *bp = buf;
  545.   bool defdisp = TRUE, stop = FALSE, insert = FALSE;
  546.   int cury, curx, begy, begx, oldattr;
  547.   WINDOW *wedit;
  548.   chtype c;
  549.  
  550.   if ((field>=MAXSTRLEN) || (buf==NULL) || (strlen(buf) > field-1)) return ERR;
  551.   strcpy (org, buf);  /* save original */
  552.  
  553.   wrefresh (win);
  554.   getyx (win, cury, curx); getbegyx (win, begy, begx);
  555.   wedit = subwin (win, 1, field, begy+cury, begx+curx);  /* window relative */
  556.   oldattr = wedit->_attrs;
  557.   colorbox (wedit, EDITBOXCOLOR, 0);
  558.  
  559.   normalcursor ();
  560. #ifdef CPUACCOUNT
  561.   nodelay (win, FALSE);
  562.   keypad (wedit, TRUE);
  563. #endif
  564.   while (!stop)
  565.   {
  566.     repainteditbox (wedit, bp-buf, buf);
  567. #ifdef CPUACCOUNT
  568.     switch (c = wgetch(wedit))
  569. #else
  570.     switch (c = waitforkey())
  571. #endif
  572.     {
  573.       case KEY_ESC:
  574.         strcpy (buf, org);  /* restore original */
  575.         stop = TRUE;
  576.         break;
  577.  
  578.       case '\n':  /* ENTER */
  579.       case KEY_UP:
  580.       case KEY_DOWN:
  581.         stop = TRUE;
  582.         break;
  583.  
  584.       case KEY_LEFT:
  585.         if (bp > buf) bp--;
  586.         break;
  587.  
  588.       case KEY_RIGHT:
  589.         defdisp = FALSE;
  590.         if (bp - buf < strlen(buf)) bp++;
  591.         break;
  592.  
  593.       case '\t':     /* TAB, because Insert not properly handled on HP-UX ! */
  594.       case KEY_IC:   /* enter insert mode */
  595.       case KEY_EIC:  /* exit insert mode */
  596.         defdisp = FALSE;
  597.         insert = !insert;
  598.         if (insert) insertcursor (); else normalcursor ();
  599.         break;
  600.  
  601.       default:
  602.         if (c == erasechar())  /* backspace, ^H */
  603.         {
  604.           if (bp > buf)
  605.           {
  606.             memmove ((void *)(bp-1), (const void *)bp, strlen(bp)+1);
  607.             bp--;
  608.           }
  609.         }
  610.         else if (c == killchar())  /* ^U */
  611.         {
  612.           bp = buf; *bp = '\0';
  613.         }
  614.         else if (c == wordchar())  /* ^W */
  615.         {
  616.           tp = bp;
  617.           while ((bp > buf) && (*(bp-1) == ' ')) bp--;
  618.           while ((bp > buf) && (*(bp-1) != ' ')) bp--;
  619.           memmove ((void *)bp, (const void *)tp, strlen(tp)+1);
  620.         }
  621.         else if (isprint(c))
  622.         {
  623.           if (defdisp) { bp = buf; *bp = '\0'; defdisp = FALSE; }
  624.           if (insert)
  625.           {
  626.             if (strlen(buf) < field-1)
  627.             {
  628.               memmove ((void *)(bp+1), (const void *)bp, strlen(bp)+1);
  629.               *bp++ = c;
  630.             }
  631.           }
  632.           else if (bp-buf < field-1)
  633.           {
  634.             if (!*bp) bp[1] = '\0'  /* append new string terminator */;
  635.             *bp++ = c;
  636.           }
  637.         }
  638.         break;
  639.     }
  640.   }
  641. #ifdef CPUACCOUNT
  642.   nodelay (win, TRUE);
  643. #endif
  644.   wattrset (wedit, oldattr);
  645.   repainteditbox (wedit, bp-buf, buf);
  646.   delwin (wedit);
  647.   return c;
  648. }
  649.  
  650. WINDOW *winputbox (WINDOW *win, int nlines, int ncols)
  651. {
  652.   int cury, curx, begy, begx;
  653.   WINDOW *winp;
  654.  
  655.   getyx (win, cury, curx); getbegyx (win, begy, begx);
  656.   winp = newwin (nlines, ncols, begy+cury, begx+curx); /* window relative */
  657.   colorbox (winp, INPUTBOXCOLOR, 1);
  658.   return winp;
  659. }
  660.  
  661. chtype getstrings (const char *desc[], char *buf[], int field)
  662. {
  663.   WINDOW *winput;
  664.   int oldy, oldx, maxy, maxx, nlines, ncols, i, n, l, max = 0;
  665.   chtype c;
  666.   bool stop = FALSE;
  667.  
  668.   for (n=0; desc[n]; n++) if ((l = strlen(desc[n])) > max) max = l;
  669.   nlines = n + 2; ncols = max + field + 4;
  670.   getyx (wbody, oldy, oldx); getmaxyx (wbody, maxy, maxx);
  671.   winput = mvwinputbox (wbody, (maxy-nlines)/2, (maxx-ncols)/2, nlines, ncols);
  672.   for (i=0; i<n; i++) mvwprintw (winput, i+1, 2, "%s", desc[i]);
  673.   i = 0;
  674.   while (!stop)
  675.   {
  676.     switch (c = mvweditstr(winput, i+1, max+3, buf[i], field))
  677.     {
  678.       case KEY_ESC:
  679.         stop = TRUE;
  680.         break;
  681.  
  682.       case KEY_UP:
  683.         i = (i+n-1)%n;
  684.         break;
  685.  
  686.       case '\n':
  687.       case '\t':
  688.       case KEY_DOWN:
  689.         if (++i == n) stop = TRUE;  /* all passed ? */
  690.         break;
  691.     }
  692.   }
  693.   delwin (winput);
  694.   touchwin (wbody); wmove (wbody, oldy, oldx); wrefresh (wbody);
  695.   return c;
  696. }
  697. /********************************* tui.c ************************************/
  698.