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