xref: /original-bsd/old/talk/talk/display.c (revision e58c8952)
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 /*
25  * max HAS to be a function, it is called with
26  * a argument of the form --foo at least once.
27  */
28 max(a,b)
29 	int a, b;
30 {
31 
32 	return (a > b ? a : b);
33 }
34 
35 /*
36  * Display some text on somebody's window, processing some control
37  * characters while we are at it.
38  */
39 display(win, text, size)
40 	register xwin_t *win;
41 	register char *text;
42 	int size;
43 {
44 	register int i;
45 	char cch;
46 
47 	for (i = 0; i < size; i++) {
48 		if (*text == '\n') {
49 			xscroll(win, 0);
50 			text++;
51 			continue;
52 		}
53 		/* erase character */
54 		if (*text == win->cerase) {
55 			wmove(win->x_win, win->x_line, max(--win->x_col, 0));
56 			getyx(win->x_win, win->x_line, win->x_col);
57 			waddch(win->x_win, ' ');
58 			wmove(win->x_win, win->x_line, win->x_col);
59 			getyx(win->x_win, win->x_line, win->x_col);
60 			text++;
61 			continue;
62 		}
63 		/*
64 		 * On word erase search backwards until we find
65 		 * the beginning of a word or the beginning of
66 		 * the line.
67 		 */
68 		if (*text == win->werase) {
69 			int endcol, xcol, i, c;
70 
71 			endcol = win->x_col;
72 			xcol = endcol - 1;
73 			while (xcol >= 0) {
74 				c = readwin(win->x_win, win->x_line, xcol);
75 				if (c != ' ')
76 					break;
77 				xcol--;
78 			}
79 			while (xcol >= 0) {
80 				c = readwin(win->x_win, win->x_line, xcol);
81 				if (c == ' ')
82 					break;
83 				xcol--;
84 			}
85 			wmove(win->x_win, win->x_line, xcol + 1);
86 			for (i = xcol + 1; i < endcol; i++)
87 				waddch(win->x_win, ' ');
88 			wmove(win->x_win, win->x_line, xcol + 1);
89 			getyx(win->x_win, win->x_line, win->x_col);
90 			continue;
91 		}
92 		/* line kill */
93 		if (*text == win->kill) {
94 			wmove(win->x_win, win->x_line, 0);
95 			wclrtoeol(win->x_win);
96 			getyx(win->x_win, win->x_line, win->x_col);
97 			text++;
98 			continue;
99 		}
100 		if (*text == '\f') {
101 			if (win == &my_win)
102 				wrefresh(curscr);
103 			text++;
104 			continue;
105 		}
106 		if (win->x_col == COLS-1) {
107 			/* check for wraparound */
108 			xscroll(win, 0);
109 		}
110 		if (*text < ' ' && *text != '\t') {
111 			waddch(win->x_win, '^');
112 			getyx(win->x_win, win->x_line, win->x_col);
113 			if (win->x_col == COLS-1) /* check for wraparound */
114 				xscroll(win, 0);
115 			cch = (*text & 63) + 64;
116 			waddch(win->x_win, cch);
117 		} else
118 			waddch(win->x_win, *text);
119 		getyx(win->x_win, win->x_line, win->x_col);
120 		text++;
121 	}
122 	wrefresh(win->x_win);
123 }
124 
125 /*
126  * Read the character at the indicated position in win
127  */
128 readwin(win, line, col)
129 	WINDOW *win;
130 {
131 	int oldline, oldcol;
132 	register int c;
133 
134 	getyx(win, oldline, oldcol);
135 	wmove(win, line, col);
136 	c = winch(win);
137 	wmove(win, oldline, oldcol);
138 	return (c);
139 }
140 
141 /*
142  * Scroll a window, blanking out the line following the current line
143  * so that the current position is obvious
144  */
145 xscroll(win, flag)
146 	register xwin_t *win;
147 	int flag;
148 {
149 
150 	if (flag == -1) {
151 		wmove(win->x_win, 0, 0);
152 		win->x_line = 0;
153 		win->x_col = 0;
154 		return;
155 	}
156 	win->x_line = (win->x_line + 1) % win->x_nlines;
157 	win->x_col = 0;
158 	wmove(win->x_win, win->x_line, win->x_col);
159 	wclrtoeol(win->x_win);
160 	wmove(win->x_win, (win->x_line + 1) % win->x_nlines, win->x_col);
161 	wclrtoeol(win->x_win);
162 	wmove(win->x_win, win->x_line, win->x_col);
163 }
164