home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / stdwin / Packs / vt / vtfunc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-18  |  4.5 KB  |  278 lines  |  [TEXT/????]

  1. /* Functions implementing ANSI operations */
  2.  
  3. #include "vtimpl.h"
  4.  
  5. /* Linefeed */
  6.  
  7. void
  8. vtlinefeed(vt, n)
  9.     VT *vt;
  10.     int n;
  11. {
  12.     while (--n >= 0) {
  13.         if (vt->cur_row == vt->scr_bot - 1) {
  14.             int scr_top = vt->scr_top;
  15.             if (scr_top == vt->topterm)
  16.                 scr_top = 0;
  17.             vtscrollup(vt, scr_top, vt->scr_bot, 1);
  18.         }
  19.         else
  20.             vtsetcursor(vt, vt->cur_row + 1, vt->cur_col);
  21.     }
  22. }
  23.  
  24. /* Reverse linefeed */
  25.  
  26. void
  27. vtrevlinefeed(vt, n)
  28.     VT *vt;
  29.     int n;
  30. {
  31.     while (--n >= 0) {
  32.         if (vt->cur_row == vt->scr_top)
  33.             vtscrolldown(vt, vt->scr_top, vt->scr_bot, 1);
  34.         else
  35.             vtsetcursor(vt, vt->cur_row - 1, vt->cur_col);
  36.     }
  37. }
  38.  
  39. /* Reset underline, inverse video attributes */
  40.  
  41. void
  42. vtresetattr(vt)
  43.     VT *vt;
  44. {
  45.     vtsetflags(vt, 0);
  46. }
  47.  
  48. /* Set attribute flag (without clearing the others) */
  49.  
  50. void
  51. vtsetattr(vt, bit)
  52.     VT *vt;
  53.     int bit;
  54. {
  55.     vtsetflags(vt, vt->gflags | (1 << bit));
  56. }
  57.  
  58. /* Save cursor position */
  59.  
  60. void
  61. vtsavecursor(vt)
  62.     VT *vt;
  63. {
  64.     vt->save_row = vt->cur_row;
  65.     vt->save_col = vt->cur_col;
  66. }
  67.  
  68. /* Restore cursor position */
  69.  
  70. void
  71. vtrestorecursor(vt)
  72.     VT *vt;
  73. {
  74.     vtsetcursor(vt, vt->save_row, vt->save_col);
  75. }
  76.  
  77. /* Process an arrow key (possibly repeated) */
  78.  
  79. void
  80. vtarrow(vt, code, repeat)
  81.     VT *vt;
  82.     int code;
  83.     int repeat;
  84. {
  85.     int row = vt->cur_row;
  86.     int col = vt->cur_col;
  87.     int minrow = 0, maxrow = vt->rows;
  88.     
  89.     CLIPMAX(col, vt->cols-1);
  90.     switch (code) {
  91.     case WC_LEFT:
  92.         col -= repeat;
  93.         break;
  94.     case WC_RIGHT:
  95.         col += repeat;
  96.         break;
  97.     case WC_UP:
  98.         row -= repeat;
  99.         break;
  100.     case WC_DOWN:
  101.         row += repeat;
  102.         break;
  103.     }
  104.     CLIPMAX(col, vt->cols-1);
  105.     CLIPMIN(col, 0);
  106.     if (vt->cur_row >= vt->scr_top)
  107.         minrow = vt->scr_top;
  108.     if (vt->cur_row < vt->scr_bot)
  109.         maxrow = vt->scr_bot;
  110.     CLIPMIN(row, minrow);
  111.     CLIPMAX(row, maxrow-1);
  112.     vtsetcursor(vt, row, col);
  113. }
  114.  
  115. /* Clear to end of line */
  116.  
  117. void
  118. vteolclear(vt, row, col)
  119.     VT *vt;
  120.     int row, col;
  121. {
  122.     if (row < vt->rows) {
  123.         if (vt->llen[row] > col) {
  124.             if (vt->lazy)
  125.                 vtchange(vt, row, col, row + 1, vt->llen[row]);
  126.             else {
  127.                 VTBEGINDRAWING(vt);
  128.                 werase(col*vt->cwidth, row*vt->cheight,
  129.                     vt->llen[row]*vt->cwidth,
  130.                     (row+1)*vt->cheight);
  131.             }
  132.             vt->llen[row] = col;
  133.         }
  134.     }
  135. }
  136.  
  137. /* Clear to end of screen */
  138.  
  139. void
  140. vteosclear(vt, row, col)
  141.     VT *vt;
  142.     int row, col;
  143. {
  144.     vteolclear(vt, row, col);
  145.     if (vt->lazy)
  146.         vtchange(vt, row + 1, 0, vt->rows, vt->cols);
  147.     else {
  148.         VTBEGINDRAWING(vt);
  149.         werase(0, (row + 1) * vt->cheight,
  150.             vt->cols * vt->cwidth, vt->rows * vt->cheight);
  151.     }
  152.     for (row = row + 1; row < vt->rows; ++row)
  153.         vt->llen[row] = 0;
  154. }
  155.  
  156. /* Delete n lines */
  157.  
  158. void
  159. vtdellines(vt, n)
  160.     VT *vt;
  161.     int n;
  162. {
  163.     vtscrollup(vt, vt->cur_row, vt->scr_bot, n);
  164. }
  165.  
  166. /* Insert n lines */
  167.  
  168. void
  169. vtinslines(vt, n)
  170.     VT *vt;
  171.     int n;
  172. {
  173.     vtscrolldown(vt, vt->cur_row, vt->scr_bot, n);
  174. }
  175.  
  176. /* Scroll a range of lines n positions up */
  177.  
  178. void
  179. vtscrollup(vt, r1, r2, n)
  180.     VT *vt;
  181.     int r1, r2;
  182.     int n;
  183. {
  184.     if (n > 0 && r1 < r2) {
  185.         int i;
  186.         vtcirculate(vt, r1, r2, -n);
  187.         /* Clear lines at bottom of scrolling screenpart */
  188.         for (i = r2 - n; i < r2; ++i)
  189.             vt->llen[i] = 0;
  190.         vtscroll(vt, r1, 0, r2, vt->cols, -n, 0);
  191.     }
  192. }
  193.  
  194.  
  195. /* Scroll a range of lines n positions down */
  196.  
  197. void
  198. vtscrolldown(vt, r1, r2, n)
  199.     VT *vt;
  200.     int r1, r2;
  201.     int n;
  202. {
  203.     if (n > 0 && r1 < r2) {
  204.         int i;
  205.         vtcirculate(vt, r1, r2, n);
  206.         for (i = r1 + n; --i >= r1; )
  207.             vt->llen[i] = 0;
  208.         vtscroll(vt, r1, 0, r2, vt->cols, n, 0);
  209.     }
  210. }
  211.  
  212. /* Insert n characters */
  213.  
  214. void
  215. vtinschars(vt, n)
  216.     VT *vt;
  217.     int n;
  218. {
  219.     int row;
  220.     
  221.     if (n > 0 && (row= vt->cur_row) < vt->rows) {
  222.         int col = vt->cur_col;
  223.         int len = vt->llen[row];
  224.         if (len > col) {
  225.             if (col+n >= vt->cols) {
  226.                 vtchange(vt, row, col, row+1, len);
  227.                 vt->llen[row] = col;
  228.             }
  229.             else {
  230.                 register int i;
  231.                 char *data = vt->data[row];
  232.                 unsigned char *flags = vt->flags[row];
  233.                 len += n;
  234.                 if (len > vt->cols)
  235.                     len = vt->cols;
  236.                 for (i = len-n; --i >= col; )
  237.                     data[i+n] = data[i];
  238.                 vtscroll(vt, row, col, row+1, len, 0, n);
  239.                 vt->llen[row] = len;
  240.                 /* Clear the inserted stretch */
  241.                 for (i = col+n; --i >= col; ) {
  242.                     data[i] = ' ';
  243.                     flags[i] = 0;
  244.                 }
  245.             }
  246.         }
  247.     }
  248. }
  249.  
  250. /* Delete n characters */
  251.  
  252. void
  253. vtdelchars(vt, n)
  254.     VT *vt;
  255.     int n;
  256. {
  257.     int row;
  258.     
  259.     if (n > 0 && (row = vt->cur_row) < vt->rows) {
  260.         int col = vt->cur_col;
  261.         int len = vt->llen[row];
  262.         if (len > col) {
  263.             if (len <= col+n) {
  264.                 vtchange(vt, row, col, row+1, len);
  265.                 vt->llen[row] = col;
  266.             }
  267.             else {
  268.                 register int i;
  269.                 char *data = vt->data[row];
  270.                 for (i = col+n; i < len; ++i)
  271.                     data[i-n] = data[i];
  272.                 vtscroll(vt, row, col, row+1, len, 0, -n);
  273.                 vt->llen[row] -= n;
  274.             }
  275.         }
  276.     }
  277. }
  278.