1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: x11twind.c,v 2.11 2024/05/23 22:49:20 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(TEXTWIND *t); |
33 |
|
34 |
|
35 |
TEXTWIND * |
36 |
xt_open( |
37 |
Display *dpy, |
38 |
Window parent, |
39 |
int x, int y, |
40 |
int width, int height, |
41 |
int bw, |
42 |
unsigned long fore, unsigned long back, |
43 |
char *fontname |
44 |
) |
45 |
{ |
46 |
int i; |
47 |
TEXTWIND *t; |
48 |
|
49 |
if ((t = (TEXTWIND *)malloc(sizeof(TEXTWIND))) == NULL) |
50 |
return(NULL); |
51 |
|
52 |
t->dpy = dpy; |
53 |
t->w = XCreateSimpleWindow(dpy, parent, x, y, width, height, |
54 |
bw, fore, back); |
55 |
if (t->w == 0) |
56 |
return(NULL); |
57 |
XMapWindow(dpy, t->w); |
58 |
|
59 |
if ((t->f = XLoadQueryFont(dpy, fontname)) == 0) |
60 |
return(NULL); |
61 |
|
62 |
/* if (!t->f.fixedwidth) check for fixedwidth later |
63 |
return(NULL); */ |
64 |
|
65 |
t->gc = XCreateGC(dpy,t->w,0,NULL); |
66 |
XSetState(dpy, t->gc, fore, back, GXcopy, AllPlanes); |
67 |
XSetFont(dpy, t->gc, t->f->fid); |
68 |
|
69 |
t->nc = (width - LEFTMAR) / |
70 |
Width(t->f); /* number of columns */ |
71 |
t->nr = height / |
72 |
Height(t->f); /* number of rows */ |
73 |
if ((t->nc < 1) | (t->nr < 1)) |
74 |
return(NULL); |
75 |
if ((t->lp = (char **)calloc(t->nr, sizeof(char *))) == NULL) |
76 |
return(NULL); |
77 |
for (i = 0; i < t->nr; i++) |
78 |
if ((t->lp[i] = calloc(t->nc+1, 1)) == NULL) |
79 |
return(NULL); |
80 |
t->r = t->c = 0; |
81 |
t->cursor = TNOCURS; |
82 |
return(t); |
83 |
|
84 |
} |
85 |
|
86 |
|
87 |
void |
88 |
xt_putc( /* output a character */ |
89 |
int c, |
90 |
TEXTWIND *t |
91 |
) |
92 |
{ |
93 |
char ch[2]; |
94 |
|
95 |
checkcurs(t); |
96 |
switch (c) { |
97 |
case '\n': |
98 |
if (t->r >= t->nr - 1) |
99 |
xt_delete(t, 0); /* scroll up 1 line */ |
100 |
else if (t->r < t->nr - 1) |
101 |
t->r++; |
102 |
/* fall through */ |
103 |
case '\r': |
104 |
t->c = 0; |
105 |
break; |
106 |
case '\b': |
107 |
while (t->c < 1 && t->r > 0) |
108 |
t->c = strlen(t->lp[--t->r]); |
109 |
if (t->c > 0) |
110 |
t->c--; |
111 |
break; |
112 |
default: |
113 |
if (t->c >= t->nc) |
114 |
xt_putc('\n', t); |
115 |
ch[0] = c; ch[1] = '\0'; |
116 |
XDrawImageString(t->dpy, t->w, t->gc, LEFTMAR+t->c*Width(t->f), |
117 |
YStart(t->f)+t->r*Height(t->f), ch, 1); |
118 |
t->lp[t->r][t->c++] = c; |
119 |
break; |
120 |
} |
121 |
restorecurs(t); |
122 |
} |
123 |
|
124 |
|
125 |
void |
126 |
xt_puts(const char *s, TEXTWIND *t) /* output a string */ |
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( /* delete a line */ |
139 |
TEXTWIND *t, |
140 |
int r |
141 |
) |
142 |
{ |
143 |
char *cp; |
144 |
int i; |
145 |
|
146 |
if ((r < 0) | (r >= t->nr)) |
147 |
return; |
148 |
checkcurs(t); |
149 |
/* move lines */ |
150 |
XCopyArea(t->dpy, t->w, t->w, t->gc, LEFTMAR, (r+1)*Height(t->f), |
151 |
t->nc*Width(t->f), (t->nr-1-r)*Height(t->f), |
152 |
LEFTMAR, r*Height(t->f)); |
153 |
cp = t->lp[r]; |
154 |
for (i = r; i < t->nr-1; i++) |
155 |
t->lp[i] = t->lp[i+1]; |
156 |
t->lp[t->nr-1] = cp; |
157 |
/* clear bottom */ |
158 |
XClearArea(t->dpy, t->w, LEFTMAR, (t->nr-1)*Height(t->f), |
159 |
t->nc*Width(t->f), Height(t->f),(Bool) 0); |
160 |
|
161 |
memset(cp, '\0', t->nc); |
162 |
restorecurs(t); /* should we reposition cursor? */ |
163 |
} |
164 |
|
165 |
|
166 |
void |
167 |
xt_insert( /* insert a line */ |
168 |
TEXTWIND *t, |
169 |
int r |
170 |
) |
171 |
{ |
172 |
char *cp; |
173 |
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 |
memset(cp, '\0', t->nc); |
190 |
restorecurs(t); /* should we reposition cursor? */ |
191 |
} |
192 |
|
193 |
|
194 |
void |
195 |
xt_redraw(TEXTWIND *t) /* redraw text window */ |
196 |
{ |
197 |
int i; |
198 |
|
199 |
XClearWindow(t->dpy, t->w); |
200 |
for (i = 0; i < t->nr; i++) |
201 |
if (strlen(t->lp[i]) > 0) |
202 |
XDrawImageString(t->dpy, t->w, t->gc, LEFTMAR, |
203 |
YStart(t->f)+i*Height(t->f), |
204 |
t->lp[i], strlen(t->lp[i])); |
205 |
restorecurs(t); |
206 |
} |
207 |
|
208 |
|
209 |
void |
210 |
xt_clear(TEXTWIND *t) /* clear text window */ |
211 |
{ |
212 |
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( /* move to new position */ |
224 |
TEXTWIND *t, |
225 |
int r, int c |
226 |
) |
227 |
{ |
228 |
if ((r < 0) | (c < 0) | (r >= t->nr) | (c >= t->nc)) |
229 |
return; |
230 |
checkcurs(t); |
231 |
t->r = r; |
232 |
t->c = c; |
233 |
restorecurs(t); |
234 |
} |
235 |
|
236 |
|
237 |
int |
238 |
xt_cursor( /* change cursor */ |
239 |
TEXTWIND *t, |
240 |
int curs |
241 |
) |
242 |
{ |
243 |
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(TEXTWIND *t) /* close text window */ |
257 |
{ |
258 |
int i; |
259 |
|
260 |
XFreeFont(t->dpy, t->f); |
261 |
XFreeGC(t->dpy,t->gc); |
262 |
XDestroyWindow(t->dpy, t->w); |
263 |
for (i = 0; i < t->nr; i++) |
264 |
free(t->lp[i]); |
265 |
free((void *)t->lp); |
266 |
free((void *)t); |
267 |
} |
268 |
|
269 |
|
270 |
static void |
271 |
togglecurs(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 |
} |