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