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