xref: /original-bsd/old/talk/talk/display.c (revision 65d10654)
1 /*-
2  * Copyright (c) 1983, 1985
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)display.c	5.1 (Berkeley) 6/6/85";
10 #endif not lint
11 
12 /*
13  * The window 'manager', initializes curses and handles the actual
14  * displaying of text
15  */
16 #include "talk.h"
17 
18 xwin_t	my_win;
19 xwin_t	his_win;
20 WINDOW	*line_win;
21 
22 int	curses_initialized = 0;
23 
24 int	readwin __P((WINDOW *, int, int));
25 void	xscroll __P((xwin_t *, int));
26 
27 /*
28  * max HAS to be a function, it is called with
29  * a argument of the form --foo at least once.
30  */
31 int
32 max(a, b)
33 	int a, b;
34 {
35 
36 	return (a > b ? a : b);
37 }
38 
39 /*
40  * Display some text on somebody's window, processing some control
41  * characters while we are at it.
42  */
43 void
44 display(win, text, size)
45 	register xwin_t *win;
46 	register char *text;
47 	int size;
48 {
49 	register int i;
50 	char cch;
51 
52 	for (i = 0; i < size; i++) {
53 		if (*text == '\n') {
54 			xscroll(win, 0);
55 			text++;
56 			continue;
57 		}
58 		/* erase character */
59 		if (*text == win->cerase) {
60 			wmove(win->x_win, win->x_line, max(--win->x_col, 0));
61 			getyx(win->x_win, win->x_line, win->x_col);
62 			waddch(win->x_win, ' ');
63 			wmove(win->x_win, win->x_line, win->x_col);
64 			getyx(win->x_win, win->x_line, win->x_col);
65 			text++;
66 			continue;
67 		}
68 		/*
69 		 * On word erase search backwards until we find
70 		 * the beginning of a word or the beginning of
71 		 * the line.
72 		 */
73 		if (*text == win->werase) {
74 			int endcol, xcol, i, c;
75 
76 			endcol = win->x_col;
77 			xcol = endcol - 1;
78 			while (xcol >= 0) {
79 				c = readwin(win->x_win, win->x_line, xcol);
80 				if (c != ' ')
81 					break;
82 				xcol--;
83 			}
84 			while (xcol >= 0) {
85 				c = readwin(win->x_win, win->x_line, xcol);
86 				if (c == ' ')
87 					break;
88 				xcol--;
89 			}
90 			wmove(win->x_win, win->x_line, xcol + 1);
91 			for (i = xcol + 1; i < endcol; i++)
92 				waddch(win->x_win, ' ');
93 			wmove(win->x_win, win->x_line, xcol + 1);
94 			getyx(win->x_win, win->x_line, win->x_col);
95 			continue;
96 		}
97 		/* line kill */
98 		if (*text == win->kill) {
99 			wmove(win->x_win, win->x_line, 0);
100 			wclrtoeol(win->x_win);
101 			getyx(win->x_win, win->x_line, win->x_col);
102 			text++;
103 			continue;
104 		}
105 		if (*text == '\f') {
106 			if (win == &my_win)
107 				wrefresh(curscr);
108 			text++;
109 			continue;
110 		}
111 		if (win->x_col == COLS-1) {
112 			/* check for wraparound */
113 			xscroll(win, 0);
114 		}
115 		if (*text < ' ' && *text != '\t') {
116 			waddch(win->x_win, '^');
117 			getyx(win->x_win, win->x_line, win->x_col);
118 			if (win->x_col == COLS-1) /* check for wraparound */
119 				xscroll(win, 0);
120 			cch = (*text & 63) + 64;
121 			waddch(win->x_win, cch);
122 		} else
123 			waddch(win->x_win, *text);
124 		getyx(win->x_win, win->x_line, win->x_col);
125 		text++;
126 	}
127 	wrefresh(win->x_win);
128 }
129 
130 /*
131  * Read the character at the indicated position in win
132  */
133 int
134 readwin(win, line, col)
135 	WINDOW *win;
136 	int line, col;
137 {
138 	int oldline, oldcol;
139 	register int c;
140 
141 	getyx(win, oldline, oldcol);
142 	wmove(win, line, col);
143 	c = winch(win);
144 	wmove(win, oldline, oldcol);
145 	return (c);
146 }
147 
148 /*
149  * Scroll a window, blanking out the line following the current line
150  * so that the current position is obvious
151  */
152 void
153 xscroll(win, flag)
154 	register xwin_t *win;
155 	int flag;
156 {
157 
158 	if (flag == -1) {
159 		wmove(win->x_win, 0, 0);
160 		win->x_line = 0;
161 		win->x_col = 0;
162 		return;
163 	}
164 	win->x_line = (win->x_line + 1) % win->x_nlines;
165 	win->x_col = 0;
166 	wmove(win->x_win, win->x_line, win->x_col);
167 	wclrtoeol(win->x_win);
168 	wmove(win->x_win, (win->x_line + 1) % win->x_nlines, win->x_col);
169 	wclrtoeol(win->x_win);
170 	wmove(win->x_win, win->x_line, win->x_col);
171 }
172