📄 axes.c
字号:
float dnum,fnum,dtic,amin,amax,base,scale,anum,atic,azero; float d1num=0.0,f1num=0.0,d2num=0.0,f2num=0.0; char str[256],dash[2],*label; /* if not visible, then simply return */ if (!w->core.visible) return; /* call callbacks before drawing axes (so grid will be on top) */ fillCallbackStruct(w,XtcwpCR_EXPOSE,event,region,&cb); XtCallCallbacks ((Widget) w,XtNexposeCallback,&cb); /* create GCs */ gca = XCreateGC(dpy,win,0,&values); gcg = XCreateGC(dpy,win,0,&values); gct = XCreateGC(dpy,win,0,&values); /* set colors */ XSetForeground(dpy,gca,axescolor); XSetForeground(dpy,gcg,gridcolor); XSetForeground(dpy,gct,titlecolor); /* set fonts and determine character dimensions */ XSetFont(dpy,gca,labelfont); XSetFont(dpy,gct,titlefont); fa = XQueryFont(dpy,labelfont); XTextExtents(fa,"2",1,&dummy,&dummy,&dummy,&cs); labelca = cs.ascent; labelcd = cs.descent; labelch = labelca+labelcd; labelcw = cs.width; ft = XQueryFont(dpy,titlefont); titleca = ft->max_bounds.ascent; titlech = ft->max_bounds.ascent+ft->max_bounds.descent; /* determine tic size */ ticsize = labelcw; /* determine numbered tic intervals */ n1num = (style==XtcwpNORMAL ? width : height)/(8*labelcw); scaxis(x1beg,x1end,&n1num,&d1num,&f1num); n2num = (style==XtcwpNORMAL ? height : width)/(8*labelcw); scaxis(x2beg,x2end,&n2num,&d2num,&f2num); /* draw horizontal axis */ if (style==XtcwpNORMAL) { amin = (x1beg<x1end)?x1beg:x1end; amax = (x1beg>x1end)?x1beg:x1end; dnum = d1num; fnum = f1num; ntic = n1tic; scale = width/(x1end+p1end-x1beg-p1beg); base = x-scale*(x1beg+p1beg); ya = y+height; ticb = ticsize; numb = ticb+labelca+ticsize/4; labelb = numb+labelch+ticsize/4; grid = grid1; label = label1; } else { amin = (x2beg<x2end)?x2beg:x2end; amax = (x2beg>x2end)?x2beg:x2end; dnum = d2num; fnum = f2num; ntic = n2tic; scale = width/(x2end+p2end-x2beg-p2beg); base = x-scale*(x2beg+p2beg); ya = y; ticb = -ticsize; numb = ticb-labelcd-ticsize/4; labelb = numb-labelch-ticsize/4; grid = grid2; label = label2; } if (grid==XtcwpSOLID) { XSetLineAttributes(dpy,gcg,1L,LineSolid,CapButt,JoinMiter); grided = True; } else if (grid==XtcwpDASH) { grided = True; XSetLineAttributes(dpy,gcg,1L,LineOnOffDash,CapButt,JoinMiter); dash[0] = 8; dash[1] = 4; XSetDashes(dpy,gcg,0,dash,2); } else if (grid==XtcwpDOT) { grided = True; XSetLineAttributes(dpy,gcg,1L,LineOnOffDash,CapButt,JoinMiter); dash[0] = 1; dash[1] = 4; XSetDashes(dpy,gcg,0,dash,2); } else grided = False; azero = 0.0001*(amax-amin); for (anum=fnum; anum<=amax; anum+=dnum) { if (anum<amin) continue; xa = base+scale*anum; if (grided) XDrawLine(dpy,win,gcg,xa,y,xa,y+height); XDrawLine(dpy,win,gca,xa,ya,xa,ya+ticb); if (anum>-azero && anum<azero) sprintf(str,"%1.5g",0.0); else sprintf(str,"%1.5g",anum); lstr = strlen(str); tw = XTextWidth(fa,str,(int)lstr); XDrawString(dpy,win,gca,xa-tw/2,ya+numb,str,(int)lstr); } dtic = dnum/ntic; for (atic=fnum-ntic*dtic-dtic; atic<=amax; atic+=dtic) { if (atic<amin) continue; xa = base+scale*atic; XDrawLine(dpy,win,gca,xa,ya,xa,ya+ticb/2); } lstr = strlen(label); tw = XTextWidth(fa,label,(int)lstr); XDrawString(dpy,win,gca,x+width/2-tw/2,ya+labelb,label,(int)lstr); /* draw vertical axis */ if (style==XtcwpNORMAL) { amin = (x2beg<x2end)?x2beg:x2end; amax = (x2beg>x2end)?x2beg:x2end; dnum = d2num; fnum = f2num; ntic = n2tic; scale = -height/(x2end+p2end-x2beg-p2beg); base = y+height-scale*(x2beg+p2beg); grid = grid2; label = label2; } else { amin = (x1beg<x1end)?x1beg:x1end; amax = (x1beg>x1end)?x1beg:x1end; dnum = d1num; fnum = f1num; ntic = n1tic; scale = height/(x1end+p1end-x1beg-p1beg); base = y-scale*(x1beg+p1beg); grid = grid1; label = label1; } xa = x; ticb = -ticsize; numb = ticb-ticsize/4; if (grid==XtcwpSOLID) { XSetLineAttributes(dpy,gcg,1L,LineSolid,CapButt,JoinMiter); grided = True; } else if (grid==XtcwpDASH) { grided = True; XSetLineAttributes(dpy,gcg,1L,LineOnOffDash,CapButt,JoinMiter); dash[0] = 8; dash[1] = 4; XSetDashes(dpy,gcg,0,dash,2); } else if (grid==XtcwpDOT) { grided = True; XSetLineAttributes(dpy,gcg,1L,LineOnOffDash,CapButt,JoinMiter); dash[0] = 1; dash[1] = 4; XSetDashes(dpy,gcg,0,dash,2); } else grided = False; azero = 0.0001*(amax-amin); for (anum=fnum; anum<=amax; anum+=dnum) { if (anum<amin) continue; ya = base+scale*anum; if (grided) XDrawLine(dpy,win,gcg,x,ya,x+width,ya); XDrawLine(dpy,win,gca,xa,ya,xa+ticb,ya); if (anum>-azero && anum<azero) sprintf(str,"%1.5g",0.0); else sprintf(str,"%1.5g",anum); lstr = strlen(str); tw = XTextWidth(fa,str,(int)lstr); XDrawString(dpy,win,gca,xa+numb-tw,ya+labelca/4,str,(int)lstr); } dtic = dnum/ntic; for (atic=fnum-ntic*dtic-dtic; atic<=amax; atic+=dtic) { if (atic<amin) continue; ya = base+scale*atic; XDrawLine(dpy,win,gca,xa,ya,xa+ticb/2,ya); } lstr = strlen(label); tw = XTextWidth(fa,label,(int)lstr); xa = x+ticb-5*labelcw-ticsize/2; if (xa<labelch+1) xa = labelch+1; XtcwpDrawString90(dpy,win,gca,xa,y+height/2+tw/2,label,(int)lstr); /* draw title */ lstr = strlen(title); tw = XTextWidth(ft,title,(int)lstr); if (style==XtcwpNORMAL) { XClearArea(dpy,win,0,y+labelca/4-labelch-labelch-titleca, w->core.width,titlech,False); XDrawString(dpy,win,gct, x+width/2-tw/2, y+labelca/4-labelch-labelch,title,(int)lstr); } else { XClearArea(dpy,win,0,y+height+labelca/4+labelch, w->core.width,titlech,False); XDrawString(dpy,win,gct, x+width/2-tw/2, y+height+labelca/4+labelch+titleca,title,(int)lstr); } /* draw axes box */ XDrawRectangle(dpy,win,gca,x,y,width,height); /* free GCs and font info */ XFreeGC(dpy,gca); XFreeGC(dpy,gcg); XFreeGC(dpy,gct); XFreeFontInfo(NULL,fa,1); XFreeFontInfo(NULL,ft,1);}static Boolean SetValues (XtcwpAxesWidget current, XtcwpAxesWidget request, XtcwpAxesWidget new_widget){ Boolean redraw = FALSE; if(redraw) { XtcwpAxesWidget junk; /* keep compiler happy */ junk = current; junk = request; junk = new_widget; junk = junk; } return redraw; } /* utilities */static void fillCallbackStruct (XtcwpAxesWidget w, int reason, XEvent *event, Region region, XtcwpAxesCallbackStruct *cb){ cb->reason = reason; cb->event = event; cb->region = region; cb->x = w->axes.x; cb->y = w->axes.y; cb->width = w->axes.width; cb->height = w->axes.height; cb->x1beg = w->axes.x1beg; cb->x1end = w->axes.x1end; cb->x2beg = w->axes.x2beg; cb->x2end = w->axes.x2end; cb->p1beg = w->axes.p1beg; cb->p1end = w->axes.p1end; cb->p2beg = w->axes.p2beg; cb->p2end = w->axes.p2end; cb->style = w->axes.style;} /* action procedures */static void inputAxes (XtcwpAxesWidget w, XEvent *event, char *args[], int nargs){ XtcwpAxesCallbackStruct cb; args += 0*(nargs); /* keep compiler happy */ /* call callback */ fillCallbackStruct(w,XtcwpCR_INPUT,event,NULL,&cb); XtCallCallbacks ((Widget) w,XtNinputCallback,&cb);} /* public functions */Boolean XtcwpPointInAxesRectangle (Widget w, Position x, Position y){ XtcwpAxesWidget aw=(XtcwpAxesWidget)w; Position xa=aw->axes.x,ya=aw->axes.y; Dimension wa=aw->axes.width,ha=aw->axes.height; return ((((int) x)>=((int) xa)) && (((int) x)<=((int) (xa+wa))) && (((int) y)>= ((int) ya)) && (((int) y)<=((int) (ya+ha)))); /* return (x>=xa && x<=xa+wa && y>=ya && y<=ya+ha); */}void XtcwpSetAxesValues (Widget w, float x1beg, float x1end, float x2beg, float x2end){ XtcwpAxesWidget aw=(XtcwpAxesWidget)w; aw->axes.x1beg = x1beg; aw->axes.x1end = x1end; aw->axes.x2beg = x2beg; aw->axes.x2end = x2end;}void XtcwpSetAxesPads (Widget w, float p1beg, float p1end, float p2beg, float p2end){ XtcwpAxesWidget aw=(XtcwpAxesWidget)w; aw->axes.p1beg = p1beg; aw->axes.p1end = p1end; aw->axes.p2beg = p2beg; aw->axes.p2end = p2end;}/* resource type converters */static void XtcwpStringToAxesGrid (XrmValue *args, int *nargs, XrmValue *fromVal, XrmValue *toVal){ static int result; char *string=fromVal->addr; args += 0*(*nargs); /* keep compiler happy */ /* convert axes grid string in fromVal to int in toVal */ if (strcmp(string,"none")==0) result = XtcwpNONE; else if (strcmp(string,"dot")==0) result = XtcwpDOT; else if (strcmp(string,"dash")==0) result = XtcwpDASH; else if (strcmp(string,"solid")==0) result = XtcwpSOLID; else { result = XtcwpNONE; XtWarning("Invalid AxesGrid specification!"); } toVal->size = sizeof(int); toVal->addr = (char *)&result;}static void XtcwpStringToAxesStyle (XrmValue *args, int *nargs, XrmValue *fromVal, XrmValue *toVal){ static int result; char *string=fromVal->addr; args += 0*(*nargs); /* keep compiler happy */ /* convert axes style string in fromVal to int in toVal */ if (strcmp(string,"normal")==0) result = XtcwpNORMAL; else if (strcmp(string,"seismic")==0) result = XtcwpSEISMIC; else { result = XtcwpNORMAL; XtWarning("Invalid AxesStyle specification!"); } toVal->size = sizeof(int); toVal->addr = (char *)&result;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -