1/****************************************************************************
2 * Copyright (c) 1998-2002,2005 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 *     and: Thomas E. Dickey                        1996-on                 *
33 ****************************************************************************/
34
35/*
36 *	lib_refresh.c
37 *
38 *	The routines wrefresh() and wnoutrefresh().
39 *
40 */
41
42#include <curses.priv.h>
43
44MODULE_ID("$Id: lib_refresh.c,v 1.33 2005/04/09 15:20:58 tom Exp $")
45
46NCURSES_EXPORT(int)
47wrefresh(WINDOW *win)
48{
49    int code;
50
51    T((T_CALLED("wrefresh(%p)"), win));
52
53    if (win == 0) {
54	code = ERR;
55    } else if (win == curscr) {
56	curscr->_clear = TRUE;
57	code = doupdate();
58    } else if ((code = wnoutrefresh(win)) == OK) {
59	if (win->_clear)
60	    newscr->_clear = TRUE;
61	code = doupdate();
62	/*
63	 * Reset the clearok() flag in case it was set for the special
64	 * case in hardscroll.c (if we don't reset it here, we'll get 2
65	 * refreshes because the flag is copied from stdscr to newscr).
66	 * Resetting the flag shouldn't do any harm, anyway.
67	 */
68	win->_clear = FALSE;
69    }
70    returnCode(code);
71}
72
73NCURSES_EXPORT(int)
74wnoutrefresh(WINDOW *win)
75{
76    NCURSES_SIZE_T limit_x;
77    NCURSES_SIZE_T i, j;
78    NCURSES_SIZE_T begx;
79    NCURSES_SIZE_T begy;
80    NCURSES_SIZE_T m, n;
81#if USE_SCROLL_HINTS
82    bool wide;
83#endif
84
85    T((T_CALLED("wnoutrefresh(%p)"), win));
86#ifdef TRACE
87    if (_nc_tracing & TRACE_UPDATE)
88	_tracedump("...win", win);
89#endif /* TRACE */
90
91    /*
92     * This function will break badly if we try to refresh a pad.
93     */
94    if ((win == 0)
95	|| (win->_flags & _ISPAD))
96	returnCode(ERR);
97
98    /* put them here so "win == 0" won't break our code */
99    begx = win->_begx;
100    begy = win->_begy;
101
102    newscr->_nc_bkgd = win->_nc_bkgd;
103    newscr->_attrs = win->_attrs;
104
105    /* merge in change information from all subwindows of this window */
106    wsyncdown(win);
107
108#if USE_SCROLL_HINTS
109    /*
110     * For pure efficiency, we'd want to transfer scrolling information
111     * from the window to newscr whenever the window is wide enough that
112     * its update will dominate the cost of the update for the horizontal
113     * band of newscr that it occupies.  Unfortunately, this threshold
114     * tends to be complex to estimate, and in any case scrolling the
115     * whole band and rewriting the parts outside win's image would look
116     * really ugly.  So.  What we do is consider the window "wide" if it
117     * either (a) occupies the whole width of newscr, or (b) occupies
118     * all but at most one column on either vertical edge of the screen
119     * (this caters to fussy people who put boxes around full-screen
120     * windows).  Note that changing this formula will not break any code,
121     * merely change the costs of various update cases.
122     */
123    wide = (begx <= 1 && win->_maxx >= (newscr->_maxx - 1));
124#endif
125
126    win->_flags &= ~_HASMOVED;
127
128    /*
129     * Microtweaking alert!  This double loop is one of the genuine
130     * hot spots in the code.  Even gcc doesn't seem to do enough
131     * common-subexpression chunking to make it really tense,
132     * so we'll force the issue.
133     */
134
135    /* limit(n) */
136    limit_x = win->_maxx;
137    /* limit(j) */
138    if (limit_x > newscr->_maxx - begx)
139	limit_x = newscr->_maxx - begx;
140
141    for (i = 0, m = begy + win->_yoffset;
142	 i <= win->_maxy && m <= newscr->_maxy;
143	 i++, m++) {
144	register struct ldat *nline = &newscr->_line[m];
145	register struct ldat *oline = &win->_line[i];
146
147	if (oline->firstchar != _NOCHANGE) {
148	    int last = oline->lastchar;
149
150	    if (last > limit_x)
151		last = limit_x;
152
153	    for (j = oline->firstchar, n = j + begx; j <= last; j++, n++) {
154		if (!CharEq(oline->text[j], nline->text[n])) {
155		    nline->text[n] = oline->text[j];
156		    CHANGED_CELL(nline, n);
157		}
158	    }
159
160	}
161#if USE_SCROLL_HINTS
162	if (wide) {
163	    int oind = oline->oldindex;
164
165	    nline->oldindex = (oind == _NEWINDEX) ? _NEWINDEX : begy + oind
166		+ win->_yoffset;
167	}
168#endif /* USE_SCROLL_HINTS */
169
170	oline->firstchar = oline->lastchar = _NOCHANGE;
171	if_USE_SCROLL_HINTS(oline->oldindex = i);
172    }
173
174    if (win->_clear) {
175	win->_clear = FALSE;
176	newscr->_clear = TRUE;
177    }
178
179    if (!win->_leaveok) {
180	newscr->_cury = win->_cury + win->_begy + win->_yoffset;
181	newscr->_curx = win->_curx + win->_begx;
182    }
183    newscr->_leaveok = win->_leaveok;
184
185#ifdef TRACE
186    if (_nc_tracing & TRACE_UPDATE)
187	_tracedump("newscr", newscr);
188#endif /* TRACE */
189    returnCode(OK);
190}
191