ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/x11twind.c
Revision: 2.9
Committed: Mon Jun 30 14:59:13 2003 UTC (20 years, 9 months ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4, rad5R2, rad4R2P2, rad5R0, rad5R1, rad3R7P2, rad3R7P1, rad4R2, rad4R1, rad4R0, rad3R6, rad3R6P1, rad3R8, rad3R9, rad4R2P1, rad5R3, HEAD
Changes since 2.8: +5 -10 lines
Log Message:
Replaced most outdated BSD function calls with their posix equivalents, and cleaned up a few other platform dependencies.

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: x11twind.c,v 2.8 2003/04/11 19:32:00 greg Exp $";
3 #endif
4 /*
5 * x11twind.c - routines for X11 text windows.
6 *
7 * Written by G. Ward
8 * 10/30/87
9 *
10 * Modified for X11 by B. V. Smith
11 * 9/26/88
12 */
13
14 #include "copyright.h"
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <X11/Xlib.h>
20
21 #include "x11twind.h"
22
23 #define checkcurs(t) if ((t)->cursor) togglecurs(t)
24
25 #define restorecurs checkcurs
26
27 /* width/height of a character in fontstruct f */
28 #define Width(f) ((f)->max_bounds.rbearing - (f)->min_bounds.lbearing)
29 #define Height(f) ((f)->ascent + (f)->descent)
30 #define YStart(f) ((f)->ascent)
31
32 static void togglecurs();
33
34
35 TEXTWIND *
36 xt_open(dpy, parent, x, y, width, height, bw, fore, back, fontname)
37 Display *dpy;
38 Window parent;
39 int x, y;
40 int width, height;
41 int bw;
42 unsigned long fore, back;
43 char *fontname;
44 {
45 register int i;
46 register TEXTWIND *t;
47
48 if ((t = (TEXTWIND *)malloc(sizeof(TEXTWIND))) == NULL)
49 return(NULL);
50
51 t->dpy = dpy;
52 t->w = XCreateSimpleWindow(dpy, parent, x, y, width, height,
53 bw, fore, back);
54 if (t->w == 0)
55 return(NULL);
56 XMapWindow(dpy, t->w);
57
58 if ((t->f = XLoadQueryFont(dpy, fontname)) == 0)
59 return(NULL);
60
61 /* if (!t->f.fixedwidth) check for fixedwidth later
62 return(NULL); */
63
64 t->gc = XCreateGC(dpy,t->w,0,NULL);
65 XSetState(dpy, t->gc, fore, back, GXcopy, AllPlanes);
66 XSetFont(dpy, t->gc, t->f->fid);
67
68 t->nc = (width - LEFTMAR) /
69 Width(t->f); /* number of columns */
70 t->nr = height /
71 Height(t->f); /* number of rows */
72 if (t->nc < 1 || t->nr < 1)
73 return(NULL);
74 if ((t->lp = (char **)calloc(t->nr, sizeof(char *))) == NULL)
75 return(NULL);
76 for (i = 0; i < t->nr; i++)
77 if ((t->lp[i] = calloc(t->nc+1, 1)) == NULL)
78 return(NULL);
79 t->r = t->c = 0;
80 t->cursor = TNOCURS;
81 return(t);
82
83 }
84
85
86 void
87 xt_putc(c, t) /* output a character */
88 int c;
89 register TEXTWIND *t;
90 {
91 char ch[2];
92
93 checkcurs(t);
94 switch (c) {
95 case '\n':
96 if (t->r >= t->nr - 1)
97 xt_delete(t, 0); /* scroll up 1 line */
98 else if (t->r < t->nr - 1)
99 t->r++;
100 /* fall through */
101 case '\r':
102 t->c = 0;
103 break;
104 case '\b':
105 while (t->c < 1 && t->r > 0)
106 t->c = strlen(t->lp[--t->r]);
107 if (t->c > 0)
108 t->c--;
109 break;
110 default:
111 if (t->c >= t->nc)
112 xt_putc('\n', t);
113 ch[0] = c; ch[1] = '\0';
114 XDrawImageString(t->dpy, t->w, t->gc, LEFTMAR+t->c*Width(t->f),
115 YStart(t->f)+t->r*Height(t->f), ch, 1);
116 t->lp[t->r][t->c++] = c;
117 break;
118 }
119 restorecurs(t);
120 }
121
122
123 void
124 xt_puts(s, t) /* output a string */
125 register char *s;
126 TEXTWIND *t;
127 {
128 int oldcurs;
129
130 oldcurs = xt_cursor(t, TNOCURS); /* for efficiency */
131 while (*s)
132 xt_putc(*s++, t);
133 xt_cursor(t, oldcurs);
134 }
135
136
137 void
138 xt_delete(t, r) /* delete a line */
139 register TEXTWIND *t;
140 int r;
141 {
142 char *cp;
143 register int i;
144
145 if (r < 0 || r >= t->nr)
146 return;
147 checkcurs(t);
148 /* move lines */
149 XCopyArea(t->dpy, t->w, t->w, t->gc, LEFTMAR, (r+1)*Height(t->f),
150 t->nc*Width(t->f), (t->nr-1-r)*Height(t->f),
151 LEFTMAR, r*Height(t->f));
152 cp = t->lp[r];
153 for (i = r; i < t->nr-1; i++)
154 t->lp[i] = t->lp[i+1];
155 t->lp[t->nr-1] = cp;
156 /* clear bottom */
157 XClearArea(t->dpy, t->w, LEFTMAR, (t->nr-1)*Height(t->f),
158 t->nc*Width(t->f), Height(t->f),(Bool) 0);
159
160 memset(cp, '\0', t->nc);
161 restorecurs(t); /* should we reposition cursor? */
162 }
163
164
165 void
166 xt_insert(t, r) /* insert a line */
167 register TEXTWIND *t;
168 int r;
169 {
170 char *cp;
171 register int i;
172
173 if (r < 0 || r >= t->nr)
174 return;
175 checkcurs(t);
176 /* move lines */
177 XCopyArea(t->dpy, t->w, t->w, t->gc, LEFTMAR, r*Height(t->f), LEFTMAR,
178 (r+1)*Height(t->f), t->nc*Width(t->f),
179 (t->nr-1-r)*Height(t->f));
180 cp = t->lp[t->nr-1];
181 for (i = t->nr-1; i > r; i--)
182 t->lp[i] = t->lp[i-1];
183 t->lp[r] = cp;
184 /* clear new line */
185 XClearArea(t->dpy, t->w, LEFTMAR, r*Height(t->f),
186 t->nc*Width(t->f), Height(t->f), (Bool) 0);
187 memset(cp, '\0', t->nc);
188 restorecurs(t); /* should we reposition cursor? */
189 }
190
191
192 void
193 xt_redraw(t) /* redraw text window */
194 register TEXTWIND *t;
195 {
196 register int i;
197
198 XClearWindow(t->dpy, t->w);
199 for (i = 0; i < t->nr; i++)
200 if (strlen(t->lp[i]) > 0)
201 XDrawImageString(t->dpy, t->w, t->gc, LEFTMAR,
202 YStart(t->f)+i*Height(t->f),
203 t->lp[i], strlen(t->lp[i]));
204 restorecurs(t);
205 }
206
207
208 void
209 xt_clear(t) /* clear text window */
210 register TEXTWIND *t;
211 {
212 register int i;
213
214 XClearWindow(t->dpy, t->w);
215 for (i = 0; i < t->nr; i++)
216 memset(t->lp[i], '\0', t->nc);
217 t->r = t->c = 0;
218 restorecurs(t);
219 }
220
221
222 void
223 xt_move(t, r, c) /* move to new position */
224 register TEXTWIND *t;
225 int r, c;
226 {
227 if (r < 0 || c < 0 || r >= t->nr || c >= t->nc)
228 return;
229 checkcurs(t);
230 t->r = r;
231 t->c = c;
232 restorecurs(t);
233 }
234
235
236 int
237 xt_cursor(t, curs) /* change cursor */
238 register TEXTWIND *t;
239 register int curs;
240 {
241 register int oldcurs;
242
243 if (curs != TNOCURS && curs != TBLKCURS)
244 return(-1);
245 oldcurs = t->cursor;
246 if (curs != oldcurs)
247 togglecurs(t);
248 t->cursor = curs;
249 return(oldcurs);
250 }
251
252
253 void
254 xt_close(t) /* close text window */
255 register TEXTWIND *t;
256 {
257 register int i;
258
259 XFreeFont(t->dpy, t->f);
260 XFreeGC(t->dpy,t->gc);
261 XDestroyWindow(t->dpy, t->w);
262 for (i = 0; i < t->nr; i++)
263 free(t->lp[i]);
264 free((void *)t->lp);
265 free((void *)t);
266 }
267
268
269 static void
270 togglecurs(t)
271 register TEXTWIND *t;
272 {
273 XSetFunction(t->dpy, t->gc, GXinvert);
274 XSetPlaneMask(t->dpy, t->gc, 1L);
275 XFillRectangle(t->dpy, t->w, t->gc,
276 t->c*Width(t->f)+LEFTMAR, t->r*Height(t->f),
277 Width(t->f), Height(t->f));
278 XSetFunction(t->dpy, t->gc, GXcopy);
279 XSetPlaneMask(t->dpy, t->gc, ~0L);
280 }