home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / ils / get.c next >
Encoding:
C/C++ Source or Header  |  1992-02-28  |  4.6 KB  |  303 lines

  1. /* %W% general getstring routine                     */
  2. /* author: Jack Alexander - this routine may be freely distibuted */
  3. #include    <curses.h>
  4. #include    "get.h"
  5.  
  6. get_num(x,y,digits,low,high,number)
  7. int    x,y,digits,low,high,*number;
  8. {
  9.     char    buf[80];
  10.     int    retval;
  11.  
  12.     buf[0]='\0';
  13.     noecho();
  14.     raw();
  15.     while(1)
  16.         switch(getst(digits,x,y,buf,digits+1,NULL,NUM_ONLY,NULL)) {
  17.             case GET_ESCAPE:
  18.                 return(GET_ESCAPE);
  19.             case GET_DOWN:
  20.             case GET_RETURN:
  21.                 retval=atoi(buf);
  22.                 if(retval < low || retval>high)
  23.                     break;
  24.                 *number = retval;
  25.                 echo();
  26.                 noraw();
  27.                 return(0);
  28.             default:
  29.                 break;
  30.         }
  31. }
  32.  
  33. /* a few hints...    */
  34. /* idx = current index into string */
  35. /* cx = current screen x position */
  36. /* cy = current screen y position */
  37. /* ix = initial screen x (upon call, where cursor is requested to be placed) */
  38. /* iy = initial screen y " " " ... */
  39. /* fw = field width */
  40. /* mx = max length of string */
  41. /* sl = string length */
  42. /* valid = string of acceptable characters within the type passed in gettype */
  43. /*         if NULL, then all type of characters passed in gettype are allowed */
  44.  
  45. int    idx, cx, cy, ix, iy, fw, mx, sl, rcode;
  46.  
  47. getst(len,sx,sy,str,fl,retlen,gettype,valid)
  48. int    len, sx, sy, fl, *retlen, gettype;
  49. char    *str, *valid;
  50. {
  51.     int    c;
  52.  
  53.  
  54.     rcode = INITIAL;
  55.     idx=0;
  56.     cx=ix=sx;
  57.     cy=iy=sy;
  58.     mx = len;
  59.     fw = fl;
  60.     str[mx]='\0';
  61.     sl=strlen(str);
  62.     if(sl!=0) {
  63.         idx =sl;        /* place cursor at string's end */
  64.         cx +=sl;
  65.     }
  66. #ifdef undef
  67.     init();
  68. #endif
  69.     while(1) {
  70.         if(gettype==SHOW) {    /* want only to display the string */
  71.             drawstr(str,0);    /* draw the string */
  72. #ifdef undef
  73.             de_init();
  74. #endif
  75.             return;
  76.         }
  77.         drawstr(str,1);    /* draw the string */
  78.         c=getch()&0x7f;
  79.         switch(c) {
  80.             case GET_CLEAR:
  81.                 clearall(str);
  82.                 break;
  83.             case GET_ESCAPE:
  84.                 rcode=GET_ESCAPE;
  85.                 break;
  86.             case GET_UP:
  87.                 rcode=dec_y_pos();
  88.                 break;
  89.             case GET_DOWN:
  90.                 rcode=inc_y_pos();
  91.                 break;
  92.             case GET_LEFT:
  93.                 rcode=dec_x_pos();
  94.                 break;
  95.             case GET_RIGHT:
  96.                 rcode=inc_x_pos(1);
  97.                 break;
  98.             case GET_DELETE:
  99.             case GET_DELETE2:
  100.                 remove_char(str);
  101.                 break;
  102.             case GET_TAB:
  103.             case GET_RETURN:
  104.                 rcode = GET_RETURN;
  105.                 break;
  106.             default:
  107.                 switch(gettype) {
  108.                     case NUM_ONLY: /* numbers only (0-9) */
  109.                         if(c>='0' && c<='9')
  110.                             if(in(c,valid))
  111.                                 add_char(str,c);
  112.                         break;
  113.                     case LETTER_ONLY:
  114.                     /* letters a-z and A-Z, and ' ' only */
  115.                         if(c==' ' || (c>='a' && c<='z') || (c>='A' && c<='Z'))
  116.                             if(in(c,valid))
  117.                                 add_char(str,c);
  118.                         break;
  119.                     case ALL_ALPHA:/* any printable chars */
  120.                         if(c>=' ' && c<=0x7f)
  121.                             if(in(c,valid))
  122.                                 add_char(str,c);
  123.                         break;
  124.                     default:
  125.                         if(c>=' ' && c<=0x7f)
  126.                             add_char(str,c);
  127.                         break;
  128.                 }
  129.                 break;
  130.         }
  131.         if(rcode != INITIAL) {
  132. #ifdef undef
  133.             de_init();
  134. #endif
  135.             if(retlen != NULL)
  136.                 *retlen = sl;
  137.             return(rcode);
  138.         }
  139.     }
  140. }
  141.  
  142. dec_y_pos()
  143. {
  144.     if(!idx)
  145.         return(GET_UP);
  146.     if(iy >= cy) {
  147.         idx = 0;
  148.         cx = ix;
  149.         return(INITIAL);
  150.     }
  151.     idx -= fw;
  152.     cy--;
  153.     return(INITIAL);
  154. }
  155.  
  156. inc_y_pos()
  157. {
  158.     if(idx>=mx-1 || idx==sl)
  159.         return(GET_DOWN);
  160.     if(idx+fw >=  mx) {
  161.         cx += mx-idx - (mx-sl);
  162.         idx += mx-idx - (mx-sl);
  163. #ifdef undef
  164.         cx += (mx - idx -fw);
  165.         idx += (mx - idx - fw);
  166. #endif
  167.         return(INITIAL);
  168.     }
  169.     if(idx+fw > sl) {
  170.         cx += sl - idx;
  171.         idx += sl - idx;
  172.         return(INITIAL);
  173.     }
  174.     idx += fw;
  175.     cy++;
  176.     return(INITIAL);
  177. }
  178.  
  179. inc_x_pos(flag)
  180. int    flag;
  181. {
  182.     if(flag && idx==sl)
  183.         return(GET_RIGHT);
  184.     if(idx == mx)
  185.         return(GET_RIGHT);
  186.     cx++;
  187.     idx++;
  188.     if(cx >= (ix+fw)) {
  189.         cx = ix;
  190.         cy++;
  191.     }
  192.     return(INITIAL);
  193. }
  194.  
  195. dec_x_pos()
  196. {
  197.     if(idx==0)
  198.         return(GET_LEFT);
  199.     idx--;
  200.     cx--;
  201.     if(cx < ix) {
  202.         cx = fw+ix-1;
  203.         cy--;
  204.     }
  205.     return(INITIAL);
  206. }
  207.  
  208. add_char(str, c)
  209. char    *str;
  210. char    c;
  211. {
  212.     register    int    i;
  213.  
  214.     if(inc_x_pos(0)!=INITIAL)
  215.         return;
  216.     for(i=mx-1;i>=idx;i--)
  217.         str[i]=str[i-1];
  218.     str[idx - 1] = c;
  219.     if(sl!=mx)
  220.         sl++;
  221. }
  222.  
  223. remove_char(str)
  224. char    *str;
  225. {
  226.     register int i;
  227.  
  228.     if(idx >= sl) {        /* remove char behind cursor */
  229.         if(dec_x_pos()!=INITIAL)
  230.             return;
  231.         str[sl-1]='\0';
  232.     }
  233.     else 
  234.         for(i=idx;i<mx;i++)
  235.             str[i]=str[i+1];
  236.     sl--;
  237. }
  238.  
  239. drawstr(str,refr)
  240. char    str[];
  241. int    refr;
  242. {
  243.     int    i, j, s, yo;
  244.  
  245.     s=sl;
  246.     move(iy,ix);
  247.     for(i=0,yo=1;i<mx;) {
  248.         if(i>=s) {
  249.             addch('.');
  250.             i++;
  251.         }
  252.         else if((s-i) < fw) {
  253.             printw("%s",&str[i]);
  254.             i += s-i;
  255.         }
  256.         else {
  257.             for(j=0;j<fw;j++)
  258.                 addch(str[i++]);
  259.         }
  260.         if((i % fw)==0) {
  261.             move(iy + yo,ix);
  262.             yo++;
  263.         }
  264.     }
  265.     move(cy,cx);
  266.     if(refr)
  267.         refresh();
  268. }
  269.  
  270. init()
  271. {
  272.     noecho();
  273.     raw();
  274. }
  275.  
  276. de_init()
  277. {
  278.     noraw();
  279.     echo();
  280. }
  281.  
  282. in(ch,allow)
  283. char    ch, allow[];
  284. {
  285.     register int    i;
  286.  
  287.     if(allow == NULL)
  288.         return(1);
  289.     for(i=0;i<strlen(allow);i++)
  290.         if(ch==allow[i])
  291.             return(1);
  292.     return(0);
  293. }
  294.  
  295. clearall(str)
  296. char    str[];
  297. {
  298.     sl=idx=0;
  299.     cx=ix;
  300.     cy=iy;
  301.     str[0]='\0';
  302. }
  303.