home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 332_02 / strget.c < prev    next >
C/C++ Source or Header  |  1990-01-06  |  6KB  |  197 lines

  1. /****************************************************************/
  2. /* Getstr() routines of the PCcurses package            */
  3. /*                                */
  4. /****************************************************************/
  5. /* This version of curses is based on ncurses, a curses version    */
  6. /* originally written by Pavel Curtis at Cornell University.    */
  7. /* I have made substantial changes to make it run on IBM PC's,    */
  8. /* and therefore consider myself free to make it public domain.    */
  9. /*                Bjorn Larsson (bl@infovox.se)    */
  10. /****************************************************************/
  11. /* 1.4:  Use of short wherever possible. Portability        */
  12. /*     improvements, echo bug, fix for signed character    */
  13. /*     (for 8-bit ASCII):                900114    */
  14. /* 1.3:     MSC -W3, Turbo'C' -w -w-pro checkes:        881005    */
  15. /* 1.2:     Max limits of by 1. Block nest error in function    */
  16. /*     backchar(). Fixed thanks to S. Creps:        881002    */
  17. /* 1.0:     Release:                    870515    */
  18. /****************************************************************/
  19.  
  20. #include <curses.h>
  21. #include <curspriv.h>
  22.  
  23. static    char    *backchar();
  24.  
  25. char _curses_strget_rcsid[] = "@(#)strget.c     v.1.4  - 900114";
  26.  
  27. static    bool     oldecho;
  28. static    bool     oldcbreak;
  29. static  bool     oldnodelay;
  30. static    char    *strbeg;
  31. static    WINDOW  *w;
  32. static    short     xbeg;
  33.  
  34. /****************************************************************/
  35. /* Wgetstr(win,str) reads in a string (terminated by \n or \r)    */
  36. /* to the buffer pointed to by 'str', and displays the input    */
  37. /* in window 'win'. The user's erase and kill characters are    */
  38. /* active.                            */
  39. /****************************************************************/
  40.  
  41. int wgetstr(win,str)
  42.   WINDOW    *win; 
  43.   char        *str;
  44.   {
  45.   w          = win;
  46.   strbeg      = str;        /* keep start for backspacing */
  47.   oldcbreak       = _cursvar.cbreak;    /* remember states */
  48.   oldecho         = _cursvar.echo;
  49.   oldnodelay      = w->_nodelay;
  50.   _cursvar.echo   = FALSE;        /* we do echo ourselves */
  51.   _cursvar.cbreak = TRUE;        /* no wait for chars */
  52.   w->_nodelay   = FALSE;        /* don't return 'NOCHARS' */
  53.   xbeg = w->_curx;            /* remember screen start x-position */
  54.  
  55.   wrefresh(w);                /* sets cursor at right place */
  56.   while ((*str = (char) getch()) != '\n')
  57.     {
  58.     if (*str == '\r')
  59.       break;
  60.     if (*str == _DCCHAR)
  61.       {
  62.       if (str > strbeg)
  63.     str = backchar(str);
  64.       } /* if */
  65.     else
  66.       if (*str == _DLCHAR)
  67.     while (str > strbeg)
  68.       str = backchar(str);
  69.       else
  70.     {
  71.         if (oldecho)            /* check if echo */
  72.           {
  73.       waddch(w,*str++);
  74.       wrefresh(w);
  75.           }
  76.     else
  77.       str++;
  78.     } /* else */
  79.       } /* while */
  80.  
  81.   *str = '\0';
  82.   _cursvar.echo   = oldecho;
  83.   _cursvar.cbreak = oldcbreak;
  84.   win->_nodelay   = oldnodelay;
  85.   return(OK);
  86.   } /* wgetstr */
  87.  
  88. /****************************************************************/
  89. /* Getstr(str) reads in a string (terminated by \n or \r) to    */
  90. /* the buffer pointed to by 'str', and displays the input in    */
  91. /* stdscr. The user's erase and kill characters are active.    */
  92. /****************************************************************/
  93.  
  94. int getstr(str)
  95.   char *str;
  96.   {
  97.   return(wgetstr(stdscr,str));
  98.   } /* getstr */
  99.  
  100. /****************************************************************/
  101. /* Mvgetstr(y,x,str) moves the stdscr cursor to a new position,    */
  102. /* then reads in a string (terminated by \n or \r) to the buf-    */
  103. /* fer pointed to by 'str', and displays the input in stdscr.    */
  104. /* The user's erase and kill characters are active.        */
  105. /****************************************************************/
  106.  
  107. int mvgetstr(y,x,str)
  108.   int y;
  109.   int x;
  110.   char *str;
  111.   {
  112.   if (wmove(stdscr,y,x) == ERR)
  113.     return(ERR);
  114.   return(wgetstr(stdscr,str));
  115.   } /* mvgetstr */
  116.  
  117. /****************************************************************/
  118. /* Mvwgetstr(win,y,x,str) moves the 'win' cursor to a new    */
  119. /* position, then reads in a string (terminated by \n or \r)    */
  120. /* to the buffer pointed to by 'str', and displays the input in    */
  121. /* stdscr. The user's erase and kill characters are active.    */
  122. /****************************************************************/
  123.  
  124. int mvwgetstr(win,y,x,str)
  125.   WINDOW *win;
  126.   int      y;
  127.   int      x;
  128.   char     *str;
  129.   {
  130.   if (wmove(win,y,x) == ERR)
  131.     return(ERR);
  132.   return(wgetstr(win,str));
  133.   } /* mvwgetstr */
  134.  
  135. /****************************************************************/
  136. /* Backchar() does a character delete with screen erase, even    */
  137. /* up to previous lines. It will not back-scroll if the begi-    */
  138. /* ning of the string has scrolled off the window. Steps back    */
  139. /* pointer 's', and returns the new value.            */
  140. /****************************************************************/
  141.  
  142. static char *backchar(s)
  143.   char      *s;
  144.   {
  145.   static short nbs;
  146.   static short x;
  147.   static char *p;
  148.   static short ts;
  149.  
  150.   x =  xbeg;
  151.   ts =  w->_tabsize;
  152.  
  153.   s--;                        /* step back string */
  154.   nbs = 1;                    /* step at least one pos */
  155.   if (((*s & 0xe0) == 0) || (*s == 0x7f))    /* ctrl-char has size 2 */
  156.     nbs++;
  157.   if (*s == '\t')                /* tabs are very special */
  158.     {
  159.     for (p = strbeg; p < s ;p++)        /* find x-pos of last char */
  160.       {
  161.       if (*p == '\t')                /* go to next tab */
  162.     x = ((x/ts)+1) * ts;
  163.       else
  164.     {
  165.     if (((*p & 0xe0) == 0) || (*p == 0x7f))    /* control character */
  166.       x += 2;
  167.     else                    /* normal char */
  168.       x++;
  169.     } /* else */
  170.       if (x >= w->_maxx)            /* go to next line? */
  171.     x = 0;
  172.       } /* for */
  173.     if (!(w->_curx))                /* if step-over newline */
  174.       nbs = w->_maxx - x;
  175.     else                    /* in-line tab */
  176.       nbs = w->_curx - x;            /* positions to erase */
  177.     } /* if */
  178.  
  179.   if ((int) oldecho)                /* check if echo */
  180.     {
  181.     while(nbs--)                /* do so many */
  182.       {
  183.       if (w->_curx > 0)                /* if not at line beginning */
  184.         waddstr(w,"\b \b");
  185.       else
  186.         if (w->_cury)                /* if not on top line */
  187.       {
  188.         mvwaddch(w,w->_cury-1,w->_maxx -1,' ');/* put space at line end */
  189.       wmove(w,w->_cury-1,w->_maxx - 1);    /* and go there again */
  190.         } /* else */
  191.       } /* while */
  192.     wrefresh(w);                /* redraw screen */
  193.     }
  194.   *(s+1) = '\0';                /* make string terminated */
  195.   return(s);
  196.   } /* backchar */
  197.