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

  1. /****************************************************************/
  2. /* Addch() 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 short wherever possible. Portability        */
  12. /*     improvements:                    900114    */
  13. /* 1.3:     MSC -W3, Turbo'C' -w -w-pro checkes:        881005    */
  14. /* 1.2:     Max limits off by 1. Fixed thanks to S. Creps:    881002    */
  15. /* 1.1:     Added 'raw' output routines (allows PC charac-        */
  16. /*     ters < 0x20 to be displayed:            880306    */
  17. /* 1.0:     Release:                    870515    */
  18. /****************************************************************/
  19.  
  20. #include <curses.h>
  21. #include <curspriv.h>
  22.  
  23. char _curses_charadd_rcsid[] = "@(#)charadd.c    v.1.4  - 900114";
  24.  
  25. /****************************************************************/
  26. /* Newline() does line advance and returns the new cursor line.    */
  27. /* If error, return -1.                        */
  28. /****************************************************************/
  29.  
  30. static    short    newline(win, lin)
  31.   WINDOW    *win;
  32.   short      lin;
  33.   {
  34.   if (++lin > win->_regbottom)
  35.     {
  36.     lin--;
  37.     if (win->_scroll)
  38.       scroll(win);
  39.     else
  40.       return(-1);
  41.     } /* if */
  42.   return(lin);
  43.   } /* newline */
  44.  
  45. /****************************************************************/
  46. /* _Chadd() inserts character 'c' at the current cursor posi-    */
  47. /* tion in window 'win'. If xlat is TRUE, _chadd() will handle    */
  48. /* things like tab, newline, cr etc.; otherwise the character    */
  49. /* is simply output.                        */
  50. /****************************************************************/
  51.  
  52. int _chadd(win, c, xlat)
  53.   register WINDOW    *win;
  54.   char             c;
  55.   bool             xlat;
  56.   {
  57.   short    x = win->_curx;
  58.   short    y = win->_cury;
  59.   short    newx;
  60.   short    ch = c;
  61.   short    ts = win->_tabsize;
  62.  
  63.   ch &= 0xff;            /* kill any sing-extend */
  64.   if (y >= win->_maxy  ||  x >= win->_maxx  ||  y < 0  ||  x < 0)
  65.     return(ERR);
  66.  
  67.   if (xlat)
  68.     {
  69.     switch (ch)
  70.       {
  71.       case '\t':  for (newx = ((x/ts) + 1) * ts; x < newx; x++)
  72.             {
  73.             if (waddch(win, ' ') == ERR)
  74.               return(ERR);
  75.             if (win->_curx == 0)    /* if tab to next line */
  76.               return(OK);        /* exit the loop */
  77.             } /* for */
  78.           return(OK);
  79.       case '\n':  if (_cursvar.autocr && !(_cursvar.raw)) /* if lf -> crlf */
  80.             x = 0;
  81.           if ((y = newline(win, y)) < 0)
  82.             return(ERR);
  83.           win->_cury = y;
  84.           win->_curx = x;
  85.           return(OK);
  86.       case '\r':  x = 0;
  87.           win->_curx = x;
  88.           return(OK);
  89.       case '\b':  if (--x < 0)            /* no back over left margin */
  90.             x = 0;
  91.           win->_curx = x;
  92.           return(OK);
  93.       case 0x7f:  if (waddch(win,'^') == ERR)
  94.             return(ERR);
  95.           return(waddch(win,'?'));
  96.       default:      break;
  97.       } /* switch */
  98.     if (ch < ' ')            /* handle control chars */
  99.       {
  100.       if (waddch(win,'^') == ERR)
  101.     return(ERR);
  102.       return(waddch(win,c + '@'));
  103.       } /* if */
  104.     } /* if xlat*/
  105.  
  106.   ch |= (win->_attrs & ATR_MSK);
  107.   if (win->_line[y][x] != ch)        /* only if data change */
  108.     {
  109.     if (win->_minchng[y] == _NO_CHANGE)
  110.       win->_minchng[y] = win->_maxchng[y] = x;
  111.     else
  112.       {
  113.       if (x < win->_minchng[y])
  114.     win->_minchng[y] = x;
  115.       else
  116.     {
  117.     if (x > win->_maxchng[y])
  118.       win->_maxchng[y] = x;
  119.     } /* else */
  120.       } /* else */
  121.     } /* if */
  122.   win->_line[y][x++] = ch;
  123.   if (x >= win->_maxx)            /* wrap around test */
  124.     {
  125.     x = 0;
  126.     if ((y = newline(win, y)) < 0)
  127.       return(ERR);
  128.     } /* if */
  129.   win->_curx = x;
  130.   win->_cury = y;
  131.   return(OK);
  132.   } /* _chadd */
  133.  
  134. /****************************************************************/
  135. /* Addch() inserts character 'c' at the current cursor posi-    */
  136. /* tion in stdscr, and takes any actions as dictated by the    */
  137. /* character.                            */
  138. /****************************************************************/
  139.  
  140. int addch(c)
  141.   char     c;
  142.   {
  143.   return (_chadd(stdscr,c,TRUE));
  144.   } /* addch */
  145.  
  146. /****************************************************************/
  147. /* Waddch() inserts character 'c' at the current cursor posi-    */
  148. /* tion in window 'win', and takes any actions as dictated by    */
  149. /* the character.                        */
  150. /****************************************************************/
  151.  
  152. int waddch(win,c)
  153.   WINDOW *win;
  154.   char     c;
  155.   {
  156.   return (_chadd(win,c,TRUE));
  157.   } /* waddch */
  158.  
  159. /****************************************************************/
  160. /* Mvaddch() moves to position in stdscr, then inserts charac-    */
  161. /* ter 'c' at that point, and takes any actions as dictated by    */
  162. /* the character.                        */
  163. /****************************************************************/
  164.  
  165. int mvaddch(y,x,c)
  166.   int     x;
  167.   int     y;
  168.   char     c;
  169.   {
  170.   if (wmove(stdscr,y,x) == ERR)
  171.     return(ERR);
  172.   return (_chadd(stdscr,c,TRUE));
  173.   } /* mvaddch */
  174.  
  175. /****************************************************************/
  176. /* Mvwaddch() moves to position in window 'win', then inserts    */
  177. /* character 'c' at that point in the window, and takes any    */
  178. /* actions as dictated by the character.            */
  179. /****************************************************************/
  180.  
  181. int mvwaddch(win,y,x,c)
  182.   WINDOW *win;
  183.   int      x;
  184.   int      y;
  185.   char      c;
  186.   {
  187.   if (wmove(win,y,x) == ERR)
  188.     return(ERR);
  189.   return (_chadd(win,c,TRUE));
  190.   } /* mvwaddch */
  191.  
  192. /****************************************************************/
  193. /* Addrawch() inserts character 'c' at the current cursor    */
  194. /* position in stdscr, disregarding any traditional interpre-    */
  195. /* tation of the character.                    */
  196. /****************************************************************/
  197.  
  198. int addrawch(c)
  199.   char     c;
  200.   {
  201.   return (_chadd(stdscr,c,FALSE));
  202.   } /* addrawch */
  203.  
  204. /****************************************************************/
  205. /* Waddrawch() inserts character 'c' at the current cursor    */
  206. /* position in window 'win', disregarding any traditional in-    */
  207. /* terpretation of the character.                */
  208. /****************************************************************/
  209.  
  210. int waddrawch(win,c)
  211.   WINDOW *win;
  212.   char     c;
  213.   {
  214.   return (_chadd(win,c,FALSE));
  215.   } /* waddrawch */
  216.  
  217. /****************************************************************/
  218. /* Mvaddrawch() moves to position in stdscr, then inserts cha-    */
  219. /* racter 'c' at that point, disregarding any traditional in-    */
  220. /* terpretation of the character.                */
  221. /****************************************************************/
  222.  
  223. int mvaddrawch(y,x,c)
  224.   int     x;
  225.   int     y;
  226.   char     c;
  227.   {
  228.   if (wmove(stdscr,y,x) == ERR)
  229.     return(ERR);
  230.   return (_chadd(stdscr,c,FALSE));
  231.   } /* mvaddrawch */
  232.  
  233. /****************************************************************/
  234. /* Mvwaddrawch() moves to position in window 'win', then in-    */
  235. /* serts character 'c' at that point in the window, disregar-    */
  236. /* ding any traditional interpretation of the character.    */
  237. /****************************************************************/
  238.  
  239. int mvwaddrawch(win,y,x,c)
  240.   WINDOW *win;
  241.   int      x;
  242.   int      y;
  243.   char      c;
  244.   {
  245.   if (wmove(win,y,x) == ERR)
  246.     return(ERR);
  247.   return (_chadd(win,c,FALSE));
  248.   } /* mvwaddrawch */
  249.