[BACK]Return to plotX.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / pari / src / graph

Annotation of OpenXM_contrib/pari/src/graph/plotX.c, Revision 1.1.1.1

1.1       maekawa     1: /*******************************************************************/
                      2: /*                                                                 */
                      3: /*                       HIGH RESOLUTION PLOT                      */
                      4: /*                                                                 */
                      5: /*******************************************************************/
                      6: /* $Id: plotX.c,v 1.1.1.1 1999/09/16 13:47:43 karim Exp $ */
                      7: #include "pari.h"
                      8: #include "rect.h"
                      9: #include "../language/anal.h"
                     10:
                     11: void  free_graph(void);
                     12:
                     13: #ifdef HPPA
                     14: #  ifndef __GNUC__
                     15:      typedef char *caddr_t;
                     16: #  endif
                     17: #endif
                     18:
                     19: BEGINEXTERN
                     20: #include <X11/Xlib.h>
                     21: #include <X11/Xutil.h>
                     22: #include <X11/Xos.h>
                     23: ENDEXTERN
                     24:
                     25: static Colormap PARI_Colormap;
                     26: static XColor  *PARI_Colors;
                     27: static XColor  *PARI_ExactColors;
                     28:
                     29: static char *PARI_DefaultColors[MAX_COLORS] =
                     30: {
                     31:   " ",
                     32:   "black",    /* Default */
                     33:   "blue",     /* Axes */
                     34:   "sienna",   /* Odd numbered curves in ploth */
                     35:   "red",      /* Curves, or Even numbered curves in ploth */
                     36:   "cornsilk",
                     37:   "grey",
                     38:   "gainsboro",
                     39: };
                     40:
                     41: static void
                     42: PARI_ColorSetUp(Display *display, char **Colors, int n)
                     43: {
                     44:   static int init_done = 0;
                     45:   int i;
                     46:
                     47:   if (init_done) return;
                     48:   init_done=1;
                     49:
                     50:   PARI_Colormap = DefaultColormap(display, 0);
                     51:   PARI_Colors = (XColor *) gpmalloc((n+1) * sizeof(XColor));
                     52:   PARI_ExactColors = (XColor *) gpmalloc((n+1) * sizeof(XColor));
                     53:   for (i=1; i<n; i++)
                     54:     XAllocNamedColor(display, PARI_Colormap, Colors[i],
                     55:                     &PARI_ExactColors[i], &PARI_Colors[i]);
                     56: }
                     57:
                     58: #ifdef SONY
                     59: typedef int (*XErrorHandler) (
                     60: #  if NeedFunctionPrototypes
                     61:   Display*,
                     62:   XErrorEvent*
                     63: #  endif
                     64:   );
                     65:
                     66: extern XErrorHandler XSetErrorHandler (
                     67: #  if NeedFunctionPrototypes
                     68:   XErrorHandler
                     69: #  endif
                     70:   );
                     71:
                     72: typedef int (*XIOErrorHandler) (
                     73: #  if NeedFunctionPrototypes
                     74:   Display*
                     75: #  endif
                     76:   );
                     77:
                     78: extern XIOErrorHandler XSetIOErrorHandler (
                     79: #  if NeedFunctionPrototypes
                     80:   XIOErrorHandler
                     81: #  endif
                     82:   );
                     83: #endif
                     84:
                     85: /* after fork(), we don't want the child to recover but to exit */
                     86: static void
                     87: exiterr(char *str)
                     88: {
                     89:   term_color(c_ERR);
                     90:   fprintferr("\n  *** X fatal error: %s\n",str);
                     91:   term_color(c_NONE); exit(1);
                     92: }
                     93:
                     94: #define MAX_BUF 256
                     95:
                     96: static int
                     97: Xerror(Display *d, XErrorEvent *err) {
                     98:   char buf[MAX_BUF];
                     99:   XGetErrorText(d,err->error_code,buf,MAX_BUF);
                    100:   exiterr(buf); return 0;
                    101: }
                    102:
                    103: static int
                    104: IOerror(Display *d) {
                    105:   char buf[MAX_BUF];
                    106:   sprintf(buf, "lost display on %s", DisplayString(d));
                    107:   exiterr(buf); return 0;
                    108: }
                    109:
                    110: static char*
                    111: zmalloc(size_t x)
                    112: {
                    113:   return x? gpmalloc(x): NULL;
                    114: }
                    115:
                    116: void
                    117: rectdraw0(long *w, long *x, long *y, long lw, long do_free)
                    118: {
                    119:   long *ptx,*pty,*c;
                    120:   long *numpoints[MAX_COLORS],*numtexts[MAX_COLORS];
                    121:   long *xtexts[MAX_COLORS],*ytexts[MAX_COLORS];
                    122:   long rcolcnt[MAX_COLORS][ROt_MAX];
                    123:   long col,i,j,x0,y0,a,b,oldwidth,oldheight,force;
                    124:   long rcnt[ROt_MAX+1];
                    125:   char **texts[MAX_COLORS];
                    126:   PariRect *e;
                    127:   RectObj *p1;
                    128:   double xs=1,ys=1;
                    129:
                    130:   int screen;
                    131:   Display *display;
                    132:   GC gc;
                    133:   Window win;
                    134:   XEvent event;
                    135:   XSizeHints size_hints;
                    136:   XFontStruct *font_info;
                    137:   XSetWindowAttributes attrib;
                    138:   XPoint *points[MAX_COLORS],**lines[MAX_COLORS];
                    139:   XSegment *seg[MAX_COLORS];
                    140:   XRectangle *rec[MAX_COLORS];
                    141:   Atom wm_delete_window, wm_protocols;
                    142:
                    143:   if (fork()) return;  /* parent process returns */
                    144:
                    145:   /* child process goes on */
                    146:   freeall();  /* PARI stack isn't needed anymore, keep rectgraph */
                    147:   PARI_get_plot(1);
                    148:   display = XOpenDisplay(NULL);
                    149:   font_info = XLoadQueryFont(display, "9x15");
                    150:   if (!font_info) exiterr("cannot open 9x15 font");
                    151:
                    152:   XSetErrorHandler(Xerror);
                    153:   XSetIOErrorHandler(IOerror);
                    154:   PARI_ColorSetUp(display,PARI_DefaultColors,MAX_COLORS);
                    155:
                    156:   for(col=1;col<MAX_COLORS;col++)
                    157:   {
                    158:     rcolcnt[col][ROt_MV]=rcolcnt[col][ROt_PT]=rcolcnt[col][ROt_LN]=0;
                    159:     rcolcnt[col][ROt_BX]=rcolcnt[col][ROt_MP]=rcolcnt[col][ROt_ML]=0;
                    160:     rcolcnt[col][ROt_ST]=rcolcnt[col][ROt_PTT]=rcolcnt[col][ROt_PTS]=rcolcnt[col][ROt_LNT]=0;
                    161:   }
                    162:
                    163:   for(i=0;i<lw;i++)
                    164:   {
                    165:     e=rectgraph[w[i]]; p1=RHead(e);
                    166:     while(p1)
                    167:     {
                    168:       switch(RoType(p1))
                    169:       {
                    170:        case ROt_MP : rcolcnt[RoCol(p1)][ROt_PT] += RoMPcnt(p1);
                    171:                      break;                 /* Multiple Point */
                    172:        case ROt_PT :                        /* Point */
                    173:        case ROt_LN :                        /* Line */
                    174:        case ROt_BX :                        /* Box */
                    175:        case ROt_ML :                        /* Multiple lines */
                    176:        case ROt_ST : rcolcnt[RoCol(p1)][RoType(p1)]++;
                    177:                      break;                 /* String */
                    178:        case ROt_MV :                        /* Move */
                    179:        case ROt_PTT:                        /* Point type change */
                    180:        case ROt_PTS:                        /* Point size change */
                    181:        case ROt_LNT: rcnt[RoType(p1)]++;    /* Line type change */
                    182:       }
                    183:       p1=RoNext(p1);
                    184:     }
                    185:   }
                    186:   for (col=1; col<MAX_COLORS; col++)
                    187:   {
                    188:     char *m;
                    189:     c = rcolcnt[col];
                    190:     points[col]=(XPoint*)zmalloc(c[ROt_PT]*sizeof(XPoint));
                    191:     seg[col]=(XSegment*)zmalloc(c[ROt_LN]*sizeof(XSegment));
                    192:     rec[col]=(XRectangle*)zmalloc(c[ROt_BX]*sizeof(XRectangle));
                    193:
                    194:     i = c[ROt_ML]; m = zmalloc(i * (sizeof(long) + sizeof(XPoint*)));
                    195:     numpoints[col]=(long*)m; i *= sizeof(XPoint*);
                    196:     m += i; lines[col]=(XPoint**)m;
                    197:
                    198:     i = c[ROt_ST]; m = zmalloc(i * (sizeof(char*) + 3*sizeof(long)));
                    199:     texts[col]=(char**)m; i *= sizeof(long);
                    200:     m += i; numtexts[col]=(long*)m;
                    201:     m += i; xtexts[col]=(long*)m;
                    202:     m += i; ytexts[col]=(long*)m;
                    203:
                    204:     c[ROt_PT]=c[ROt_LN]=c[ROt_BX]=c[ROt_ML]=c[ROt_ST]=0;
                    205:   }
                    206:
                    207:   screen = DefaultScreen(display);
                    208:   win = XCreateSimpleWindow
                    209:     (display, RootWindow(display, screen), 0, 0, w_width, w_height,
                    210:      4, BlackPixel(display, screen), WhitePixel(display, screen));
                    211:
                    212:   size_hints.flags = PPosition | PSize;
                    213:   size_hints.x = 0;
                    214:   size_hints.y = 0;
                    215:   size_hints.width  = w_width;
                    216:   size_hints.height = w_height;
                    217:   XSetStandardProperties
                    218:     (display, win, "rectplot", NULL, None, NULL, 0, &size_hints);
                    219:
                    220:   wm_delete_window = XInternAtom(display, "WM_DELETE_WINDOW", False);
                    221:   wm_protocols = XInternAtom(display, "WM_PROTOCOLS", False);
                    222:   XSetWMProtocols(display,win,&wm_delete_window, 1);
                    223:
                    224:   XSelectInput (display, win,
                    225:     ExposureMask | ButtonPressMask | StructureNotifyMask);
                    226:
                    227:   /* enable backing-store */
                    228:   attrib.backing_store = Always;
                    229:   attrib.backing_planes = AllPlanes;
                    230:   XChangeWindowAttributes(display,win,CWBackingStore|CWBackingPlanes,&attrib);
                    231:
                    232:   gc = XCreateGC(display, win, 0, NULL);
                    233:   XSetFont(display, gc, font_info->fid);
                    234:
                    235:   XMapWindow(display, win);
                    236:   oldwidth  = w_width;
                    237:   oldheight = w_height; force = 1;
                    238:
                    239:   for(;;)
                    240:   {
                    241:     XNextEvent(display, &event);
                    242:     switch(event.type)
                    243:     {
                    244:       case ClientMessage:
                    245:         if (event.xclient.message_type != wm_protocols ||
                    246:             event.xclient.data.l[0] != wm_delete_window) break;
                    247:       case ButtonPress:
                    248:       case DestroyNotify:
                    249:        XUnloadFont(display,font_info->fid); XFreeGC(display,gc);
                    250: #define myfree(x) if (x) free(x)
                    251:        for(col=1;col<MAX_COLORS;col++)
                    252:        {
                    253:          myfree(points[col]); myfree(seg[col]); myfree(rec[col]);
                    254:          for(i=0;i<rcolcnt[col][ROt_ML];i++) myfree(lines[col][i]);
                    255:          myfree(numpoints[col]); myfree(texts[col]);
                    256:         }
                    257: #undef myfree
                    258:        free_graph(); if (do_free) { free(w); free(x); free(y); }
                    259:        XCloseDisplay(display); exit(0);
                    260:
                    261:       case ConfigureNotify:
                    262:       {
                    263:         int width  = event.xconfigure.width;
                    264:         int height = event.xconfigure.height;
                    265:
                    266:         if (width == oldwidth && height == oldheight) break;
                    267:         oldwidth  = width;
                    268:         oldheight = height; force = 1;
                    269:
                    270:         /* recompute scale */
                    271:        xs=((double)width)/w_width; ys=((double)height)/w_height;
                    272:       }
                    273:       case Expose: if (!force) break;
                    274:         force = 0;
                    275:        for(i=0; i<lw; i++)
                    276:        {
                    277:          e=rectgraph[w[i]];p1=RHead(e);x0=x[i];y0=y[i];
                    278:          while(p1)
                    279:          {
                    280:            col=RoCol(p1); c=rcolcnt[col];
                    281:            switch(RoType(p1))
                    282:            {
                    283:              case ROt_PT:
                    284:                points[col][c[ROt_PT]].x=(long)((RoPTx(p1)+x0)*xs);
                    285:                points[col][c[ROt_PT]].y=(long)((RoPTy(p1)+y0)*ys);
                    286:                c[ROt_PT]++;break;
                    287:              case ROt_LN:
                    288:                seg[col][c[ROt_LN]].x1= (long)((RoLNx1(p1)+x0)*xs);
                    289:                seg[col][c[ROt_LN]].y1= (long)((RoLNy1(p1)+y0)*ys);
                    290:                seg[col][c[ROt_LN]].x2= (long)((RoLNx2(p1)+x0)*xs);
                    291:                seg[col][c[ROt_LN]].y2= (long)((RoLNy2(p1)+y0)*ys);
                    292:                c[ROt_LN]++;break;
                    293:              case ROt_BX:
                    294:                a=rec[col][c[ROt_BX]].x = (long)((RoBXx1(p1)+x0)*xs);
                    295:                b=rec[col][c[ROt_BX]].y = (long)((RoBXy1(p1)+y0)*ys);
                    296:                rec[col][c[ROt_BX]].width = (long)((RoBXx2(p1)+x0-a)*xs);
                    297:                rec[col][c[ROt_BX]].height = (long)((RoBXy2(p1)+y0-b)*ys);
                    298:                c[ROt_BX]++;break;
                    299:              case ROt_MP:
                    300:                ptx=RoMPxs(p1); pty=RoMPys(p1);
                    301:                for(j=0;j<RoMPcnt(p1);j++)
                    302:                {
                    303:                  points[col][c[ROt_PT]+j].x= (long)((ptx[j]+x0)*xs);
                    304:                  points[col][c[ROt_PT]+j].y= (long)((pty[j]+y0)*ys);
                    305:                }
                    306:                c[ROt_PT]+=RoMPcnt(p1);break;
                    307:              case ROt_ML:
                    308:                ptx=RoMLxs(p1); pty=RoMLys(p1);
                    309:                numpoints[col][c[ROt_ML]] = RoMLcnt(p1);
                    310:                lines[col][c[ROt_ML]] =
                    311:                  (XPoint*)zmalloc(RoMLcnt(p1)*sizeof(XPoint));
                    312:                for(j=0;j<RoMLcnt(p1);j++)
                    313:                {
                    314:                  lines[col][c[ROt_ML]][j].x= (long)((ptx[j]+x0)*xs);
                    315:                  lines[col][c[ROt_ML]][j].y= (long)((pty[j]+y0)*ys);
                    316:                }
                    317:                c[ROt_ML]++;break;
                    318:              case ROt_ST:
                    319:                texts[col][c[ROt_ST]]=RoSTs(p1);
                    320:                numtexts[col][c[ROt_ST]]=RoSTl(p1);
                    321:                xtexts[col][c[ROt_ST]]= (long)((RoSTx(p1)+x0)*xs);
                    322:                ytexts[col][c[ROt_ST]]= (long)((RoSTy(p1)+y0)*ys);
                    323:                c[ROt_ST]++;break;
                    324:              default: break;
                    325:            }
                    326:            p1=RoNext(p1);
                    327:          }
                    328:        }
                    329:        for(col=1; col<MAX_COLORS; col++)
                    330:        {
                    331:           c = rcolcnt[col];
                    332:          XSetForeground(display, gc, PARI_Colors[col].pixel);
                    333:          if(c[ROt_PT]) XDrawPoints(display,win,gc,points[col],c[ROt_PT],0);
                    334:          if(c[ROt_LN]) XDrawSegments(display,win,gc,seg[col],c[ROt_LN]);
                    335:          if(c[ROt_BX]) XDrawRectangles(display,win,gc,rec[col],c[ROt_BX]);
                    336:          for(i=0;i<c[ROt_ML];i++)
                    337:            XDrawLines(display,win,gc,lines[col][i],numpoints[col][i],0);
                    338:          for(i=0;i<c[ROt_ST];i++)
                    339:            XDrawString(display,win,gc, xtexts[col][i],ytexts[col][i],
                    340:               texts[col][i],numtexts[col][i]);
                    341:
                    342:          c[ROt_PT]=c[ROt_LN]=c[ROt_BX]=c[ROt_ML]=c[ROt_ST]=0;
                    343:         }
                    344:     }
                    345:   }
                    346: }
                    347:
                    348: void
                    349: PARI_get_plot(long fatal)
                    350: {
                    351:   Display *display;
                    352:   int screen;
                    353:
                    354:   if (pari_plot.init) return;
                    355:   if (!(display = XOpenDisplay(NULL)))
                    356:   {
                    357:     if (fatal) exiterr("no X server");
                    358:     err(talker, "no X server");
                    359:   }
                    360:   screen = DefaultScreen(display);
                    361:   w_width = DisplayWidth(display, screen) - 40;
                    362:   w_height = DisplayHeight(display, screen) - 60;
                    363:   f_height = 15; f_width = 9;
                    364:   h_unit = 5; v_unit = 5;
                    365:   pari_plot.init = 1;
                    366:   XCloseDisplay(display);
                    367: }
                    368:
                    369: long
                    370: term_set(char *s) { return 1; }
                    371:
                    372: long
                    373: plot_outfile_set(char *s) { return 1; }
                    374:
                    375: void
                    376: set_pointsize(double d)
                    377: {
                    378: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>