home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / disks / disk373.lzh / Multiplot / source / mplot_src / src.zoo / clipboard.c < prev    next >
C/C++ Source or Header  |  1990-08-02  |  15KB  |  569 lines

  1. #include <graphics/display.h>
  2. #include <libraries/dosextens.h>
  3. #include <libraries/diskfont.h>
  4. #include <exec/exec.h>
  5. #include <exec/types.h>
  6. #include <exec/ports.h>
  7. #include <exec/io.h>
  8. #include <intuition/intuitionbase.h>
  9. #include <graphics/regions.h>
  10. #include <devices/clipboard.h>
  11. #include <stdio.h>
  12. #include <workbench/startup.h>
  13. #include <workbench/workbench.h>
  14. #include <workbench/icon.h>
  15. #include <graphics/gfxmacros.h>
  16. #include <graphics/gfxbase.h>
  17. #include <math.h>
  18. #include <proto/all.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "struct.h"
  22. #include "mp.h"
  23. #include "plotlim.h"
  24.  
  25. struct IOClipReq *clipboardIO =0;
  26. struct MsgPort *clipboardMsgPort =0;
  27. struct MsgPort *satisfyMsgPort =0;
  28.  
  29. long CBOpen(long);
  30. void CBClose(void);
  31. void writeLong(long *);
  32. void CBCutS(UBYTE *);
  33. long CBPasteS(UBYTE *);
  34. long CBPost (void);
  35. long CBCurrentReadID(void);
  36. long CBCurrentWriteID(void);
  37. BOOL CBCheckSatisfy(long *);
  38. void CBSatisfyPost(UBYTE *);
  39. void CBCut(UBYTE *,long);
  40.  
  41.  
  42. extern int MAXVERT;
  43. extern int MAXHORIZ;
  44.  
  45. #define BUFLNLNGTH 60
  46. #define TXTBLCKLNGTH 85
  47. #define BUFEXTRAS 150
  48.  
  49. char *ClipBuffer;
  50.  
  51. ClipCopy(Pict,Seln)
  52. struct Pict *Pict;
  53. struct Selection *Seln;
  54. {
  55. struct Selection *TempSeln;
  56. struct TextBox *TempText;
  57. struct Plot *Plot, *TempPlot;
  58. short i;
  59. long  tempval, plotno;
  60. FFP *xarray, *yarray, *earray;
  61. char *TempClip;
  62. UBYTE c;
  63. ULONG postID;
  64.  
  65.   c=0;
  66.   postID=0;
  67.  
  68.   if (!Seln) {
  69.       Message(" Nothing selected to copy");
  70.       return(FALSE);
  71.    }
  72.  
  73.   if (CBOpen(PRIMARY_CLIP)) {
  74.      Message(" Unable to open clipboard");
  75.      return(FALSE);
  76.   }
  77.  
  78.   if (Seln->SelectedPlot) {
  79.     plotno=tempval=0;
  80.  
  81.     for (TempSeln=Seln;(TempSeln&&TempSeln->SelectedPlot);TempSeln=TempSeln->NextSelection)
  82.       {
  83.           plotno++;
  84.           tempval+=TempSeln->SelectedPlot->NPts;
  85.           for (TempPlot=TempSeln->SelectedPlot;TempPlot->Continued;TempPlot=TempPlot->NextPlot,tempval+=TempPlot->NPts);
  86.       }
  87.     ClipBuffer = (char *) AllocMem(sizeof(char)*(tempval*BUFLNLNGTH+plotno*BUFEXTRAS),MEMF_CLEAR);
  88.     if (!ClipBuffer) {
  89.         Message(" Insufficient memory for copy");
  90.         CBClose();
  91.         return(FALSE);
  92.     }
  93.  
  94.     TempClip=ClipBuffer;
  95.     for (TempSeln=Seln;(TempSeln&&TempSeln->SelectedPlot);TempSeln=TempSeln->NextSelection)
  96.       {
  97.          Plot=TempSeln->SelectedPlot;
  98.          TempClip+=sprintf(TempClip,"*LEGEND* %s\n",Plot->Legend->String);
  99.          TempClip+=sprintf(TempClip,"*AUTOSCRIPT* %d %d %d %d %d %d %d %d\n",
  100.          Plot->Color,Plot->PlotType,Plot->Lines,Plot->PointSize,Plot->PointType,Plot->Enabled,MAXHORIZ-Plot->Legend->x,Plot->Legend->y);
  101.          do {
  102.             xarray=Plot->x; yarray=Plot->y; earray=Plot->e;
  103.             for (i=0;i<Plot->NPts;i++,xarray++,yarray++,earray++)
  104.                {
  105.                   if (Pict->ErrBar) {TempClip+=sprintf(TempClip,"%.5g  %.5g  %.5g\n",*xarray,*yarray,*earray);}
  106.                   else {TempClip+=sprintf(TempClip,"%.5g  %.5g\n",*xarray,*yarray);}
  107.                }
  108.          } while ((Plot->Continued)&&(Plot=Plot->NextPlot));
  109.          TempClip+=sprintf(TempClip,"\n\n");
  110.       }
  111.     CBCutS(ClipBuffer);
  112.     CBClose();
  113.     if (ClipBuffer) FreeMem(ClipBuffer,sizeof(char)*(tempval*BUFLNLNGTH+plotno*BUFEXTRAS));
  114.   }
  115.   else {
  116.     for (TempSeln=Seln,tempval=0;(TempSeln&&TempSeln->SelectedBox);tempval++,TempSeln=TempSeln->NextSelection) ;
  117.  
  118.     ClipBuffer = (char *) AllocMem(sizeof(char)*(tempval*TXTBLCKLNGTH+BUFEXTRAS),MEMF_CLEAR);
  119.     if (!ClipBuffer) {
  120.         Message(" Insufficient memory for copy");
  121.         CBClose();
  122.         return(FALSE);
  123.     }
  124.  
  125.     TempClip=ClipBuffer;
  126.     TempClip+=sprintf(TempClip,"*EXTRATEXT*\n");
  127.     for (TempSeln=Seln;(TempSeln&&TempSeln->SelectedBox);TempSeln=TempSeln->NextSelection)
  128.       {
  129.           TempText=TempSeln->SelectedBox;
  130.           TempClip+=sprintf(TempClip,"%d %d %s\n",MAXHORIZ-TempText->x,MAXVERT-TempText->y,TempText->String);
  131.       }
  132.     CBCutS(ClipBuffer);
  133.     CBClose();
  134.     if (ClipBuffer) FreeMem(ClipBuffer,sizeof(char)*(tempval*TXTBLCKLNGTH+BUFEXTRAS));
  135.   }
  136.   return(TRUE);
  137. }
  138.  
  139.  
  140. ClipPaste(Pict)
  141. struct Pict *Pict;
  142. {
  143. long tempval=0;
  144. UBYTE c;
  145. ULONG postID;
  146.  
  147.   c=0;
  148.   postID=0;
  149.   if (CBOpen(PRIMARY_CLIP)) {
  150.      Message(" Unable to open clipboard");
  151.      return(FALSE);
  152.   }
  153.  if (!(tempval= CBPasteQueryLength())) {
  154.       Message("   Nothing in clipboard ");
  155.       CBClose();
  156.       return(FALSE);
  157.   }
  158.   ClipBuffer = (char *) AllocMem(sizeof(char)*tempval,MEMF_CLEAR);
  159.   if (!ClipBuffer) {
  160.       Message(" Insufficient memory for paste");
  161.       CBClose();
  162.       return(FALSE);
  163.   }
  164.   CBPasteS(ClipBuffer);
  165.   GetStringDat(ClipBuffer, Pict);
  166.   CBClose();
  167.   if (ClipBuffer) FreeMem(ClipBuffer,sizeof(char)*tempval);
  168.   return(TRUE);
  169. }
  170.  
  171.  
  172.  
  173. #define xcol 0
  174. #define ycol 1
  175. #define ecol 2
  176. #define COLUMN_OK(z) (z<0 ? TRUE : numeric(wp[z]))
  177. #define LINE_IS_DATA (COLUMN_OK(xcol) && COLUMN_OK(ycol))
  178. #define PLOT_OPENED (!PlotClosed && Plot->NPts)
  179.  
  180. extern short PlotClosed, Continued;
  181. extern short xcoord;
  182.  
  183. char line[100];
  184. FFP *x, *y, *e;
  185.  
  186. GetStringDat(String, Pict)
  187. char *String;
  188. struct Pict *Pict;
  189. {
  190. short i, maxcol=3;
  191. char *wp[3], *cbuf, *getwrd(), *GetLine();
  192. struct Plot *Plot=NULL, *PrevPlot, *GetStructPlot(), *OpenPlot();
  193.  
  194.    PlotClosed=TRUE;
  195.  
  196.  
  197.    while ((*String != '\0')&&(String=GetLine(line,String,100))) {
  198.       cbuf=line;
  199.       if (StringKeyWord(Pict,&String,&Plot)) ;
  200.       else
  201.         {
  202.  
  203.            for (i=0; i<=maxcol; i++) wp[i] = getwrd(&cbuf);
  204.            if (LINE_IS_DATA) {
  205.               if (PlotClosed) {
  206.                  Plot=OpenPlot(Pict);
  207.                  if (!Plot) {Message(" Insufficient memory for paste"); return(FALSE);}
  208.               }
  209.               else if (Plot->NPts == MAXPOINTS) {
  210.                  Continued = TRUE; PrevPlot = Plot;
  211.                  if (!(Plot->NextPlot = GetStructPlot())) {
  212.                      Message(" Insufficient memory for paste");
  213.                      if (PLOT_OPENED) { Pict->NPlt++; PlotClosed = TRUE; }
  214.                      return(FALSE);
  215.                  }
  216.                  else {
  217.                      Plot = Plot->NextPlot;
  218.                      Plot->Enabled = TRUE;
  219.                      Plot->Color = PrevPlot->Color;
  220.                      Plot->PointType = PrevPlot->PointType;
  221.                      Plot->PlotType= PrevPlot->PlotType;
  222.                      Plot->PointSize = PrevPlot->PointSize;
  223.                      Plot->Lines=PrevPlot->Lines;
  224.                      x = Plot->x; y = Plot->y; e = Plot->e;
  225.                  }
  226.               }
  227.               else if (Continued) {PrevPlot->Continued = TRUE; Continued = FALSE;}
  228.  
  229.               *(x++) = ( xcol>=0 ? atoFFP(wp[xcol]) : (FFP)(xcoord++));
  230.               *(y++) = atoFFP(wp[ycol]);
  231.               *(e++) = ( ecol>=0 ? atoFFP(wp[ecol]) : (FFP)(0.0));
  232.               Plot->NPts++;
  233.            }
  234.            else  {        /* Line is not data */
  235.               if (PLOT_OPENED) {
  236.                  Pict->NPlt++;
  237.                  PlotClosed = TRUE;
  238.               }
  239.            }       /* End of line is not data */
  240.         }      /* End of not a legend */
  241.    }        /* End of while loop */
  242.    if (PLOT_OPENED) Pict->NPlt++;
  243. }
  244.  
  245.  
  246. char *GetLine(cb,String,max)
  247. char *String, *cb;
  248. int max;
  249. {
  250. int i=0;
  251.  
  252.   while ((String[i]!='\n')&&(String[i]!='\0')&&(i<max-1))
  253.     {
  254.        cb[i]=String[i];
  255.        i++;
  256.     }
  257.    cb[i]='\0';
  258.    trch('\t',' ',cb); trch(',',' ',cb); trch('\r',' ',cb);
  259.    if (String[i]=='\n') return(String+i+1);
  260.    else return(String+i);
  261. }
  262.  
  263.  
  264.  
  265. extern struct TextBox *ExtraText;
  266. extern struct Remember *Key;
  267.  
  268. /******************/
  269. int StringKeyWord(Pict,p_String,p_Plot)
  270. char **p_String;
  271. struct Pict *Pict;
  272. struct Plot **p_Plot;
  273. {
  274. short tempLegX, tempLegY;
  275. char *GetLine(), *cbuf, *String;
  276. struct TextBox *TempText;
  277. struct Plot *Plot;
  278.  
  279.  
  280.    Plot=*p_Plot; String=*p_String;
  281.    cbuf=line;
  282.  
  283.    if (same(cbuf,"*",1)) {
  284.       if (same(cbuf,"*AUTOSCRIPT*",12))
  285.         {
  286.           if (PlotClosed) {
  287.               Plot=OpenPlot(Pict);
  288.               if (!Plot) {Message(" Insufficient memory for paste"); return(FALSE);}
  289.           }
  290.           getwrd(&cbuf);
  291.           sscanf(cbuf,"%hd %hd %hd %hd %hd %hd %hd %hd",
  292.                   &(Plot->Color),&(Plot->PlotType),&(Plot->Lines),&(Plot->PointSize),&(Plot->PointType),&(Plot->Enabled),&tempLegX,&(Plot->Legend->y));
  293.           Plot->Legend->x=MAXHORIZ-tempLegX;
  294.           *p_Plot=Plot; return(TRUE);
  295.         }
  296.       else if (same(cbuf,"*LEGEND*",8))
  297.         {
  298.            if (PlotClosed) {
  299.               Plot=OpenPlot(Pict);
  300.               if (!Plot) {Message(" Insufficient memory for paste"); return(FALSE);}
  301.            }
  302.            cbuf+=8; while(*cbuf==' ') cbuf++; stccpy(Plot->Legend->String,cbuf,79);
  303.            *p_Plot=Plot; return(TRUE);
  304.         }
  305.       else if (same(cbuf,"*EXTRATEXT*",11)) {
  306.         while ((*String != '\0')&&(String=GetLine(line,String,100))) {
  307.            cbuf=line;
  308.            if (ExtraText)
  309.              {
  310.                  for (TempText=ExtraText;TempText->NextText;TempText=TempText->NextText);
  311.                  if (TempText->NextText=(struct TextBox *) AllocRemember(&Key,(sizeof(struct TextBox)),MEMF_CLEAR))
  312.                    {
  313.                       TempText=TempText->NextText;
  314.                       if (sscanf(cbuf,"%hd %hd",&tempLegX,&tempLegY));
  315.                         {
  316.                            TempText->x=MAXHORIZ-tempLegX;
  317.                            TempText->y=MAXVERT-tempLegY;
  318.  
  319.                            getwrd(&cbuf); getwrd(&cbuf); while(*cbuf==' ') cbuf++;
  320.                         }
  321.                       stccpy(TempText->String,cbuf,79);
  322.                       TempText->NextText=NULL;
  323.                    }
  324.                  else Message("  Not enough memory for text");
  325.               }
  326.             else
  327.               {
  328.                  if (ExtraText=(struct TextBox *) AllocRemember(&Key,(sizeof(struct TextBox)),MEMF_CLEAR))
  329.                    {
  330.                       if (sscanf(cbuf,"%hd %hd",&tempLegX,&tempLegY))
  331.                         {
  332.                            ExtraText->x=MAXHORIZ-tempLegX;
  333.                            ExtraText->y=MAXVERT-tempLegY;
  334.                            getwrd(&cbuf); getwrd(&cbuf); while (*cbuf==' ') cbuf++;
  335.                         }
  336.                       stccpy(ExtraText->String,cbuf,79);
  337.                       ExtraText->NextText=NULL;
  338.                    }
  339.                  else Message("  Not enough memory for text");
  340.                }
  341.         }
  342.         *p_String=String; return(TRUE);
  343.       }
  344.    }
  345.    return (FALSE);
  346. }
  347.  
  348. /*** PLOT TYPES ***/
  349. #define BOTHPLOT 2
  350.  
  351. extern struct NewScreen newscreen;
  352. struct Plot *OpenPlot(Pict)
  353. struct Pict *Pict;
  354. {
  355. struct Plot *Plot;
  356. static int i;
  357.  
  358.   if (newscreen.Depth==1) i=1;
  359.   if (Pict->Plot)  {
  360.      for (Plot=Pict->Plot;Plot->NextPlot;Plot=Plot->NextPlot) ;
  361.      if (! (Plot->NextPlot = GetStructPlot()))  return(NULL);
  362.      else Plot = Plot->NextPlot;
  363.    }
  364.    else {
  365.      if (!(Pict->Plot = GetStructPlot())) return(NULL);
  366.      else Plot = Pict->Plot;
  367.    }
  368.    x = Plot->x; y = Plot->y; e = Plot->e;
  369.    Plot->Enabled = TRUE;
  370.    Plot->Color = PLOTCOLORBASE + i;
  371.    Plot->PointType = 6;
  372.    Plot->PlotType=BOTHPLOT;
  373.    Plot->PointSize = DEFAULT_POINT_SIZE;
  374.    Plot->Lines=TRUE;
  375.    PlotClosed = FALSE; xcoord=0;
  376.    switch(newscreen.Depth) {
  377.      case 1:  break;
  378.      case 2:  if ((i==1)||(i==9)) i++;
  379.      case 3:  if (i==5) i++;
  380.      default: i++; if (i>11) i=0; break;
  381.      }
  382.    return(Plot);
  383. }
  384.  
  385.  
  386.  
  387.  
  388. long CBPasteQueryLength()
  389. {
  390. long length,slen;
  391. long len[5];
  392.  
  393.   clipboardIO->io_Command=CMD_READ;
  394.   clipboardIO->io_ClipID=0;
  395.   clipboardIO->io_Offset=0;
  396.   clipboardIO->io_Data=(char *)len;
  397.   clipboardIO->io_Length=20;
  398.   DoIO(clipboardIO);
  399.  
  400.   length=len[1];
  401.   if (clipboardIO->io_Error) slen=0;    /* Nothing in clipboard */
  402.   else slen=len[4];
  403.  
  404.   clipboardIO->io_Offset+=length;
  405.   clipboardIO->io_Length=1;
  406.   clipboardIO->io_Data=0;
  407.   DoIO(clipboardIO);
  408.  
  409.   return(slen);
  410. }
  411.  
  412.  
  413. /******** COMMODORE STANDARD CLIPBOARD INTERFACE FUNCTIONS **********/
  414.  
  415. long CBOpen(unit)
  416. long unit;
  417. {
  418. long error;
  419.   clipboardMsgPort=CreatePort(0,0);
  420.   satisfyMsgPort=CreatePort(0,0);
  421.  
  422.   clipboardIO=(struct IOClipReq *) CreateExtIO(clipboardMsgPort,sizeof(struct IOClipReq));
  423.  
  424.   if ((error=OpenDevice("clipboard.device",unit,clipboardIO,0)))
  425.     return(error);
  426.     return(0);
  427. }
  428.  
  429. void CBClose()
  430. {
  431.   CloseDevice(clipboardIO);
  432.   DeletePort(satisfyMsgPort);
  433.   DeletePort(clipboardMsgPort);
  434.   DeleteExtIO(clipboardIO);
  435. }
  436.  
  437. void CBCut(stream,length)
  438. UBYTE *stream;
  439. long length;
  440. {
  441.   clipboardIO->io_Command=CMD_WRITE;
  442.   clipboardIO->io_Data=stream;
  443.   clipboardIO->io_Length=length;
  444.   clipboardIO->io_Offset=0;
  445.   clipboardIO->io_ClipID=0;
  446.   DoIO(clipboardIO);
  447.   clipboardIO->io_Command=CMD_UPDATE;
  448.   DoIO(clipboardIO);
  449. }
  450.  
  451. void CBCutS(string)
  452. UBYTE *string;
  453. {
  454.   clipboardIO->io_ClipID=0;
  455.   CBSatisfyPost(string);
  456. }
  457.  
  458. void writeLong(ldata)
  459. long *ldata;
  460. {
  461.   clipboardIO->io_Command=CMD_WRITE;
  462.   clipboardIO->io_Data=(char *)ldata;
  463.   clipboardIO->io_Length=4;
  464.   DoIO(clipboardIO);
  465. }
  466.  
  467. void CBSatisfyPost(string)
  468. UBYTE *string;
  469. {
  470. long length,slen=strlen(string);
  471. BOOL odd=(slen & 1);
  472.  
  473.   length= (odd) ? slen+1 : slen;
  474.   clipboardIO->io_Offset=0;
  475.   writeLong((long *)"FORM");
  476.   length +=12;
  477.   writeLong(&length);
  478.   writeLong((long *)"FTXT");
  479.   writeLong((long *)"CHRS");
  480.   writeLong(&slen);
  481.   clipboardIO->io_Command=CMD_WRITE;
  482.   clipboardIO->io_Data=(char *)string;
  483.   clipboardIO->io_Length=slen;
  484.   DoIO(clipboardIO);
  485.  
  486.   if (odd)
  487.     {
  488.        clipboardIO->io_Command=CMD_WRITE;
  489.        clipboardIO->io_Data="";
  490.        clipboardIO->io_Length=1;
  491.        DoIO(clipboardIO);
  492.     }
  493.   clipboardIO->io_Command=CMD_UPDATE;
  494.   DoIO(clipboardIO);
  495. }
  496.  
  497.  
  498. long CBPasteS(string)
  499. UBYTE *string;
  500. {
  501. long length,slen;
  502. long len[5];
  503.  
  504.   clipboardIO->io_Command=CMD_READ;
  505.   clipboardIO->io_ClipID=0;
  506.   clipboardIO->io_Offset=0;
  507.   clipboardIO->io_Data=(char *)len;
  508.   clipboardIO->io_Length=20;
  509.   DoIO(clipboardIO);
  510.  
  511.   length=len[1];
  512.   slen=len[4];
  513.   clipboardIO->io_Data=(char *)string;
  514.   clipboardIO->io_Length=slen;
  515.   DoIO(clipboardIO);
  516.  
  517.   clipboardIO->io_Offset+=length;
  518.   clipboardIO->io_Length=1;
  519.   clipboardIO->io_Data=0;
  520.   DoIO(clipboardIO);
  521.  
  522.   string[slen]='\0';
  523.   return(slen);
  524. }
  525.  
  526. long CBPost()
  527. {
  528.   clipboardIO->io_Command=CBD_POST;
  529.   clipboardIO->io_Data=(char*)satisfyMsgPort;;
  530.   clipboardIO->io_ClipID=0;
  531.   DoIO(clipboardIO);
  532.   return(clipboardIO->io_ClipID);
  533. }
  534.  
  535. long CBCurrentReadID()
  536. {
  537.   clipboardIO->io_Command=CBD_CURRENTREADID;
  538.   DoIO(clipboardIO);
  539.   return(clipboardIO->io_ClipID);
  540. }
  541.  
  542. long CBCurrentWriteID()
  543. {
  544.   clipboardIO->io_Command=CBD_CURRENTWRITEID;
  545.   DoIO(clipboardIO);
  546.   return(clipboardIO->io_ClipID);
  547. }
  548.  
  549. BOOL CBCheckSatisfy(idVar)
  550. long *idVar;
  551. {
  552. struct SatisfyMsg *sm;
  553.  
  554.   if (*idVar==0) return(TRUE);
  555.   if (*idVar < CBCurrentWriteID())
  556.     {
  557.       *idVar=0;
  558.       return(TRUE);
  559.     }
  560.   if (sm=(struct SatisfyMsg *)GetMsg(satisfyMsgPort))
  561.     {
  562.       if (*idVar==sm->sm_ClipID) return(TRUE);
  563.     }
  564.   return(FALSE);
  565. }
  566.  
  567.  
  568.  
  569.