| 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 |
greg |
1.2 |
#ifndef BSD
|
| 24 |
|
|
#define bzero(d,n) (void)memset(d,0,n)
|
| 25 |
|
|
extern char *memset();
|
| 26 |
|
|
#endif
|
| 27 |
|
|
|
| 28 |
greg |
1.1 |
#define checkcurs(t) if ((t)->cursor) togglecurs(t)
|
| 29 |
|
|
|
| 30 |
|
|
#define restorecurs checkcurs
|
| 31 |
|
|
|
| 32 |
|
|
/* width/height of a character in fontstruct f */
|
| 33 |
|
|
#define Width(f) ((f)->max_bounds.rbearing - (f)->min_bounds.lbearing)
|
| 34 |
|
|
#define Height(f) ((f)->ascent + (f)->descent)
|
| 35 |
|
|
#define YStart(f) ((f)->ascent)
|
| 36 |
|
|
|
| 37 |
|
|
extern char *calloc(), *malloc();
|
| 38 |
|
|
|
| 39 |
|
|
|
| 40 |
|
|
TEXTWIND *
|
| 41 |
|
|
xt_open(dpy, gc, parent, x, y, width, height, bw, fontname)
|
| 42 |
|
|
Display *dpy;
|
| 43 |
|
|
GC gc;
|
| 44 |
|
|
Window parent;
|
| 45 |
|
|
int x, y;
|
| 46 |
|
|
int width, height;
|
| 47 |
|
|
int bw;
|
| 48 |
|
|
char *fontname;
|
| 49 |
|
|
{
|
| 50 |
|
|
register int i;
|
| 51 |
|
|
register TEXTWIND *t;
|
| 52 |
|
|
|
| 53 |
|
|
if ((t = (TEXTWIND *)malloc(sizeof(TEXTWIND))) == NULL)
|
| 54 |
|
|
return(NULL);
|
| 55 |
|
|
|
| 56 |
|
|
t->dpy = dpy;
|
| 57 |
|
|
t->w = XCreateSimpleWindow(dpy, parent, x, y, width, height, bw,
|
| 58 |
|
|
BlackPixel(dpy,DefaultScreen(dpy)),
|
| 59 |
|
|
WhitePixel(dpy,DefaultScreen(dpy)));
|
| 60 |
|
|
/*
|
| 61 |
|
|
t->w = XCreateWindow(dpy, parent, x, y, width, height, bw, 0,
|
| 62 |
|
|
CopyFromParent, CopyFromParent, 0, 0);
|
| 63 |
|
|
*/
|
| 64 |
|
|
|
| 65 |
|
|
if (t->w == 0)
|
| 66 |
|
|
return(NULL);
|
| 67 |
|
|
XMapWindow(dpy, t->w);
|
| 68 |
|
|
|
| 69 |
|
|
if ((t->f = XLoadQueryFont(dpy, fontname)) == 0)
|
| 70 |
|
|
return(NULL);
|
| 71 |
|
|
|
| 72 |
|
|
/* if (!t->f.fixedwidth) check for fixedwidth later
|
| 73 |
|
|
return(NULL); */
|
| 74 |
|
|
t->gc = XCreateGC(dpy,t->w,0,NULL);
|
| 75 |
|
|
|
| 76 |
|
|
XCopyGC(dpy, gc, ~0L, t->gc);
|
| 77 |
|
|
|
| 78 |
|
|
XSetFont(dpy, t->gc, t->f->fid);
|
| 79 |
|
|
XSetFunction(dpy, t->gc, GXcopy);
|
| 80 |
|
|
|
| 81 |
|
|
t->nc = (width - LEFTMAR) /
|
| 82 |
|
|
Width(t->f); /* number of columns */
|
| 83 |
|
|
t->nr = height /
|
| 84 |
|
|
Height(t->f); /* number of rows */
|
| 85 |
|
|
if (t->nc < 1 || t->nr < 1)
|
| 86 |
|
|
return(NULL);
|
| 87 |
|
|
if ((t->lp = (char **)calloc(t->nr, sizeof(char *))) == NULL)
|
| 88 |
|
|
return(NULL);
|
| 89 |
|
|
for (i = 0; i < t->nr; i++)
|
| 90 |
|
|
if ((t->lp[i] = calloc(t->nc+1, 1)) == NULL)
|
| 91 |
|
|
return(NULL);
|
| 92 |
|
|
t->r = t->c = 0;
|
| 93 |
|
|
t->cursor = TNOCURS;
|
| 94 |
|
|
return(t);
|
| 95 |
|
|
|
| 96 |
|
|
}
|
| 97 |
|
|
|
| 98 |
|
|
|
| 99 |
|
|
xt_puts(s, t) /* output a string */
|
| 100 |
|
|
register char *s;
|
| 101 |
|
|
TEXTWIND *t;
|
| 102 |
|
|
{
|
| 103 |
|
|
int oldcurs;
|
| 104 |
|
|
|
| 105 |
|
|
oldcurs = xt_cursor(t, TNOCURS); /* for efficiency */
|
| 106 |
|
|
while (*s)
|
| 107 |
|
|
xt_putc(*s++, t);
|
| 108 |
|
|
xt_cursor(t, oldcurs);
|
| 109 |
|
|
}
|
| 110 |
|
|
|
| 111 |
|
|
|
| 112 |
|
|
xt_putc(c, t) /* output a character */
|
| 113 |
|
|
char c;
|
| 114 |
|
|
register TEXTWIND *t;
|
| 115 |
|
|
{
|
| 116 |
|
|
checkcurs(t);
|
| 117 |
|
|
switch (c) {
|
| 118 |
|
|
case '\n':
|
| 119 |
|
|
if (t->r >= t->nr - 1)
|
| 120 |
|
|
xt_delete(t, 0); /* scroll up 1 line */
|
| 121 |
|
|
else if (t->r < t->nr - 1)
|
| 122 |
|
|
t->r++;
|
| 123 |
|
|
/* fall through */
|
| 124 |
|
|
case '\r':
|
| 125 |
|
|
t->c = 0;
|
| 126 |
|
|
break;
|
| 127 |
|
|
case '\b':
|
| 128 |
|
|
while (t->c < 1 && t->r > 0)
|
| 129 |
|
|
t->c = strlen(t->lp[--t->r]);
|
| 130 |
|
|
if (t->c > 0)
|
| 131 |
|
|
t->c--;
|
| 132 |
|
|
break;
|
| 133 |
|
|
default:
|
| 134 |
|
|
if (t->c >= t->nc)
|
| 135 |
|
|
xt_putc('\n', t);
|
| 136 |
|
|
XDrawImageString(t->dpy, t->w, t->gc, LEFTMAR+t->c*Width(t->f),
|
| 137 |
|
|
YStart(t->f)+t->r*Height(t->f), &c, 1);
|
| 138 |
|
|
t->lp[t->r][t->c++] = c;
|
| 139 |
|
|
break;
|
| 140 |
|
|
}
|
| 141 |
|
|
restorecurs(t);
|
| 142 |
|
|
}
|
| 143 |
|
|
|
| 144 |
|
|
|
| 145 |
|
|
xt_delete(t, r) /* delete 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 |
|
|
XCopyArea(t->dpy, t->w, t->w, t->gc, LEFTMAR, (r+1)*Height(t->f),
|
| 157 |
|
|
t->nc*Width(t->f), (t->nr-1-r)*Height(t->f),
|
| 158 |
|
|
LEFTMAR, r*Height(t->f));
|
| 159 |
|
|
cp = t->lp[r];
|
| 160 |
|
|
for (i = r; i < t->nr-1; i++)
|
| 161 |
|
|
t->lp[i] = t->lp[i+1];
|
| 162 |
|
|
t->lp[t->nr-1] = cp;
|
| 163 |
|
|
/* clear bottom */
|
| 164 |
|
|
XClearArea(t->dpy, t->w, LEFTMAR, (t->nr-1)*Height(t->f),
|
| 165 |
|
|
t->nc*Width(t->f), Height(t->f),(Bool) 0);
|
| 166 |
|
|
|
| 167 |
|
|
bzero(cp, t->nc);
|
| 168 |
|
|
restorecurs(t); /* should we reposition cursor? */
|
| 169 |
|
|
}
|
| 170 |
|
|
|
| 171 |
|
|
|
| 172 |
|
|
xt_insert(t, r) /* insert a line */
|
| 173 |
|
|
register TEXTWIND *t;
|
| 174 |
|
|
int r;
|
| 175 |
|
|
{
|
| 176 |
|
|
char *cp;
|
| 177 |
|
|
register int i;
|
| 178 |
|
|
|
| 179 |
|
|
if (r < 0 || r >= t->nr)
|
| 180 |
|
|
return;
|
| 181 |
|
|
checkcurs(t);
|
| 182 |
|
|
/* move lines */
|
| 183 |
|
|
XCopyArea(t->dpy, t->w, t->w, t->gc, LEFTMAR, r*Height(t->f), LEFTMAR,
|
| 184 |
|
|
(r+1)*Height(t->f), t->nc*Width(t->f),
|
| 185 |
|
|
(t->nr-1-r)*Height(t->f));
|
| 186 |
|
|
cp = t->lp[t->nr-1];
|
| 187 |
|
|
for (i = t->nr-1; i > r; i--)
|
| 188 |
|
|
t->lp[i] = t->lp[i-1];
|
| 189 |
|
|
t->lp[r] = cp;
|
| 190 |
|
|
/* clear new line */
|
| 191 |
|
|
XClearArea(t->dpy, t->w, LEFTMAR, r*Height(t->f),
|
| 192 |
|
|
t->nc*Width(t->f), Height(t->f), (Bool) 0);
|
| 193 |
|
|
bzero(cp, t->nc);
|
| 194 |
|
|
restorecurs(t); /* should we reposition cursor? */
|
| 195 |
|
|
}
|
| 196 |
|
|
|
| 197 |
|
|
|
| 198 |
|
|
xt_redraw(t) /* redraw text window */
|
| 199 |
|
|
register TEXTWIND *t;
|
| 200 |
|
|
{
|
| 201 |
|
|
register int i;
|
| 202 |
|
|
|
| 203 |
|
|
XClearWindow(t->dpy, t->w);
|
| 204 |
|
|
for (i = 0; i < t->nr; i++)
|
| 205 |
|
|
if (strlen(t->lp[i]) > 0)
|
| 206 |
|
|
XDrawImageString(t->dpy, t->w, t->gc, LEFTMAR,
|
| 207 |
|
|
YStart(t->f)+i*Height(t->f),
|
| 208 |
|
|
t->lp[i], strlen(t->lp[i]));
|
| 209 |
|
|
restorecurs(t);
|
| 210 |
|
|
}
|
| 211 |
|
|
|
| 212 |
|
|
|
| 213 |
|
|
xt_clear(t) /* clear text window */
|
| 214 |
|
|
register TEXTWIND *t;
|
| 215 |
|
|
{
|
| 216 |
|
|
register int i;
|
| 217 |
|
|
|
| 218 |
|
|
XClearWindow(t->dpy, t->w);
|
| 219 |
|
|
for (i = 0; i < t->nr; i++)
|
| 220 |
|
|
bzero(t->lp[i], t->nc);
|
| 221 |
|
|
t->r = t->c = 0;
|
| 222 |
|
|
restorecurs(t);
|
| 223 |
|
|
}
|
| 224 |
|
|
|
| 225 |
|
|
|
| 226 |
|
|
xt_move(t, r, c) /* move to new position */
|
| 227 |
|
|
register TEXTWIND *t;
|
| 228 |
|
|
int r, c;
|
| 229 |
|
|
{
|
| 230 |
|
|
if (r < 0 || c < 0 || r >= t->nr || c >= t->nc)
|
| 231 |
|
|
return;
|
| 232 |
|
|
checkcurs(t);
|
| 233 |
|
|
t->r = r;
|
| 234 |
|
|
t->c = c;
|
| 235 |
|
|
restorecurs(t);
|
| 236 |
|
|
}
|
| 237 |
|
|
|
| 238 |
|
|
|
| 239 |
|
|
int
|
| 240 |
|
|
xt_cursor(t, curs) /* change cursor */
|
| 241 |
|
|
register TEXTWIND *t;
|
| 242 |
|
|
register int curs;
|
| 243 |
|
|
{
|
| 244 |
|
|
register int oldcurs;
|
| 245 |
|
|
|
| 246 |
|
|
if (curs != TNOCURS && curs != TBLKCURS)
|
| 247 |
|
|
return(-1);
|
| 248 |
|
|
oldcurs = t->cursor;
|
| 249 |
|
|
if (curs != oldcurs)
|
| 250 |
|
|
togglecurs(t);
|
| 251 |
|
|
t->cursor = curs;
|
| 252 |
|
|
return(oldcurs);
|
| 253 |
|
|
}
|
| 254 |
|
|
|
| 255 |
|
|
|
| 256 |
|
|
xt_close(t) /* close text window */
|
| 257 |
|
|
register TEXTWIND *t;
|
| 258 |
|
|
{
|
| 259 |
|
|
register int i;
|
| 260 |
|
|
|
| 261 |
|
|
XFreeFont(t->dpy, t->f);
|
| 262 |
|
|
XFreeGC(t->dpy,t->gc);
|
| 263 |
|
|
XDestroyWindow(t->dpy, t->w);
|
| 264 |
|
|
for (i = 0; i < t->nr; i++)
|
| 265 |
|
|
free(t->lp[i]);
|
| 266 |
|
|
free((char *)t->lp);
|
| 267 |
|
|
free((char *)t);
|
| 268 |
|
|
}
|
| 269 |
|
|
|
| 270 |
|
|
|
| 271 |
|
|
static
|
| 272 |
|
|
togglecurs(t)
|
| 273 |
|
|
register TEXTWIND *t;
|
| 274 |
|
|
{
|
| 275 |
|
|
XSetFunction(t->dpy, t->gc, GXinvert);
|
| 276 |
|
|
XSetPlaneMask(t->dpy, t->gc, 1L);
|
| 277 |
|
|
XFillRectangle(t->dpy, t->w, t->gc,
|
| 278 |
|
|
t->c*Width(t->f)+LEFTMAR, t->r*Height(t->f),
|
| 279 |
|
|
Width(t->f), Height(t->f));
|
| 280 |
|
|
XSetFunction(t->dpy, t->gc, GXcopy);
|
| 281 |
|
|
XSetPlaneMask(t->dpy, t->gc, ~0L);
|
| 282 |
|
|
}
|