home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume4 / gnumacs-blit / part01 / 5620m / term.c < prev   
C/C++ Source or Header  |  1989-02-03  |  12KB  |  463 lines

  1. /*
  2.  * 5620_mouse - A layers program able to send mouse coordinates but otherwise
  3.  *              behaving like a normal layer
  4.  *
  5.  * Version 2.1
  6.  *
  7.  * Public domain software.
  8.  * Written by Gernot Heiser (heiser@ethz.uucp) in April 1987
  9.  * based on the vt100 terminal emulator by Leif Samuelsson
  10.  *    as modified by Peter Lamb
  11.  * History :
  12.  *   Version 1.3, Gernot Heiser, Stefan Zeiger, July 1987:
  13.  *      selection feedback
  14.  *   Version 1.4, Gernot Heiser, August 1987:
  15.  *      different selection kinds; special ESC-sequence for selection
  16.  *   Version 1.5, Gernot Heiser, September 1987:
  17.  *      define characteristics of layers to clone in startup file
  18.  *      ensure mouse coordinates are sent "as seen"
  19.  *      box cursor when layer is inactive
  20.  *   Version 1.6, Gernot Heiser, September 1987:
  21.  *      scroll bars
  22.  *   Version 1.9, Gernot Heiser, 1988-02-02:
  23.  *      test for valid parameters in scroll_region
  24.  *   Version 2.0, Gernot Heiser, 1988-02-27:
  25.  *      adapted to mux
  26.  *   Version 2.1, Gernot Heiser, 1988-05-29:
  27.  *      fixed several problems with mux version
  28.  */
  29.  
  30.  
  31. /* This module contains termcap and tty routines */
  32.  
  33. #include "5620.h"
  34.  
  35. #ifdef MUX
  36.  
  37. Texture T_grey = {
  38.     0xaaaa, 0xffff, 0xaaaa, 0xffff, 0xaaaa, 0xffff, 0xaaaa, 0xffff,
  39.     0xaaaa, 0xffff, 0xaaaa, 0xffff, 0xaaaa, 0xffff, 0xaaaa, 0xffff
  40. };
  41. #endif
  42.  
  43. #define rowy(r)        (Trect.origin.y + (r) * charheight)
  44. #define colx(c)        (Trect.origin.x + (c) * charwidth)
  45.  
  46.    /* bitmaps for selection feedback patterns */
  47. static struct Bitmap feedback_bm [nbr_patterns+1]; /* note: [0] unused! */
  48.  
  49. extern
  50. void init_term(t)
  51. register struct tstat *t;
  52. {
  53. #define WordsPerLine ((XMAX-1) / WORDSIZE + 1)
  54. #define RowsPerLine  20    /* >= charheight */
  55.  
  56.    /* bitmap storage for selection feedback patterns */
  57.    static Word feedback_bm_storage [nbr_patterns][RowsPerLine][WordsPerLine];
  58.  
  59.    int bm, r, i;
  60.  
  61.    clr = F_CLR; or = F_OR;
  62. #ifdef MUX
  63.    charwidth  = P->defaultfont->info['0'].width;
  64.    charheight = P->defaultfont->height;
  65. #else
  66.    charwidth  = defont.info['A'+1].x - defont.info['A'].x;
  67.    charheight = defont.height + 2;
  68. #endif
  69.  
  70.  
  71.    if (cloned)
  72.       return;
  73.  
  74.    /* init selection feedback bitmap storage.
  75.       The first two rows of copy and move bitmaps as well as the last
  76.       two rows of the delete and copy bitmap are to be filled with the
  77.       ragged pattern (already contained in the first row of the copy bitmap.
  78.       All the other rows are to be initialized with all ones bits. */
  79.  
  80. #define ragged_pat  0xF0F0F0F0
  81. #define solid_pat   0xFFFFFFFF
  82. #define ragged_rows 3
  83.    
  84.    if (charheight > RowsPerLine) {      /* feedback bitmap too small */
  85.       exit();
  86.    }
  87.  
  88.    for (bm = select; bm <= nbr_patterns; bm++) {
  89.       for (r = 0; r < charheight; r++) {
  90.      for (i=0; i < WordsPerLine; i++) {
  91.         if (bm==copy    &&  r <  ragged_rows ||
  92.         bm==delete  &&  r >= charheight-ragged_rows) {
  93.            feedback_bm_storage [bm-1][r][i] =
  94.                                  ragged_pat;
  95.          } else if (bm == move) {   /* move pattern is copy & delete */
  96.            feedback_bm_storage [bm-1][r][i] =
  97.                                  feedback_bm_storage [copy-1][r][i] &
  98.                                  feedback_bm_storage [delete-1][r][i];
  99.          } else if (bm > move) {    /* inverse patterns */
  100.            feedback_bm_storage [bm-1][r][i] =
  101.                                 ~feedback_bm_storage [bm-3-1][r][i];
  102.          } else {
  103.            feedback_bm_storage [bm-1][r][i] =
  104.                                  solid_pat;
  105.          }
  106.       }
  107.        }
  108.       feedback_bm [bm].base          = (Word*) feedback_bm_storage [bm-1];
  109.       feedback_bm [bm].width         = WordsPerLine;
  110.       feedback_bm [bm].rect.origin.x = 0;
  111.       feedback_bm [bm].rect.origin.y = 0;
  112.       feedback_bm [bm].rect.corner.x = XMAX;
  113.       feedback_bm [bm].rect.corner.y = charheight;
  114.     }
  115. }
  116.  
  117.  
  118. /* clear_bos - clear from beginning of screen to cursor
  119.  */
  120. extern
  121. clear_bos(t, c, r)
  122. register struct tstat *t;
  123. int c,r;
  124. {
  125.     cursor_off(t);
  126.     rectf(&display,
  127.     Rect(Trect.origin.x, Trect.origin.y, colx(ncols), rowy(r)), clr);
  128.     clear_bol(t, c,r);
  129. }
  130.  
  131.  
  132. /* clear_eos - Clear from cursor to end of screen.
  133.  */
  134. extern
  135. clear_eos(t)
  136. register struct tstat *t;
  137. {
  138.     cursor_off(t);
  139.     clear_eol(t, col-1,arow-1);
  140.     rectf(&display,
  141.     Rect(colx(0), rowy(arow), Trect.corner.x, Trect.corner.y), clr);
  142. }
  143.  
  144.  
  145. /* clear_bol - Clear from beginning of line to cursor.
  146.  */
  147. extern
  148. clear_bol(t, c,r)
  149. register struct tstat *t;
  150. int c,r;
  151. {
  152.     cursor_off(t);
  153.     rectf(&display, Rect(colx(0), rowy(r), colx(c), rowy(r+1)), clr);
  154. }
  155.  
  156.  
  157. /* clear_eol - Clear from cursor to end of line.
  158.  */
  159. extern
  160. clear_eol(t, c, r)
  161. register struct tstat *t;
  162. int c, r;
  163. {
  164.     cursor_off(t);
  165.     rectf(&display, Rect(colx(c), rowy(r), colx(ncols), rowy(r+1)), clr);
  166. }
  167.  
  168. /* Scroll region between lin1 and lin2 inclusive n lines up or down
  169.  */
  170. extern
  171. void scroll_region(t, lin1, lin2, n, upward) 
  172.       register struct tstat *t;
  173.       int lin1, lin2, n;
  174. {
  175.    cursor_off(t);
  176.    lin2 = lin2 > bottom_margin-1 ? bottom_margin-1 : lin2;
  177.    n    = n    > lin2+1-lin1     ? lin2+1-lin1     : n;
  178.    if (upward) {
  179.       if (n > 0) {
  180.          bitblt(&display,
  181.                 Rect(Trect.origin.x, rowy(lin1+n),
  182.                      Trect.corner.x, rowy(lin2+1)),
  183.                 &display,
  184.                 Pt(Trect.origin.x, rowy(lin1)), F_STORE);
  185.          rectf(&display, Rect(Trect.origin.x, rowy(lin2+1-n),
  186.                               Trect.corner.x, rowy(lin2+1)),   clr);
  187.       }
  188.    } else {
  189.       if (n > 0) {
  190.          bitblt(&display,
  191.                 Rect(Trect.origin.x, rowy(lin1),
  192.                      Trect.corner.x, rowy(lin2+1-n)),
  193.                 &display,
  194.                 Pt(Trect.origin.x, rowy(lin1+n)), F_STORE);
  195.          rectf(&display, Rect(Trect.origin.x, rowy(lin1),
  196.                               Trect.corner.x, rowy(lin1+n)),   clr);
  197.       }
  198.    }
  199. }
  200.  
  201.  
  202. #define swap(a, b)   {int temp; temp=a; a=b; b=temp;}
  203.  
  204. extern
  205. void hilight_region(t, from_row, from_col, to_row, to_col, kind)
  206.       register struct tstat *t;
  207.       int from_row, from_col, to_row, to_col, kind;
  208. {
  209.    int t_c;
  210.    
  211.    if (from_row>to_row || from_row==to_row && from_col>to_col) {
  212.       swap (from_row, to_row);
  213.       swap (from_col, to_col);
  214.    }
  215.    
  216.    for (t_c = ncols; from_row <= to_row; from_row++) {
  217.       if (from_row == to_row)
  218.      t_c = to_col;
  219.       
  220.       bitblt (&feedback_bm [kind],
  221.           Rect (from_col*charwidth, 0, t_c*charwidth, charheight),
  222.           &display,
  223.           Pt (colx (from_col), rowy (from_row)),
  224.           F_XOR);
  225.       from_col = 0;
  226.    }
  227. }
  228.  
  229.  
  230. extern
  231. clear_screen(t)
  232.       register struct tstat *t;
  233. {
  234.    cursor_off(t);
  235.    rectf(&display, Trect, clr);
  236.    if (scroll_active)
  237.       rectf  (&display,                           /* redraw scrollbar     */
  238.           Rect (Trect.origin.x-BORDER-2, Trect.origin.y,
  239.             Trect.origin.x-BORDER-1, Trect.corner.y),   or);
  240. }
  241.  
  242.  
  243. extern
  244. cursor_off(t)
  245.       register struct tstat *t;
  246. {
  247.    single_cursor_off(t, &cursc, &cursr, is_current);
  248.    single_cursor_off(t, &mcursc, &mcursr, &T_grey);
  249. }
  250.  
  251.  
  252. extern
  253. cursor_on(t)
  254.       register struct tstat *t;
  255. {
  256.    int r = arow-1, c = col-1;
  257.    
  258.    if (is_current != ((own()&MOUSE) != 0)) {
  259.       single_cursor_off (t, &cursc, &cursr, is_current);
  260.       is_current = !is_current;
  261.    }
  262.    if (is_current && tock || !is_current)
  263.       single_cursor_on(t, c, r, &cursc, &cursr, is_current);
  264.    else
  265.       single_cursor_off(t, &cursc, &cursr, is_current);
  266.    if (mouse_en && mbuts && !select_kind)  /* not for selection */
  267.       single_cursor_on(t, mcol, mrow, &mcursc, &mcursr, &T_grey);
  268.    else
  269.       single_cursor_off(t, &mcursc, &mcursr, &T_grey);
  270. }
  271.  
  272.  
  273. extern
  274. mcursor_off(t)
  275.       register struct tstat *t;
  276. {
  277.    single_cursor_off(t, &mcursc, &mcursr, &T_grey);
  278. }
  279.  
  280.  
  281. /* In the following procedures (lastr < 0) indicates the cursor is off.
  282.    Note that (lastc < 0) means the cursor is in the scroll bar!         */
  283.  
  284. static
  285. single_cursor_on(t, c, r, lastc, lastr, tex)
  286.       register struct tstat *t;
  287.       int *lastc, *lastr;
  288.       Texture *tex;
  289. {
  290.    if(*lastr >= 0 && (*lastc != c || *lastr != r)) {
  291.       xcursor(t, *lastc, *lastr, tex);
  292.       *lastr = -1;
  293.    }
  294.    if(*lastr < 0)
  295.       xcursor(t, *lastc = c, *lastr = r, tex);
  296. }
  297.  
  298.  
  299. static
  300. single_cursor_off(t, lastc, lastr, tex)
  301.       register struct tstat *t;
  302.       int *lastc, *lastr;
  303.       Texture *tex;
  304. {
  305.    if(*lastr >= 0) {
  306.       xcursor(t, *lastc, *lastr, tex);
  307.       *lastr = -1;
  308.    }
  309. }
  310.  
  311.  
  312. static
  313. xcursor(t, c, r, tex)
  314.       register struct tstat *t;
  315.       Texture *tex;
  316. {
  317.    Rectangle curs_r;
  318.    if (c < 0) {     /* in scroll bar */
  319.       curs_r.origin.x = Srect.origin.x - 2;
  320.       curs_r.corner.x = Trect.origin.x - BORDER - 3;
  321.    }
  322.    else {
  323.       curs_r.origin.x = colx (c);
  324.       curs_r.corner.x = colx (c+1);
  325.    }
  326.    curs_r.origin.y = rowy (r);
  327.    curs_r.corner.y = rowy (r+1);
  328.    if (tex == 0 || tex == (Texture *)1) {  /* warning: ugly hack! (gh) */
  329.       rectf(&display, curs_r, F_XOR);
  330.       if (tex == 0)
  331.      rectf (&display, inset (curs_r, 1), F_XOR);
  332.    }
  333.    else
  334.       texture(&display, curs_r, tex, F_XOR);
  335. }
  336.  
  337.  
  338. extern
  339. splat(t, ch)
  340.       register struct tstat *t;
  341. {
  342.    out_buf[0] = ch;
  343.    out_buf[1] = '\0';
  344.    buf_ptr    = 1;
  345.    splat_n (t);
  346. }
  347.  
  348.  
  349. extern
  350. splat_n(t)
  351.       register struct tstat *t;
  352. {
  353.    Rectangle crect;
  354.    cursor_off(t);
  355.    crect.origin.x = colx(col-1);
  356.    crect.origin.y = rowy(arow-1);
  357.    crect.corner.x = colx(col-1+buf_ptr);
  358.    crect.corner.y = rowy(arow);
  359.    if(insert_mode)
  360.       bitblt(&display,
  361.          Rect(crect.origin.x, crect.origin.y,
  362.           Trect.corner.x-charwidth, crect.corner.y),
  363.          &display,
  364.          Pt(crect.corner.x, crect.origin.y), F_STORE);
  365.    
  366.    rectf(&display, crect, clr);
  367.    string(&defont, out_buf, &display, crect.origin, or);
  368.    if(graph_att & BOLD)
  369.       string(&defont, out_buf, &display,
  370.          Pt(crect.origin.x+1, crect.origin.y+1), or);
  371.    if(graph_att & UNDER)
  372.       rectf(&display,
  373.         Rect(crect.origin.x, crect.origin.y+defont.ascent+2,
  374.          crect.corner.x, crect.origin.y+defont.ascent+3), or);
  375.    if(graph_att & REV)
  376.       rectf(&display, crect, F_XOR);
  377. }
  378.  
  379.  
  380. extern
  381. ins_nchar(t, n)
  382.       register struct tstat *t;
  383.       register int n;
  384. {
  385.    Rectangle crect;
  386.    cursor_off(t);
  387.    crect.origin.x = colx(col-1);
  388.    crect.origin.y = rowy(arow-1);
  389.    crect.corner.x = colx(col-1+n);
  390.    crect.corner.y = rowy(arow);
  391.    bitblt(&display,
  392.       Rect (crect.origin.x, crect.origin.y,
  393.         Trect.corner.x-n*charwidth, crect.corner.y),
  394.       &display,
  395.       Pt(crect.corner.x, crect.origin.y), F_STORE);
  396.    rectf(&display, crect, clr);
  397. }
  398.  
  399.  
  400. extern
  401. del_nchar(t, n)
  402.       register struct tstat *t;
  403.       register int n;
  404. {
  405.    Rectangle crect;
  406.    cursor_off(t);
  407.    crect.origin.x = colx(col-1);
  408.    crect.origin.y = rowy(arow-1);
  409.    crect.corner.x = colx(col-1+n);
  410.    crect.corner.y = rowy(arow);
  411.    bitblt(&display,
  412.       Rect(crect.corner.x, crect.origin.y,
  413.            Trect.corner.x, crect.corner.y),
  414.       &display,
  415.       Pt(crect.origin.x, crect.origin.y), F_STORE);
  416.    rectf(&display, Rect(colx(ncols-n), crect.origin.y,
  417.             Trect.corner.x, crect.corner.y), clr);
  418. }
  419.  
  420.  
  421. extern
  422. inverse_vid(t, inv)
  423.       register struct tstat *t;
  424. {
  425.    if(inv && clr == F_CLR) {
  426.       clr = F_OR; or = F_CLR;
  427.       goto flip;;
  428.    } else  if(!inv && clr != F_CLR) {
  429.       clr = F_CLR; or = F_OR;
  430.     flip:
  431.       rectf(&display, D_rect, F_XOR);
  432.    }
  433. }
  434.  
  435.  
  436. extern
  437. install_scrollbar (t)
  438.       register struct tstat *t;
  439. {
  440.    Trect.origin.x = Srect.origin.x + Srect.corner.x - colx (--ncols);
  441.    bitblt (&display,
  442.        Rpt (Srect.origin,
  443.         Pt (Srect.corner.x - (Trect.origin.x - Srect.origin.x),
  444.             Srect.corner.y)),
  445.        &display, Trect.origin, F_STORE);   /* move screen contents */
  446.    rectf  (&display, Rpt (Srect.origin, Pt (Trect.origin.x, Trect.corner.y)),
  447.        clr);                               /* clear scrollbar      */
  448.    rectf  (&display,                           /* draw line            */
  449.        Rect (Trect.origin.x-BORDER-2, Trect.origin.y,
  450.          Trect.origin.x-BORDER-1, Trect.corner.y),   or);
  451. } /* install_scrollbar */
  452.  
  453.  
  454. extern
  455. remove_scrollbar (t)
  456.       register struct tstat *t;
  457. {
  458.    bitblt (&display, Trect, &display, Srect.origin, F_STORE);   /* move */
  459.    Trect.origin.x = Srect.origin.x;
  460.    rectf  (&display,                            /* clear right column   */
  461.        Rpt (Pt (colx (ncols++), Trect.origin.y), Trect.corner), clr);
  462. } /* remove_scrollbar */
  463.