home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / dirs / rramdisk_451.lzh / 'Liner / Source / Console.c < prev    next >
C/C++ Source or Header  |  1991-02-06  |  7KB  |  282 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.  
  60.    Buffer[0]=CSI;
  61.    len+=stci_d(&Buffer[1],y);
  62.    Buffer[len]=0x3b;
  63.    len+=stci_d(&Buffer[len+1],x);
  64.    Buffer[len+1]=0x48;
  65.    Buffer[len+2]=NULL;
  66.    WriteConsole(Buffer,-1);
  67.    CurX=x;
  68.    CurY=y;
  69. }
  70.  
  71. void CursorRight(Qualifier) /*Move the cursor one to the right*/
  72. USHORT Qualifier;
  73. {
  74.    UBYTE TempX;
  75.    int min;
  76.  
  77.    min=MinX(CurrentItem);
  78.    if(ErrorInTitle)
  79.       TitleErrorCancel();
  80.    if(InvsMode < NOINV)
  81.       CancelInvs();
  82.    if(CurX >= MaxX(CurrentItem)) /*If at the end of the line*/
  83.    {            /*move to the start of the next one*/
  84.       TempX=CurX;
  85.       CurX=1;
  86.       if(!CursorDown())
  87.      CurX=TempX;
  88.    }
  89.    else
  90.       if(Qualifier & 3) /*Handle SHIFT-cursor-right*/
  91.      PlotCursor(CurX+FindNextWord(&CurrentItem->Text[CurX-min]),
  92.            CurY);
  93.       else
  94.      if(Qualifier & 8) /*Handle CTRL-cursor-right*/
  95.         PlotCursor(MaxX(CurrentItem),CurY); /*(end of line)*/
  96.      else
  97.         PlotCursor(CurX+1,CurY);
  98. }
  99.  
  100. void CursorLeft(Qualifier) /*Move the cursor one to the left*/
  101. USHORT Qualifier;
  102. {
  103.    UBYTE TempX;
  104.  
  105.    if(ErrorInTitle)
  106.       TitleErrorCancel();
  107.    if(InvsMode < NOINV)
  108.       CancelInvs();
  109.    if(CurX <= MinX(CurrentItem)) /*If at the beginning of the line*/
  110.    {             /*move to the end of the previous one*/
  111.       TempX=CurX;
  112.       CurX=100;
  113.       if(!CursorUp())
  114.      CurX=TempX;
  115.    }
  116.    else
  117.       if(Qualifier & 3) /*Handle SHIFT-cursor-left*/
  118.      PlotCursor(MinX(CurrentItem)+FindPrevWord(
  119.            CurrentItem->Text,CurX-MinX(CurrentItem)),CurY);
  120.       else
  121.      if(Qualifier & 8) /*Handle CTRL-cursor-left*/
  122.         PlotCursor(MinX(CurrentItem),CurY);
  123.      else
  124.         PlotCursor(CurX-1,CurY);
  125. }
  126.  
  127. FindNextWord(string)
  128. char *string;
  129. {
  130.    int end;
  131.    for(end=0;string[end] != ' ' && string[end] != NULL;end++);
  132.    if(end==strlen(string))
  133.       return(end);
  134.  
  135.    for(end++;string[end] ==' ' && string[end] != NULL;end++);
  136.    return(end);
  137. }
  138.  
  139. FindPrevWord(string,start)
  140. char *string;
  141. int start;
  142. {
  143.    int end;
  144.  
  145.    end=start;
  146.    if(string[end]!=' ' && end > 0 && string[end-1]==' ')
  147.       end--;
  148.  
  149.    for(;string[end-1] != ' ' && end > 0;end--);
  150.  
  151.    while(end > 0 && string[end]== ' ')
  152.       for(end--;string[end-1] != ' ' && end > 0;end--);
  153.    return(end);
  154. }
  155.  
  156. void InsertChar(Text,Character,Pos)   /*Insert a char into an item*/
  157. char *Text,Character;
  158. int Pos;
  159. {
  160.    int c;
  161.  
  162.    for(c=strlen(Text);c>=Pos;c--)
  163.       Text[c+1]=Text[c];
  164.    Text[Pos]=Character;
  165. }
  166.  
  167. void DeleteChar(Text,Pos)    /*Delete a char in an item*/
  168. char *Text;
  169. int Pos;
  170. {
  171.    int lim;
  172.  
  173.    for(lim=strlen(Text);Pos<lim;Pos++)
  174.       Text[Pos]=Text[Pos+1];
  175. }
  176.  
  177. WholeScreenDown() /*Scrolls down by an entire screen*/
  178. {
  179.    struct LineItem *item;
  180.    UBYTE c,X,Y;
  181.  
  182.    item=(struct LineItem *)ScrnBtm;
  183.    if(item->NextItem == NULL)   /*No lines after screen bottom?*/
  184.       {  /*Move to last line*/
  185.       CurrentItem=(struct LineItem *)LastItem;
  186.       PlotCursor(MinX(CurrentItem),DispRows);
  187.       return(FALSE);
  188.       }
  189.    /*Get the first screen item*/
  190.    item=FirstScrnItem=(struct LineItem *)item->NextItem;
  191.    for(c=1;(c < CurY) && (item->NextItem != NULL);c++)
  192.       item=(struct LineItem *)item->NextItem; /*Get item at old Y*/
  193.  
  194.    CurrentItem=(struct LineItem *)item;  /*It is now the CurrentItem*/
  195.    Y=c;     /*In case CurY is no longer possible (23 lines down)*/
  196.    /*If CurX's old position is no longer possible*/
  197.    /*move the cursor to the nearest possible position*/
  198.    if(CurX < MinX(CurrentItem))
  199.       X=MinX(CurrentItem);
  200.    else
  201.       if(CurX > MaxX(CurrentItem))
  202.      X=MaxX(CurrentItem);
  203.       else
  204.      X=CurX;
  205.  
  206.    for(c=1;(c < SCRNHEIGHT-CurY) && (item->NextItem != NULL);c++)
  207.       item=(struct LineItem *)item->NextItem; /*Find screen bottom*/
  208.    ScrnBtm=(struct LineItem *)item;  /*Got it!*/
  209.    DispRows=c; /*Get the number of lines*/
  210.    PrintItemList(FirstScrnItem,1); /*Refresh the display*/
  211.    PlotCursor(X,Y);
  212.    return(TRUE);     /*Done!*/
  213. }
  214.  
  215. WholeScreenUp() /*Move up one screenfull of lines*/
  216. {
  217.    struct LineItem *item;
  218.    UBYTE c,X,Y;
  219.  
  220.    Y=CurY;
  221.    item=(struct LineItem *)FirstScrnItem;
  222.    if(item->PrevItem == NULL) /*At the top?*/
  223.       {  /*Move cursor to first line*/
  224.       CurrentItem=(struct LineItem *)FirstItem;
  225.       PlotCursor(MinX(CurrentItem),1);
  226.       return(FALSE);
  227.       }
  228.    for(c=0;(c<SCRNHEIGHT) & (item->PrevItem != NULL);c++)
  229.       item=(struct LineItem *)item->PrevItem;
  230.    FirstScrnItem=(struct LineItem *)item;
  231.    for(c=1;(c<CurY) && (item->NextItem != NULL);c++) /*Get new CurrentItem*/
  232.       item=(struct LineItem *)item->NextItem;
  233.    CurrentItem=(struct LineItem *)item;
  234.  
  235.    if(CurX < MinX(CurrentItem))  /*If CurX is no longer possible*/
  236.       X=MinX(CurrentItem);    /*get closest possible X*/
  237.    else
  238.       if(CurX > MaxX(CurrentItem))
  239.      X=MaxX(CurrentItem);
  240.       else
  241.      X=CurX;
  242.    for(;(c<SCRNHEIGHT) & (item->NextItem != NULL);c++)
  243.       item=(struct LineItem *)item->NextItem; /*Get ScrnBtm*/
  244.    ScrnBtm=(struct LineItem *)item;
  245.    DispRows=c;      /*Number of lines*/
  246.    PrintItemList(FirstScrnItem,1); /*Refresh display*/
  247.    PlotCursor(X,Y);
  248.    return(TRUE); /*Done!*/
  249. }
  250.  
  251. HandleTAB(Qualifier) /*The TAB key was pressed...*/
  252. USHORT Qualifier;
  253. {
  254.    UBYTE TempY;
  255.    CheckModified();
  256.    if(InvsMode < NOINV)  /*Block highlighted*/
  257.       if(Qualifier & 3) /*If SHIFT-TAB, pull out*/
  258.      if(InvsMode==BLOCK_DOWN)
  259.         PullOutBlock(StartIItem,EndIItem,InvY,TRUE);
  260.      else
  261.         PullOutBlock(EndIItem,StartIItem,EndIY,TRUE);
  262.       else  /*otherwise, push in*/
  263.      if(InvsMode==BLOCK_DOWN)
  264.         PushInBlock(StartIItem,EndIItem,InvY,TRUE);
  265.      else
  266.         PushInBlock(EndIItem,StartIItem,EndIY,TRUE);
  267.    else
  268.    {
  269.       if(CancelInvs())
  270.      return(FALSE);
  271.       TempY=CurY;
  272.       PlotCursor(1,TempY);
  273.       if(Qualifier & 3)    /*TAB out (shift pressed)*/
  274.      PullOut(CurrentItem);
  275.       else
  276.      PushIn(CurrentItem); /*TAB in*/
  277.       PlotCursor(MinX(CurrentItem),TempY);
  278.    }
  279. }
  280.  
  281. /*~~~End of console.c*/
  282.