lib_refresh.c revision 50276
1/****************************************************************************
2 * Copyright (c) 1998 Free Software Foundation, Inc.                        *
3 *                                                                          *
4 * Permission is hereby granted, free of charge, to any person obtaining a  *
5 * copy of this software and associated documentation files (the            *
6 * "Software"), to deal in the Software without restriction, including      *
7 * without limitation the rights to use, copy, modify, merge, publish,      *
8 * distribute, distribute with modifications, sublicense, and/or sell       *
9 * copies of the Software, and to permit persons to whom the Software is    *
10 * furnished to do so, subject to the following conditions:                 *
11 *                                                                          *
12 * The above copyright notice and this permission notice shall be included  *
13 * in all copies or substantial portions of the Software.                   *
14 *                                                                          *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22 *                                                                          *
23 * Except as contained in this notice, the name(s) of the above copyright   *
24 * holders shall not be used in advertising or otherwise to promote the     *
25 * sale, use or other dealings in this Software without prior written       *
26 * authorization.                                                           *
27 ****************************************************************************/
28
29/****************************************************************************
30 *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31 *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32 ****************************************************************************/
33
34
35
36/*
37 *	lib_refresh.c
38 *
39 *	The routines wrefresh() and wnoutrefresh().
40 *
41 */
42
43#include <curses.priv.h>
44
45MODULE_ID("$Id: lib_refresh.c,v 1.24 1999/07/31 11:36:37 juergen Exp $")
46
47int wrefresh(WINDOW *win)
48{
49int code;
50
51	T((T_CALLED("wrefresh(%p)"), win));
52
53	if (win == curscr) {
54		curscr->_clear = TRUE;
55		code = doupdate();
56	} else if ((code = wnoutrefresh(win)) == OK) {
57		if (win->_clear)
58			newscr->_clear = TRUE;
59		code = doupdate();
60		/*
61		 * Reset the clearok() flag in case it was set for the special
62		 * case in hardscroll.c (if we don't reset it here, we'll get 2
63		 * refreshes because the flag is copied from stdscr to newscr).
64		 * Resetting the flag shouldn't do any harm, anyway.
65		 */
66		win->_clear = FALSE;
67	}
68	returnCode(code);
69}
70
71int wnoutrefresh(WINDOW *win)
72{
73short	limit_x;
74short	i, j;
75short	begx;
76short	begy;
77short	m, n;
78bool	wide;
79
80	T((T_CALLED("wnoutrefresh(%p)"), win));
81#ifdef TRACE
82	if (_nc_tracing & TRACE_UPDATE)
83	    _tracedump("...win", win);
84#endif /* TRACE */
85
86	/*
87	 * This function will break badly if we try to refresh a pad.
88	 */
89	if ((win == 0)
90	 || (win->_flags & _ISPAD))
91		returnCode(ERR);
92
93	/* put them here so "win == 0" won't break our code */
94	begx = win->_begx;
95	begy = win->_begy;
96
97	newscr->_bkgd  = win->_bkgd;
98	newscr->_attrs = win->_attrs;
99
100	/* merge in change information from all subwindows of this window */
101	wsyncdown(win);
102
103	/*
104	 * For pure efficiency, we'd want to transfer scrolling information
105	 * from the window to newscr whenever the window is wide enough that
106	 * its update will dominate the cost of the update for the horizontal
107	 * band of newscr that it occupies.  Unfortunately, this threshold
108	 * tends to be complex to estimate, and in any case scrolling the
109	 * whole band and rewriting the parts outside win's image would look
110	 * really ugly.  So.  What we do is consider the window "wide" if it
111	 * either (a) occupies the whole width of newscr, or (b) occupies
112	 * all but at most one column on either vertical edge of the screen
113	 * (this caters to fussy people who put boxes around full-screen
114	 * windows).  Note that changing this formula will not break any code,
115	 * merely change the costs of various update cases.
116	 */
117	wide = (begx <= 1 && win->_maxx >= (newscr->_maxx - 1));
118
119	win->_flags &= ~_HASMOVED;
120
121	/*
122	 * Microtweaking alert!  This double loop is one of the genuine
123	 * hot spots in the code.  Even gcc doesn't seem to do enough
124	 * common-subexpression chunking to make it really tense,
125	 * so we'll force the issue.
126	 */
127
128	/* limit(n) */
129	limit_x = win->_maxx;
130	/* limit(j) */
131	if (limit_x > win->_maxx)
132		limit_x = win->_maxx;
133
134	for (i = 0, m = begy + win->_yoffset;
135	     i <= win->_maxy && m <= newscr->_maxy;
136	     i++, m++) {
137		register struct ldat	*nline = &newscr->_line[m];
138		register struct ldat	*oline = &win->_line[i];
139
140		if (oline->firstchar != _NOCHANGE) {
141			int last = oline->lastchar;
142
143			if (last > limit_x)
144				last = limit_x;
145
146			for (j = oline->firstchar, n = j + begx; j <= last; j++, n++) {
147				if (oline->text[j] != nline->text[n]) {
148					nline->text[n] = oline->text[j];
149					CHANGED_CELL(nline, n);
150				}
151			}
152
153		}
154
155#if USE_SCROLL_HINTS
156		if (wide) {
157		    int	oind = oline->oldindex;
158
159		    nline->oldindex = (oind == _NEWINDEX) ? _NEWINDEX : begy + oind + win->_yoffset;
160		}
161#endif /* USE_SCROLL_HINTS */
162
163		oline->firstchar = oline->lastchar = _NOCHANGE;
164		if_USE_SCROLL_HINTS(oline->oldindex = i);
165	}
166
167	if (win->_clear) {
168		win->_clear = FALSE;
169		newscr->_clear = TRUE;
170	}
171
172	if (! win->_leaveok) {
173		newscr->_cury = win->_cury + win->_begy + win->_yoffset;
174		newscr->_curx = win->_curx + win->_begx;
175	}
176	newscr->_leaveok = win->_leaveok;
177
178#ifdef TRACE
179	if (_nc_tracing & TRACE_UPDATE)
180	    _tracedump("newscr", newscr);
181#endif /* TRACE */
182	returnCode(OK);
183}
184