home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / t / tel2305s.zip / ENGINE / SCRLBACK.C < prev    next >
C/C++ Source or Header  |  1992-02-22  |  14KB  |  682 lines

  1. /*
  2. *    SCRLBACK.C
  3. ****************************************************************************
  4. *                                                                          *
  5. *      NCSA Telnet for the PC                                              *
  6. *                                                                          *
  7. *      National Center for Supercomputing Applications                     *
  8. *      152 Computing Applications Building                                 *
  9. *      605 E. Springfield Ave.                                             *
  10. *      Champaign, IL  61820                                                *
  11. *                                                                          *
  12. *      This program is in the public domain.                               *
  13. *                                                                          *
  14. ****************************************************************************
  15. *   Revisions:
  16. *   6-26-90     Separated from look.c by Heeren Pathak
  17. */
  18.  
  19.  
  20. #ifdef MSC
  21. #define mousecl mousecml
  22. #endif
  23.  
  24. #ifdef MOUSE
  25. #include "mouse.h"
  26. #endif
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <fcntl.h>
  30. #include <ctype.h>
  31. #include <conio.h>
  32. #ifdef MSC
  33. #ifdef __TURBOC__
  34. #include <alloc.h>
  35. #else
  36. #include <malloc.h>
  37. #endif
  38. #endif
  39.  
  40. #include "whatami.h"
  41. #include "nkeys.h"
  42. #include "windat.h"
  43. #include "hostform.h"
  44. #include "protocol.h"
  45. #include "externs.h"
  46.  
  47. static int its_in(long ,long );
  48.  
  49. extern int scroll,                /* which way do we scroll */
  50.     cflag,                        /* are we copying ? */
  51.     cbuf,                        /* do we have things in the buffer? */
  52.     mcflag,                        /* mouse button flag */
  53.     vsrow,                        /* VS row */
  54.     numline,
  55.     mighty;                        /* mouse present? */
  56.  
  57. extern long size,                /* size of buffer */
  58.     cstart,                        /* starting location of VS copy buffer */
  59.     cend,                        /* ending location of VS copy buffer */
  60.     cdist;                        /* distance to base point */
  61.  
  62. extern struct config def;
  63. extern char *copybuf,
  64.     *lineend;
  65.  
  66. /***********************************************************************/
  67. /*  scrollback
  68. *   Take keyboard keys to manipulate the screen's scrollback
  69. *   Written by Heeren Pathak 4/1/89 along with the mouse routinues.
  70. */
  71. void scrollback(struct twin *tw)
  72. {
  73.     int row,                /* current row */
  74.         col,                /* current col */
  75.         c;                     /* input */
  76.     long i,                    /* loop index  */
  77.         temp;                /* general purpose */
  78.  
  79.     if(tw->termstate==TEKTYPE)
  80.         return;
  81.     set_cur(0);
  82.     c=n_chkchar();
  83. #ifdef SCRL_MOUSE
  84.     if(c==-1) {
  85.          c=chkmouse();
  86.          mouse=0;
  87.       } /* end if */
  88. #endif
  89.     switch (scroll) {
  90.         case -1:            /* scroll stuff down */
  91.             if(c==CURDN || c==PGDN || c==E_CURDN || c==E_PGDN) {
  92.                 scroll=0;
  93.                 c=0;
  94.             }
  95.             row=n_row();
  96.             col=n_col();
  97.             if(vsrow>(-tw->bkscroll)) {        /* have not reached limit of buffer */
  98.                 vsrow--;
  99.                 row=0;
  100.                 VSscrolback(tw->vs,1);
  101.                 temp=4;
  102.                 if(cflag || cbuf) {
  103.                     i=cdist;
  104.                     cdist=(long)(vsrow+row)*80+col;
  105.                     if(cbuf || i>cdist) {
  106.                         temp-=setattr(0,tw->colors[0],tw->colors[2]);
  107.                         temp-=setattr(1,tw->colors[0],tw->colors[2]);
  108.                       }
  109.                   }
  110.                 for(i=0; i<temp*1000; i++);    /* delay */
  111.                 n_cur(0,col);
  112.             }
  113.             break;
  114.  
  115.         case 1:                /* scroll stuff up */
  116.             row=n_row();
  117.             col=n_col();
  118.             if(c==CURUP || c==PGUP || c==E_CURUP || c==E_PGUP) {
  119.                 scroll=0;
  120.                 c=0;
  121.             }
  122.             if(vsrow<0) {
  123.                 ++vsrow;
  124.                 row=numline;
  125.                 VSscrolforward(tw->vs,1);
  126.                 temp=4;
  127.                 if(cflag || cbuf) {
  128.                     i=cdist;
  129.                     cdist=(long)(vsrow+row)*80+col;
  130.                     if(cbuf || cdist>i) {
  131.                         temp-=setattr(numline-1,tw->colors[0],tw->colors[2]);
  132.                          temp-=setattr(numline,tw->colors[0],tw->colors[2]);
  133.                       }
  134.                   }
  135.                 for(i=0; i<temp*1000; i++);    /* delay */
  136.                 n_cur(numline,col);
  137.               }
  138.             break;                
  139.        }    /* end switch */
  140.  
  141.     switch (c) {            /* just move around */
  142. #ifdef MOUSE
  143.         case CTRLCURRT:
  144.         case E_CTRLCURRT: /* Send a message so speed up mouse */
  145.             nm_mousespeed(-1);
  146.             break;
  147.  
  148.         case CTRLCURLF:
  149.         case E_CTRLCURLF: /* Send a message so speed up mouse */
  150.             nm_mousespeed(1);
  151.             break;
  152. #endif
  153.         case CTRLPGDN:
  154.         case E_CTRLPGDN:
  155.             col=79; 
  156.             row=numline;
  157.             VSscrolforward(tw->vs,-vsrow);
  158.             vsrow=0;
  159.             cdist=(long)(row+vsrow)*80+col;
  160.             n_cur(row,col);
  161.             if(cflag || cbuf)
  162.                 updatescr(tw->colors[2]);
  163.             break;
  164.  
  165.         case CTRLPGUP:
  166.         case E_CTRLPGUP:
  167.             row=col=0; 
  168.             VSscrolback(tw->vs,(tw->bkscroll - vsrow));
  169.             vsrow=(-tw->bkscroll);
  170.             cdist=(long)(row+vsrow)*80+col;
  171.             n_cur(row,col);
  172.             if(cflag || cbuf)
  173.                 updatescr(tw->colors[2]);
  174.             break;
  175.  
  176.         case HOME:
  177.         case E_HOME:
  178.             row=n_row();
  179.             col=n_col();
  180.             if(col) {
  181.                 col=0;
  182.                 c=0;
  183.               }    /* end if */
  184.             else {
  185.                 row=0;
  186.                 c=1;
  187.               }    /* end else */
  188.             cdist=(long)(row+vsrow)*80+col;
  189.             n_cur(row,col);
  190.             if(cflag || cbuf)
  191.                 if(c)
  192.                     updatescr(tw->colors[2]);
  193.                 else
  194.                     setattr(row,tw->colors[0],tw->colors[2]);
  195.             break;
  196.  
  197.         case ENDKEY:
  198.         case E_ENDKEY:
  199.             row=n_row();
  200.             col=n_col();
  201.             if(col!=79) {
  202.                 col=79;
  203.                 c=0;
  204.               }    /* end if */
  205.             else {
  206.                 row=numline;
  207.                 c=1;
  208.               }    /* end else */
  209.  
  210.             cdist=(long)(row+vsrow)*80+col;
  211.  
  212.             if(c)
  213.                 n_cur(numline,col);
  214.             else
  215.                 n_cur(row,col);
  216.  
  217.             if(cflag || cbuf)
  218.                 if(c)
  219.                     updatescr(tw->colors[2]);
  220.                 else
  221.                     setattr(row,tw->colors[0],tw->colors[2]);
  222.             break;                                         
  223.  
  224.         case CURLF:
  225.         case E_CURLF:
  226.             row=n_row();
  227.             col=n_col();
  228.             if(col>0) {
  229.                 col--;
  230.                 cdist=(long)(row+vsrow)*80+col;
  231.                 if(cflag || cbuf) {
  232.                     if(is_in(row,vsrow,col+1)) 
  233.                         (*attrptr)(tw->colors[2]);
  234.                     else 
  235.                         (*attrptr)(tw->colors[0]);
  236.                   }
  237.                 n_cur(row,col);
  238.                 if(cflag || cbuf) {
  239.                     if(is_in(row,vsrow,col))
  240.                         (*attrptr)(tw->colors[2]);
  241.                     else 
  242.                         (*attrptr)(tw->colors[0]);
  243.                   }
  244.               }
  245.             break;
  246.  
  247.         case CURRT:
  248.         case E_CURRT:
  249.             row=n_row();
  250.             col=n_col();
  251.             if(col<79) {
  252.                 col++;
  253.                 cdist=(long)(row+vsrow)*80+col;
  254.                 if(cflag || cbuf) {
  255.                     if(is_in(row,vsrow,col-1)) 
  256.                         (*attrptr)(tw->colors[2]);
  257.                     else 
  258.                         (*attrptr)(tw->colors[0]);
  259.                   }
  260.                 n_cur(row,col);
  261.                 if(cflag || cbuf) {
  262.                     if(is_in(row,vsrow,col)) 
  263.                         (*attrptr)(tw->colors[2]);
  264.                     else 
  265.                         (*attrptr)(tw->colors[0]);
  266.                   }
  267.               }
  268.             break;
  269.  
  270.         case CURUP:
  271.         case E_CURUP:
  272.             row=n_row();
  273.             col=n_col();
  274.             if(row>=1) {
  275.                 row--;
  276.                 if(cflag) {
  277.                     cdist=(long)(vsrow+row)*80+col;
  278.                     setattr(row+1,tw->colors[0],tw->colors[2]);
  279.                     setattr(row,tw->colors[0],tw->colors[2]);
  280.                   }
  281.               }
  282.             else {
  283.                 if(vsrow>(-tw->bkscroll)) {
  284.                     if (def.autoscroll)
  285.                         scroll=-1;
  286.                     vsrow--;
  287.                     row=0;
  288.                     VSscrolback(tw->vs,1);
  289.                     if(cflag || cbuf) {
  290.                         cdist=(long)(vsrow+row)*80+col;
  291.                         setattr(1,tw->colors[0],tw->colors[2]);
  292.                         setattr(0,tw->colors[0],tw->colors[2]);
  293.                       }
  294.                   }
  295.                 else 
  296.                     if(row>0){
  297.                         row--;
  298.                         if(cflag || cbuf) {
  299.                             cdist=(long)(vsrow+row)*80+col;
  300.                             setattr(row+1,tw->colors[0],tw->colors[2]);
  301.                             setattr(row,tw->colors[0],tw->colors[2]);
  302.                           }
  303.                       }                
  304.               }
  305.             n_cur(row,col);
  306.             break;
  307.  
  308.         case CURDN:
  309.         case E_CURDN:
  310.             row=n_row();
  311.             col=n_col();
  312.             if(row<numline) {
  313.                 ++row;
  314.                 if(cflag) {
  315.                     cdist=(long)(vsrow+row)*80+col;
  316.                     setattr(row-1,tw->colors[0],tw->colors[2]);
  317.                     setattr(row,tw->colors[0],tw->colors[2]);
  318.                   }
  319.               }
  320.             else{
  321.                 if(vsrow<0) {
  322.                     if (def.autoscroll)
  323.                         scroll=1;
  324.                     ++vsrow;
  325.                     row=numline;
  326.                     VSscrolforward(tw->vs,1);
  327.                     if(cflag || cbuf) {
  328.                         cdist=(long)(vsrow+row)*80+col;
  329.                         setattr(numline-1,tw->colors[0],tw->colors[2]);
  330.                         setattr(numline,tw->colors[0],tw->colors[2]);
  331.                       }
  332.                   }
  333.                 else 
  334.                     if(row<numline) {
  335.                         ++row;
  336.                         if(cflag || cbuf) {
  337.                             cdist=(long)(vsrow+row)*80+col;
  338.                             setattr(row-1,tw->colors[0],tw->colors[2]);
  339.                             setattr(row,tw->colors[0],(int)tw->colors[2]);
  340.                           }
  341.                       }
  342.               }
  343.             n_cur(row,col);
  344.             break;
  345.  
  346.         case PGUP:
  347.         case E_PGUP:
  348.             row=n_row();
  349.             col=n_col();
  350.             VSscrolback(tw->vs,numline+1);
  351.             vsrow-=numline+1;
  352.             if(vsrow<(-tw->bkscroll)) 
  353.                 vsrow=(-tw->bkscroll);
  354.             cdist=(long)(vsrow+row)*80+col;
  355.             if(cflag || cbuf)
  356.                 updatescr(tw->colors[2]);
  357.             n_cur(row,col);
  358.             break;
  359.  
  360.         case PGDN:
  361.         case E_PGDN:
  362.             row=n_row();
  363.             col=n_col();
  364.             VSscrolforward(tw->vs,numline+1);
  365.             vsrow+=numline+1;
  366.             if(vsrow>0) 
  367.                 vsrow=0;
  368.             cdist=(long)(vsrow+row)*80+col;
  369.             if(cflag || cbuf)
  370.                 updatescr(tw->colors[2]);
  371.             n_cur(row,col);
  372.             break;
  373.  
  374.         case (int)' ':
  375.         case (int)'\n':
  376.             row=n_row();
  377.             col=n_col();
  378.             if(!cflag) {
  379.                 if((n_flags()&3)) {
  380.                     cflag=1;
  381.                     cbuf=0;
  382.                     remark(row,col,vsrow,tw);
  383.                   }
  384.                 else {
  385.                     if(cbuf)
  386.                         resetscr(tw);
  387.                     cflag=1;
  388.                     cbuf=0;
  389.                     cstart=(long)(vsrow+row)*80+col;
  390.                     cdist=cstart;
  391.                     setattr(row,tw->colors[0],tw->colors[2]);
  392.                   }
  393.               }
  394.             else {
  395.                 cflag=0;
  396.                 cbuf=1;
  397.                 cend=(long)(vsrow+row)*80+col;
  398.               }
  399.             n_cur(row,col);
  400.             break;
  401.  
  402.         case ALTC:
  403.             if(cend<cstart) {
  404.                 temp=cend;
  405.                 cend=cstart;
  406.                 cstart=temp;
  407.               }
  408.             size=((cend-cstart+2));
  409.             if(size>32700)
  410.                 break;
  411.             if(copybuf!=NULL) 
  412.                 free(copybuf);
  413.             if((copybuf=(char *)malloc((unsigned int)(size+1)))==NULL)
  414.                 break;
  415.             size=VSgettext(tw->vs,(int)(cstart%80)-1,(int)(cstart/80),
  416.             (int)(cend%80),(int)(cend/80),copybuf,size,lineend,0);
  417.             copybuf[size]='\0';
  418.             break;
  419.                                         
  420.         default:
  421.             break;
  422.       }
  423.     set_cur(1);
  424. }
  425.  
  426. /*************************************************
  427. *
  428. * setattr()
  429. * sets the attribute of a line 
  430. */
  431. int setattr(int row,int attrn,int attrh)
  432. {
  433.     int i,
  434.         col;
  435.  
  436.     if(is_in(row,vsrow,0) || is_in(row,vsrow,79) || its_in((long)(row+vsrow-1)*80,(long)(row+vsrow+1)*80+79 )) {
  437.         n_row();
  438.         col=n_col();
  439.         for(i=0; i<80;i++) {
  440.             n_cur(row,i);
  441.             if(is_in(row,vsrow,i))
  442.                 (*attrptr)((char)attrh);
  443.             else
  444.                 (*attrptr)((char)attrn);
  445.           }
  446.         n_cur(row,col);
  447.         return(1);
  448.       }
  449.     return(0);
  450. }
  451.  
  452. /************************************************
  453. *
  454. * resetscr(struct twin *tw)
  455. * clear marked text from screen
  456. */
  457.  
  458. void resetscr(struct twin *tw)
  459. {
  460.     int row,col,r,c;
  461.  
  462.     r=n_row();
  463.     c=n_col();
  464.     for(row=0; row<numline+1; row++)
  465.         for(col=0; col<80; col++) {
  466.             n_cur(row,col);
  467.             (*attrptr)((char)(tw->colors[0]));
  468.           }
  469.     n_cur(r,c);
  470. }
  471.  
  472. /*************************************************
  473. *
  474. * updatescr()
  475. * marks screen
  476. */
  477. void updatescr(int attr)
  478. {
  479.     int row,
  480.         col,
  481.         r,
  482.         c;
  483.  
  484.     r=n_row();
  485.     c=n_col();
  486.     for(row=0;row<numline+1;row++)
  487.         for(col=0;col<80;col++) {
  488.             if(is_in(row,vsrow,col)) {
  489.                 n_cur(row,col);
  490.                 (*attrptr)(attr);
  491.               }
  492.           }
  493.     n_cur(r,c);
  494. }
  495.  
  496. /**********************************************************************
  497. *
  498. * chkmouse()
  499. * gets mouse movement and button info
  500. */
  501.  
  502. #ifdef MOUSE
  503. #ifdef SCRL_MOUSE
  504. int num_right = 0;
  505. int num_down = 0;
  506. #endif
  507. #define MIN_MOVE 6
  508. #endif
  509.  
  510. #ifdef SCRL_MOUSE
  511. int chkmouse(void )
  512. {
  513.     int m1=3, m2=0 ,m3=0 ,m4=0;        /* mouse variables */
  514.  
  515.     printf("using chkmouse\n");
  516.     if(!mighty) 
  517.         return(-1);
  518.  
  519.     m1=11;
  520.  
  521.     mousecl(&m1,&m2,&m3,&m4);            /* read absolute positions */
  522.  
  523. #ifndef MOUSE
  524.     if(m3>2)
  525.         return(CURRT);
  526.     if(m3<-2)
  527.         return(CURLF);
  528.     if(m4>2)
  529.         return(CURDN);
  530.     if(m4<-2)
  531.         return(CURUP);
  532. #else
  533.     num_right += m3;
  534.     num_down += m4;
  535.     if (abs(num_right)>abs(num_down)) {
  536.         if(num_right >= MIN_MOVE) {
  537.             num_right-=MIN_MOVE;
  538.             return(CURRT);
  539.          }    /* end if */
  540.         if(num_right <= -MIN_MOVE) {
  541.             num_right+=MIN_MOVE;
  542.             return(CURLF);
  543.          }    /* end if */
  544.       } /* end if */
  545.     if(num_down >= MIN_MOVE) {
  546.         num_down-=MIN_MOVE;
  547.         return(CURDN);
  548.      }    /* end if */
  549.     if(num_down <= -MIN_MOVE) {
  550.         num_down+=MIN_MOVE;
  551.         return(CURUP);
  552.       } /* end if */
  553. #endif
  554.  
  555.     m1=3;                                /* read mouse buttons */
  556.     mousecl(&m1,&m2,&m3,&m4);
  557.  
  558.     if(m2 & 1) 
  559.         m2=1;
  560.     else 
  561.         m2=0;
  562.     if(m2!=mcflag) {
  563.         mcflag=m2;
  564.         return((int)' ');
  565.       }
  566.     return(-1);
  567. }
  568. #endif
  569.  
  570. #ifndef MOUSE
  571. /*****************************************************************
  572. *
  573. * initmouse()
  574. * check to see if we have a mouse
  575. */
  576. int initmouse(void )
  577. {
  578.     int m1=0, m2=0, m3=0, m4=0;              /* mouse variables */
  579.  
  580.     m1=0;                         
  581.     mousecl(&m1,&m2,&m3,&m4);                /* initialize mouse driver */
  582.     if(!m1) 
  583.         return(0);
  584.     m1=2;
  585.     mousecl(&m1,&m2,&m3,&m4);                /* turn off software cursor */
  586.     return(1);
  587. }
  588. #endif
  589.  
  590. /**************************************
  591. *
  592. * is_in()
  593. * check to see if cursor is at marked location
  594. */
  595. int is_in(int row,int vs_row,int col)
  596. {
  597.     long vsloc,
  598.         c,
  599.         end,
  600.         start;
  601.  
  602.     start=cstart;
  603.     if(cbuf) 
  604.         end=cend;
  605.     else 
  606.         if(cflag) 
  607.             end=cdist;
  608.          else 
  609.             return(0);
  610.     if(end<start) {
  611.         c=end;
  612.         end=start;
  613.         start=c;
  614.       }
  615.     vsloc=(long)(row+vs_row)*80+col;
  616.     if((start<=vsloc) && (end>=vsloc)) 
  617.         return(1);
  618.     return(0);
  619. }
  620.  
  621. /*******************************************
  622. *
  623. * its_in()
  624. *
  625. * checks to see if marked area is between two points
  626. */
  627. static int its_in(long vsstart,long vsend)
  628. {
  629.     long start,end;
  630.  
  631.     start=cstart;
  632.     if(cbuf) 
  633.         end=cend;
  634.     else 
  635.         if(cflag) 
  636.             end=cdist;
  637.         else 
  638.             return(0);
  639.     if(((vsstart<=start) && (vsend>=start)) || ((vsstart<=end) && (vsend>=end)))
  640.         return(1);
  641.     return(0);
  642. }
  643.  
  644. /********************************************
  645. *
  646. * remark()
  647. *
  648. * it resets the the end points of the marked text
  649. *
  650. */
  651. void remark(int row,int col,int vs_row,struct twin *tw)
  652. {
  653.     long vsloc, dstart, dend;
  654.     int x, y;
  655.  
  656.     vsloc=(long)(row+vs_row)*80+col;
  657.     dstart=vsloc-cstart;
  658.     dend=vsloc-cend;
  659.     if(dstart<0) 
  660.         dstart=-dstart;
  661.     if(dend<0) 
  662.         dend=-dend;
  663.     if(dstart<dend) {
  664.         cstart=cend;
  665.         cdist=vsloc;
  666.       }
  667.     else 
  668.         cdist=vsloc;
  669.     for(y=0; y<numline+1; y++)
  670.         for(x=0; x<80; x++) {
  671.             if(is_in(y,vs_row,x)) {
  672.                 n_cur(y,x);        
  673.                 (*attrptr)(tw->colors[2]);
  674.               }    /* end if */
  675.             else {
  676.                 n_cur(y,x);        
  677.                 (*attrptr)(tw->colors[0]);
  678.               }    /* end else */
  679.           }    /* end for */
  680.     n_cur(row,col);
  681. }
  682.