1 |
greg |
1.1 |
#ifndef lint |
2 |
greg |
2.12 |
static const char RCSid[] = "$Id: x11twind.c,v 2.11 2024/05/23 22:49:20 greg Exp $"; |
3 |
greg |
1.1 |
#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 |
greg |
2.6 |
#include "copyright.h" |
15 |
greg |
2.5 |
|
16 |
greg |
1.1 |
#include <stdio.h> |
17 |
greg |
2.5 |
#include <stdlib.h> |
18 |
schorsch |
2.9 |
#include <string.h> |
19 |
greg |
1.1 |
#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 |
greg |
2.11 |
static void togglecurs(TEXTWIND *t); |
33 |
greg |
2.3 |
|
34 |
greg |
1.1 |
|
35 |
|
|
TEXTWIND * |
36 |
greg |
2.10 |
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 |
greg |
1.1 |
{ |
46 |
greg |
2.10 |
int i; |
47 |
|
|
TEXTWIND *t; |
48 |
greg |
1.1 |
|
49 |
|
|
if ((t = (TEXTWIND *)malloc(sizeof(TEXTWIND))) == NULL) |
50 |
|
|
return(NULL); |
51 |
|
|
|
52 |
|
|
t->dpy = dpy; |
53 |
greg |
2.2 |
t->w = XCreateSimpleWindow(dpy, parent, x, y, width, height, |
54 |
|
|
bw, fore, back); |
55 |
greg |
1.1 |
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 |
greg |
2.2 |
|
65 |
greg |
1.1 |
t->gc = XCreateGC(dpy,t->w,0,NULL); |
66 |
greg |
2.2 |
XSetState(dpy, t->gc, fore, back, GXcopy, AllPlanes); |
67 |
greg |
1.1 |
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 |
greg |
2.12 |
if ((t->nc < 1) | (t->nr < 1)) |
74 |
greg |
1.1 |
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 |
greg |
2.5 |
void |
88 |
greg |
2.10 |
xt_putc( /* output a character */ |
89 |
|
|
int c, |
90 |
|
|
TEXTWIND *t |
91 |
|
|
) |
92 |
greg |
1.1 |
{ |
93 |
greg |
2.8 |
char ch[2]; |
94 |
|
|
|
95 |
greg |
1.1 |
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 |
greg |
2.8 |
ch[0] = c; ch[1] = '\0'; |
116 |
greg |
1.1 |
XDrawImageString(t->dpy, t->w, t->gc, LEFTMAR+t->c*Width(t->f), |
117 |
greg |
2.8 |
YStart(t->f)+t->r*Height(t->f), ch, 1); |
118 |
greg |
1.1 |
t->lp[t->r][t->c++] = c; |
119 |
|
|
break; |
120 |
|
|
} |
121 |
|
|
restorecurs(t); |
122 |
greg |
2.7 |
} |
123 |
|
|
|
124 |
|
|
|
125 |
|
|
void |
126 |
greg |
2.10 |
xt_puts(const char *s, TEXTWIND *t) /* output a string */ |
127 |
greg |
2.7 |
{ |
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 |
greg |
1.1 |
} |
135 |
|
|
|
136 |
|
|
|
137 |
greg |
2.5 |
void |
138 |
greg |
2.10 |
xt_delete( /* delete a line */ |
139 |
|
|
TEXTWIND *t, |
140 |
|
|
int r |
141 |
|
|
) |
142 |
greg |
1.1 |
{ |
143 |
|
|
char *cp; |
144 |
greg |
2.10 |
int i; |
145 |
greg |
1.1 |
|
146 |
greg |
2.12 |
if ((r < 0) | (r >= t->nr)) |
147 |
greg |
1.1 |
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 |
schorsch |
2.9 |
memset(cp, '\0', t->nc); |
162 |
greg |
1.1 |
restorecurs(t); /* should we reposition cursor? */ |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
|
166 |
greg |
2.5 |
void |
167 |
greg |
2.10 |
xt_insert( /* insert a line */ |
168 |
|
|
TEXTWIND *t, |
169 |
|
|
int r |
170 |
|
|
) |
171 |
greg |
1.1 |
{ |
172 |
|
|
char *cp; |
173 |
greg |
2.10 |
int i; |
174 |
greg |
1.1 |
|
175 |
greg |
2.12 |
if ((r < 0) | (r >= t->nr)) |
176 |
greg |
1.1 |
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 |
schorsch |
2.9 |
memset(cp, '\0', t->nc); |
190 |
greg |
1.1 |
restorecurs(t); /* should we reposition cursor? */ |
191 |
|
|
} |
192 |
|
|
|
193 |
|
|
|
194 |
greg |
2.5 |
void |
195 |
greg |
2.10 |
xt_redraw(TEXTWIND *t) /* redraw text window */ |
196 |
greg |
1.1 |
{ |
197 |
greg |
2.10 |
int i; |
198 |
greg |
1.1 |
|
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 |
greg |
2.5 |
void |
210 |
greg |
2.10 |
xt_clear(TEXTWIND *t) /* clear text window */ |
211 |
greg |
1.1 |
{ |
212 |
greg |
2.10 |
int i; |
213 |
greg |
1.1 |
|
214 |
|
|
XClearWindow(t->dpy, t->w); |
215 |
|
|
for (i = 0; i < t->nr; i++) |
216 |
schorsch |
2.9 |
memset(t->lp[i], '\0', t->nc); |
217 |
greg |
1.1 |
t->r = t->c = 0; |
218 |
|
|
restorecurs(t); |
219 |
|
|
} |
220 |
|
|
|
221 |
|
|
|
222 |
greg |
2.5 |
void |
223 |
greg |
2.10 |
xt_move( /* move to new position */ |
224 |
|
|
TEXTWIND *t, |
225 |
|
|
int r, int c |
226 |
|
|
) |
227 |
greg |
1.1 |
{ |
228 |
greg |
2.12 |
if ((r < 0) | (c < 0) | (r >= t->nr) | (c >= t->nc)) |
229 |
greg |
1.1 |
return; |
230 |
|
|
checkcurs(t); |
231 |
|
|
t->r = r; |
232 |
|
|
t->c = c; |
233 |
|
|
restorecurs(t); |
234 |
|
|
} |
235 |
|
|
|
236 |
|
|
|
237 |
|
|
int |
238 |
greg |
2.10 |
xt_cursor( /* change cursor */ |
239 |
|
|
TEXTWIND *t, |
240 |
|
|
int curs |
241 |
|
|
) |
242 |
greg |
1.1 |
{ |
243 |
greg |
2.10 |
int oldcurs; |
244 |
greg |
1.1 |
|
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 |
greg |
2.5 |
void |
256 |
greg |
2.10 |
xt_close(TEXTWIND *t) /* close text window */ |
257 |
greg |
1.1 |
{ |
258 |
greg |
2.10 |
int i; |
259 |
greg |
1.1 |
|
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 |
greg |
2.5 |
free((void *)t->lp); |
266 |
|
|
free((void *)t); |
267 |
greg |
1.1 |
} |
268 |
|
|
|
269 |
|
|
|
270 |
greg |
2.5 |
static void |
271 |
greg |
2.10 |
togglecurs(TEXTWIND *t) |
272 |
greg |
1.1 |
{ |
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 |
|
|
} |