1 |
greg |
1.1 |
/* Copyright (c) 1987 Regents of the University of California */ |
2 |
|
|
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
5 |
|
|
#endif |
6 |
|
|
|
7 |
|
|
/* |
8 |
|
|
* xtwind.c - routines for X text windows. |
9 |
|
|
* |
10 |
|
|
* 10/30/87 |
11 |
|
|
*/ |
12 |
|
|
|
13 |
|
|
#include <stdio.h> |
14 |
|
|
|
15 |
|
|
#include <X/Xlib.h> |
16 |
|
|
|
17 |
|
|
#include "xtwind.h" |
18 |
|
|
|
19 |
greg |
1.3 |
#ifndef BSD |
20 |
|
|
#define bzero(d,n) (void)memset(d,0,n) |
21 |
|
|
extern char *memset(); |
22 |
|
|
#endif |
23 |
|
|
|
24 |
greg |
1.1 |
#define checkcurs(t) if ((t)->cursor) togglecurs(t) |
25 |
|
|
|
26 |
|
|
#define restorecurs checkcurs |
27 |
|
|
|
28 |
|
|
#define togglecurs(t) XPixFill((t)->w, (t)->c*(t)->f.width+LEFTMAR, \ |
29 |
|
|
(t)->r*(t)->f.height, (t)->f.width, \ |
30 |
|
|
(t)->f.height, 0, 0, GXinvert, 1) |
31 |
|
|
|
32 |
|
|
extern char *calloc(), *malloc(); |
33 |
|
|
|
34 |
|
|
|
35 |
|
|
TEXTWIND * |
36 |
|
|
xt_open(parent, x, y, width, height, border, fontname) |
37 |
|
|
Window parent; |
38 |
|
|
int x, y; |
39 |
|
|
int width, height; |
40 |
|
|
int border; |
41 |
|
|
char *fontname; |
42 |
|
|
{ |
43 |
|
|
register int i; |
44 |
|
|
register TEXTWIND *t; |
45 |
|
|
|
46 |
|
|
if ((t = (TEXTWIND *)malloc(sizeof(TEXTWIND))) == NULL) |
47 |
|
|
return(NULL); |
48 |
|
|
t->w = XCreateWindow(parent, x, y, width, height, |
49 |
|
|
border, BlackPixmap, WhitePixmap); |
50 |
|
|
if (t->w == 0) |
51 |
|
|
return(NULL); |
52 |
|
|
XMapWindow(t->w); |
53 |
|
|
if ((i = XGetFont(fontname)) == 0) |
54 |
|
|
return(NULL); |
55 |
|
|
if (XQueryFont(i, &t->f) == 0) |
56 |
|
|
return(NULL); |
57 |
|
|
if (!t->f.fixedwidth) |
58 |
|
|
return(NULL); |
59 |
|
|
t->nc = (width - LEFTMAR) / t->f.width; |
60 |
|
|
t->nr = height / t->f.height; |
61 |
|
|
if (t->nc < 1 || t->nr < 1) |
62 |
|
|
return(NULL); |
63 |
|
|
if ((t->lp = (char **)calloc(t->nr+1, sizeof(char *))) == NULL) |
64 |
|
|
return(NULL); |
65 |
|
|
for (i = 0; i < t->nr; i++) |
66 |
|
|
if ((t->lp[i] = calloc(t->nc+1, 1)) == NULL) |
67 |
|
|
return(NULL); |
68 |
|
|
t->r = t->c = 0; |
69 |
|
|
t->cursor = TNOCURS; |
70 |
|
|
return(t); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
|
74 |
|
|
xt_puts(s, t) /* output a string */ |
75 |
|
|
register char *s; |
76 |
|
|
TEXTWIND *t; |
77 |
|
|
{ |
78 |
|
|
int oldcurs = xt_cursor(t, TNOCURS); |
79 |
|
|
|
80 |
|
|
while (*s) |
81 |
|
|
xt_putc(*s++, t); |
82 |
|
|
xt_cursor(t, oldcurs); |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
|
86 |
|
|
xt_putc(c, t) /* output a character */ |
87 |
|
|
char c; |
88 |
|
|
register TEXTWIND *t; |
89 |
|
|
{ |
90 |
|
|
int oldcurs = xt_cursor(t, TNOCURS); |
91 |
|
|
|
92 |
|
|
switch (c) { |
93 |
|
|
case '\n': |
94 |
|
|
if (t->r >= t->nr - 1) |
95 |
|
|
xt_delete(t, 0); /* scroll up 1 line */ |
96 |
greg |
1.2 |
else |
97 |
greg |
1.1 |
t->r++; |
98 |
|
|
/* fall through */ |
99 |
|
|
case '\r': |
100 |
|
|
t->c = 0; |
101 |
|
|
break; |
102 |
|
|
case '\b': |
103 |
|
|
while (t->c < 1 && t->r > 0) |
104 |
|
|
t->c = strlen(t->lp[--t->r]); |
105 |
|
|
if (t->c > 0) |
106 |
|
|
t->c--; |
107 |
|
|
break; |
108 |
|
|
default: |
109 |
|
|
if (t->c >= t->nc) |
110 |
|
|
xt_putc('\n', t); |
111 |
|
|
XText(t->w, LEFTMAR + t->c*t->f.width, t->r*t->f.height, |
112 |
|
|
&c, 1, t->f.id, BlackPixel, WhitePixel); |
113 |
|
|
t->lp[t->r][t->c++] = c; |
114 |
|
|
break; |
115 |
|
|
} |
116 |
|
|
xt_cursor(t, oldcurs); |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
|
120 |
|
|
xt_delete(t, r) /* delete a line */ |
121 |
|
|
register TEXTWIND *t; |
122 |
|
|
int r; |
123 |
|
|
{ |
124 |
|
|
char *cp; |
125 |
|
|
register int i; |
126 |
|
|
|
127 |
|
|
if (r < 0 || r >= t->nr) |
128 |
|
|
return; |
129 |
|
|
checkcurs(t); |
130 |
|
|
/* move lines */ |
131 |
|
|
XMoveArea(t->w, LEFTMAR, (r+1)*t->f.height, LEFTMAR, r*t->f.height, |
132 |
|
|
t->nc*t->f.width, (t->nr-1-r)*t->f.height); |
133 |
|
|
cp = t->lp[r]; |
134 |
|
|
for (i = r; i < t->nr-1; i++) |
135 |
|
|
t->lp[i] = t->lp[i+1]; |
136 |
|
|
t->lp[t->nr-1] = cp; |
137 |
|
|
/* clear bottom */ |
138 |
|
|
XPixSet(t->w, LEFTMAR, (t->nr-1)*t->f.height, |
139 |
|
|
t->nc*t->f.width, t->f.height, WhitePixel); |
140 |
|
|
bzero(cp, t->nc); |
141 |
|
|
restorecurs(t); /* should we reposition cursor? */ |
142 |
|
|
} |
143 |
|
|
|
144 |
|
|
|
145 |
|
|
xt_insert(t, r) /* insert a line */ |
146 |
|
|
register TEXTWIND *t; |
147 |
|
|
int r; |
148 |
|
|
{ |
149 |
|
|
char *cp; |
150 |
|
|
register int i; |
151 |
|
|
|
152 |
|
|
if (r < 0 || r >= t->nr) |
153 |
|
|
return; |
154 |
|
|
checkcurs(t); |
155 |
|
|
/* move lines */ |
156 |
|
|
XMoveArea(t->w, LEFTMAR, r*t->f.height, LEFTMAR, (r+1)*t->f.height, |
157 |
|
|
t->nc*t->f.width, (t->nr-1-r)*t->f.height); |
158 |
|
|
cp = t->lp[t->nr-1]; |
159 |
|
|
for (i = t->nr-1; i > r; i--) |
160 |
|
|
t->lp[i] = t->lp[i-1]; |
161 |
|
|
t->lp[r] = cp; |
162 |
|
|
/* clear new line */ |
163 |
|
|
XPixSet(t->w, LEFTMAR, r*t->f.height, |
164 |
|
|
t->nc*t->f.width, t->f.height, WhitePixel); |
165 |
|
|
bzero(cp, t->nc); |
166 |
|
|
restorecurs(t); /* should we reposition cursor? */ |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
|
170 |
|
|
xt_redraw(t) /* redraw text window */ |
171 |
|
|
register TEXTWIND *t; |
172 |
|
|
{ |
173 |
|
|
register int i; |
174 |
|
|
|
175 |
|
|
checkcurs(t); |
176 |
|
|
XClear(t->w); |
177 |
|
|
for (i = 0; i < t->nr; i++) |
178 |
|
|
XText(t->w, LEFTMAR, i*t->f.height, t->lp[i], strlen(t->lp[i]), |
179 |
|
|
t->f.id, BlackPixel, WhitePixel); |
180 |
|
|
restorecurs(t); |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
|
184 |
|
|
xt_clear(t) /* clear text window */ |
185 |
|
|
register TEXTWIND *t; |
186 |
|
|
{ |
187 |
|
|
register int i; |
188 |
|
|
|
189 |
|
|
checkcurs(t); |
190 |
|
|
XClear(t->w); |
191 |
|
|
for (i = 0; i < t->nr; i++) |
192 |
|
|
bzero(t->lp[i], t->nc); |
193 |
|
|
t->r = t->c = 0; |
194 |
|
|
restorecurs(t); |
195 |
|
|
} |
196 |
|
|
|
197 |
|
|
|
198 |
|
|
xt_move(t, r, c) /* move to new position */ |
199 |
|
|
register TEXTWIND *t; |
200 |
|
|
int r, c; |
201 |
|
|
{ |
202 |
|
|
if (r < 0 || c < 0 || r >= t->nr || c >= t->nc) |
203 |
|
|
return; |
204 |
|
|
checkcurs(t); |
205 |
|
|
t->r = r; |
206 |
|
|
t->c = c; |
207 |
|
|
restorecurs(t); |
208 |
|
|
} |
209 |
|
|
|
210 |
|
|
|
211 |
|
|
int |
212 |
|
|
xt_cursor(t, curs) /* change cursor */ |
213 |
|
|
register TEXTWIND *t; |
214 |
|
|
register int curs; |
215 |
|
|
{ |
216 |
|
|
register int oldcurs; |
217 |
|
|
|
218 |
|
|
if (curs != TNOCURS && curs != TBLKCURS) |
219 |
|
|
return(-1); |
220 |
|
|
oldcurs = t->cursor; |
221 |
|
|
if (curs != oldcurs) |
222 |
|
|
togglecurs(t); |
223 |
|
|
t->cursor = curs; |
224 |
|
|
return(oldcurs); |
225 |
|
|
} |
226 |
|
|
|
227 |
|
|
|
228 |
|
|
xt_close(t) /* close text window */ |
229 |
|
|
register TEXTWIND *t; |
230 |
|
|
{ |
231 |
|
|
register int i; |
232 |
|
|
|
233 |
|
|
XFreeFont(t->f.id); |
234 |
|
|
XDestroyWindow(t->w); |
235 |
|
|
for (i = 0; i < t->nr; i++) |
236 |
|
|
free(t->lp[i]); |
237 |
|
|
free((char *)t->lp); |
238 |
|
|
free((char *)t); |
239 |
|
|
} |