diff --git a/.gitignore b/.gitignore index 9d7c5735..1c31d85a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.gitignore etc lib bin/* @@ -13,7 +14,6 @@ LICENSE*ACCEPTED MAILHOME* sac *.o - *.a - junk* +.vscode/settings.json diff --git a/src/Xtcwp/main/xgraph.c b/src/Xtcwp/main/xgraph.c index 505c4e88..c5019b6b 100644 --- a/src/Xtcwp/main/xgraph.c +++ b/src/Xtcwp/main/xgraph.c @@ -6,108 +6,112 @@ #include "par.h" #include "Xtcwp/Xtcwp.h" #include "Xtcwp/Axes.h" -#include +#include #include /*********************** self documentation **********************/ char *sdoc[] = { -" ", -" XGRAPH - X GRAPHer ", -" Graphs n[i] pairs of (x,y) coordinates, for i = 1 to nplot. ", -" ", -" xgraph n= [optional parameters] NPMAX) err("too many plots"); - for (i=nn; i0) - for (i=npairs; i0) - for (i=nd1; i0) - for (i=nf1; i0) - for (i=nd2; i0) - for (i=nf2; i NPMAX) + err("too many plots"); + for (i = nn; i < nplot; ++i) + n[i] = n[nn - 1]; + if (npairs > 0) + for (i = npairs; i < nplot; ++i) + pairs[i] = pairs[npairs - 1]; + if (nd1 > 0) + for (i = nd1; i < nplot; ++i) + d1[i] = d1[nd1 - 1]; + if (nf1 > 0) + for (i = nf1; i < nplot; ++i) + f1[i] = f1[nf1 - 1]; + if (nd2 > 0) + for (i = nd2; i < nplot; ++i) + d2[i] = d2[nd2 - 1]; + if (nf2 > 0) + for (i = nf2; i < nplot; ++i) + f2[i] = f2[nf2 - 1]; /* read, regularize and compute extreme values of data */ - data = (float **)malloc(nplot*sizeof(float*)); + data = (float **)malloc(nplot * sizeof(float *)); x2max = x1max = -FLT_MAX; - x2min = x1min = FLT_MAX; - for (iplot=0; iplotWM_PROTOCOLS: XQuit()\n")); + XtRealizeWidget(toplevel); - XtMainLoop(); + Display *dpy = XtDisplay(toplevel); + wm_delete_window = XInternAtom(dpy, "WM_DELETE_WINDOW", False); + XSetWMProtocols(dpy, XtWindow(toplevel), &wm_delete_window, 1); + XtMainLoop(); return EXIT_SUCCESS; } -void resizeCB (Widget w, - XtPointer clientdata, - XtcwpAxesCallbackStruct *ca) +void resizeCB(Widget w, XtPointer clientdata, XtcwpAxesCallbackStruct *ca) { - if(((char *) clientdata)-((char *) clientdata)) resizeCB(w,clientdata,ca); - /* printf("resize callback\n"); */ + if (((char *)clientdata) - ((char *)clientdata)) + resizeCB(w, clientdata, ca); } #define NCOLOR 8 @@ -510,319 +579,337 @@ static char *color[NCOLOR] = { "yellow", }; -void exposeCB (Widget w, - ExposeCD *cd, - XtcwpAxesCallbackStruct *ca) +void exposeCB(Widget w, ExposeCD *cd, XtcwpAxesCallbackStruct *ca) { - int nplot=cd->nplot; - int *n=cd->n; - float **data=cd->data; - int *linewidth=cd->linewidth; - int *linecolor=cd->linecolor; - int *mark=cd->mark; - int *marksize=cd->marksize; - int nplot_start, nplot_end; /* JGHACK */ - Position x=ca->x,y=ca->y; - Dimension width=ca->width,height=ca->height; - float x1beg=ca->x1beg,x1end=ca->x1end, - x2beg=ca->x2beg,x2end=ca->x2end; - int style=ca->style; - Display *dpy=XtDisplay(w); - Window win=XtWindow(w); + int nplot = cd->nplot; + int *n = cd->n; + float **data = cd->data; + int *linewidth = cd->linewidth; + int *linecolor = cd->linecolor; + int *mark = cd->mark; + int *marksize = cd->marksize; + int nplot_start, nplot_end; + Position x = ca->x, y = ca->y; + Dimension width = ca->width, height = ca->height; + float x1beg = ca->x1beg, x1end = ca->x1end, + x2beg = ca->x2beg, x2end = ca->x2end; + int style = ca->style; + Display *dpy = XtDisplay(w); + Window win = XtWindow(w); XWindowAttributes wa; Colormap cmap; - XColor scolor,ecolor; + XColor scolor, ecolor; XRectangle rect; - unsigned long black=BlackPixelOfScreen(XtScreen(w)); + unsigned long black = BlackPixelOfScreen(XtScreen(w)); GC gc; - int iplot,icolor; - float xmin,xmax,ymin,ymax,xbase,xscale,ybase,yscale; - static int firstexpose=1; + int iplot, icolor; + float xmin, xmax, ymin, ymax, xbase, xscale, ybase, yscale; + static int firstexpose = 1; /* if first expose, check style and, if necessary, swap x,y coords */ - if (firstexpose) { - if (style==XtcwpSEISMIC) { - /* JGHACK ......*/ - if (cd->plot_direction==1) { - nplot_start=0; - nplot_end=nplot-1; - } else { - nplot_start=nplot-1; - nplot_end=0; - } - for (iplot=nplot_start; (cd->plot_direction==1) ? (iplot<=nplot_end) : (iplot>=nplot_end); - iplot+=cd->plot_direction) { - /* .......JGHACK */ - int ni=n[iplot],i; - for (i=0; iplot_direction == 1) + { + nplot_start = 0; + nplot_end = nplot - 1; + } + else + { + nplot_start = nplot - 1; + nplot_end = 0; + } + for (iplot = nplot_start; (cd->plot_direction == 1) ? (iplot <= nplot_end) : (iplot >= nplot_end); + iplot += cd->plot_direction) + { + int ni = n[iplot], i; + for (i = 0; i < ni; ++i) + { + float temp = data[iplot][2 * i]; + data[iplot][2 * i] = data[iplot][2 * i + 1]; + data[iplot][2 * i + 1] = temp; + } } } - } - firstexpose = 0; + firstexpose = 0; } - + /* determine current colormap */ - XGetWindowAttributes(dpy,win,&wa); + XGetWindowAttributes(dpy, win, &wa); cmap = wa.colormap; /* create GC */ - gc = XCreateGC(dpy,win,0L,NULL); - + gc = XCreateGC(dpy, win, 0, NULL); + /* set clip to axes box */ - rect.x = x; rect.y = y; rect.width = width; rect.height = height; - XSetClipRectangles(dpy,gc,0,0,&rect,1,Unsorted); + rect.x = x; + rect.y = y; + rect.width = width; + rect.height = height; + XSetClipRectangles(dpy, gc, 0, 0, &rect, 1, Unsorted); /* determine min and max coordinates */ xmin = x; - xmax = x+((int) width); + xmax = x + ((int)width); ymin = y; - ymax = y+((int) height); + ymax = y + ((int)height); /* determine base and scale factors */ - if (style==XtcwpNORMAL) { - xscale = ((float) width)/(x1end-x1beg); - xbase = x-x1beg*xscale; - yscale = ((float) height)/(x2beg-x2end); - ybase = y-x2end*yscale; - } else { - xscale = ((float) width)/(x2end-x2beg); - xbase = x-x2beg*xscale; - yscale = ((float) height)/(x1end-x1beg); - ybase = y-x1beg*yscale; + if (style == XtcwpNORMAL) + { + xscale = ((float)width) / (x1end - x1beg); + xbase = x - x1beg * xscale; + yscale = ((float)height) / (x2beg - x2end); + ybase = y - x2end * yscale; } - + else + { + xscale = ((float)width) / (x2end - x2beg); + xbase = x - x2beg * xscale; + yscale = ((float)height) / (x1end - x1beg); + ybase = y - x1beg * yscale; + } + + if (cd->plot_direction == 1) + { + nplot_start = 0; + nplot_end = nplot - 1; + } + else + { + nplot_start = nplot - 1; + nplot_end = 0; + } + /* loop over plots */ - /* JGHACK ......*/ - if (cd->plot_direction==1) { - nplot_start=0; - nplot_end=nplot-1; - } else { - nplot_start=nplot-1; - nplot_end=0; - } - for (iplot=nplot_start; (cd->plot_direction==1) ? (iplot<=nplot_end) : (iplot>=nplot_end); - iplot+=cd->plot_direction) - { - /* ........JGHACK */ + for (iplot = nplot_start; (cd->plot_direction == 1) ? (iplot <= nplot_end) : (iplot >= nplot_end); + iplot += cd->plot_direction) + { /* set line width */ - XSetLineAttributes(dpy,gc, - linewidth[iplot], - LineSolid, - CapButt, - JoinMiter); - + XSetLineAttributes(dpy, gc, + linewidth[iplot], + LineSolid, + CapButt, + JoinMiter); + /* set line color */ icolor = linecolor[iplot]; - if (icolor<0) icolor = 0; - else if (icolor>=NCOLOR) icolor = NCOLOR-1; - if (XAllocNamedColor(dpy,cmap,color[icolor],&scolor,&ecolor)) - XSetForeground(dpy,gc,ecolor.pixel); + if (icolor < 0) + icolor = 0; + else if (icolor >= NCOLOR) + icolor = NCOLOR - 1; + if (XAllocNamedColor(dpy, cmap, color[icolor], &scolor, &ecolor)) + XSetForeground(dpy, gc, ecolor.pixel); else - XSetForeground(dpy,gc,black); - + XSetForeground(dpy, gc, black); + /* draw lines between points */ - if (linewidth[iplot]!=0) { - int xi,yi,in,xilast,yilast,inlast,i,ni=n[iplot]; + if (linewidth[iplot] != 0) + { + int xi, yi, in, xilast, yilast, inlast, i, ni = n[iplot]; float *pdata; - + pdata = data[iplot]; - xi = xbase+xscale*(*pdata++); - yi = ybase+yscale*(*pdata++); - in = (xi>=xmin && xi<=xmax && yi>=ymin && yi<=ymax); - for (i=1; i= xmin && xi <= xmax && yi >= ymin && yi <= ymax); + for (i = 1; i < ni; ++i) + { xilast = xi; yilast = yi; inlast = in; - xi = xbase+xscale*(*pdata++); - yi = ybase+yscale*(*pdata++); - in = (xi>=xmin && xi<=xmax && - yi>=ymin && yi<=ymax); + xi = xbase + xscale * (*pdata++); + yi = ybase + yscale * (*pdata++); + in = (xi >= xmin && xi <= xmax && + yi >= ymin && yi <= ymax); if (in || inlast) - XDrawLine(dpy,win,gc, - xilast,yilast,xi,yi); + XDrawLine(dpy, win, gc, + xilast, yilast, xi, yi); } } /* draw marks at points */ - if (marksize[iplot]!=0) { - int xi,yi,in,i,ni=n[iplot]; + if (marksize[iplot] != 0) + { + int xi, yi, in, i, ni = n[iplot]; float *pdata; - + pdata = data[iplot]; - for (i=0; i=xmin && xi<=xmax && - yi>=ymin && yi<=ymax); - if (in) xDrawMark(dpy,win,gc,xi,yi, - mark[iplot],marksize[iplot]); + for (i = 0; i < ni; ++i) + { + xi = xbase + xscale * (*pdata++); + yi = ybase + yscale * (*pdata++); + in = (xi >= xmin && xi <= xmax && + yi >= ymin && yi <= ymax); + if (in) + xDrawMark(dpy, win, gc, xi, yi, + mark[iplot], marksize[iplot]); } } } /* free GC */ - XFreeGC(dpy,gc); + XFreeGC(dpy, gc); } -void inputCB (Widget w, - XtPointer clientdata, - XtcwpAxesCallbackStruct *ca) +void inputCB(Widget w, XtPointer clientdata, XtcwpAxesCallbackStruct *ca) { - int x=ca->x,y=ca->y,width=ca->width,height=ca->height; - float x1beg=ca->x1beg,x1end=ca->x1end,x2beg=ca->x2beg,x2end=ca->x2end; - int style=ca->style; - XEvent *event=ca->event; - int xb,yb,wb,hb; - float x1begn,x1endn,x2begn,x2endn; - static int firstinput=1; - static float x1begs,x1ends,x2begs,x2ends; + int x = ca->x, y = ca->y, width = ca->width, height = ca->height; + float x1beg = ca->x1beg, x1end = ca->x1end, x2beg = ca->x2beg, x2end = ca->x2end; + int style = ca->style; + XEvent *event = ca->event; + int xb, yb, wb, hb; + float x1begn, x1endn, x2begn, x2endn; + static int firstinput = 1; + static float x1begs, x1ends, x2begs, x2ends; /* if first input, save initial axes limits */ - if (firstinput) { + if (firstinput) + { x1begs = x1beg; x1ends = x1end; x2begs = x2beg; x2ends = x2end; - firstinput = (int) (((char *) clientdata)-((char *) clientdata))/*0*/; + firstinput = (int)(((char *)clientdata) - ((char *)clientdata)); } - + /* track pointer and get rubber box */ - XtcwpRubberBox(XtDisplay(w),XtWindow(w),*event,&xb,&yb,&wb,&hb); + XtcwpRubberBox(XtDisplay(w), XtWindow(w), *event, &xb, &yb, &wb, &hb); /* if new box has zero width or height */ - if (wb==0 || hb==0) { - + if (wb == 0 || hb == 0) + { /* restore initial limits */ - XtcwpSetAxesValues(w,x1begs,x1ends,x2begs,x2ends); - - /* else if non-zero zoom box */ - } else { - + XtcwpSetAxesValues(w, x1begs, x1ends, x2begs, x2ends); + + /* else if non-zero zoom box */ + } + else + { /* clip box */ - if (xbx+width) wb = x-xb+width; - if (yb+hb>y+height) hb = y-yb+height; - + if (xb + wb > x + width) + wb = x - xb + width; + if (yb + hb > y + height) + hb = y - yb + height; + /* determine axes limits */ - if (style==XtcwpNORMAL) { - x1begn = x1beg+(xb-x)*(x1end-x1beg)/width; - x1endn = x1beg+(xb+wb-x)*(x1end-x1beg)/width; - x2begn = x2end+(yb+hb-y)*(x2beg-x2end)/height; - x2endn = x2end+(yb-y)*(x2beg-x2end)/height; - } else { - x1endn = x1beg+(yb+hb-y)*(x1end-x1beg)/height; - x1begn = x1beg+(yb-y)*(x1end-x1beg)/height; - x2begn = x2beg+(xb-x)*(x2end-x2beg)/width; - x2endn = x2beg+(xb+wb-x)*(x2end-x2beg)/width; + if (style == XtcwpNORMAL) + { + x1begn = x1beg + (xb - x) * (x1end - x1beg) / width; + x1endn = x1beg + (xb + wb - x) * (x1end - x1beg) / width; + x2begn = x2end + (yb + hb - y) * (x2beg - x2end) / height; + x2endn = x2end + (yb - y) * (x2beg - x2end) / height; } - + else + { + x1endn = x1beg + (yb + hb - y) * (x1end - x1beg) / height; + x1begn = x1beg + (yb - y) * (x1end - x1beg) / height; + x2begn = x2beg + (xb - x) * (x2end - x2beg) / width; + x2endn = x2beg + (xb + wb - x) * (x2end - x2beg) / width; + } + /* set axes limits */ - XtcwpSetAxesValues(w,x1begn,x1endn,x2begn,x2endn); + XtcwpSetAxesValues(w, x1begn, x1endn, x2begn, x2endn); } - + /* force an expose event */ - XClearArea(XtDisplay(w),XtWindow(w),0,0,0,0,True); + XClearArea(XtDisplay(w), XtWindow(w), 0, 0, 0, 0, True); } -static void xDrawMark(Display *dpy, Drawable d, GC gc, - int x, int y, int index, int size) +static void xDrawMark(Display *dpy, Drawable d, GC gc, int x, int y, int index, int size) { XPoint points[4]; - switch (index%NMARKS) { + switch (index % NMARKS) + { case MPLUS: /* plus */ - XDrawLine(dpy,d,gc,x-(int)(0.5*size),y,x+(int)(0.5*size),y); - XDrawLine(dpy,d,gc,x,y-(int)(0.5*size),x,y+(int)(0.5*size)); + XDrawLine(dpy, d, gc, x - (int)(0.5 * size), y, x + (int)(0.5 * size), y); + XDrawLine(dpy, d, gc, x, y - (int)(0.5 * size), x, y + (int)(0.5 * size)); break; case MASTERISK: /* asterisk */ - XDrawLine(dpy,d,gc,x-(int)(0.5*size),y,x+(int)(0.5*size),y); - XDrawLine(dpy,d,gc,x-(int)(0.25*size),y-(int)(0.433*size), - x+(int)(0.25*size),y+(int)(0.433*size)); - XDrawLine(dpy,d,gc,x+(int)(0.25*size),y-(int)(0.433*size), - x-(int)(0.25*size),y+(int)(0.433*size)); + XDrawLine(dpy, d, gc, x - (int)(0.5 * size), y, x + (int)(0.5 * size), y); + XDrawLine(dpy, d, gc, x - (int)(0.25 * size), y - (int)(0.433 * size), + x + (int)(0.25 * size), y + (int)(0.433 * size)); + XDrawLine(dpy, d, gc, x + (int)(0.25 * size), y - (int)(0.433 * size), + x - (int)(0.25 * size), y + (int)(0.433 * size)); break; case MCROSS: /* X */ - XDrawLine(dpy,d,gc,x-(int)(0.5*size),y-(int)(0.5*size), - x+(int)(0.5*size),y+(int)(0.5*size)); - XDrawLine(dpy,d,gc,x+(int)(0.5*size),y-(int)(0.5*size), - x-(int)(0.5*size),y+(int)(0.5*size)); + XDrawLine(dpy, d, gc, x - (int)(0.5 * size), y - (int)(0.5 * size), + x + (int)(0.5 * size), y + (int)(0.5 * size)); + XDrawLine(dpy, d, gc, x + (int)(0.5 * size), y - (int)(0.5 * size), + x - (int)(0.5 * size), y + (int)(0.5 * size)); break; case MTRIANGLE: /* triangle */ - points[0].x = x-(int)(0.5*size); - points[0].y = y+(int)(0.25*size); - points[1].x = x+(int)(0.5*size); - points[1].y = y+(int)(0.25*size); - points[2].x = x; - points[2].y = y-(int)(0.559*size); - points[3].x = x-(int)(0.5*size); - points[3].y = y+(int)(0.25*size); - XDrawLines(dpy,d,gc,points,4,CoordModeOrigin); + points[0].x = x - (int)(0.5 * size); + points[0].y = y + (int)(0.25 * size); + points[1].x = x + (int)(0.5 * size); + points[1].y = y + (int)(0.25 * size); + points[2].x = x; + points[2].y = y - (int)(0.559 * size); + points[3].x = x - (int)(0.5 * size); + points[3].y = y + (int)(0.25 * size); + XDrawLines(dpy, d, gc, points, 4, CoordModeOrigin); break; case MSQUARE: /* square */ - XDrawRectangle(dpy,d,gc,x-(int)(0.5*size),y-(int)(0.5*size), - size,size); + XDrawRectangle(dpy, d, gc, x - (int)(0.5 * size), y - (int)(0.5 * size), + size, size); break; case MCIRCLE: /* circle */ - XDrawArc(dpy,d,gc,x-(int)(0.5*size),y-(int)(0.5*size), - size,size,0,360*64); + XDrawArc(dpy, d, gc, x - (int)(0.5 * size), y - (int)(0.5 * size), + size, size, 0, 360 * 64); break; case MFILLEDTRIANGLE: /* filled triangle */ - points[0].x = x-(int)(0.75*size); - points[0].y = y+(int)(0.375*size); - points[1].x = x+(int)(0.75*size); - points[1].y = y+(int)(0.375*size); - points[2].x = x; - points[2].y = y-(int)(0.838*size); - points[3].x = x-(int)(0.75*size); - points[3].y = y+(int)(0.375*size); - XFillPolygon(dpy,d,gc,points,4,Convex,CoordModeOrigin); + points[0].x = x - (int)(0.75 * size); + points[0].y = y + (int)(0.375 * size); + points[1].x = x + (int)(0.75 * size); + points[1].y = y + (int)(0.375 * size); + points[2].x = x; + points[2].y = y - (int)(0.838 * size); + points[3].x = x - (int)(0.75 * size); + points[3].y = y + (int)(0.375 * size); + XFillPolygon(dpy, d, gc, points, 4, Convex, CoordModeOrigin); break; case MFILLEDSQUARE: /* filled square */ - XFillRectangle(dpy,d,gc,x-(int)(0.5*size),y-(int)(0.5*size), - size,size); + XFillRectangle(dpy, d, gc, x - (int)(0.5 * size), y - (int)(0.5 * size), + size, size); break; case MFILLEDCIRCLE: /* filled circle */ - XFillArc(dpy,d,gc,x-(int)(0.5*size),y-(int)(0.5*size), - size,size,0,360*64); + XFillArc(dpy, d, gc, x - (int)(0.5 * size), y - (int)(0.5 * size), + size, size, 0, 360 * 64); break; } } /***************************************************************************** -Bill Wingle's routine to handle key presses. +Functions to exit by key press (q or Q) and pointer press of top right "X" *****************************************************************************/ -void key_pressed (Widget w, graphics_data *data, XKeyEvent *event) +void keyQuit(Widget w, graphics_data *data, XKeyEvent *event) { - char buffer[2]; - int bufsize = 2; - KeySym key; - XComposeStatus compose; - - XLookupString (event, buffer, bufsize, &key, &compose); - - if (key==XK_q || key==XK_Q) { - exit(0); - } - + char buffer[2]; + int bufsize = 2; + KeySym key; + XComposeStatus compose; + + XLookupString(event, buffer, bufsize, &key, &compose); + if (key == XK_q || key == XK_Q) + { + exit(0); + } } - -/* ARGSUSED */ - -/* -static void Quit (Widget w, - XEvent *event, - String *params, - Cardinal *num_params) +void XQuit(Widget w, XEvent *ev, String *vector, Cardinal *count) { - exit(0); + if (ev->type == ClientMessage && + ((Atom)ev->xclient.data.l[0]) == wm_delete_window) + exit(0); } -*/