home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 500-599 / ff589.lza / Term / TermSrc.lha / termRaster.c < prev    next >
C/C++ Source or Header  |  1991-12-08  |  10KB  |  538 lines

  1. /* $Revision Header * Header built automatically - do not edit! *************
  2.  *
  3.  *    (C) Copyright 1990 by Olaf 'Olsen' Barthel & MXM
  4.  *
  5.  *    Name .....: Serial.c
  6.  *    Created ..: Saturday 02-Mar-91 18:30
  7.  *    Revision .: 0
  8.  *
  9.  *    Date            Author          Comment
  10.  *    =========       ========        ====================
  11.  *    02-Mar-91       Olsen           Created this file!
  12.  *
  13.  * $Revision Header ********************************************************/
  14.  
  15. #include "termGlobal.h"
  16.  
  17.     /* RasterMarkArea(WORD Column,WORD Line,WORD Length):
  18.      *
  19.      *    Mark an area in the term Buffer window.
  20.      */
  21.  
  22. VOID
  23. RasterMarkArea(WORD Column,WORD Line,WORD Length)
  24. {
  25.     STATIC WORD OldColumn = -1,OldLine = -1,OldLength = -1;
  26.  
  27.     if(OldColumn != Column || OldLine != Line || OldLength != Length)
  28.     {
  29.         if(OldColumn != -1)
  30.             ClipBlit(RPort,0,0,RPort,OldColumn << 3,OldLine << 3,OldLength << 3,8,0x50);
  31.  
  32.         ClipBlit(RPort,0,0,RPort,Column << 3,Line << 3,Length << 3,8,0x50);
  33.     }
  34.  
  35.     OldColumn    = Column;
  36.     OldLine        = Line;
  37.     OldLength    = Length;
  38. }
  39.  
  40.     /* RasterClip():
  41.      *
  42.      *    Put a character raster portion into the clipboard.
  43.      */
  44.  
  45. VOID
  46. RasterClip(BYTE SingleChar,BYTE Xerox)
  47. {
  48.     struct IntuiMessage    *Massage;
  49.     ULONG             Code,Class;
  50.     BYTE             SkipLoop = FALSE;
  51.     LONG             ThisLine,ThisColumn,MyColumn,SomeColumn;
  52.     UBYTE            *TheLine;
  53.  
  54.         /* Remember initial mouse position. */
  55.  
  56.     ThisColumn    = Window -> MouseX >> 3;
  57.     ThisLine    = Window -> MouseY >> 3;
  58.  
  59.     if(ThisColumn <= LastColumn && ThisLine <= LastLine)
  60.     {
  61.         RasterMarkArea(-1,-1,-1);
  62.  
  63.             /* Find the approriate line and its length. */
  64.  
  65.         TheLine = &Raster[RasterWidth * ThisLine];
  66.  
  67.         if(SingleChar)
  68.         {
  69.             if(TheLine[ThisColumn] && TheLine[ThisColumn] != ' ')
  70.             {
  71.                 SerWrite(&TheLine[ThisColumn],1);
  72.  
  73.                 if(Xerox)
  74.                 {
  75.                     switch(Config . SendCR)
  76.                     {
  77.                         case CR_IGNORE:    break;
  78.  
  79.                         case CR_ASCR:    SerWrite("\r",1);
  80.                                 break;
  81.  
  82.                         case CR_ASCRLF:    SerWrite("\r\n",2);
  83.                                 break;
  84.                     }
  85.                 }
  86.             }
  87.  
  88.             return;
  89.         }
  90.  
  91.             /* Resonable dimensions? */
  92.  
  93.         if(ThisColumn <= LastColumn)
  94.         {
  95.             MyColumn = ThisColumn;
  96.  
  97.                 /* Loop until left mouse button is release. */
  98.  
  99.             while(!SkipLoop)
  100.             {
  101.                 WaitPort(Window -> UserPort);
  102.  
  103.                 while(Massage = (struct IntuiMessage *)GetMsg(Window -> UserPort))
  104.                 {
  105.                     Class    = Massage -> Class;
  106.                     Code    = Massage -> Code;
  107.  
  108.                     ReplyMsg(Massage);
  109.  
  110.                         /* We're finished! */
  111.  
  112.                     if(Class == IDCMP_MOUSEBUTTONS && Code == SELECTUP)
  113.                     {
  114.                         SkipLoop = TRUE;
  115.  
  116.                             /* Did we get a reasonable mouse
  117.                              * position?
  118.                              */
  119.  
  120.                         if(MyColumn != ThisColumn)
  121.                         {
  122.                                 /* Preserve right order of
  123.                                  * numbers.
  124.                                  */
  125.  
  126.                             if(MyColumn < ThisColumn)
  127.                             {
  128.                                 LONG Help;
  129.  
  130.                                 Help        = ThisColumn;
  131.                                 ThisColumn    = MyColumn;
  132.                                 MyColumn    = Help;
  133.                             }
  134.  
  135.                             RasterMarkArea(-1,-1,-1);
  136.  
  137.                                 /* Clip the contents of the line to
  138.                                  * the clipboard.
  139.                                  */
  140.  
  141.                             SaveClip(&TheLine[ThisColumn],MyColumn - ThisColumn);
  142.  
  143.                             if(Xerox)
  144.                             {
  145.                                 SerWrite(&TheLine[ThisColumn],MyColumn - ThisColumn);
  146.  
  147.                                 switch(Config . SendCR)
  148.                                 {
  149.                                     case CR_IGNORE:    break;
  150.  
  151.                                     case CR_ASCR:    SerWrite("\r",1);
  152.                                             break;
  153.  
  154.                                     case CR_ASCRLF:    SerWrite("\r\n",2);
  155.                                             break;
  156.                                 }
  157.                             }
  158.                         }
  159.  
  160.                         break;
  161.                     }
  162.  
  163.                         /* The mouse has moved. */
  164.  
  165.                     if(Class == IDCMP_MOUSEMOVE)
  166.                     {
  167.                         STATIC LONG OldColumn = ~0;
  168.  
  169.                             /* Determine new mouse position. */
  170.  
  171.                         SomeColumn = MyColumn;
  172.  
  173.                         MyColumn = Massage -> MouseX >> 3;
  174.  
  175.                         if((Massage -> MouseY >> 3) < ThisLine)
  176.                             MyColumn = 0;
  177.  
  178.                         if((Massage -> MouseY >> 3) > ThisLine)
  179.                             MyColumn = LastColumn + 1;
  180.  
  181.                             /* Don't redraw the line if nothing
  182.                              * has changed.
  183.                              */
  184.  
  185.                         if(OldColumn != MyColumn)
  186.                         {
  187.                             OldColumn = MyColumn;
  188.  
  189.                                 /* Reasonable position? */
  190.  
  191.                             if(MyColumn <= LastColumn + 1)
  192.                             {
  193.                                 if(MyColumn >= 0)
  194.                                 {
  195.                                         /* Highlight the selected
  196.                                          * area (invert).
  197.                                          */
  198.  
  199.                                     if(MyColumn != ThisColumn)
  200.                                     {
  201.                                         if(MyColumn < ThisColumn)
  202.                                             RasterMarkArea(MyColumn,ThisLine,ThisColumn - MyColumn);
  203.                                         else
  204.                                             RasterMarkArea(ThisColumn,ThisLine,MyColumn - ThisColumn);
  205.                                     }
  206.                                 }
  207.                             }
  208.                             else
  209.                                 MyColumn = SomeColumn;
  210.                         }
  211.                     }
  212.                 }
  213.             }
  214.         }
  215.     }
  216. }
  217.  
  218.     /* DeleteRaster():
  219.      *
  220.      *    Free the contents of the character raster.
  221.      */
  222.  
  223. VOID
  224. DeleteRaster()
  225. {
  226.     if(Raster)
  227.     {
  228.         FreeVec(Raster);
  229.  
  230.         Raster = NULL;
  231.     }
  232.  
  233.     if(RasterAttr)
  234.     {
  235.         FreeVec(RasterAttr);
  236.  
  237.         RasterAttr = NULL;
  238.     }
  239. }
  240.  
  241.     /* CreateRaster():
  242.      *
  243.      *    Create the character raster.
  244.      */
  245.  
  246. BYTE
  247. CreateRaster()
  248. {
  249.         /* Width of the screen * 2 (in characters).
  250.          * extra for Double width
  251.          */
  252.  
  253.     RasterWidth    = Window -> Width >> 2;
  254.  
  255.         /* Height of the character raster. */
  256.  
  257.     RasterHeight    = Window -> Height >> 3;
  258.  
  259.         /* Allocate the raster. */
  260.  
  261.     if(!(Raster = (UBYTE *)AllocVec(RasterWidth * RasterHeight * 2,MEMF_PUBLIC|MEMF_CLEAR)))
  262.         return(FALSE);
  263.  
  264.         /* Allocate the raster attributes. */
  265.  
  266.     if(!(RasterAttr = (UBYTE *)AllocVec(RasterHeight * 2,MEMF_PUBLIC|MEMF_CLEAR)))
  267.     {
  268.         FreeVec(Raster);
  269.  
  270.         Raster = NULL;
  271.  
  272.         return(FALSE);
  273.     }
  274.  
  275.     return(TRUE);
  276. }
  277.  
  278.     /* RasterEraseScreen(BYTE Mode):
  279.      *
  280.      *    Erase parts of the screen.
  281.      */
  282.  
  283. VOID
  284. RasterEraseScreen(BYTE Mode)
  285. {
  286.     WORD First,Last;
  287.  
  288.     switch(Mode)
  289.     {
  290.         case 1:    First    = 0;
  291.             Last    = CursorY * RasterWidth + CursorX; 
  292.             memset(&RasterAttr[0],SCALE_NORMAL,CursorY + 1);
  293.             break;
  294.  
  295.         case 2:    First    = 0;
  296.             Last    = (RasterHeight + 1) * RasterWidth - 1;
  297.             memset(&RasterAttr[0],SCALE_NORMAL,RasterHeight + 1);
  298.             break;
  299.  
  300.         default:First    = CursorY * RasterWidth;
  301.             Last    = (RasterHeight + 1) * RasterWidth - 1;
  302.             memset(&RasterAttr[CursorY],SCALE_NORMAL,RasterHeight - CursorY + 1);
  303.             break;
  304.     }
  305.  
  306.     memset(&Raster[First],' ',Last - First);
  307. }
  308.  
  309.     /* RasterEraseLine(BYTE Mode):
  310.      *
  311.      *    Erase parts of the current cursor line.
  312.      */
  313.  
  314. VOID
  315. RasterEraseLine(BYTE Mode)
  316. {
  317.     WORD First,Last;
  318.  
  319.     switch(Mode)
  320.     {
  321.         case 1:    First    = CursorY * RasterWidth;
  322.             Last    = First + CursorX;
  323.             break;
  324.  
  325.         case 2:    First    = CursorY * RasterWidth;
  326.             Last    = First + RasterWidth - 1;
  327.             break;
  328.  
  329.         default:First    = CursorY * RasterWidth + CursorX;
  330.             Last    = (CursorY + 1) * RasterWidth - 1;
  331.             break;
  332.     }
  333.  
  334.     memset(&Raster[First],' ',Last - First);
  335. }
  336.  
  337.     /* RasterEraseCharacters(WORD Chars):
  338.      *
  339.      *    Erase a number of characters in the current cursor
  340.      *    line.
  341.      */
  342.  
  343. VOID
  344. RasterEraseCharacters(WORD Chars)
  345. {
  346.     WORD i,First,Diff = RasterWidth - CursorX;
  347.     UBYTE *FirstPtr,*LastPtr;
  348.  
  349.     First = CursorY * RasterWidth + CursorX;
  350.  
  351.     FirstPtr = &Raster[First];
  352.     LastPtr = &Raster[First + Chars];
  353.  
  354.     i = 0;
  355.  
  356.     while(i++ < Diff)
  357.     {
  358.         *FirstPtr++ = *LastPtr;
  359.  
  360.         *LastPtr++ = ' ';
  361.     }
  362. }
  363.  
  364.     /* RasterClearLine(WORD Lines):
  365.      *
  366.      *    Clear the current line.
  367.      */
  368.  
  369. VOID
  370. RasterClearLine(WORD Lines)
  371. {
  372.     if(CursorY + Lines >= RasterHeight)
  373.         RasterEraseScreen(0);
  374.     else
  375.     {
  376.         WORD     Max = (RasterHeight - (CursorY + Lines)) * RasterWidth,First,Last;
  377.         UBYTE    *From,*To;
  378.  
  379.         From    = &Raster[(CursorY + Lines)    * RasterWidth];
  380.         To    = &Raster[ CursorY        * RasterWidth];
  381.  
  382.         while(Max--)
  383.             *To++ = *From++;
  384.  
  385.         First    = (RasterHeight - Lines) * RasterWidth;
  386.         Last    = (RasterHeight + 1) * RasterWidth - 1;
  387.  
  388.         memset(&RasterAttr[RasterHeight - Lines],SCALE_NORMAL,Lines);
  389.  
  390.         memset(&Raster[First],' ',Last - First);
  391.     }
  392. }
  393.  
  394.     /* RasterInsertLine(WORD Lines):
  395.      *
  396.      *    Insert a number of lines at the current cursor line.
  397.      */
  398.  
  399. VOID
  400. RasterInsertLine(WORD Lines)
  401. {
  402.     if(CursorY + Lines > RasterHeight)
  403.         RasterEraseScreen(0);
  404.     else
  405.     {
  406.         WORD     From,To,
  407.              Max;
  408.         UBYTE    *FromPtr,
  409.             *ToPtr;
  410.  
  411.         Max    = (LastLine + 1 - Lines - CursorY) * RasterWidth;
  412.  
  413.         From    = RasterWidth * (LastLine + 1 - Lines) - 1;
  414.         To    = RasterWidth * (LastLine + 1)         - 1;
  415.  
  416.         FromPtr    = &Raster[From];
  417.         ToPtr    = &Raster[To];
  418.  
  419.         while(Max--)
  420.             *ToPtr-- = *FromPtr--;
  421.  
  422.         memset(&Raster[CursorY * RasterWidth],' ',Lines * RasterWidth);
  423.     }
  424. }
  425.  
  426.     /* RasterScrollRegion(WORD Direction,WORD RasterTop,WORD RasterBottom,WORD RasterLines):
  427.      *
  428.      *    Scroll the contents of the character raster up/down.
  429.      */
  430.  
  431. VOID
  432. RasterScrollRegion(WORD Direction,WORD RasterTop,WORD RasterBottom,WORD RasterLines)
  433. {
  434.     WORD First,Last,i,Max,Dir;
  435.     UBYTE *FirstPtr,*LastPtr;
  436.  
  437.     Max = RasterLines * RasterWidth;
  438.     Dir = Direction < 0 ? -Direction : Direction;
  439.     
  440.     if(Dir > RasterLines)
  441.     {
  442.             /* All that is needed is to delete the lines. */
  443.     
  444.         memset(&Raster[RasterTop * RasterWidth],' ',Max);
  445.     }
  446.     else
  447.     {
  448.         if(Dir)
  449.             Max -= (Dir - 1) * RasterWidth;
  450.         
  451.         if(Direction < 0)
  452.         {
  453.             First    = (RasterTop + RasterLines    ) * RasterWidth - 1;
  454.             Last    = (RasterTop + RasterLines + Dir) * RasterWidth - 1;
  455.     
  456.             FirstPtr = &Raster[First];
  457.             LastPtr = &Raster[Last];
  458.     
  459.             i = 0;
  460.     
  461.             while(i++ < Max)
  462.                 *LastPtr-- = *FirstPtr--;
  463.  
  464.             for(i = RasterBottom ; i >= (RasterTop + Dir) ; i--)
  465.                 RasterAttr[i] = RasterAttr[i - Dir];
  466.  
  467.             memset(&Raster[RasterTop * RasterWidth],' ',RasterWidth * Dir);
  468.  
  469.             memset(&RasterAttr[RasterTop],SCALE_NORMAL,Dir);
  470.         }
  471.         else
  472.         {
  473.             First    = RasterTop * RasterWidth + RasterWidth * Dir;
  474.             Last    = RasterTop * RasterWidth;
  475.     
  476.             FirstPtr = &Raster[First];
  477.             LastPtr = &Raster[Last];
  478.     
  479.             i = 0;
  480.     
  481.             while(i++ < Max)
  482.                 *LastPtr++ = *FirstPtr++;
  483.     
  484.             for(i = RasterTop ; i <= (RasterBottom - Dir) ; i++)
  485.                 RasterAttr[i] = RasterAttr[i + Dir];
  486.  
  487.             memset(&Raster[(RasterLines - Dir + 1) * RasterWidth],' ',RasterWidth * Dir);
  488.  
  489.             memset(&RasterAttr[RasterBottom - Dir],SCALE_NORMAL,Dir);
  490.         }
  491.     }
  492. }
  493.  
  494.     /* RasterShiftChar(WORD Size):
  495.      *
  496.      *    Shift the characters following the current cursor
  497.      *    position Size characters to the right.
  498.      */
  499.  
  500. VOID
  501. RasterShiftChar(WORD Size)
  502. {
  503.     WORD i,First;
  504.     UBYTE *FirstPtr,*LastPtr;
  505.  
  506.     First = CursorY * RasterWidth + RasterWidth - 1;
  507.  
  508.     FirstPtr = &Raster[First];
  509.     LastPtr = &Raster[First - Size];
  510.  
  511.     i = RasterWidth - Size;
  512.  
  513.     while(i-- > CursorX)
  514.         *FirstPtr-- = *LastPtr--;
  515. }
  516.  
  517.     /* RasterPutString(UBYTE *String,WORD Length):
  518.      *
  519.      *    Put a string into the character raster.
  520.      */
  521.  
  522. VOID
  523. RasterPutString(UBYTE *String,WORD Length)
  524. {
  525.     if(Length == 1)
  526.     {
  527.         if(CursorX + 1 < RasterWidth)
  528.             Raster[CursorY * RasterWidth + CursorX] = String[0];
  529.     }
  530.     else
  531.     {
  532.         if(CursorX + Length >= RasterWidth)
  533.             Length = RasterWidth - CursorX;
  534.  
  535.         CopyMem(String,&Raster[CursorY * RasterWidth + CursorX],Length);
  536.     }
  537. }
  538.