ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/meta/x11plot.c
Revision: 1.1
Committed: Sat Feb 22 02:07:26 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2     static const char RCSid[] = "$Id$";
3     #endif
4     /*
5     * X window plotting functions
6     *
7     * 2/26/86
8     */
9    
10    
11     #include "meta.h"
12    
13     #include "plot.h"
14    
15     #undef TRUE
16    
17     #undef FALSE
18    
19     #include <X11/Xlib.h>
20    
21    
22     #define BORWIDTH 5
23    
24     #define BlackPix BlackPixel(dpy,0 )
25     #define WhitePix WhitePixel(dpy,0 )
26    
27     #define mapx(x) CONV(x,dxsize)
28     #define mapy(y) CONV((XYSIZE-1)-(y),dysize)
29    
30     #define MAXVERT 128
31    
32     #define MAXCOLOR 65535
33    
34     /* X window fonts */
35     static struct {
36     char *name; /* font name */
37     Font id; /* font id */
38     } font[] = {
39     {"9x15", 0},
40     {"8x13", 0},
41     {"6x13", 0},
42     {"vtdwidth", 0},
43     {"accordbfx", 0}
44     };
45    
46     /* map to our matrix string types */
47     static int fontmap[16] = {0,3,0,3,1,3,1,3,2,4,2,4,2,4,2,4};
48    
49     #define LONG_DASHED_LIST_LENGTH 2
50     #define SHORT_DASHED_LIST_LENGTH 2
51     #define DOTTED_LIST_LENGTH 2
52    
53     int dash_list_length[] = {
54     0,
55     LONG_DASHED_LIST_LENGTH,
56     SHORT_DASHED_LIST_LENGTH,
57     DOTTED_LIST_LENGTH,
58     };
59    
60     unsigned char long_dashed[LONG_DASHED_LIST_LENGTH] = {4,7};
61     unsigned char short_dashed[SHORT_DASHED_LIST_LENGTH] = {4,3};
62     unsigned char dotted[DOTTED_LIST_LENGTH] = {5,2};
63    
64     unsigned char *Linetypes[] = {
65     0,
66     long_dashed,
67     short_dashed,
68     dotted,
69     };
70    
71     Display *dpy;
72     Window wind; /* our window */
73     Colormap cmap;
74     GC gc;
75     Font curfont; /* current font */
76     int curwidth; /* current width of lines */
77     int curfill; /* current fill style (FillTiled or FillSolid) */
78     int curcol; /* current color */
79     int curlinetype; /* current line style */
80     XGCValues gcval;
81     int pixel[4];
82     int dxsize, dysize; /* window size */
83     int debug = False; /* use XSynchronize if true */
84    
85     init(name, geom) /* initialize window */
86     char *name;
87     char *geom;
88     {
89     char defgeom[32];
90     XColor cdef,dum;
91     XEvent evnt;
92     int dummy;
93    
94     curfont = curfill = curcol = -1;
95     curlinetype = 0;
96    
97     if ((dpy=XOpenDisplay("")) == NULL)
98     error(SYSTEM, "can't open display");
99    
100     /* for debugging so we don't have to flush always */
101     if (debug)
102     (void) XSynchronize(dpy, True);
103    
104     dxsize = DisplayWidth(dpy,0) - 2*BORWIDTH-4;
105     dysize = DisplayHeight(dpy,0) - 2*BORWIDTH-26;
106    
107     /* temporary */
108     dxsize = dxsize = 800;
109     adjustsize();
110    
111     sprintf(defgeom, "=%dx%d+2+25", dxsize, dysize);
112     /* XUseGeometry(dpy,0,geom,defgeom,BORWIDTH,100,100,100,100,
113     &xoff,&yoff,&dxsize,&dysize); */
114     gc = DefaultGC(dpy,0); /* get default gc */
115     cmap = DefaultColormap(dpy,0); /* and colormap */
116    
117     wind = XCreateSimpleWindow(dpy,DefaultRootWindow(dpy),0,0,dxsize,dysize,
118     BORWIDTH,BlackPix,WhitePix);
119     if (wind == 0)
120     error(SYSTEM, "can't create window");
121     XStoreName(dpy, wind, name);
122     XMapRaised(dpy, wind);
123     XSelectInput(dpy, wind, StructureNotifyMask | ButtonPressMask | ExposureMask);
124     if (DisplayPlanes(dpy,0) < 2) { /* less than 2 color planes, use black */
125     pixel[0] = pixel[1] = pixel[2] = pixel[3] = BlackPix;
126     } else {
127     if (XAllocNamedColor(dpy, cmap, "black", &dum, &cdef)==0)
128     error(USER,"cannot allocate black!");
129     pixel[0] = cdef.pixel;
130     if (XAllocNamedColor(dpy, cmap, "red", &dum, &cdef)==0)
131     {
132     error(WARNING,"cannot allocate red");
133     cdef.pixel = BlackPix;
134     }
135     pixel[1] = cdef.pixel;
136     if (XAllocNamedColor(dpy, cmap, "green", &dum, &cdef)==0)
137     {
138     error(WARNING,"cannot allocate green");
139     cdef.pixel = BlackPix;
140     }
141     pixel[2] = cdef.pixel;
142     if (XAllocNamedColor(dpy, cmap, "blue", &dum, &cdef)==0)
143     {
144     error(WARNING,"cannot allocate blue");
145     cdef.pixel = BlackPix;
146     }
147     pixel[3] = cdef.pixel;
148     }
149    
150     while (1)
151     {
152     XNextEvent(dpy, &evnt);
153     if (evnt.type == ConfigureNotify) /* wait for first ConfigureNotify */
154     break;
155     }
156     dxsize = evnt.xconfigure.width;
157     dysize = evnt.xconfigure.height;
158     adjustsize();
159     while (1)
160     {
161     XNextEvent(dpy, &evnt);
162     if (evnt.type == Expose) /* wait for first Expose */
163     break;
164     }
165     }
166    
167     adjustsize()
168     {
169     if (dxsize > dysize)
170     dxsize = dysize;
171     else
172     dysize = dxsize;
173     }
174    
175     endpage() /* end of this graph */
176     {
177     XEvent evnt;
178     int quit=False;
179    
180     XFlush(dpy);
181     XBell(dpy, 0);
182     while ( !quit ) {
183     XNextEvent(dpy, &evnt);
184     switch (evnt.type) {
185     case ConfigureNotify:
186     dxsize = evnt.xconfigure.width;
187     dysize = evnt.xconfigure.height;
188     adjustsize();
189     break;
190     case Expose:
191     replay((int)((long)XYSIZE*evnt.xexpose.x/dxsize),
192     (int)((long)XYSIZE*(dysize-evnt.xexpose.y-evnt.xexpose.height)/dysize),
193     (int)((long)XYSIZE*(evnt.xexpose.x+evnt.xexpose.width)/dxsize),
194     (int)((long)XYSIZE*(dysize-evnt.xexpose.y)/dysize));
195     break;
196     case ButtonPress:
197     quit = True;
198     break;
199     } /* switch */
200     } /* for */
201     XClearWindow(dpy, wind);
202    
203     }
204    
205    
206    
207     printstr(p) /* output a string */
208    
209     register PRIMITIVE *p;
210    
211     {
212     int fn;
213     int col;
214    
215     fn = fontmap[(p->arg0 >> 2) & 017]; /* get font number */
216     /* set font */
217     if (font[fn].id == 0)
218     {
219     font[fn].id = XLoadFont(dpy, font[fn].name);
220     if (font[fn].id == 0)
221     {
222     sprintf(errmsg, "can't open font \"%s\"", font[fn].name);
223     error(SYSTEM, errmsg);
224     }
225     }
226     col = p->arg0 & 03;
227     if (curcol != col)
228     {
229     curcol = col;
230     XSetForeground(dpy, gc, pixel[col]);
231     }
232     if (curfont != font[fn].id)
233     {
234     curfont = font[fn].id;
235     XSetFont(dpy, gc, curfont);
236     }
237    
238     XDrawImageString(dpy, wind, gc, mapx(p->xy[XMN]), mapy(p->xy[YMN]),
239     p->args, strlen(p->args));
240     }
241    
242    
243    
244    
245    
246     plotlseg(p) /* plot a line segment */
247    
248     register PRIMITIVE *p;
249    
250     {
251     int x1, y1, x2, y2;
252     int linetype, ps, pw;
253     int col;
254    
255     linetype = (p->arg0 >> 4) & 03; /* line style (solid, dashed, etc) */
256    
257     col = p->arg0 & 03; /* color */
258    
259     ps = WIDTH((p->arg0 >> 2) & 03);
260     pw = CONV((ps)/2, dxsize);
261    
262     x1 = mapx(p->xy[XMN]);
263     x2 = mapx(p->xy[XMX]);
264     if (p->arg0 & 0100) { /* reverse slope */
265     y1 = mapy(p->xy[YMX]);
266     y2 = mapy(p->xy[YMN]);
267     } else {
268     y1 = mapy(p->xy[YMN]);
269     y2 = mapy(p->xy[YMX]);
270     }
271    
272     if (curcol != col || curwidth != pw)
273     {
274     curcol = col;
275     gcval.foreground = pixel[col];
276     curwidth = pw;
277     gcval.line_width = pw*2+1; /* convert to thickness in pixels */
278     gcval.join_style = JoinRound;
279     XChangeGC(dpy, gc, GCJoinStyle|GCLineWidth|GCForeground, &gcval);
280     }
281     if (curlinetype != linetype)
282     {
283     curlinetype = linetype;
284     if (linetype==0)
285     gcval.line_style = LineSolid;
286     else
287     {
288     gcval.line_style = LineOnOffDash;
289     XSetDashes(dpy, gc, 0, Linetypes[linetype], dash_list_length[linetype]);
290     }
291     XChangeGC(dpy, gc, GCLineStyle, &gcval);
292     }
293     XDrawLine(dpy,wind,gc,x1,y1,x2,y2);
294     }
295    
296    
297     #ifdef nyet
298    
299     fillrect(p) /* fill a rectangle */
300    
301     register PRIMITIVE *p;
302    
303     {
304     int left, right, top, bottom;
305    
306     left = mapx(p->xy[XMN]);
307     bottom = mapy(p->xy[YMN]);
308     right = mapx(p->xy[XMX]);
309     top = mapy(p->xy[YMX]);
310    
311     fill(left, top, right, bottom, pixpat(pati[(p->arg0>>2)&03],p->arg0&03));
312    
313     }
314    
315    
316    
317    
318     filltri(p) /* fill a triangle */
319    
320     register PRIMITIVE *p;
321    
322     {
323     Vertex v[4];
324     int left, right, top, bottom;
325    
326     left = mapx(p->xy[XMN]);
327     right = mapx(p->xy[XMX]);
328     top = mapy(p->xy[YMX]);
329     bottom = mapy(p->xy[YMN]);
330    
331     switch (p->arg0 & 060) {
332     case 0: /* right (& down) */
333     v[0].x = left; v[0].y = bottom;
334     v[1].x = right; v[1].y = bottom;
335     v[2].x = right; v[2].y = top;
336     break;
337     case 020: /* up */
338     v[0].x = right; v[0].y = bottom;
339     v[1].x = right; v[1].y = top;
340     v[2].x = left; v[2].y = top;
341     break;
342     case 040: /* left */
343     v[0].x = right; v[0].y = top;
344     v[1].x = left; v[1].y = top;
345     v[2].x = left; v[2].y = bottom;
346     break;
347     case 060: /* down */
348     v[0].x = left; v[0].y = top;
349     v[1].x = left; v[1].y = bottom;
350     v[2].x = right; v[2].y = bottom;
351     break;
352     }
353     v[3].x = v[0].x; v[3].y = v[0].y;
354     v[0].flags = v[1].flags = v[2].flags = v[3].flags = 0;
355     if (curfill != FillTiled)
356     {
357     XSetFillStyle(dpy, gc, FillTiled);
358     curfill = FillTiled;
359     XSetTile(dpy, gc, pixpat(pati[(p->arg0>>2)&03],p->arg0&03));
360     }
361     XDrawFIlled(dpy, wind, gc, v, 4);
362     }
363    
364    
365    
366     xform(xp, yp, p) /* transform a point according to p */
367    
368     register int *xp, *yp;
369     register PRIMITIVE *p;
370    
371     {
372     int x, y;
373    
374     switch (p->arg0 & 060) {
375     case 0: /* right */
376     x = *xp;
377     y = *yp;
378     break;
379     case 020: /* up */
380     x = (XYSIZE-1) - *yp;
381     y = *xp;
382     break;
383     case 040: /* left */
384     x = (XYSIZE-1) - *xp;
385     y = (XYSIZE-1) - *yp;
386     break;
387     case 060: /* down */
388     x = *yp;
389     y = (XYSIZE-1) - *xp;
390     break;
391     }
392    
393     *xp = CONV(x, p->xy[XMX] - p->xy[XMN]) + p->xy[XMN];
394     *yp = CONV(y, p->xy[YMX] - p->xy[YMN]) + p->xy[YMN];
395    
396     }
397    
398    
399    
400     fillpoly(p) /* fill a polygon */
401    
402     register PRIMITIVE *p;
403    
404     {
405     int x0, y0, curx, cury;
406     PolyHandle polyh;
407     char *nextscan();
408     register char *s;
409    
410     polyh = OpenPoly();
411    
412     if ((s = nextscan(nextscan(p->args, "%d", &x0), "%d", &y0)) == NULL)
413     error(USER, "illegal polygon spec in fillpoly");
414    
415     xform(&x0, &y0, p);
416     x0 = mapx(x0); y0 = mapy(y0);
417     MoveTo(x0, y0);
418    
419     while ((s = nextscan(nextscan(s, "%d", &curx), "%d", &cury)) != NULL) {
420     xform(&curx, &cury, p);
421     curx = mapx(curx); cury = mapy(cury);
422     LineTo(curx, cury);
423     }
424     LineTo(x0, y0);
425    
426     ClosePoly();
427    
428     if (p->arg0 & 0100) { /* draw border */
429     if (curpat != 0) {
430     PenPat(macpat[0]);
431     curpat = 0;
432     }
433     if (curpsiz != 1) {
434     PenSize(1, 1);
435     curpsiz = 1;
436     }
437     if (curpmod != patOr) {
438     PenMode(patOr);
439     curpmod = patOr;
440     }
441     FramePoly(polyh);
442     }
443    
444     setfill(p->arg0 & 077);
445     PaintPoly(polyh);
446     KillPoly(polyh);
447     xpos = -1;
448     ypos = -1;
449    
450     }
451    
452     fill(xmin,ymin,xmax,ymax,pm)
453     int xmin,ymin,xmax,ymax;
454     Pixmap pm;
455     {
456     if (pm != 0 && curpat != pm)
457     {
458     XSetTile(dpy, gc, pm);
459     curpat = pm;
460     }
461     XFillRectangle(dpy, wind, gc, xmin, ymin, xmax-xmin+1, ymax-ymin+1);
462     }
463    
464     #else
465    
466     filltri() {}
467     fillpoly() {}
468     fillrect() {}
469    
470    
471     #endif