1 |
greg |
1.1 |
#ifndef lint |
2 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
3 |
|
|
#endif |
4 |
|
|
|
5 |
|
|
/* Copyright (c) 1989 Regents of the University of California */ |
6 |
|
|
|
7 |
|
|
/* |
8 |
|
|
* x11twind.c - routines for X11 text windows. |
9 |
|
|
* |
10 |
|
|
* Written by G. Ward |
11 |
|
|
* 10/30/87 |
12 |
|
|
* |
13 |
|
|
* Modified for X11 by B. V. Smith |
14 |
|
|
* 9/26/88 |
15 |
|
|
*/ |
16 |
|
|
|
17 |
|
|
#include <stdio.h> |
18 |
|
|
|
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 |
|
|
extern char *calloc(), *malloc(); |
33 |
|
|
|
34 |
|
|
|
35 |
|
|
TEXTWIND * |
36 |
|
|
xt_open(dpy, gc, parent, x, y, width, height, bw, fontname) |
37 |
|
|
Display *dpy; |
38 |
|
|
GC gc; |
39 |
|
|
Window parent; |
40 |
|
|
int x, y; |
41 |
|
|
int width, height; |
42 |
|
|
int bw; |
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, bw, |
53 |
|
|
BlackPixel(dpy,DefaultScreen(dpy)), |
54 |
|
|
WhitePixel(dpy,DefaultScreen(dpy))); |
55 |
|
|
/* |
56 |
|
|
t->w = XCreateWindow(dpy, parent, x, y, width, height, bw, 0, |
57 |
|
|
CopyFromParent, CopyFromParent, 0, 0); |
58 |
|
|
*/ |
59 |
|
|
|
60 |
|
|
if (t->w == 0) |
61 |
|
|
return(NULL); |
62 |
|
|
XMapWindow(dpy, t->w); |
63 |
|
|
|
64 |
|
|
if ((t->f = XLoadQueryFont(dpy, fontname)) == 0) |
65 |
|
|
return(NULL); |
66 |
|
|
|
67 |
|
|
/* if (!t->f.fixedwidth) check for fixedwidth later |
68 |
|
|
return(NULL); */ |
69 |
|
|
t->gc = XCreateGC(dpy,t->w,0,NULL); |
70 |
|
|
|
71 |
|
|
XCopyGC(dpy, gc, ~0L, t->gc); |
72 |
|
|
|
73 |
|
|
XSetFont(dpy, t->gc, t->f->fid); |
74 |
|
|
XSetFunction(dpy, t->gc, GXcopy); |
75 |
|
|
|
76 |
|
|
t->nc = (width - LEFTMAR) / |
77 |
|
|
Width(t->f); /* number of columns */ |
78 |
|
|
t->nr = height / |
79 |
|
|
Height(t->f); /* number of rows */ |
80 |
|
|
if (t->nc < 1 || t->nr < 1) |
81 |
|
|
return(NULL); |
82 |
|
|
if ((t->lp = (char **)calloc(t->nr, sizeof(char *))) == NULL) |
83 |
|
|
return(NULL); |
84 |
|
|
for (i = 0; i < t->nr; i++) |
85 |
|
|
if ((t->lp[i] = calloc(t->nc+1, 1)) == NULL) |
86 |
|
|
return(NULL); |
87 |
|
|
t->r = t->c = 0; |
88 |
|
|
t->cursor = TNOCURS; |
89 |
|
|
return(t); |
90 |
|
|
|
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
|
94 |
|
|
xt_puts(s, t) /* output a string */ |
95 |
|
|
register char *s; |
96 |
|
|
TEXTWIND *t; |
97 |
|
|
{ |
98 |
|
|
int oldcurs; |
99 |
|
|
|
100 |
|
|
oldcurs = xt_cursor(t, TNOCURS); /* for efficiency */ |
101 |
|
|
while (*s) |
102 |
|
|
xt_putc(*s++, t); |
103 |
|
|
xt_cursor(t, oldcurs); |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
|
107 |
|
|
xt_putc(c, t) /* output a character */ |
108 |
|
|
char c; |
109 |
|
|
register TEXTWIND *t; |
110 |
|
|
{ |
111 |
|
|
checkcurs(t); |
112 |
|
|
switch (c) { |
113 |
|
|
case '\n': |
114 |
|
|
if (t->r >= t->nr - 1) |
115 |
|
|
xt_delete(t, 0); /* scroll up 1 line */ |
116 |
|
|
else if (t->r < t->nr - 1) |
117 |
|
|
t->r++; |
118 |
|
|
/* fall through */ |
119 |
|
|
case '\r': |
120 |
|
|
t->c = 0; |
121 |
|
|
break; |
122 |
|
|
case '\b': |
123 |
|
|
while (t->c < 1 && t->r > 0) |
124 |
|
|
t->c = strlen(t->lp[--t->r]); |
125 |
|
|
if (t->c > 0) |
126 |
|
|
t->c--; |
127 |
|
|
break; |
128 |
|
|
default: |
129 |
|
|
if (t->c >= t->nc) |
130 |
|
|
xt_putc('\n', t); |
131 |
|
|
XDrawImageString(t->dpy, t->w, t->gc, LEFTMAR+t->c*Width(t->f), |
132 |
|
|
YStart(t->f)+t->r*Height(t->f), &c, 1); |
133 |
|
|
t->lp[t->r][t->c++] = c; |
134 |
|
|
break; |
135 |
|
|
} |
136 |
|
|
restorecurs(t); |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
|
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 |
|
|
xt_insert(t, r) /* insert a line */ |
168 |
|
|
register TEXTWIND *t; |
169 |
|
|
int r; |
170 |
|
|
{ |
171 |
|
|
char *cp; |
172 |
|
|
register int i; |
173 |
|
|
|
174 |
|
|
if (r < 0 || r >= t->nr) |
175 |
|
|
return; |
176 |
|
|
checkcurs(t); |
177 |
|
|
/* move lines */ |
178 |
|
|
XCopyArea(t->dpy, t->w, t->w, t->gc, LEFTMAR, r*Height(t->f), LEFTMAR, |
179 |
|
|
(r+1)*Height(t->f), t->nc*Width(t->f), |
180 |
|
|
(t->nr-1-r)*Height(t->f)); |
181 |
|
|
cp = t->lp[t->nr-1]; |
182 |
|
|
for (i = t->nr-1; i > r; i--) |
183 |
|
|
t->lp[i] = t->lp[i-1]; |
184 |
|
|
t->lp[r] = cp; |
185 |
|
|
/* clear new line */ |
186 |
|
|
XClearArea(t->dpy, t->w, LEFTMAR, r*Height(t->f), |
187 |
|
|
t->nc*Width(t->f), Height(t->f), (Bool) 0); |
188 |
|
|
bzero(cp, t->nc); |
189 |
|
|
restorecurs(t); /* should we reposition cursor? */ |
190 |
|
|
} |
191 |
|
|
|
192 |
|
|
|
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 |
|
|
xt_clear(t) /* clear text window */ |
209 |
|
|
register TEXTWIND *t; |
210 |
|
|
{ |
211 |
|
|
register int i; |
212 |
|
|
|
213 |
|
|
XClearWindow(t->dpy, t->w); |
214 |
|
|
for (i = 0; i < t->nr; i++) |
215 |
|
|
bzero(t->lp[i], t->nc); |
216 |
|
|
t->r = t->c = 0; |
217 |
|
|
restorecurs(t); |
218 |
|
|
} |
219 |
|
|
|
220 |
|
|
|
221 |
|
|
xt_move(t, r, c) /* move to new position */ |
222 |
|
|
register TEXTWIND *t; |
223 |
|
|
int r, c; |
224 |
|
|
{ |
225 |
|
|
if (r < 0 || c < 0 || r >= t->nr || c >= t->nc) |
226 |
|
|
return; |
227 |
|
|
checkcurs(t); |
228 |
|
|
t->r = r; |
229 |
|
|
t->c = c; |
230 |
|
|
restorecurs(t); |
231 |
|
|
} |
232 |
|
|
|
233 |
|
|
|
234 |
|
|
int |
235 |
|
|
xt_cursor(t, curs) /* change cursor */ |
236 |
|
|
register TEXTWIND *t; |
237 |
|
|
register int curs; |
238 |
|
|
{ |
239 |
|
|
register int oldcurs; |
240 |
|
|
|
241 |
|
|
if (curs != TNOCURS && curs != TBLKCURS) |
242 |
|
|
return(-1); |
243 |
|
|
oldcurs = t->cursor; |
244 |
|
|
if (curs != oldcurs) |
245 |
|
|
togglecurs(t); |
246 |
|
|
t->cursor = curs; |
247 |
|
|
return(oldcurs); |
248 |
|
|
} |
249 |
|
|
|
250 |
|
|
|
251 |
|
|
xt_close(t) /* close text window */ |
252 |
|
|
register TEXTWIND *t; |
253 |
|
|
{ |
254 |
|
|
register int i; |
255 |
|
|
|
256 |
|
|
XFreeFont(t->dpy, t->f); |
257 |
|
|
XFreeGC(t->dpy,t->gc); |
258 |
|
|
XDestroyWindow(t->dpy, t->w); |
259 |
|
|
for (i = 0; i < t->nr; i++) |
260 |
|
|
free(t->lp[i]); |
261 |
|
|
free((char *)t->lp); |
262 |
|
|
free((char *)t); |
263 |
|
|
} |
264 |
|
|
|
265 |
|
|
|
266 |
|
|
static |
267 |
|
|
togglecurs(t) |
268 |
|
|
register TEXTWIND *t; |
269 |
|
|
{ |
270 |
|
|
XSetFunction(t->dpy, t->gc, GXinvert); |
271 |
|
|
XSetPlaneMask(t->dpy, t->gc, 1L); |
272 |
|
|
XFillRectangle(t->dpy, t->w, t->gc, |
273 |
|
|
t->c*Width(t->f)+LEFTMAR, t->r*Height(t->f), |
274 |
|
|
Width(t->f), Height(t->f)); |
275 |
|
|
XSetFunction(t->dpy, t->gc, GXcopy); |
276 |
|
|
XSetPlaneMask(t->dpy, t->gc, ~0L); |
277 |
|
|
} |