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