home *** CD-ROM | disk | FTP | other *** search
- #include <graphics/display.h>
- #include <libraries/dosextens.h>
- #include <libraries/diskfont.h>
- #include <exec/exec.h>
- #include <exec/types.h>
- #include <exec/ports.h>
- #include <exec/io.h>
- #include <intuition/intuitionbase.h>
- #include <graphics/regions.h>
- #include <devices/clipboard.h>
- #include <stdio.h>
- #include <workbench/startup.h>
- #include <workbench/workbench.h>
- #include <workbench/icon.h>
- #include <graphics/gfxmacros.h>
- #include <graphics/gfxbase.h>
- #include <math.h>
- #include <proto/all.h>
- #include <stdlib.h>
- #include <string.h>
- #include "struct.h"
- #include "mp.h"
- #include "plotlim.h"
-
- struct IOClipReq *clipboardIO =0;
- struct MsgPort *clipboardMsgPort =0;
- struct MsgPort *satisfyMsgPort =0;
-
- long CBOpen(long);
- void CBClose(void);
- void writeLong(long *);
- void CBCutS(UBYTE *);
- long CBPasteS(UBYTE *);
- long CBPost (void);
- long CBCurrentReadID(void);
- long CBCurrentWriteID(void);
- BOOL CBCheckSatisfy(long *);
- void CBSatisfyPost(UBYTE *);
- void CBCut(UBYTE *,long);
-
-
- extern int MAXVERT;
- extern int MAXHORIZ;
-
- #define BUFLNLNGTH 60
- #define TXTBLCKLNGTH 85
- #define BUFEXTRAS 150
-
- char *ClipBuffer;
-
- ClipCopy(Pict,Seln)
- struct Pict *Pict;
- struct Selection *Seln;
- {
- struct Selection *TempSeln;
- struct TextBox *TempText;
- struct Plot *Plot, *TempPlot;
- short i;
- long tempval, plotno;
- FFP *xarray, *yarray, *earray;
- char *TempClip;
- UBYTE c;
- ULONG postID;
-
- c=0;
- postID=0;
-
- if (!Seln) {
- Message(" Nothing selected to copy");
- return(FALSE);
- }
-
- if (CBOpen(PRIMARY_CLIP)) {
- Message(" Unable to open clipboard");
- return(FALSE);
- }
-
- if (Seln->SelectedPlot) {
- plotno=tempval=0;
-
- for (TempSeln=Seln;(TempSeln&&TempSeln->SelectedPlot);TempSeln=TempSeln->NextSelection)
- {
- plotno++;
- tempval+=TempSeln->SelectedPlot->NPts;
- for (TempPlot=TempSeln->SelectedPlot;TempPlot->Continued;TempPlot=TempPlot->NextPlot,tempval+=TempPlot->NPts);
- }
- ClipBuffer = (char *) AllocMem(sizeof(char)*(tempval*BUFLNLNGTH+plotno*BUFEXTRAS),MEMF_CLEAR);
- if (!ClipBuffer) {
- Message(" Insufficient memory for copy");
- CBClose();
- return(FALSE);
- }
-
- TempClip=ClipBuffer;
- for (TempSeln=Seln;(TempSeln&&TempSeln->SelectedPlot);TempSeln=TempSeln->NextSelection)
- {
- Plot=TempSeln->SelectedPlot;
- TempClip+=sprintf(TempClip,"*LEGEND* %s\n",Plot->Legend->String);
- TempClip+=sprintf(TempClip,"*AUTOSCRIPT* %d %d %d %d %d %d %d %d\n",
- Plot->Color,Plot->PlotType,Plot->Lines,Plot->PointSize,Plot->PointType,Plot->Enabled,MAXHORIZ-Plot->Legend->x,Plot->Legend->y);
- do {
- xarray=Plot->x; yarray=Plot->y; earray=Plot->e;
- for (i=0;i<Plot->NPts;i++,xarray++,yarray++,earray++)
- {
- if (Pict->ErrBar) {TempClip+=sprintf(TempClip,"%.5g %.5g %.5g\n",*xarray,*yarray,*earray);}
- else {TempClip+=sprintf(TempClip,"%.5g %.5g\n",*xarray,*yarray);}
- }
- } while ((Plot->Continued)&&(Plot=Plot->NextPlot));
- TempClip+=sprintf(TempClip,"\n\n");
- }
- CBCutS(ClipBuffer);
- CBClose();
- if (ClipBuffer) FreeMem(ClipBuffer,sizeof(char)*(tempval*BUFLNLNGTH+plotno*BUFEXTRAS));
- }
- else {
- for (TempSeln=Seln,tempval=0;(TempSeln&&TempSeln->SelectedBox);tempval++,TempSeln=TempSeln->NextSelection) ;
-
- ClipBuffer = (char *) AllocMem(sizeof(char)*(tempval*TXTBLCKLNGTH+BUFEXTRAS),MEMF_CLEAR);
- if (!ClipBuffer) {
- Message(" Insufficient memory for copy");
- CBClose();
- return(FALSE);
- }
-
- TempClip=ClipBuffer;
- TempClip+=sprintf(TempClip,"*EXTRATEXT*\n");
- for (TempSeln=Seln;(TempSeln&&TempSeln->SelectedBox);TempSeln=TempSeln->NextSelection)
- {
- TempText=TempSeln->SelectedBox;
- TempClip+=sprintf(TempClip,"%d %d %s\n",MAXHORIZ-TempText->x,MAXVERT-TempText->y,TempText->String);
- }
- CBCutS(ClipBuffer);
- CBClose();
- if (ClipBuffer) FreeMem(ClipBuffer,sizeof(char)*(tempval*TXTBLCKLNGTH+BUFEXTRAS));
- }
- return(TRUE);
- }
-
-
- ClipPaste(Pict)
- struct Pict *Pict;
- {
- long tempval=0;
- UBYTE c;
- ULONG postID;
-
- c=0;
- postID=0;
- if (CBOpen(PRIMARY_CLIP)) {
- Message(" Unable to open clipboard");
- return(FALSE);
- }
- if (!(tempval= CBPasteQueryLength())) {
- Message(" Nothing in clipboard ");
- CBClose();
- return(FALSE);
- }
- ClipBuffer = (char *) AllocMem(sizeof(char)*tempval,MEMF_CLEAR);
- if (!ClipBuffer) {
- Message(" Insufficient memory for paste");
- CBClose();
- return(FALSE);
- }
- CBPasteS(ClipBuffer);
- GetStringDat(ClipBuffer, Pict);
- CBClose();
- if (ClipBuffer) FreeMem(ClipBuffer,sizeof(char)*tempval);
- return(TRUE);
- }
-
-
-
- #define xcol 0
- #define ycol 1
- #define ecol 2
- #define COLUMN_OK(z) (z<0 ? TRUE : numeric(wp[z]))
- #define LINE_IS_DATA (COLUMN_OK(xcol) && COLUMN_OK(ycol))
- #define PLOT_OPENED (!PlotClosed && Plot->NPts)
-
- extern short PlotClosed, Continued;
- extern short xcoord;
-
- char line[100];
- FFP *x, *y, *e;
-
- GetStringDat(String, Pict)
- char *String;
- struct Pict *Pict;
- {
- short i, maxcol=3;
- char *wp[3], *cbuf, *getwrd(), *GetLine();
- struct Plot *Plot=NULL, *PrevPlot, *GetStructPlot(), *OpenPlot();
-
- PlotClosed=TRUE;
-
-
- while ((*String != '\0')&&(String=GetLine(line,String,100))) {
- cbuf=line;
- if (StringKeyWord(Pict,&String,&Plot)) ;
- else
- {
-
- for (i=0; i<=maxcol; i++) wp[i] = getwrd(&cbuf);
- if (LINE_IS_DATA) {
- if (PlotClosed) {
- Plot=OpenPlot(Pict);
- if (!Plot) {Message(" Insufficient memory for paste"); return(FALSE);}
- }
- else if (Plot->NPts == MAXPOINTS) {
- Continued = TRUE; PrevPlot = Plot;
- if (!(Plot->NextPlot = GetStructPlot())) {
- Message(" Insufficient memory for paste");
- if (PLOT_OPENED) { Pict->NPlt++; PlotClosed = TRUE; }
- return(FALSE);
- }
- else {
- Plot = Plot->NextPlot;
- Plot->Enabled = TRUE;
- Plot->Color = PrevPlot->Color;
- Plot->PointType = PrevPlot->PointType;
- Plot->PlotType= PrevPlot->PlotType;
- Plot->PointSize = PrevPlot->PointSize;
- Plot->Lines=PrevPlot->Lines;
- x = Plot->x; y = Plot->y; e = Plot->e;
- }
- }
- else if (Continued) {PrevPlot->Continued = TRUE; Continued = FALSE;}
-
- *(x++) = ( xcol>=0 ? atoFFP(wp[xcol]) : (FFP)(xcoord++));
- *(y++) = atoFFP(wp[ycol]);
- *(e++) = ( ecol>=0 ? atoFFP(wp[ecol]) : (FFP)(0.0));
- Plot->NPts++;
- }
- else { /* Line is not data */
- if (PLOT_OPENED) {
- Pict->NPlt++;
- PlotClosed = TRUE;
- }
- } /* End of line is not data */
- } /* End of not a legend */
- } /* End of while loop */
- if (PLOT_OPENED) Pict->NPlt++;
- }
-
-
- char *GetLine(cb,String,max)
- char *String, *cb;
- int max;
- {
- int i=0;
-
- while ((String[i]!='\n')&&(String[i]!='\0')&&(i<max-1))
- {
- cb[i]=String[i];
- i++;
- }
- cb[i]='\0';
- trch('\t',' ',cb); trch(',',' ',cb); trch('\r',' ',cb);
- if (String[i]=='\n') return(String+i+1);
- else return(String+i);
- }
-
-
-
- extern struct TextBox *ExtraText;
- extern struct Remember *Key;
-
- /******************/
- int StringKeyWord(Pict,p_String,p_Plot)
- char **p_String;
- struct Pict *Pict;
- struct Plot **p_Plot;
- {
- short tempLegX, tempLegY;
- char *GetLine(), *cbuf, *String;
- struct TextBox *TempText;
- struct Plot *Plot;
-
-
- Plot=*p_Plot; String=*p_String;
- cbuf=line;
-
- if (same(cbuf,"*",1)) {
- if (same(cbuf,"*AUTOSCRIPT*",12))
- {
- if (PlotClosed) {
- Plot=OpenPlot(Pict);
- if (!Plot) {Message(" Insufficient memory for paste"); return(FALSE);}
- }
- getwrd(&cbuf);
- sscanf(cbuf,"%hd %hd %hd %hd %hd %hd %hd %hd",
- &(Plot->Color),&(Plot->PlotType),&(Plot->Lines),&(Plot->PointSize),&(Plot->PointType),&(Plot->Enabled),&tempLegX,&(Plot->Legend->y));
- Plot->Legend->x=MAXHORIZ-tempLegX;
- *p_Plot=Plot; return(TRUE);
- }
- else if (same(cbuf,"*LEGEND*",8))
- {
- if (PlotClosed) {
- Plot=OpenPlot(Pict);
- if (!Plot) {Message(" Insufficient memory for paste"); return(FALSE);}
- }
- cbuf+=8; while(*cbuf==' ') cbuf++; stccpy(Plot->Legend->String,cbuf,79);
- *p_Plot=Plot; return(TRUE);
- }
- else if (same(cbuf,"*EXTRATEXT*",11)) {
- while ((*String != '\0')&&(String=GetLine(line,String,100))) {
- cbuf=line;
- if (ExtraText)
- {
- for (TempText=ExtraText;TempText->NextText;TempText=TempText->NextText);
- if (TempText->NextText=(struct TextBox *) AllocRemember(&Key,(sizeof(struct TextBox)),MEMF_CLEAR))
- {
- TempText=TempText->NextText;
- if (sscanf(cbuf,"%hd %hd",&tempLegX,&tempLegY));
- {
- TempText->x=MAXHORIZ-tempLegX;
- TempText->y=MAXVERT-tempLegY;
-
- getwrd(&cbuf); getwrd(&cbuf); while(*cbuf==' ') cbuf++;
- }
- stccpy(TempText->String,cbuf,79);
- TempText->NextText=NULL;
- }
- else Message(" Not enough memory for text");
- }
- else
- {
- if (ExtraText=(struct TextBox *) AllocRemember(&Key,(sizeof(struct TextBox)),MEMF_CLEAR))
- {
- if (sscanf(cbuf,"%hd %hd",&tempLegX,&tempLegY))
- {
- ExtraText->x=MAXHORIZ-tempLegX;
- ExtraText->y=MAXVERT-tempLegY;
- getwrd(&cbuf); getwrd(&cbuf); while (*cbuf==' ') cbuf++;
- }
- stccpy(ExtraText->String,cbuf,79);
- ExtraText->NextText=NULL;
- }
- else Message(" Not enough memory for text");
- }
- }
- *p_String=String; return(TRUE);
- }
- }
- return (FALSE);
- }
-
- /*** PLOT TYPES ***/
- #define BOTHPLOT 2
-
- extern struct NewScreen newscreen;
- struct Plot *OpenPlot(Pict)
- struct Pict *Pict;
- {
- struct Plot *Plot;
- static int i;
-
- if (newscreen.Depth==1) i=1;
- if (Pict->Plot) {
- for (Plot=Pict->Plot;Plot->NextPlot;Plot=Plot->NextPlot) ;
- if (! (Plot->NextPlot = GetStructPlot())) return(NULL);
- else Plot = Plot->NextPlot;
- }
- else {
- if (!(Pict->Plot = GetStructPlot())) return(NULL);
- else Plot = Pict->Plot;
- }
- x = Plot->x; y = Plot->y; e = Plot->e;
- Plot->Enabled = TRUE;
- Plot->Color = PLOTCOLORBASE + i;
- Plot->PointType = 6;
- Plot->PlotType=BOTHPLOT;
- Plot->PointSize = DEFAULT_POINT_SIZE;
- Plot->Lines=TRUE;
- PlotClosed = FALSE; xcoord=0;
- switch(newscreen.Depth) {
- case 1: break;
- case 2: if ((i==1)||(i==9)) i++;
- case 3: if (i==5) i++;
- default: i++; if (i>11) i=0; break;
- }
- return(Plot);
- }
-
-
-
-
- long CBPasteQueryLength()
- {
- long length,slen;
- long len[5];
-
- clipboardIO->io_Command=CMD_READ;
- clipboardIO->io_ClipID=0;
- clipboardIO->io_Offset=0;
- clipboardIO->io_Data=(char *)len;
- clipboardIO->io_Length=20;
- DoIO(clipboardIO);
-
- length=len[1];
- if (clipboardIO->io_Error) slen=0; /* Nothing in clipboard */
- else slen=len[4];
-
- clipboardIO->io_Offset+=length;
- clipboardIO->io_Length=1;
- clipboardIO->io_Data=0;
- DoIO(clipboardIO);
-
- return(slen);
- }
-
-
- /******** COMMODORE STANDARD CLIPBOARD INTERFACE FUNCTIONS **********/
-
- long CBOpen(unit)
- long unit;
- {
- long error;
- clipboardMsgPort=CreatePort(0,0);
- satisfyMsgPort=CreatePort(0,0);
-
- clipboardIO=(struct IOClipReq *) CreateExtIO(clipboardMsgPort,sizeof(struct IOClipReq));
-
- if ((error=OpenDevice("clipboard.device",unit,clipboardIO,0)))
- return(error);
- return(0);
- }
-
- void CBClose()
- {
- CloseDevice(clipboardIO);
- DeletePort(satisfyMsgPort);
- DeletePort(clipboardMsgPort);
- DeleteExtIO(clipboardIO);
- }
-
- void CBCut(stream,length)
- UBYTE *stream;
- long length;
- {
- clipboardIO->io_Command=CMD_WRITE;
- clipboardIO->io_Data=stream;
- clipboardIO->io_Length=length;
- clipboardIO->io_Offset=0;
- clipboardIO->io_ClipID=0;
- DoIO(clipboardIO);
- clipboardIO->io_Command=CMD_UPDATE;
- DoIO(clipboardIO);
- }
-
- void CBCutS(string)
- UBYTE *string;
- {
- clipboardIO->io_ClipID=0;
- CBSatisfyPost(string);
- }
-
- void writeLong(ldata)
- long *ldata;
- {
- clipboardIO->io_Command=CMD_WRITE;
- clipboardIO->io_Data=(char *)ldata;
- clipboardIO->io_Length=4;
- DoIO(clipboardIO);
- }
-
- void CBSatisfyPost(string)
- UBYTE *string;
- {
- long length,slen=strlen(string);
- BOOL odd=(slen & 1);
-
- length= (odd) ? slen+1 : slen;
- clipboardIO->io_Offset=0;
- writeLong((long *)"FORM");
- length +=12;
- writeLong(&length);
- writeLong((long *)"FTXT");
- writeLong((long *)"CHRS");
- writeLong(&slen);
- clipboardIO->io_Command=CMD_WRITE;
- clipboardIO->io_Data=(char *)string;
- clipboardIO->io_Length=slen;
- DoIO(clipboardIO);
-
- if (odd)
- {
- clipboardIO->io_Command=CMD_WRITE;
- clipboardIO->io_Data="";
- clipboardIO->io_Length=1;
- DoIO(clipboardIO);
- }
- clipboardIO->io_Command=CMD_UPDATE;
- DoIO(clipboardIO);
- }
-
-
- long CBPasteS(string)
- UBYTE *string;
- {
- long length,slen;
- long len[5];
-
- clipboardIO->io_Command=CMD_READ;
- clipboardIO->io_ClipID=0;
- clipboardIO->io_Offset=0;
- clipboardIO->io_Data=(char *)len;
- clipboardIO->io_Length=20;
- DoIO(clipboardIO);
-
- length=len[1];
- slen=len[4];
- clipboardIO->io_Data=(char *)string;
- clipboardIO->io_Length=slen;
- DoIO(clipboardIO);
-
- clipboardIO->io_Offset+=length;
- clipboardIO->io_Length=1;
- clipboardIO->io_Data=0;
- DoIO(clipboardIO);
-
- string[slen]='\0';
- return(slen);
- }
-
- long CBPost()
- {
- clipboardIO->io_Command=CBD_POST;
- clipboardIO->io_Data=(char*)satisfyMsgPort;;
- clipboardIO->io_ClipID=0;
- DoIO(clipboardIO);
- return(clipboardIO->io_ClipID);
- }
-
- long CBCurrentReadID()
- {
- clipboardIO->io_Command=CBD_CURRENTREADID;
- DoIO(clipboardIO);
- return(clipboardIO->io_ClipID);
- }
-
- long CBCurrentWriteID()
- {
- clipboardIO->io_Command=CBD_CURRENTWRITEID;
- DoIO(clipboardIO);
- return(clipboardIO->io_ClipID);
- }
-
- BOOL CBCheckSatisfy(idVar)
- long *idVar;
- {
- struct SatisfyMsg *sm;
-
- if (*idVar==0) return(TRUE);
- if (*idVar < CBCurrentWriteID())
- {
- *idVar=0;
- return(TRUE);
- }
- if (sm=(struct SatisfyMsg *)GetMsg(satisfyMsgPort))
- {
- if (*idVar==sm->sm_ClipID) return(TRUE);
- }
- return(FALSE);
- }
-
-
-
-