📄 xwigb.c
字号:
/* else if window exposed */ } else if (event.type==Expose) { /* clear all expose events from queue */ while (XCheckTypedEvent(dpy,Expose,&event)); /* if necessary, make new image */ if (imageOutOfDate) { if (image!=NULL) { free1(image->data); XDestroyImage(image); } image = newBitmap(dpy,width,height, n1,d1,f1,n2,x2,z, x1begb,x1endb,x2begb,x2endb, xcur,clip,wt,va, &p2beg,&p2end); imageOutOfDate = 0; } /* draw image (before axes so grid lines visible) */ XPutImage(dpy,win,gci,image,0,0,x,y, image->width,image->height); /* draw axes on top of image */ xDrawAxesBox(dpy,win, x,y,width,height, x1begb,x1endb,0.0,0.0, d1num,f1num,n1tic,grid1,label1, x2begb,x2endb,p2beg,p2end, d2num,f2num,n2tic,grid2,label2, labelfont,title,titlefont, labelcolor,titlecolor,gridcolor, style); /* else if key down */ } else if (event.type==KeyPress) { XLookupString(&event,keybuf,0,&keysym,&keystat); if (keysym==XK_s) { xMousePrint(dpy,win,event,style, mpicksfp, x,y,width,height, x1begb,x1endb,x2begb,x2endb); } else if (keysym==XK_Q) { /* This is the exit from the event loop */ break; } else { continue; } /* else if button down (1 == zoom, 2 == mouse tracking */ } else if (event.type==ButtonPress) { /* if 1st button: zoom */ if (event.xbutton.button==Button1) { /* track pointer and get new box */ xRubberBox(dpy,win,event,&xb,&yb,&wb,&hb); /* if new box has tiny width or height */ if (wb<4 || hb<4) { /* reset box to initial values */ x1begb = x1beg; x1endb = x1end; x2begb = x2beg; x2endb = x2end; /* else, if new box has non-zero width /* if new box has zero width or height */ } else { /* calculate new box parameters */ zoomBox(x,y,width,height, xb,yb,wb,hb, x2begb,x2endb, x1begb,x1endb, &x2begb,&x2endb, &x1begb,&x1endb); } /* clear area and force an expose event */ XClearArea(dpy,win,0,0,0,0,True); /* note that image is out of date */ imageOutOfDate = 1; /* else if 2nd button down: display mouse coords */ } else if (event.xbutton.button==Button2) { showloc = 1; xMouseLoc(dpy,win,event,style,showloc, x,y,width,height,x1begb,x1endb, x2begb,x2endb,p2beg,p2end); } else { continue; } /* else if pointer has moved */ } else if (event.type==MotionNotify) { /* if button2 down, show mouse location */ if (showloc) xMouseLoc(dpy,win,event,style,True, x,y,width,height,x1begb,x1endb, x2begb,x2endb,p2beg,p2end); /* else if button2 released, stop tracking */ } else if (event.type==ButtonRelease && event.xbutton.button==Button2) { showloc = 0; } } /* end of event loop */ /* close connection to X server */ XCloseDisplay(dpy);} /* update parameters associated with zoom box */static void zoomBox (int x, int y, int w, int h, int xb, int yb, int wb, int hb, float x1, float x2, float y1, float y2, float *x1b, float *x2b, float *y1b, float *y2b){ /* if width and/or height of box are zero, just copy values */ if (wb==0 || hb==0) { *x1b = x1; *x2b = x2; *y1b = y1; *y2b = y2; return; } /* clip box */ if (xb<x) { wb -= x-xb; xb = x; } if (yb<y) { hb -= y-yb; yb = y; } if (xb+wb>x+w) wb = x-xb+w; if (yb+hb>y+h) hb = y-yb+h; /* determine box limits */ *x1b = x1+(xb-x)*(x2-x1)/w; *x2b = x1+(xb+wb-x)*(x2-x1)/w; *y1b = y1+(yb-y)*(y2-y1)/h; *y2b = y1+(yb+hb-y)*(y2-y1)/h;}/* return pointer to new image bitmap of rasterized wiggles */static XImage *newBitmap (Display *dpy, int width, int height, int n1, float d1, float f1, int n2, float *x2, float *z, float x1beg, float x1end, float x2beg, float x2end, float xcur, float clip, int wt, int va, float *p2begp, float *p2endp){ int widthpad,nbpr,i1beg,i1end,if1r,n1r,b1fz,b1lz,i2,i,n2in; float x2min,x2max,p2beg,p2end,bscale,boffset,bxcur,bx2; unsigned char *bits; int scr=DefaultScreen(dpy); /* determine bitmap dimensions and allocate space for bitmap */ widthpad = (1+(width-1)/(BitmapPad(dpy)/8))*BitmapPad(dpy)/8; nbpr = 1+(widthpad-1)/8; bits = ealloc1(nbpr*height,sizeof(unsigned char)); for (i=0; i<nbpr*height; ++i) bits[i] = 0; /* determine number of traces that fall within axis 2 bounds */ x2min = MIN(x2beg,x2end); x2max = MAX(x2beg,x2end); for (i2=0,n2in=0; i2<n2; i2++) if (x2[i2]>=x2min && x2[i2]<=x2max) n2in++; /* determine pads for wiggle excursion along axis 2 */ xcur = fabs(xcur); if (n2in>1) xcur *= (x2max-x2min)/(n2in-1); p2beg = (x2end>=x2beg)?-xcur:xcur; p2end = (x2end>=x2beg)?xcur:-xcur; /* determine scale and offset to map x2 units to bitmap units */ bscale = (width-1)/(x2end+p2end-x2beg-p2beg); boffset = -(x2beg+p2beg)*bscale; bxcur = xcur*bscale; /* adjust x1beg and x1end to fall on sampled values */ i1beg = NINT((x1beg-f1)/d1); i1beg = MAX(0,MIN(n1-1,i1beg)); x1beg = f1+i1beg*d1; i1end = NINT((x1end-f1)/d1); i1end = MAX(0,MIN(n1-1,i1end)); x1end = f1+i1end*d1; /* determine first sample and number of samples to rasterize */ if1r = MIN(i1beg,i1end); n1r = MAX(i1beg,i1end)-if1r+1; /* determine bits corresponding to first and last samples */ b1fz = (x1end>x1beg)?0:height-1; b1lz = (x1end>x1beg)?height-1:0; /* rasterize traces */ for (i2=0; i2<n2; i2++,z+=n1) { /* skip traces not in bounds */ if (x2[i2]<x2min || x2[i2]>x2max) continue; /* determine bitmap coordinate of trace */ bx2 = boffset+x2[i2]*bscale; /* rasterize one trace */ rfwtva(n1r,&z[if1r],-clip,clip,va?0:clip, (int)(bx2-bxcur),(int)(bx2+bxcur),b1fz,b1lz, wt,nbpr,bits); }/* !!! inserted by Z. Li to fix problem of SUN's display colormap different from rs-6000's colormap !!! */ for (i=0; i<nbpr*height; ++i) bits[i] = 255 - bits[i]; /* return axis 2 pads */ *p2begp = p2beg; *p2endp = p2end; /* return pointer to image */ return XCreateImage(dpy,DefaultVisual(dpy,scr), 1,XYBitmap,0,bits,widthpad,height,BitmapPad(dpy),nbpr);} void xMouseLoc(Display *dpy, Window win, XEvent event, int style, Bool show, int x, int y, int width, int height, float x1begb, float x1endb, float x2begb, float x2endb, float p2beg, float p2end){ XGCValues values; static XFontStruct *fs=NULL; static XCharStruct overall; static GC gc; int dummy,xoffset=10,yoffset=10; float x1,x2; char string[256]; /* if first time, get font attributes and make gc */ if (fs==NULL) { fs = XLoadQueryFont(dpy,"fixed"); gc = XCreateGC(dpy,win,0,&values); XSetFont(dpy,gc,fs->fid); overall.width = 1; overall.ascent = 1; overall.descent = 1; } /* erase previous string */ XClearArea(dpy,win,xoffset,yoffset, overall.width,overall.ascent+overall.descent,False); /* if not showing, then return */ if (!show) return; /* convert mouse location to (x1,x2) coordinates */ if (style==NORMAL) { x1 = x1begb+(x1endb-x1begb)*(event.xmotion.x-x)/width; x2 = p2end+x2endb+(p2beg+x2begb-x2endb-p2end)* (event.xmotion.y-y)/height; } else { x1 = x1begb+(x1endb-x1begb)*(event.xmotion.y-y)/height; x2 = p2beg+x2begb+(p2end+x2endb-x2begb-p2beg)* (event.xmotion.x-x)/width; } /* draw string indicating mouse location */ sprintf(string,"(%0.3g,%0.3g)",x1,x2); XTextExtents(fs,string,strlen(string),&dummy,&dummy,&dummy,&overall); XDrawImageString(dpy,win,gc,xoffset,yoffset+overall.ascent, string,strlen(string));}void xMousePrint(Display *dpy, Window win, XEvent event, int style, FILE *mpicksfp, int x, int y, int width, int height, float x1begb, float x1endb, float x2begb, float x2endb){ float x1,x2; /* convert mouse location to (x1,x2) coordinates */ if (style==NORMAL) { x1 = x1begb+(x1endb-x1begb)*(event.xmotion.x-x)/width; x2 = x2endb+(x2begb-x2endb)*(event.xmotion.y-y)/height; } else { x1 = x1begb+(x1endb-x1begb)*(event.xmotion.y-y)/height; x2 = x2begb+(x2endb-x2begb)*(event.xmotion.x-x)/width; } /* write string indicating mouse location */ fprintf(mpicksfp, "%0.4g %0.4g\n", x1, x2);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -