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