xref: /original-bsd/lib/libcurses/refresh.c (revision cfde0222)
1 /*
2  * make the current screen look like "win" over the area coverd by
3  * win.
4  *
5  * @(#)refresh.c	1.9 (Berkeley) 05/01/85
6  */
7 
8 # include	"curses.ext"
9 
10 # ifdef DEBUG
11 # define	STATIC
12 # else
13 # define	STATIC	static
14 # endif
15 
16 STATIC short	ly, lx;
17 
18 STATIC bool	curwin;
19 
20 WINDOW	*_win = NULL;
21 
22 wrefresh(win)
23 reg WINDOW	*win;
24 {
25 	reg short	wy;
26 	reg int		retval;
27 	reg WINDOW	*orig;
28 
29 	/*
30 	 * make sure were in visual state
31 	 */
32 	if (_endwin) {
33 		_puts(VS);
34 		_puts(TI);
35 		_endwin = FALSE;
36 	}
37 
38 	/*
39 	 * initialize loop parameters
40 	 */
41 
42 	ly = curscr->_cury;
43 	lx = curscr->_curx;
44 	wy = 0;
45 	_win = win;
46 	curwin = (win == curscr);
47 
48 	if (win->_clear || curscr->_clear || curwin) {
49 		if ((win->_flags & _FULLWIN) || curscr->_clear) {
50 			_puts(CL);
51 			ly = 0;
52 			lx = 0;
53 			if (!curwin) {
54 				curscr->_clear = FALSE;
55 				curscr->_cury = 0;
56 				curscr->_curx = 0;
57 				werase(curscr);
58 			}
59 			touchwin(win);
60 		}
61 		win->_clear = FALSE;
62 	}
63 	if (!CA) {
64 		if (win->_curx != 0)
65 			_putchar('\n');
66 		if (!curwin)
67 			werase(curscr);
68 	}
69 # ifdef DEBUG
70 	fprintf(outf, "REFRESH(%0.2o): curwin = %d\n", win, curwin);
71 	fprintf(outf, "REFRESH:\n\tfirstch\tlastch\n");
72 # endif
73 	for (wy = 0; wy < win->_maxy; wy++) {
74 # ifdef DEBUG
75 		fprintf(outf, "%d\t%d\t%d\n", wy, win->_firstch[wy],
76 			win->_lastch[wy]);
77 # endif
78 		if (win->_firstch[wy] != _NOCHANGE)
79 			if (makech(win, wy) == ERR)
80 				return ERR;
81 			else {
82 				if (win->_firstch[wy] >= win->_ch_off)
83 					win->_firstch[wy] = win->_maxx +
84 							    win->_ch_off;
85 				if (win->_lastch[wy] < win->_maxx +
86 						       win->_ch_off)
87 					win->_lastch[wy] = win->_ch_off;
88 				if (win->_lastch[wy] < win->_firstch[wy])
89 					win->_firstch[wy] = _NOCHANGE;
90 			}
91 # ifdef DEBUG
92 		fprintf(outf, "\t%d\t%d\n", win->_firstch[wy],
93 			win->_lastch[wy]);
94 # endif
95 	}
96 
97 	if (win == curscr)
98 		domvcur(ly, lx, win->_cury, win->_curx);
99 	else {
100 		if (win->_leave) {
101 			curscr->_cury = ly;
102 			curscr->_curx = lx;
103 			ly -= win->_begy;
104 			lx -= win->_begx;
105 			if (ly >= 0 && ly < win->_maxy && lx >= 0 &&
106 			    lx < win->_maxx) {
107 				win->_cury = ly;
108 				win->_curx = lx;
109 			}
110 			else
111 				win->_cury = win->_curx = 0;
112 		}
113 		else {
114 			domvcur(ly, lx, win->_cury + win->_begy,
115 				win->_curx + win->_begx);
116 			curscr->_cury = win->_cury + win->_begy;
117 			curscr->_curx = win->_curx + win->_begx;
118 		}
119 	}
120 	retval = OK;
121 ret:
122 	_win = NULL;
123 	fflush(stdout);
124 	return retval;
125 }
126 
127 /*
128  * make a change on the screen
129  */
130 STATIC
131 makech(win, wy)
132 reg WINDOW	*win;
133 short		wy;
134 {
135 	reg char	*nsp, *csp, *ce;
136 	reg short	wx, lch, y;
137 	reg int		nlsp, clsp;	/* last space in lines		*/
138 
139 	wx = win->_firstch[wy] - win->_ch_off;
140 	if (wx >= win->_maxx)
141 		return OK;
142 	else if (wx < 0)
143 		wx = 0;
144 	lch = win->_lastch[wy] - win->_ch_off;
145 	if (lch < 0)
146 		return OK;
147 	else if (lch >= win->_maxx)
148 		lch = win->_maxx - 1;;
149 	y = wy + win->_begy;
150 
151 	if (curwin)
152 		csp = " ";
153 	else
154 		csp = &curscr->_y[wy + win->_begy][wx + win->_begx];
155 
156 	nsp = &win->_y[wy][wx];
157 	if (CE && !curwin) {
158 		for (ce = &win->_y[wy][win->_maxx - 1]; *ce == ' '; ce--)
159 			if (ce <= win->_y[wy])
160 				break;
161 		nlsp = ce - win->_y[wy];
162 	}
163 
164 	if (!curwin)
165 		ce = CE;
166 	else
167 		ce = NULL;
168 
169 	while (wx <= lch) {
170 		if (*nsp != *csp) {
171 			domvcur(ly, lx, y, wx + win->_begx);
172 # ifdef DEBUG
173 			fprintf(outf, "MAKECH: 1: wx = %d, lx = %d\n", wx, lx);
174 # endif
175 			ly = y;
176 			lx = wx + win->_begx;
177 			while (*nsp != *csp && wx <= lch) {
178 				if (ce != NULL && wx >= nlsp && *nsp == ' ') {
179 					/*
180 					 * check for clear to end-of-line
181 					 */
182 					ce = &curscr->_y[ly][COLS - 1];
183 					while (*ce == ' ')
184 						if (ce-- <= csp)
185 							break;
186 					clsp = ce - curscr->_y[ly] - win->_begx;
187 # ifdef DEBUG
188 					fprintf(outf, "MAKECH: clsp = %d, nlsp = %d\n", clsp, nlsp);
189 # endif
190 					if (clsp - nlsp >= strlen(CE)
191 					    && clsp < win->_maxx) {
192 # ifdef DEBUG
193 						fprintf(outf, "MAKECH: using CE\n");
194 # endif
195 						_puts(CE);
196 						lx = wx + win->_begx;
197 						while (wx++ <= clsp)
198 							*csp++ = ' ';
199 						return OK;
200 					}
201 					ce = NULL;
202 				}
203 				/*
204 				 * enter/exit standout mode as appropriate
205 				 */
206 				if (SO && (*nsp&_STANDOUT) != (curscr->_flags&_STANDOUT)) {
207 					if (*nsp & _STANDOUT) {
208 						_puts(SO);
209 						curscr->_flags |= _STANDOUT;
210 					}
211 					else {
212 						_puts(SE);
213 						curscr->_flags &= ~_STANDOUT;
214 					}
215 				}
216 				wx++;
217 				if (wx >= win->_maxx && wy == win->_maxy - 1)
218 					if (win->_scroll) {
219 					    if ((curscr->_flags&_STANDOUT) &&
220 					        (win->_flags & _ENDLINE))
221 						    if (!MS) {
222 							_puts(SE);
223 							curscr->_flags &= ~_STANDOUT;
224 						    }
225 					    if (!curwin)
226 						_putchar((*csp = *nsp) & 0177);
227 					    else
228 						_putchar(*nsp & 0177);
229 					    if (win->_flags&_FULLWIN && !curwin)
230 						scroll(curscr);
231 					    ly = win->_begy+win->_cury;
232 					    lx = win->_begx+win->_curx;
233 					    return OK;
234 					}
235 					else if (win->_flags&_SCROLLWIN) {
236 					    lx = --wx;
237 					    return ERR;
238 					}
239 				if (!curwin)
240 					_putchar((*csp++ = *nsp) & 0177);
241 				else
242 					_putchar(*nsp & 0177);
243 # ifdef FULLDEBUG
244 				fprintf(outf,
245 					"MAKECH:putchar(%c)\n", *nsp & 0177);
246 # endif
247 				if (UC && (*nsp & _STANDOUT)) {
248 					_putchar('\b');
249 					_puts(UC);
250 				}
251 				nsp++;
252 			}
253 # ifdef DEBUG
254 			fprintf(outf, "MAKECH: 2: wx = %d, lx = %d\n", wx, lx);
255 # endif
256 			if (lx == wx + win->_begx)	/* if no change */
257 				break;
258 			lx = wx + win->_begx;
259 		}
260 		else if (wx < lch)
261 			while (*nsp == *csp && wx < lch) {
262 				nsp++;
263 				if (!curwin)
264 					csp++;
265 				++wx;
266 			}
267 		else
268 			break;
269 # ifdef DEBUG
270 		fprintf(outf, "MAKECH: 3: wx = %d, lx = %d\n", wx, lx);
271 # endif
272 	}
273 	return OK;
274 }
275 
276 /*
277  * perform a mvcur, leaving standout mode if necessary
278  */
279 STATIC
280 domvcur(oy, ox, ny, nx)
281 int	oy, ox, ny, nx; {
282 
283 	if (curscr->_flags & _STANDOUT && !MS) {
284 		_puts(SE);
285 		curscr->_flags &= ~_STANDOUT;
286 	}
287 	mvcur(oy, ox, ny, nx);
288 }
289