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