home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / dirs / rramdisk_451.lzh / 'Liner / Source / clip.c < prev    next >
C/C++ Source or Header  |  1991-02-06  |  7KB  |  274 lines

  1. /*Clipboard I/O routines*/
  2. #include "Globals.h"
  3.  
  4. #define WCLIP_FIRST_WRITE  0
  5. #define WCLIP_CONT_WRITE   1
  6. #define WCLIP_LAST_WRITE   2
  7.  
  8. void SetupClipboard()  /*Setup the clipboard device*/
  9. {
  10.    if(OpenDevice("clipboard.device",0L,&ClipboardIO,0L)!=0)
  11.    {
  12.       Leave(0,"Can't open clipboard!");
  13.       CloseWindow(Window);
  14.       CloseScreen(Screen);
  15.       exit(200);
  16.    }
  17.  
  18.       /*Establish the clipboard communication port*/
  19.    clipboardMsgPort.mp_Node.ln_Type=NT_MSGPORT;
  20.    clipboardMsgPort.mp_Flags=0;
  21.    clipboardMsgPort.mp_SigBit=AllocSignal(-1);
  22.    clipboardMsgPort.mp_SigTask=(struct Task *)FindTask((char *)NULL);
  23.    AddPort(&clipboardMsgPort);
  24.    ClipboardIO.io_Message.mn_ReplyPort=(struct MsgPort *)&clipboardMsgPort;
  25. }
  26.  
  27. void WriteInvsTextToClip(StartLoc,Chars,String)
  28. int StartLoc,Chars;    /*Write a highlighted text block to clipboard*/
  29. char *String;
  30. {
  31.    char WorkString[80];
  32.  
  33.    strcpy(WorkString,"");
  34.    strcpy(WorkString,&String[StartLoc]);
  35.    WorkString[Chars]=NULL;
  36.    WriteStringClip(WorkString);
  37. }
  38.  
  39. void WriteStringClip(string) /*Write a single string to the Clipboard*/
  40. char *string;
  41. {
  42.    int len;
  43.  
  44.    /*Write the data, in IFF (Formatted TeXT) form*/
  45.    WriteClip(&ClipboardIO,"FORM",4,WCLIP_FIRST_WRITE);
  46.    len=strlen(string)+12;
  47.    WriteClip(&ClipboardIO,&len,4,WCLIP_CONT_WRITE);
  48.    WriteClip(&ClipboardIO,"FTXT",4,WCLIP_CONT_WRITE);
  49.    WriteClip(&ClipboardIO,"CHRS",4,WCLIP_CONT_WRITE);
  50.    len=strlen(string);
  51.    WriteClip(&ClipboardIO,&len,4,WCLIP_CONT_WRITE);
  52.    WriteClip(&ClipboardIO,string,len,WCLIP_LAST_WRITE);
  53. }
  54.  
  55. void WriteClip(ioreq,buf,bufsiz,whichone)   /*Writes a clip to the clipboard*/
  56. register struct IOClipReq *ioreq;
  57. char *buf;
  58. int bufsiz;
  59. int whichone;
  60. {
  61.    if(whichone==WCLIP_FIRST_WRITE)  /*First time*/
  62.    {
  63.       ioreq->io_Offset=0;
  64.       ioreq->io_ClipID=0;
  65.    }
  66.    ioreq->io_Command=CMD_WRITE;
  67.    ioreq->io_Data=buf;
  68.    ioreq->io_Length=bufsiz;
  69.    DoIO(ioreq);
  70.    if(whichone==WCLIP_LAST_WRITE)  /*Last time, tell clipboard to make the*/
  71.       {                /*data available to the system*/
  72.       ioreq->io_Command=CMD_UPDATE;
  73.       DoIO(ioreq);
  74.       }
  75. }
  76.  
  77. void ShutDownClipboard()
  78. {
  79.    RemPort(&clipboardMsgPort);
  80.    CloseDevice(&ClipboardIO);
  81. }
  82.  
  83. void ReadItemString(Start,Len,string)
  84. int Start,Len;      /*Read a string from the clipboard and put it into*/
  85. char *string;      /*the specified string*/
  86. {
  87.    char WorkString[160];
  88.    char TmpString[160];
  89.    register int c;
  90.  
  91.    strcpy(TmpString,"");
  92.    strcpy(WorkString,"");
  93.  
  94.    ReadString(WorkString,159);
  95.    strcpy(TmpString,string);
  96.    TmpString[Start]=NULL;
  97.    strcat(TmpString,WorkString);
  98.    strncat(TmpString,&string[Start],159-strlen(TmpString));
  99.  
  100.    TmpString[159]=NULL;  /*Trundicate the inputted string*/
  101.  
  102.    for(c=0;c<159 && TmpString[c]!=NULL;c++)
  103.       if(TmpString[c]==0x0a)
  104.      TmpString[c]=NULL;
  105.    strcpy(string,TmpString);
  106. }
  107.  
  108. ReadString(string,len) /*Read a string from the Clipboard*/
  109. char *string;
  110. int len;
  111. {
  112.    char WorkingString[180];
  113.    int length;
  114.  
  115.    strcpy(WorkingString,"");
  116.  
  117.    length=ReadClip(&ClipboardIO,WorkingString,179);
  118.    if(!strncmp("FTXT",WorkingString,4))
  119.       return(FALSE);
  120.    strcpy(string,&WorkingString[20]);
  121.    string[20+len]=NULL;
  122.    return(TRUE);
  123.  
  124. }
  125.  
  126. ReadClip(ioreq,buf,bufsiz)
  127. register struct IOClipReq *ioreq;
  128. char *buf;     /*Read data from the Clipboard*/
  129. int bufsiz;
  130. {
  131.    int length;
  132.  
  133.    ioreq->io_Command=CMD_READ;
  134.    ioreq->io_Data=buf;
  135.    ioreq->io_Length=bufsiz-1;
  136.    ioreq->io_Offset=0;
  137.    ioreq->io_ClipID=0;
  138.  
  139.    DoIO(ioreq);
  140.  
  141.    length=ioreq->io_Actual;
  142.    *(buf+length)='\0';
  143.  
  144.    if(ioreq->io_Actual <(bufsiz-1))
  145.       {
  146.       ioreq->io_Command=CMD_READ;
  147.       ioreq->io_Length=1;
  148.       ioreq->io_Data=NULL;
  149.       DoIO(ioreq);
  150.       }
  151.    return(length);
  152. }
  153.  
  154. SnagBlock(Source,Last)  /*Copy block of lines into clip area*/
  155. struct LineItem *Source,*Last;
  156. {
  157.    struct LineItem *Dest;
  158.    struct LineItem *item;
  159.    char buffer[90];
  160.    ULONG blocklen=0;
  161.    ULONG len;
  162.  
  163.       /*This keeps a continuation from being CUT all by itself...*/
  164.    Source=(struct LineItem *)FindPrevNonCont(Source);
  165.  
  166.    item=(struct LineItem *)Source;
  167.    while(item != Last->NextItem) /*Get the length of all the data*/
  168.    {
  169.       MakeTextLine(buffer,item);
  170.       blocklen+=strlen(buffer);
  171.       item=(struct LineItem *)item->NextItem;
  172.    }
  173.  
  174.    /*Write out the IFF FORM and FTXT chunk header*/
  175.    WriteClip(&ClipboardIO,"FORM",4,WCLIP_FIRST_WRITE);
  176.    len=blocklen+12;
  177.    WriteClip(&ClipboardIO,&len,4,WCLIP_CONT_WRITE);
  178.    WriteClip(&ClipboardIO,"FTXT",4,WCLIP_CONT_WRITE);
  179.    WriteClip(&ClipboardIO,"CHRS",4,WCLIP_CONT_WRITE);
  180.    WriteClip(&ClipboardIO,&blocklen,4,WCLIP_CONT_WRITE);
  181.    MakeTextLine(buffer,Source); /*Send the first line to the clipboard*/
  182.    WriteClip(&ClipboardIO,buffer,strlen(buffer),WCLIP_CONT_WRITE);
  183.  
  184.    ClipStart=ClipEnd=Dest=(struct LineItem *)InsertItem(NULL,NULL);
  185.    if(ClipStart==NULL)
  186.    {
  187.       Leave(0,"Memory too fragmented to perform CUT/COPY!");
  188.       return(FALSE);
  189.    }
  190.  
  191.    Dest->Level=Source->Level;
  192.    strcpy(Dest->Text,Source->Text);
  193.    Source=(struct LineItem *)Source->NextItem;
  194.  
  195.    while(Source != Last->NextItem) /*Go through the list*/
  196.       {  /*and copy into the clip area*/
  197.       ClipEnd=(struct LineItem *)Dest;
  198.       Dest=(struct LineItem *)InsertItem(NULL,Dest);
  199.       if(Dest==NULL)
  200.       {
  201.      Leave(0,"Memory too fragmented to perform CUT/COPY!");
  202.      FreeListMem(ClipStart,ClipEnd);
  203.      return(FALSE);
  204.       }
  205.  
  206.       Dest->Level=Source->Level;  /*Put lines into an internal clip area*/
  207.       Dest->cont=Source->cont;
  208.       strcpy(Dest->Text,Source->Text);
  209.       MakeTextLine(buffer,Source);  /*And also sends them out to the clipboard*/
  210.       WriteClip(&ClipboardIO,buffer,strlen(buffer),WCLIP_CONT_WRITE);
  211.       Source=(struct LineItem *)Source->NextItem;
  212.       }
  213.    ClipEnd=Dest;
  214.    WriteClip(&ClipboardIO,NULL,NULL,WCLIP_LAST_WRITE); /*Finish it off*/
  215.    return(TRUE);
  216. }
  217.  
  218. void AddBlock(Start) /*Insert a SnagBlocked item list into the main list*/
  219. struct LineItem *Start; /*starting at Start*/
  220. {
  221.    struct LineItem *Item,*Dest;
  222.    int TempX,TempY;
  223.    UBYTE OrigLevel;
  224.  
  225.    Item=(struct LineItem *)ClipStart;  /*Start at the beginning*/
  226.    Dest=(struct LineItem *)InsertItem(Start->NextItem,Start);
  227.    if(Dest==NULL)
  228.    {
  229.       Leave(0,"Memory too fragmented to perform paste!");
  230.       return;
  231.    }
  232.  
  233.    OrigLevel=Item->Level;
  234.    Dest->Level=5;
  235.    Dest->ItemNumber=Item->ItemNumber;
  236.    strcpy(Dest->Text,Item->Text);
  237.  
  238.    for(;Dest->Level > OrigLevel;Dest->Level--) /*Adjust the ItemNumbers*/
  239.    {
  240.       AddItem(Dest);
  241.       RemItem(Dest);
  242.    }
  243.  
  244.    AddItem(Dest);
  245.    AddItem(Dest->NextItem);
  246.  
  247.    Item=(struct LineItem *)ClipStart->NextItem;
  248.    while(Item != NULL)  /*Copy the lines to the main outline*/
  249.    {
  250.       Dest=(struct LineItem *)InsertItem(Dest->NextItem,Dest);
  251.       if(Dest==NULL)
  252.       {
  253.      Leave(0,"Memory too fragmented to continue with paste!");
  254.      PrintItemList(Start,1);
  255.      return;
  256.       }
  257.  
  258.       Dest->Level=Item->Level;
  259.       Dest->ItemNumber=Item->ItemNumber;
  260.       Dest->cont=Item->cont;
  261.       strcpy(Dest->Text,Item->Text);
  262.       AddItem(Dest);
  263.       Item=(struct LineItem *)Item->NextItem;
  264.    }
  265.    TempY=CurY;
  266.    TempX=CurX;
  267.    PrintItemList(CurrentItem,CurY); /*Redraw the screen*/
  268.    PlotCursor(TempX,TempY);
  269.    if(LastItem==Start)
  270.       LastItem=(struct LineItem *)Dest;
  271. }
  272.  
  273. /*~~~End of clip.c*/
  274.