home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / AECUR101 / WGETSTR.C < prev    next >
C/C++ Source or Header  |  1990-03-08  |  9KB  |  340 lines

  1. /*----------------------------------------------------------------------
  2.  *
  3.  *  wgetstr.c
  4.  *
  5.  *  copyright (c) 1987,88,89,90 J. Alan Eldridge
  6.  *
  7.  *  The Berkeley Curses function is:
  8.  *
  9.  *  wgetstr(win,buf)
  10.  *
  11.  *  The extended functions are:
  12.  *
  13.  *  wegetstr(win,buf)
  14.  *  weditstr(win,buf,max)
  15.  *  weditfld(win,buf,len,term,ins,skip,fix,zap)
  16.  *  wedittxt(win,buf,len,term,ins,skip,fix,filter)
  17.  *
  18.  *----------------------------------------------------------------------
  19.  */
  20.  
  21. #include "curses.h"
  22.  
  23. /*----------------------------------------------------------------------
  24.  *
  25.  * WEDITSTR
  26.  *
  27.  * edit buf with maximum length max, output to win, end on newline
  28.  * the newline is not echoed and is not in the buffer
  29.  *
  30.  *----------------------------------------------------------------------
  31.  */
  32.  
  33. static int
  34. okfunc()
  35. {
  36.     return 1;
  37. }
  38.  
  39. int
  40. weditstr(win, buf, cnt)
  41. WINDOW  *win;
  42. char    *buf;
  43. int     cnt;
  44. {
  45.     static int nl_esc[] = { K_NL, K_ESC, K_ILLEGAL };
  46.  
  47.     return wedittxt(win, buf, cnt, nl_esc, 1, 0, -1, 0, okfunc);
  48. }
  49.  
  50. /*----------------------------------------------------------------------
  51.  *
  52.  * WGETSTR
  53.  *
  54.  * the standard Curses string input function
  55.  *
  56.  * gets a string, ends on newline, output to win, newline is echoed
  57.  *
  58.  *----------------------------------------------------------------------
  59.  */
  60.  
  61. int
  62. wgetstr(win, buf)
  63. WINDOW  *win;
  64. char    *buf;
  65. {
  66.     *buf = 0;
  67.     return wegetstr(win,buf);
  68. }
  69.  
  70. /*----------------------------------------------------------------------
  71.  * 
  72.  * WEGETSTR
  73.  *
  74.  * same as wgetstr (which calls it), but the buffer is not emptied
  75.  *
  76.  *----------------------------------------------------------------------
  77.  */
  78.  
  79. int
  80. wegetstr(win, buf)
  81. WINDOW  *win;
  82. char    *buf;
  83. {
  84.     static int nl[] = { K_NL, K_ILLEGAL };
  85.  
  86.     int c;
  87.     char localbuf[512];
  88.  
  89.     strcpy(localbuf,buf);
  90.     c = weditfld(win, localbuf, 511, nl, 1, 0, -1, 0);
  91.     strcpy(buf, localbuf);
  92.     if (c == '\n') {
  93.         waddch(win, c);
  94.         wrefresh(win);
  95.     }
  96.  
  97.     return c;
  98. }
  99.  
  100. /*----------------------------------------------------------------------
  101.  *
  102.  *  WEDITTXT & WEDITFLD -- main editing functions
  103.  *
  104.  *  wedittxt(win, buf, len, term, ins, skip, fix, zap, filter)
  105.  *  WINDOW  *win;
  106.  *  char    *buf;
  107.  *  int     len;
  108.  *  int     *term;
  109.  *  int     ins,
  110.  *          skip,
  111.  *          fix,
  112.  *          zap;
  113.  *  int     (*filter)();
  114.  *
  115.  *  edit the field whose initial contents are contained in buf
  116.  *  screen output is to window win
  117.  *  the maximum length of the field contents is len
  118.  *  term is string of ints which, when received from the
  119.  *      keyboard, will terminate the edit
  120.  *  ins tells whether insert mode can be turned on
  121.  *  skip tells whether auto-field skipping should occur:
  122.  *      (this is for data entry forms, and only works if fix,
  123.  *      explained next, is set > 0)
  124.  *      if you're at the end of the buffer and press space or
  125.  *      you're at the beginning of the buffer and cursor left
  126.  *      the edit terminates, returning TAB or BACKTAB
  127.  *  fix tells how to handle trailing spaces:
  128.  *      < 0 means truncate trailing spaces
  129.  *      0 means leave them alone
  130.  *      > 0 add spaces and keep field length at its maximum
  131.  *  zap tells whether to erase the field if the first character typed
  132.  *      is a printing character (useful for data entry forms)    
  133.  *  filter is a function that takes 1 argument and returns non-zero
  134.  *      if that character is ok to put into the text and zero if the
  135.  *      character is illegal. the character passed will be a printing
  136.  *      character.
  137.  *
  138.  *  weditfld() takes all arguments that wedittxt() does except
  139.  *  for the filter; no filtering is done.
  140.  *
  141.  *----------------------------------------------------------------------
  142.  */
  143.  
  144. int
  145. weditfld(win, buf, len, term, ins, skip, fix, zap)
  146. WINDOW  *win;
  147. char    *buf;
  148. int     len;
  149. int *term;
  150. int     ins,
  151.         skip,
  152.         fix,
  153.         zap;
  154. {
  155.     return wedittxt(win, buf, len, term, ins, skip, fix, zap, okfunc);
  156. }
  157.  
  158. int
  159. wedittxt(win, buf, len, term, ins, skip, fix, zap, filter)
  160. WINDOW  *win;
  161. char    *buf;
  162. int     len;
  163. int     *term;
  164. int     ins,
  165.         skip,
  166.         fix,
  167.         zap;
  168. int     (*filter)();
  169. {
  170.     int c, 
  171.         l = strlen(buf), 
  172.         insmode = 0, 
  173.         pos = 0, 
  174.         y, x;
  175.  
  176.     char *cp = buf + l;
  177.  
  178.     if (fix > 0 && l < len) {
  179.         while (l < len) {
  180.             l++;
  181.             *cp++ = ' ';
  182.         }
  183.         *cp = 0;
  184.     } else if (fix < 0) {
  185.         while (l > 0 && *(cp-1) == ' ') {
  186.             l--;
  187.             *cp-- = 0;
  188.         }
  189.     }
  190.  
  191.     getyx(win,y,x);
  192.     waddstr(win,buf);
  193.     wmove(win,y,x);
  194.     wrefresh(win);
  195.  
  196.     while (kb_matchkey(term, c=wgetch(win)) == -1) {
  197.         if (skip && fix > 0)
  198.             if ((c == ' ' || c == K_RIGHT) && pos == len)
  199.                 return '\t';
  200.             else if ((c == '\b' || c == K_LEFT) && pos == 0)
  201.                 return K_BACKTAB;
  202.         if (isascii(c) && isprint(c) && (*filter)(c)) {
  203.             if (zap) {
  204.                 int y,x,f;
  205.  
  206.                 while (pos > 0) {
  207.                     pos--;
  208.                     waddch(win, '\b');
  209.                 }
  210.                 f = fix > 0 ? ' ' : 0;
  211.                 getyx(win,y,x);
  212.                 while (l > 0) {
  213.                     buf[--l] = f;
  214.                     waddch(win,' ');
  215.                 }
  216.                 if (fix > 0)
  217.                     l = len;
  218.                 wmove(win,y,x);
  219.                 zap = 0;
  220.             }
  221.             if (pos >= l)
  222.                 if (l < len) {
  223.                     buf[l++]=c;
  224.                     buf[++pos] = 0;
  225.                     waddch(win, c);
  226.                 } else
  227.                     beep();
  228.             else if (insmode)
  229.                 if (l < len || (l == len && buf[l-1] == ' ')) {
  230.                     int n, y, x;
  231.                     for (n=l-(l>=len);n>pos;n--)
  232.                         buf[n] = buf[n-1];
  233.                     waddch(win, buf[pos]=c);
  234.                     if (l < len)
  235.                         buf[++l] = 0;
  236.                     getyx(win, y, x);
  237.                     waddstr(win, buf+(++pos));
  238.                     wmove(win, y, x);
  239.                 } else
  240.                     beep();
  241.             else
  242.                 waddch(win, buf[pos++]=c);
  243.             wrefresh(win);
  244.             zap = 0;
  245.             continue;
  246.         }
  247.         switch (c) {
  248.         case K_LEFT:
  249.             if (pos > 0) {
  250.                 pos--;
  251.                 waddch(win, '\b');
  252.             } else
  253.                 beep();
  254.             break;
  255.         case K_RIGHT:
  256.             if (pos < l) {
  257.                 pos++;
  258.                 waddch(win, winch(win));
  259.             } else
  260.                 beep();
  261.             break;
  262.         case K_HOME:
  263.             while (pos > 0) {
  264.                 pos--;
  265.                 waddch(win, '\b');
  266.             }
  267.             break;
  268.         case K_END:
  269.             while (pos < l) {
  270.                 pos++;
  271.                 waddch(win, winch(win));
  272.             }
  273.             break;
  274.         case K_INS:
  275.             if (ins)
  276.                 insmode = !insmode;
  277.             else
  278.                 beep();
  279.             break;
  280.         case K_CTL_X:
  281.             /* ^x cancels line */
  282.             {
  283.                 int y,x,f;
  284.  
  285.                 while (pos > 0) {
  286.                     pos--;
  287.                     waddch(win, '\b');
  288.                 }
  289.                 f = fix > 0 ? ' ' : 0;
  290.                 getyx(win,y,x);
  291.                 while (l > 0) {
  292.                     buf[--l] = f;
  293.                     waddch(win,' ');
  294.                 }
  295.                 if (fix > 0)
  296.                     l = len;
  297.                 wmove(win,y,x);
  298.             }
  299.             break;
  300.         case '\b':
  301.         case K_DEL:
  302.             if (c == '\b')
  303.                 if (pos > 0) {
  304.                     pos--;
  305.                     waddch(win, '\b');
  306.                 } else {
  307.                     beep();
  308.                     break;
  309.                 }
  310.             if (pos < l && l > 0) {
  311.                 int p,y,x;
  312.                 for (p = pos; p < l-1; p++)
  313.                     buf[p] = buf[p+1];
  314.                 buf[p] = ' ';
  315.                 getyx(win,y,x);
  316.                 waddstr(win,buf+pos);
  317.                 wmove(win,y,x);
  318.                 if (fix <= 0) {
  319.                     buf[p] = 0;
  320.                     l--;
  321.                 }
  322.             } else
  323.                 beep();
  324.             break;
  325.         default:
  326.             beep();
  327.             break;
  328.         }
  329.         wrefresh(win);
  330.         zap = 0;
  331.     }
  332.  
  333.     if (c == '\n') 
  334.         while (pos++ < l) 
  335.             waddch(win, winch(win));
  336.  
  337.     return c;
  338. }
  339.  
  340.