1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: x11plot.c,v 1.1 2003/02/22 02:07:26 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 |
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 |
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 |
#ifdef nyet |
301 |
|
302 |
static void |
303 |
fill(xmin,ymin,xmax,ymax,pm) |
304 |
int xmin,ymin,xmax,ymax; |
305 |
Pixmap pm; |
306 |
{ |
307 |
if (pm != 0 && curpat != pm) |
308 |
{ |
309 |
XSetTile(dpy, gc, pm); |
310 |
curpat = pm; |
311 |
} |
312 |
XFillRectangle(dpy, wind, gc, xmin, ymin, xmax-xmin+1, ymax-ymin+1); |
313 |
} |
314 |
|
315 |
fillrect(p) /* fill a rectangle */ |
316 |
|
317 |
register PRIMITIVE *p; |
318 |
|
319 |
{ |
320 |
int left, right, top, bottom; |
321 |
|
322 |
left = mapx(p->xy[XMN]); |
323 |
bottom = mapy(p->xy[YMN]); |
324 |
right = mapx(p->xy[XMX]); |
325 |
top = mapy(p->xy[YMX]); |
326 |
|
327 |
fill(left, top, right, bottom, pixpat(pati[(p->arg0>>2)&03],p->arg0&03)); |
328 |
|
329 |
} |
330 |
|
331 |
|
332 |
|
333 |
|
334 |
filltri(p) /* fill a triangle */ |
335 |
|
336 |
register PRIMITIVE *p; |
337 |
|
338 |
{ |
339 |
Vertex v[4]; |
340 |
int left, right, top, bottom; |
341 |
|
342 |
left = mapx(p->xy[XMN]); |
343 |
right = mapx(p->xy[XMX]); |
344 |
top = mapy(p->xy[YMX]); |
345 |
bottom = mapy(p->xy[YMN]); |
346 |
|
347 |
switch (p->arg0 & 060) { |
348 |
case 0: /* right (& down) */ |
349 |
v[0].x = left; v[0].y = bottom; |
350 |
v[1].x = right; v[1].y = bottom; |
351 |
v[2].x = right; v[2].y = top; |
352 |
break; |
353 |
case 020: /* up */ |
354 |
v[0].x = right; v[0].y = bottom; |
355 |
v[1].x = right; v[1].y = top; |
356 |
v[2].x = left; v[2].y = top; |
357 |
break; |
358 |
case 040: /* left */ |
359 |
v[0].x = right; v[0].y = top; |
360 |
v[1].x = left; v[1].y = top; |
361 |
v[2].x = left; v[2].y = bottom; |
362 |
break; |
363 |
case 060: /* down */ |
364 |
v[0].x = left; v[0].y = top; |
365 |
v[1].x = left; v[1].y = bottom; |
366 |
v[2].x = right; v[2].y = bottom; |
367 |
break; |
368 |
} |
369 |
v[3].x = v[0].x; v[3].y = v[0].y; |
370 |
v[0].flags = v[1].flags = v[2].flags = v[3].flags = 0; |
371 |
if (curfill != FillTiled) |
372 |
{ |
373 |
XSetFillStyle(dpy, gc, FillTiled); |
374 |
curfill = FillTiled; |
375 |
XSetTile(dpy, gc, pixpat(pati[(p->arg0>>2)&03],p->arg0&03)); |
376 |
} |
377 |
XDrawFIlled(dpy, wind, gc, v, 4); |
378 |
} |
379 |
|
380 |
|
381 |
|
382 |
xform(xp, yp, p) /* transform a point according to p */ |
383 |
|
384 |
register int *xp, *yp; |
385 |
register PRIMITIVE *p; |
386 |
|
387 |
{ |
388 |
int x, y; |
389 |
|
390 |
switch (p->arg0 & 060) { |
391 |
case 0: /* right */ |
392 |
x = *xp; |
393 |
y = *yp; |
394 |
break; |
395 |
case 020: /* up */ |
396 |
x = (XYSIZE-1) - *yp; |
397 |
y = *xp; |
398 |
break; |
399 |
case 040: /* left */ |
400 |
x = (XYSIZE-1) - *xp; |
401 |
y = (XYSIZE-1) - *yp; |
402 |
break; |
403 |
case 060: /* down */ |
404 |
x = *yp; |
405 |
y = (XYSIZE-1) - *xp; |
406 |
break; |
407 |
} |
408 |
|
409 |
*xp = CONV(x, p->xy[XMX] - p->xy[XMN]) + p->xy[XMN]; |
410 |
*yp = CONV(y, p->xy[YMX] - p->xy[YMN]) + p->xy[YMN]; |
411 |
|
412 |
} |
413 |
|
414 |
|
415 |
|
416 |
fillpoly(p) /* fill a polygon */ |
417 |
|
418 |
register PRIMITIVE *p; |
419 |
|
420 |
{ |
421 |
int x0, y0, curx, cury; |
422 |
PolyHandle polyh; |
423 |
char *nextscan(); |
424 |
register char *s; |
425 |
|
426 |
polyh = OpenPoly(); |
427 |
|
428 |
if ((s = nextscan(nextscan(p->args, "%d", &x0), "%d", &y0)) == NULL) |
429 |
error(USER, "illegal polygon spec in fillpoly"); |
430 |
|
431 |
xform(&x0, &y0, p); |
432 |
x0 = mapx(x0); y0 = mapy(y0); |
433 |
MoveTo(x0, y0); |
434 |
|
435 |
while ((s = nextscan(nextscan(s, "%d", &curx), "%d", &cury)) != NULL) { |
436 |
xform(&curx, &cury, p); |
437 |
curx = mapx(curx); cury = mapy(cury); |
438 |
LineTo(curx, cury); |
439 |
} |
440 |
LineTo(x0, y0); |
441 |
|
442 |
ClosePoly(); |
443 |
|
444 |
if (p->arg0 & 0100) { /* draw border */ |
445 |
if (curpat != 0) { |
446 |
PenPat(macpat[0]); |
447 |
curpat = 0; |
448 |
} |
449 |
if (curpsiz != 1) { |
450 |
PenSize(1, 1); |
451 |
curpsiz = 1; |
452 |
} |
453 |
if (curpmod != patOr) { |
454 |
PenMode(patOr); |
455 |
curpmod = patOr; |
456 |
} |
457 |
FramePoly(polyh); |
458 |
} |
459 |
|
460 |
setfill(p->arg0 & 077); |
461 |
PaintPoly(polyh); |
462 |
KillPoly(polyh); |
463 |
xpos = -1; |
464 |
ypos = -1; |
465 |
|
466 |
} |
467 |
|
468 |
#else |
469 |
|
470 |
void filltri(PRIMITIVE *p) {} |
471 |
void fillpoly(PRIMITIVE *p) {} |
472 |
void fillrect(PRIMITIVE *p) {} |
473 |
|
474 |
#endif |