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