1 |
/* |
2 |
* xtwind.c - routines for X text windows. |
3 |
*/ |
4 |
|
5 |
/* ==================================================================== |
6 |
* The Radiance Software License, Version 1.0 |
7 |
* |
8 |
* Copyright (c) 1990 - 2002 The Regents of the University of California, |
9 |
* through Lawrence Berkeley National Laboratory. All rights reserved. |
10 |
* |
11 |
* Redistribution and use in source and binary forms, with or without |
12 |
* modification, are permitted provided that the following conditions |
13 |
* are met: |
14 |
* |
15 |
* 1. Redistributions of source code must retain the above copyright |
16 |
* notice, this list of conditions and the following disclaimer. |
17 |
* |
18 |
* 2. Redistributions in binary form must reproduce the above copyright |
19 |
* notice, this list of conditions and the following disclaimer in |
20 |
* the documentation and/or other materials provided with the |
21 |
* distribution. |
22 |
* |
23 |
* 3. The end-user documentation included with the redistribution, |
24 |
* if any, must include the following acknowledgment: |
25 |
* "This product includes Radiance software |
26 |
* (http://radsite.lbl.gov/) |
27 |
* developed by the Lawrence Berkeley National Laboratory |
28 |
* (http://www.lbl.gov/)." |
29 |
* Alternately, this acknowledgment may appear in the software itself, |
30 |
* if and wherever such third-party acknowledgments normally appear. |
31 |
* |
32 |
* 4. The names "Radiance," "Lawrence Berkeley National Laboratory" |
33 |
* and "The Regents of the University of California" must |
34 |
* not be used to endorse or promote products derived from this |
35 |
* software without prior written permission. For written |
36 |
* permission, please contact [email protected]. |
37 |
* |
38 |
* 5. Products derived from this software may not be called "Radiance", |
39 |
* nor may "Radiance" appear in their name, without prior written |
40 |
* permission of Lawrence Berkeley National Laboratory. |
41 |
* |
42 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED |
43 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
44 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
45 |
* DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR |
46 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
47 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
48 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
49 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
50 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
51 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
52 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
53 |
* SUCH DAMAGE. |
54 |
* ==================================================================== |
55 |
* |
56 |
* This software consists of voluntary contributions made by many |
57 |
* individuals on behalf of Lawrence Berkeley National Laboratory. For more |
58 |
* information on Lawrence Berkeley National Laboratory, please see |
59 |
* <http://www.lbl.gov/>. |
60 |
*/ |
61 |
|
62 |
#include <stdio.h> |
63 |
|
64 |
#include <X/Xlib.h> |
65 |
|
66 |
#include "xtwind.h" |
67 |
|
68 |
#ifndef BSD |
69 |
#define bzero(d,n) (void)memset(d,0,n) |
70 |
extern char *memset(); |
71 |
#endif |
72 |
|
73 |
#define checkcurs(t) if ((t)->cursor) togglecurs(t) |
74 |
|
75 |
#define restorecurs checkcurs |
76 |
|
77 |
#define togglecurs(t) XPixFill((t)->w, (t)->c*(t)->f.width+LEFTMAR, \ |
78 |
(t)->r*(t)->f.height, (t)->f.width, \ |
79 |
(t)->f.height, 0, 0, GXinvert, 1) |
80 |
|
81 |
extern char *calloc(), *malloc(); |
82 |
|
83 |
|
84 |
TEXTWIND * |
85 |
xt_open(parent, x, y, width, height, border, fontname) |
86 |
Window parent; |
87 |
int x, y; |
88 |
int width, height; |
89 |
int border; |
90 |
char *fontname; |
91 |
{ |
92 |
register int i; |
93 |
register TEXTWIND *t; |
94 |
|
95 |
if ((t = (TEXTWIND *)malloc(sizeof(TEXTWIND))) == NULL) |
96 |
return(NULL); |
97 |
t->w = XCreateWindow(parent, x, y, width, height, |
98 |
border, BlackPixmap, WhitePixmap); |
99 |
if (t->w == 0) |
100 |
return(NULL); |
101 |
XMapWindow(t->w); |
102 |
if ((i = XGetFont(fontname)) == 0) |
103 |
return(NULL); |
104 |
if (XQueryFont(i, &t->f) == 0) |
105 |
return(NULL); |
106 |
if (!t->f.fixedwidth) |
107 |
return(NULL); |
108 |
t->nc = (width - LEFTMAR) / t->f.width; |
109 |
t->nr = height / t->f.height; |
110 |
if (t->nc < 1 || t->nr < 1) |
111 |
return(NULL); |
112 |
if ((t->lp = (char **)calloc(t->nr+1, sizeof(char *))) == NULL) |
113 |
return(NULL); |
114 |
for (i = 0; i < t->nr; i++) |
115 |
if ((t->lp[i] = calloc(t->nc+1, 1)) == NULL) |
116 |
return(NULL); |
117 |
t->r = t->c = 0; |
118 |
t->cursor = TNOCURS; |
119 |
return(t); |
120 |
} |
121 |
|
122 |
|
123 |
xt_puts(s, t) /* output a string */ |
124 |
register char *s; |
125 |
TEXTWIND *t; |
126 |
{ |
127 |
int oldcurs = xt_cursor(t, TNOCURS); |
128 |
|
129 |
while (*s) |
130 |
xt_putc(*s++, t); |
131 |
xt_cursor(t, oldcurs); |
132 |
} |
133 |
|
134 |
|
135 |
xt_putc(c, t) /* output a character */ |
136 |
char c; |
137 |
register TEXTWIND *t; |
138 |
{ |
139 |
int oldcurs = xt_cursor(t, TNOCURS); |
140 |
|
141 |
switch (c) { |
142 |
case '\n': |
143 |
if (t->r >= t->nr - 1) |
144 |
xt_delete(t, 0); /* scroll up 1 line */ |
145 |
else |
146 |
t->r++; |
147 |
/* fall through */ |
148 |
case '\r': |
149 |
t->c = 0; |
150 |
break; |
151 |
case '\b': |
152 |
while (t->c < 1 && t->r > 0) |
153 |
t->c = strlen(t->lp[--t->r]); |
154 |
if (t->c > 0) |
155 |
t->c--; |
156 |
break; |
157 |
default: |
158 |
if (t->c >= t->nc) |
159 |
xt_putc('\n', t); |
160 |
XText(t->w, LEFTMAR + t->c*t->f.width, t->r*t->f.height, |
161 |
&c, 1, t->f.id, BlackPixel, WhitePixel); |
162 |
t->lp[t->r][t->c++] = c; |
163 |
break; |
164 |
} |
165 |
xt_cursor(t, oldcurs); |
166 |
} |
167 |
|
168 |
|
169 |
xt_delete(t, r) /* delete a line */ |
170 |
register TEXTWIND *t; |
171 |
int r; |
172 |
{ |
173 |
char *cp; |
174 |
register int i; |
175 |
|
176 |
if (r < 0 || r >= t->nr) |
177 |
return; |
178 |
checkcurs(t); |
179 |
/* move lines */ |
180 |
XMoveArea(t->w, LEFTMAR, (r+1)*t->f.height, LEFTMAR, r*t->f.height, |
181 |
t->nc*t->f.width, (t->nr-1-r)*t->f.height); |
182 |
cp = t->lp[r]; |
183 |
for (i = r; i < t->nr-1; i++) |
184 |
t->lp[i] = t->lp[i+1]; |
185 |
t->lp[t->nr-1] = cp; |
186 |
/* clear bottom */ |
187 |
XPixSet(t->w, LEFTMAR, (t->nr-1)*t->f.height, |
188 |
t->nc*t->f.width, t->f.height, WhitePixel); |
189 |
bzero(cp, t->nc); |
190 |
restorecurs(t); /* should we reposition cursor? */ |
191 |
} |
192 |
|
193 |
|
194 |
xt_insert(t, r) /* insert a line */ |
195 |
register TEXTWIND *t; |
196 |
int r; |
197 |
{ |
198 |
char *cp; |
199 |
register int i; |
200 |
|
201 |
if (r < 0 || r >= t->nr) |
202 |
return; |
203 |
checkcurs(t); |
204 |
/* move lines */ |
205 |
XMoveArea(t->w, LEFTMAR, r*t->f.height, LEFTMAR, (r+1)*t->f.height, |
206 |
t->nc*t->f.width, (t->nr-1-r)*t->f.height); |
207 |
cp = t->lp[t->nr-1]; |
208 |
for (i = t->nr-1; i > r; i--) |
209 |
t->lp[i] = t->lp[i-1]; |
210 |
t->lp[r] = cp; |
211 |
/* clear new line */ |
212 |
XPixSet(t->w, LEFTMAR, r*t->f.height, |
213 |
t->nc*t->f.width, t->f.height, WhitePixel); |
214 |
bzero(cp, t->nc); |
215 |
restorecurs(t); /* should we reposition cursor? */ |
216 |
} |
217 |
|
218 |
|
219 |
xt_redraw(t) /* redraw text window */ |
220 |
register TEXTWIND *t; |
221 |
{ |
222 |
register int i; |
223 |
|
224 |
checkcurs(t); |
225 |
XClear(t->w); |
226 |
for (i = 0; i < t->nr; i++) |
227 |
XText(t->w, LEFTMAR, i*t->f.height, t->lp[i], strlen(t->lp[i]), |
228 |
t->f.id, BlackPixel, WhitePixel); |
229 |
restorecurs(t); |
230 |
} |
231 |
|
232 |
|
233 |
xt_clear(t) /* clear text window */ |
234 |
register TEXTWIND *t; |
235 |
{ |
236 |
register int i; |
237 |
|
238 |
checkcurs(t); |
239 |
XClear(t->w); |
240 |
for (i = 0; i < t->nr; i++) |
241 |
bzero(t->lp[i], t->nc); |
242 |
t->r = t->c = 0; |
243 |
restorecurs(t); |
244 |
} |
245 |
|
246 |
|
247 |
xt_move(t, r, c) /* move to new position */ |
248 |
register TEXTWIND *t; |
249 |
int r, c; |
250 |
{ |
251 |
if (r < 0 || c < 0 || r >= t->nr || c >= t->nc) |
252 |
return; |
253 |
checkcurs(t); |
254 |
t->r = r; |
255 |
t->c = c; |
256 |
restorecurs(t); |
257 |
} |
258 |
|
259 |
|
260 |
int |
261 |
xt_cursor(t, curs) /* change cursor */ |
262 |
register TEXTWIND *t; |
263 |
register int curs; |
264 |
{ |
265 |
register int oldcurs; |
266 |
|
267 |
if (curs != TNOCURS && curs != TBLKCURS) |
268 |
return(-1); |
269 |
oldcurs = t->cursor; |
270 |
if (curs != oldcurs) |
271 |
togglecurs(t); |
272 |
t->cursor = curs; |
273 |
return(oldcurs); |
274 |
} |
275 |
|
276 |
|
277 |
xt_close(t) /* close text window */ |
278 |
register TEXTWIND *t; |
279 |
{ |
280 |
register int i; |
281 |
|
282 |
XFreeFont(t->f.id); |
283 |
XDestroyWindow(t->w); |
284 |
for (i = 0; i < t->nr; i++) |
285 |
free(t->lp[i]); |
286 |
free((void *)t->lp); |
287 |
free((void *)t); |
288 |
} |