home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / dirs / 'liner_394.lzh / 'Liner / Source / console.c < prev    next >
C/C++ Source or Header  |  1990-10-28  |  8KB  |  285 lines

  1. /*                              console.c                             */
  2. /*Contains routines that interact with the console.device (generally) */
  3.  
  4. #include "globals.h"
  5.  
  6. void WriteConsole(Buffer,Length)   /*Write a string to the console*/
  7. char *Buffer;
  8. int Length;
  9. {
  10.    consoleWriteMsg->io_Command=CMD_WRITE;
  11.    consoleWriteMsg->io_Data=(char *)Buffer;
  12.    consoleWriteMsg->io_Length=Length;
  13.    DoIO(consoleWriteMsg);
  14. }
  15.  
  16. void JumpToTop() /*Jump to top of outline*/
  17. {
  18.    FirstScrnItem=CurrentItem=(struct LineItem *)FirstItem;
  19.    PrintItemList(CurrentItem,1);
  20.    PlotCursor(MinX(CurrentItem),1);
  21. }
  22.  
  23. void JumpToBottom() /*Jump to bottom of outline*/
  24. {
  25.    struct LineItem *Item;
  26.    int c;
  27.  
  28.    CurrentItem=(struct LineItem *)LastItem;
  29.    for(c=0,Item=(struct LineItem *)LastItem;Item!=FirstItem && 
  30.          c < SCRNHEIGHT-1;Item=(struct LineItem *)Item->PrevItem,c++);
  31.    FirstScrnItem=(struct LineItem *)Item;
  32.    PrintItemList(FirstScrnItem,1);
  33.    PlotCursor(MinX(CurrentItem),DispRows);
  34. }
  35.  
  36. void PrintItem(Item)   /*Print an item at the current Y location*/
  37. struct LineItem *Item;
  38. {
  39.    char ConsoleBuffer[256];
  40.    SHORT maxlen;
  41.    
  42.    ConsoleBuffer[0]=CSI;
  43.    ConsoleBuffer[1]=0x4b;
  44.    ConsoleBuffer[2]=NULL;
  45.  
  46.    GetOutlineChars(Item,ConsoleBuffer);
  47.    strcat(ConsoleBuffer,"  ");
  48.    strcat(ConsoleBuffer,Item->Text);
  49.    maxlen=strlen(Item->Text)-MaxLen(Item->Level);
  50.  
  51.    WriteConsole(ConsoleBuffer,-1);
  52. }
  53.  
  54. void PlotCursor(x,y)      /*Plot the console cursor on the screen*/
  55. int x,y;
  56. {
  57.    char Buffer[10];
  58.    int len=1;
  59.    int true_y;
  60.    
  61.    true_y=(prefs.DS) ? (y*2)-1 : y;/*If double spaced, translate y to 'true' y*/
  62.  
  63.    Buffer[0]=CSI;
  64.    len+=stci_d(&Buffer[1],true_y,9);
  65.    Buffer[len]=0x3b;
  66.    len+=stci_d(&Buffer[len+1],x,6);
  67.    Buffer[len+1]=0x48;
  68.    Buffer[len+2]=NULL;
  69.    WriteConsole(Buffer,-1);
  70.    CurX=x;
  71.    CurY=y;
  72. }
  73.  
  74. void CursorRight(Qualifier) /*Move the cursor one to the right*/
  75. USHORT Qualifier;
  76. {
  77.    UBYTE TempX;
  78.    int min;
  79.  
  80.    min=MinX(CurrentItem);
  81.    if(ErrorInTitle)
  82.       TitleErrorCancel();
  83.    if(InvsMode < NOINV)
  84.       CancelInvs();
  85.    if(CurX >= MaxX(CurrentItem)) /*If at the end of the line*/
  86.    {                    /*move to the start of the next one*/
  87.       TempX=CurX;
  88.       CurX=1;
  89.       if(!CursorDown())
  90.          CurX=TempX;
  91.    }
  92.    else
  93.       if(Qualifier & 3) /*Handle SHIFT-cursor-right*/
  94.          PlotCursor(CurX+FindNextWord(&CurrentItem->Text[CurX-min]),
  95.                CurY);
  96.       else
  97.          if(Qualifier & 8) /*Handle CTRL-cursor-right*/
  98.             PlotCursor(MaxX(CurrentItem),CurY); /*(end of line)*/
  99.          else
  100.             PlotCursor(CurX+1,CurY);
  101. }
  102.  
  103. void CursorLeft(Qualifier) /*Move the cursor one to the left*/
  104. USHORT Qualifier;
  105. {
  106.    UBYTE TempX;
  107.  
  108.    if(ErrorInTitle)
  109.       TitleErrorCancel();
  110.    if(InvsMode < NOINV)
  111.       CancelInvs();
  112.    if(CurX <= MinX(CurrentItem)) /*If at the beginning of the line*/
  113.    {                 /*move to the end of the previous one*/
  114.       TempX=CurX;
  115.       CurX=100;
  116.       if(!CursorUp())
  117.          CurX=TempX;
  118.    }
  119.    else
  120.       if(Qualifier & 3) /*Handle SHIFT-cursor-left*/
  121.          PlotCursor(MinX(CurrentItem)+FindPrevWord(
  122.                CurrentItem->Text,CurX-MinX(CurrentItem)),CurY);
  123.       else
  124.          if(Qualifier & 8) /*Handle CTRL-cursor-left*/
  125.             PlotCursor(MinX(CurrentItem),CurY);
  126.          else
  127.             PlotCursor(CurX-1,CurY);
  128. }
  129.  
  130. FindNextWord(string)
  131. char *string;
  132. {
  133.    int end;
  134.    for(end=0;string[end] != ' ' && string[end] != NULL;end++);
  135.    if(end==strlen(string))
  136.       return(end);
  137.    
  138.    for(end++;string[end] ==' ' && string[end] != NULL;end++);
  139.    return(end);
  140. }
  141.  
  142. FindPrevWord(string,start)
  143. char *string;
  144. int start;
  145. {
  146.    int end;
  147.    
  148.    end=start;
  149.    if(string[end]!=' ' && end > 0 && string[end-1]==' ')
  150.       end--;
  151.    
  152.    for(;string[end-1] != ' ' && end > 0;end--);
  153.    
  154.    while(end > 0 && string[end]== ' ')
  155.       for(end--;string[end-1] != ' ' && end > 0;end--);
  156.    return(end);
  157. }
  158.  
  159. void InsertChar(Text,Character,Pos)   /*Insert a char into an item*/
  160. char *Text,Character;
  161. int Pos;
  162. {
  163.    int c;
  164.  
  165.    for(c=strlen(Text);c>=Pos;c--)
  166.       Text[c+1]=Text[c];
  167.    Text[Pos]=Character;
  168. }
  169.  
  170. void DeleteChar(Text,Pos)    /*Delete a char in an item*/
  171. char *Text;
  172. int Pos;
  173. {
  174.    int lim;
  175.  
  176.    for(lim=strlen(Text);Pos<lim;Pos++)
  177.       Text[Pos]=Text[Pos+1];
  178. }
  179.  
  180. WholeScreenDown() /*Scrolls down by an entire screen*/
  181. {
  182.    struct LineItem *item;
  183.    UBYTE c,X,Y;
  184.  
  185.    item=(struct LineItem *)ScrnBtm;
  186.    if(item->NextItem == NULL)   /*No lines after screen bottom?*/
  187.       {  /*Move to last line*/
  188.       CurrentItem=(struct LineItem *)LastItem;
  189.       PlotCursor(MinX(CurrentItem),DispRows);
  190.       return(FALSE);
  191.       }
  192.    /*Get the first screen item*/
  193.    item=FirstScrnItem=(struct LineItem *)item->NextItem;
  194.    for(c=1;(c < CurY) && (item->NextItem != NULL);c++)
  195.       item=(struct LineItem *)item->NextItem; /*Get item at old Y*/
  196.  
  197.    CurrentItem=(struct LineItem *)item;  /*It is now the CurrentItem*/
  198.    Y=c;     /*In case CurY is no longer possible (23 lines down)*/
  199.    /*If CurX's old position is no longer possible*/
  200.    /*move the cursor to the nearest possible position*/
  201.    if(CurX < MinX(CurrentItem))
  202.       X=MinX(CurrentItem);
  203.    else
  204.       if(CurX > MaxX(CurrentItem))
  205.          X=MaxX(CurrentItem);
  206.       else
  207.          X=CurX;
  208.  
  209.    for(c=1;(c < SCRNHEIGHT-CurY) && (item->NextItem != NULL);c++)
  210.       item=(struct LineItem *)item->NextItem; /*Find screen bottom*/
  211.    ScrnBtm=(struct LineItem *)item;  /*Got it!*/
  212.    DispRows=c; /*Get the number of lines*/
  213.    PrintItemList(FirstScrnItem,1); /*Refresh the display*/
  214.    PlotCursor(X,Y);
  215.    return(TRUE);     /*Done!*/
  216. }
  217.  
  218. WholeScreenUp() /*Move up one screenfull of lines*/
  219. {
  220.    struct LineItem *item;
  221.    UBYTE c,X,Y;
  222.  
  223.    Y=CurY;
  224.    item=(struct LineItem *)FirstScrnItem;
  225.    if(item->PrevItem == NULL) /*At the top?*/
  226.       {  /*Move cursor to first line*/
  227.       CurrentItem=(struct LineItem *)FirstItem;
  228.       PlotCursor(MinX(CurrentItem),1);
  229.       return(FALSE);
  230.       }
  231.    for(c=0;(c<SCRNHEIGHT) & (item->PrevItem != NULL);c++)
  232.       item=(struct LineItem *)item->PrevItem;
  233.    FirstScrnItem=(struct LineItem *)item;
  234.    for(c=1;(c<CurY) && (item->NextItem != NULL);c++) /*Get new CurrentItem*/
  235.       item=(struct LineItem *)item->NextItem;
  236.    CurrentItem=(struct LineItem *)item;
  237.  
  238.    if(CurX < MinX(CurrentItem))  /*If CurX is no longer possible*/
  239.       X=MinX(CurrentItem);    /*get closest possible X*/
  240.    else
  241.       if(CurX > MaxX(CurrentItem))
  242.          X=MaxX(CurrentItem);
  243.       else
  244.          X=CurX;
  245.    for(;(c<SCRNHEIGHT) & (item->NextItem != NULL);c++)
  246.       item=(struct LineItem *)item->NextItem; /*Get ScrnBtm*/
  247.    ScrnBtm=(struct LineItem *)item;
  248.    DispRows=c;    /*Number of lines*/
  249.    PrintItemList(FirstScrnItem,1); /*Refresh display*/
  250.    PlotCursor(X,Y);
  251.    return(TRUE); /*Done!*/
  252. }
  253.  
  254. HandleTAB(Qualifier) /*The TAB key was pressed...*/
  255. USHORT Qualifier;
  256. {
  257.    UBYTE TempY;
  258.    CheckModified();
  259.    if(InvsMode < NOINV)  /*Block highlighted*/
  260.       if(Qualifier & 3) /*If SHIFT-TAB, pull out*/
  261.          if(InvsMode==BLOCK_DOWN)
  262.             PullOutBlock(StartIItem,EndIItem,InvY,TRUE);
  263.          else
  264.             PullOutBlock(EndIItem,StartIItem,EndIY,TRUE);
  265.       else  /*otherwise, push in*/
  266.          if(InvsMode==BLOCK_DOWN)
  267.             PushInBlock(StartIItem,EndIItem,InvY,TRUE);
  268.          else
  269.             PushInBlock(EndIItem,StartIItem,EndIY,TRUE);
  270.    else
  271.    {
  272.       if(CancelInvs())
  273.          return(FALSE);
  274.       TempY=CurY;
  275.       PlotCursor(1,TempY);
  276.       if(Qualifier & 3)    /*TAB out (shift pressed)*/
  277.          PullOut(CurrentItem);
  278.       else
  279.          PushIn(CurrentItem); /*TAB in*/
  280.       PlotCursor(MinX(CurrentItem),TempY);
  281.    }
  282. }
  283.  
  284. /*End of console.c*/
  285.