|
|
1.1 root 1: #include <X/mit-copyright.h>
2:
3: /* Copyright Massachusetts Institute of Technology 1984, 1985 */
4:
5: #include "gedit.h"
6: #include <X/Xlib.h>
7:
8: #ifndef lint
9: static char *rcsid_gx_c = "$Header: gx.c,v 10.7 86/11/19 19:20:37 jg Rel $";
10: #endif lint
11:
12: char *index();
13:
14: static short crbits[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
15:
16: Window w, trw;
17: Font font;
18: int forepix, backpix, invplanes;
19:
20: char mousechanged;
21: short mousex, mousey;
22:
23: short wminx; /* lower left of display window */
24: short wminy;
25: short wmaxx; /* upper right of display window */
26: short wmaxy;
27: short chrwid;
28: short chrhgt;
29:
30: /* command dispatch per key */
31: fptr dispatch[256];
32:
33: /* used to fill in dispatch upon initialization */
34: int mleft(),newlabel(),stop(),delobj(),editobj(),mright(),toggle();
35: int quit(),instantiate(),newline(),redisplay(),mdown(),neworg();
36: int mup(),rotateobj(),selobj(),multiplier(),setwindow(),scale();
37: int letgo(),rescale(),newin(),newout(),snap(),curup(),curdown();
38: int home(),setcolor(),lastgo(),angle(),Help();
39: int editlast(), scaleup(), scaledn(), fixwindow();
40:
41: struct icmd { char *keys; fptr func; } cmds[] = {
42: "[", scaleup,
43: "]", scaledn,
44: "Aa\001", angle,
45: "Bb\002Hh\010", mleft,
46: "Cc", newlabel,
47: "\003\021", stop,
48: "Dd\004", delobj,
49: "Ee\005", editobj,
50: "Ff\006", mright,
51: "Gg", toggle,
52: "\007", quit,
53: "Hh", Help,
54: "Ii", instantiate,
55: "Ll", newline,
56: "Mm", lastgo,
57: "\014", redisplay,
58: "Nn\016", mdown,
59: "\017", neworg,
60: "Pp\020", mup,
61: "Rr\022", rotateobj,
62: "Ss\023", selobj,
63: "Uu\025", multiplier,
64: "Ww\027", setwindow,
65: "Xx\030", scale,
66: "Zz\032 ", letgo,
67: "^", editlast,
68: "*", rescale,
69: "<", newin,
70: ">", newout,
71: "!", snap,
72: "+", curup,
73: "-", curdown,
74: "@", home,
75: "\031", fixwindow,
76: NULL, NULL
77: };
78:
79: /* see if there is input ready from the user */
80: UserReady()
81: { XEvent ev;
82: int n, x, y;
83: Window sub;
84:
85: while (XPending()) {
86: XPeekEvent(&ev);
87: switch (ev.type) {
88: case MouseMoved:
89: mousechanged = 1;
90: XNextEvent(&ev);
91: XUpdateMouse(w, &x, &y, &sub);
92: mousex = x;
93: mousey = y;
94: return(1);
95: case ButtonPressed:
96: return(1);
97: case KeyPressed:
98: XLookupMapping((XKeyPressedEvent *)(&ev), &n);
99: if (n == 1) return (1);
100: XNextEvent(&ev);
101: break;
102: case ExposeRegion:
103: case ExposeWindow:
104: return(1);
105: }
106: }
107: return(0);
108: }
109:
110: /* get character from user */
111: UserChar()
112: { XEvent ev;
113: int n;
114: char *str;
115:
116: while (1) {
117: XNextEvent(&ev);
118: switch (ev.type) {
119: case ButtonPressed:
120: n = ((XButtonPressedEvent *) (&ev))->detail & ValueMask;
121: switch (n) {
122: case LeftButton:
123: return('l');
124: case MiddleButton:
125: return('z');
126: case RightButton:
127: return('s');
128: }
129: break;
130: case KeyPressed:
131: str = XLookupMapping((XKeyPressedEvent *)(&ev), &n);
132: if (n == 1) return (*str);
133: break;
134: case ExposeRegion:
135: return('\014');
136: case ExposeWindow:
137: if (((XExposeWindowEvent *) (&ev))->width == wmaxx &&
138: ((XExposeWindowEvent *) (&ev))->height == wmaxy)
139: return('\014');
140: wmaxx = ((XExposeWindowEvent *) (&ev))->width;
141: wmaxy = ((XExposeWindowEvent *) (&ev))->height;
142: XChangeWindow(trw, wmaxx, wmaxy - (chrhgt << 1));
143: return('\031');
144: }
145: }
146: }
147:
148:
149: /* display grid */
150: drawgrid(x,y,incrx,incry)
151: { int i;
152: Vertex vert[2];
153:
154: if (incrx <= 16) {
155: vert[0].x = x;
156: vert[0].flags = 0;
157: vert[1].x = wmaxx;
158: vert[1].flags = 0;
159: while (y > 0) {
160: vert[0].y = y;
161: vert[1].y = y;
162: XDrawDashed(trw, vert, 2, 1, 1, forepix,
163: XMakePattern(1, incrx, 1), GXcopy, AllPlanes);
164: y -= incry;
165: }
166: XClearVertexFlag();
167: } else if (incry <= 16) {
168: vert[0].y = y;
169: vert[0].flags = 0;
170: vert[1].y = 0;
171: vert[1].flags = 0;
172: while (x < wmaxx) {
173: vert[0].x = x;
174: vert[1].x = x;
175: XDrawDashed(trw, vert, 2, 1, 1, forepix,
176: XMakePattern(1, incry, 1), GXcopy, AllPlanes);
177: x += incrx;
178: }
179: XClearVertexFlag();
180: } else {
181: while (x < wmaxx) {
182: for (i = y; i > 0; i -= incry) {
183: XPixSet(trw, x, i, 1, 1, forepix);
184: }
185: x += incrx;
186: }
187: }
188: }
189:
190: /* set up display adaptor for 80x25 alpha color, release keyboard */
191: gexit()
192: {
193: }
194:
195: track()
196: { int x,y;
197: int code = 0;
198:
199: mousechanged = 0;
200:
201: x = mousex * cur_state.dscale;
202: y = (wmaxy - mousey) * cur_state.dscale;
203: x += cur_state.worgx;
204: y += cur_state.worgy;
205:
206: if (x!=cur_state.curx || y!=cur_state.cury) {
207: if (x >= cur_state.wmaxx)
208: x = cur_state.wmaxx - 1;
209: else if (x < cur_state.worgx)
210: x = cur_state.worgx;
211: if (y >= cur_state.wmaxy)
212: y = cur_state.wmaxy - 1;
213: else if (y < cur_state.worgy)
214: y = cur_state.worgy;
215: cur_state.curx = x;
216: cur_state.cury = y;
217: }
218:
219: return(code);
220: }
221:
222: remouse(x, y, doit)
223: {
224: if (x >= cur_state.wmaxx || x < cur_state.worgx ||
225: y >= cur_state.wmaxy || y < cur_state.worgy) {
226: cur_state.curx = x;
227: cur_state.cury = y;
228: return(RECENTER);
229: }
230: if (doit || (x != cur_state.curx || y != cur_state.cury)) {
231: cur_state.curx = x;
232: cur_state.cury = y;
233: x -= cur_state.worgx;
234: y -= cur_state.worgy;
235: x /= cur_state.dscale;
236: y /= cur_state.dscale;
237: y = wmaxy - y;
238: if (x != mousex || y != mousey) {
239: XCondWarpMouse(w, x, y, w, mousex, mousey, 1, 1);
240: /* XWarpMouse(w, x, y); */
241: XFlush();
242: }
243: }
244: return(0);
245: }
246:
247: /* set up display adaptor for graphics and initialize keyboard */
248: char *gentry(argc,argv)
249: int argc;
250: char **argv;
251: { char *prog;
252: char *fname = NULL;
253: register struct icmd *p;
254: register char *q;
255: WindowInfo winfo;
256: FontInfo finfo;
257: char *fore_color, *back_color, *brdr_color, *font_name;
258: char *geometry = NULL;
259: char *display = NULL;
260: int reverse = 0;
261: Color cdef;
262: char def[32];
263: OpaqueFrame frame;
264:
265: /* fill in command dispatch table */
266: for (p = cmds; p->keys != NULL; p++)
267: for (q = p->keys; *q; q++) dispatch[*q & 0xFF] = p->func;
268:
269: prog = *argv;
270: if ((q = XGetDefault(prog, "ReverseVideo")) &&
271: strcmp(q, "on") == 0)
272: reverse = 1;
273: frame.bdrwidth = 2;
274: if (q = XGetDefault(prog, "BorderWidth"))
275: frame.bdrwidth = atoi(q);
276: fore_color = XGetDefault(prog, "ForeGround");
277: back_color = XGetDefault(prog, "BackGround");
278: brdr_color = XGetDefault(prog, "Border");
279: font_name = XGetDefault(prog, "BodyFont");
280: for (argv++; --argc; argv++) {
281: if (!strcmp(*argv, "-rv")) {
282: reverse = 1;
283: } else if (!strcmp(*argv, "-fg") && argc) {
284: argv++;
285: argc--;
286: fore_color = *argv;
287: } else if (!strcmp(*argv, "-bg") && argc) {
288: argv++;
289: argc--;
290: back_color = *argv;
291: } else if (!strcmp(*argv, "-bd") && argc) {
292: argv++;
293: argc--;
294: brdr_color = *argv;
295: } else if (!strcmp(*argv, "-fn") && argc) {
296: argv++;
297: argc--;
298: font_name = *argv;
299: } else if (**argv == '=') {
300: geometry = *argv;
301: } else if (index(*argv, ':')) {
302: display = *argv;
303: } else if (**argv == '-' || fname != NULL) {
304: printf("usage: xgedit [-rv] [-fn <font>] [-fg <color>] [-bg <color>] [-bd <color>] [=<geometry>] [host:display] [file]\n");
305: exit(1);
306: } else {
307: fname = *argv;
308: }
309: }
310: if (!XOpenDisplay(display)) {
311: fprintf(stderr, "%s: Can't open display '%s'\n",
312: prog, XDisplayName(display));
313: exit(1);
314: }
315: if (reverse) {
316: forepix = WhitePixel;
317: backpix = BlackPixel;
318: frame.background = BlackPixmap;
319: frame.border = WhitePixmap;
320: } else {
321: forepix = BlackPixel;
322: backpix = WhitePixel;
323: frame.background = WhitePixmap;
324: frame.border = BlackPixmap;
325: }
326: if (DisplayCells() > 2) {
327: if (fore_color && XParseColor(fore_color, &cdef) &&
328: XGetHardwareColor(&cdef))
329: forepix = cdef.pixel;
330: if (back_color && XParseColor(back_color, &cdef) &&
331: XGetHardwareColor(&cdef))
332: frame.background = XMakeTile(backpix = cdef.pixel);
333: if (brdr_color && XParseColor(brdr_color, &cdef) &&
334: XGetHardwareColor(&cdef))
335: frame.border = XMakeTile(cdef.pixel);
336: }
337: invplanes = forepix ^ backpix;
338: if (font_name == NULL) font_name = "8x13";
339: font = XGetFont(font_name);
340: if (!font) {
341: fprintf(stderr, "no such font: %s\n", font_name);
342: exit(1);
343: }
344: XQueryFont(font, &finfo);
345: XQueryWindow(RootWindow, &winfo);
346: chrwid = finfo.width;
347: chrhgt = finfo.height;
348: frame.x = 1;
349: frame.y = 1;
350: frame.width = 80 * chrwid;
351: if (frame.width + (frame.bdrwidth<<1) > winfo.width)
352: frame.width = winfo.width - (frame.bdrwidth<<1);
353: frame.height = 25 * chrhgt;
354: if (frame.height + (frame.bdrwidth<<1) > winfo.height)
355: frame.height = winfo.height - (frame.bdrwidth<<1);
356: sprintf(def, "=%dx%d+1+1", frame.width, frame.height);
357: wminx = 0;
358: wminy = 2*chrhgt;
359: w = XCreate(prog, prog, geometry, def, &frame, 5 * chrwid, wminy + 5 * chrhgt);
360: wmaxx = frame.width;
361: wmaxy = frame.height;
362: trw = XCreateTransparency(w, 0, 0, wmaxx, wmaxy - (chrhgt << 1));
363: XMapWindow(trw);
364: XDefineCursor(w, XCreateCursor(16, 16, crbits, crbits, 7, 7,
365: backpix, forepix, GXnoop));
366: XSetResizeHint(w, 1, wminy + 1, 1, 1);
367: XMapWindow(w);
368: XSelectInput(w, KeyPressed|ButtonPressed|MouseMoved|ExposeRegion);
369: mousechanged = 0;
370: return(fname);
371: }
372:
373: Beep()
374: { XFeep(0);
375: }
376:
377: /* give status info on second to last line */
378: banner()
379: { char temp[100];
380:
381: if (cur_state.curobj != NULL)
382: sprintf(temp,"XGEDIT %d:%d %c %s",
383: cur_state.mscale,cur_state.dscale,
384: cur_state.curobj->modified ? '*':' ',
385: cur_state.curobj->name);
386: else sprintf(temp,"XGEDIT %d:%d",
387: cur_state.mscale,cur_state.dscale);
388:
389: XPixSet(w, 0, wmaxy - (chrhgt << 1), wmaxx, chrhgt, backpix);
390:
391: disp_str(temp, 0, chrhgt, 1, HIGHLIGHT, 1);
392: }
393:
394: /* display a string at the specified position, return lenth */
395: disp_str(s,x,y,reverse,xorflag,info)
396: char *s;
397: { int len;
398: len = strlen(s);
399: if (reverse)
400: XText(info ? w : trw, x, wmaxy - y - chrhgt, s, len, font,
401: backpix, forepix);
402: else
403: XTextMaskPad(info ? w : trw, x, wmaxy - y - chrhgt, s, len, font,
404: 0, 0, forepix, xorflag ? GXinvert : GXcopy,
405: xorflag ? invplanes: AllPlanes);
406: return(len);
407: }
408:
409: /* display a character at the specified position */
410: disp_char(ch,x,y,reverse,xorflag,info)
411: { char c = ch;
412: if (reverse)
413: XText(info ? w : trw, x, wmaxy - y - chrhgt, &c, 1, font,
414: backpix, forepix);
415: else
416: XTextMaskPad(info ? w : trw, x, wmaxy - y - chrhgt, &c, 1, font,
417: 0, 0, forepix, xorflag ? GXinvert : GXcopy,
418: xorflag ? invplanes : AllPlanes);
419: }
420:
421: /* Update display, cursor */
422: DpyUp(tflag)
423: { XEvent ev;
424:
425: if (tflag) tcurxor();
426: dcurxor();
427: do {
428: XPeekEvent(&ev);
429: } while (UserReady() == 0);
430: if (tflag) tcurxor();
431: dcurxor();
432: }
433:
434: /* xor the text cursor */
435: tcurxor()
436: { XPixFill(w, incol*chrwid, wmaxy - chrhgt, chrwid, chrhgt, forepix, (Bitmap) NULL,
437: GXinvert, invplanes);
438: }
439:
440: clearprompt()
441: { incol = 0;
442: XPixSet(w, 0, wmaxy - chrhgt, wmaxx, chrhgt, backpix);
443: }
444:
445: clearscreen()
446: { XClear(w);
447: banner();
448: clearprompt();
449: }
450:
451: static int lastx, lasty;
452: static int lastflag;
453: static int lastcnt = 1000;
454:
455: /* draw a vector from <FromX,FromY> to <ToX,ToY> */
456: line(FromX,FromY,ToX,ToY,xorflag)
457: { Vertex vert[2];
458: vert[0].x = FromX;
459: vert[0].y = wmaxy - FromY;
460: vert[0].flags = VertexDontDraw;
461: vert[1].x = ToX;
462: vert[1].y = wmaxy - ToY;
463: vert[1].flags = VertexDrawLastPoint;
464: if (xorflag == lastflag && lastcnt < 250 &&
465: ((FromX == lastx && FromY == lasty &&
466: XAppendVertex(&vert[1], 1) > 0)
467: || XAppendVertex(vert, 2) > 0)) {
468: lastx = ToX;
469: lasty = ToY;
470: lastcnt++;
471: return;
472: }
473: lastx = ToX;
474: lasty = ToY;
475: lastflag = xorflag;
476: lastcnt = 1;
477: XDraw(trw, vert, 2, 1, 1, forepix, xorflag ? GXinvert : GXcopy,
478: xorflag ? invplanes : AllPlanes);
479: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.