1/****************************************************************************
2 * Copyright (c) 1998-2006,2007 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.41 2007/09/29 20:39:34 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 src_row, src_col;
78    NCURSES_SIZE_T begx;
79    NCURSES_SIZE_T begy;
80    NCURSES_SIZE_T dst_row, dst_col;
81#if USE_SCROLL_HINTS
82    bool wide;
83#endif
84
85    T((T_CALLED("wnoutrefresh(%p)"), win));
86#ifdef TRACE
87    if (USE_TRACEF(TRACE_UPDATE)) {
88	_tracedump("...win", win);
89	_nc_unlock_global(tracef);
90    }
91#endif /* TRACE */
92
93    /*
94     * This function will break badly if we try to refresh a pad.
95     */
96    if ((win == 0)
97	|| (win->_flags & _ISPAD))
98	returnCode(ERR);
99
100    /* put them here so "win == 0" won't break our code */
101    begx = win->_begx;
102    begy = win->_begy;
103
104    newscr->_nc_bkgd = win->_nc_bkgd;
105    WINDOW_ATTRS(newscr) = WINDOW_ATTRS(win);
106
107    /* merge in change information from all subwindows of this window */
108    wsyncdown(win);
109
110#if USE_SCROLL_HINTS
111    /*
112     * For pure efficiency, we'd want to transfer scrolling information
113     * from the window to newscr whenever the window is wide enough that
114     * its update will dominate the cost of the update for the horizontal
115     * band of newscr that it occupies.  Unfortunately, this threshold
116     * tends to be complex to estimate, and in any case scrolling the
117     * whole band and rewriting the parts outside win's image would look
118     * really ugly.  So.  What we do is consider the window "wide" if it
119     * either (a) occupies the whole width of newscr, or (b) occupies
120     * all but at most one column on either vertical edge of the screen
121     * (this caters to fussy people who put boxes around full-screen
122     * windows).  Note that changing this formula will not break any code,
123     * merely change the costs of various update cases.
124     */
125    wide = (begx <= 1 && win->_maxx >= (newscr->_maxx - 1));
126#endif
127
128    win->_flags &= ~_HASMOVED;
129
130    /*
131     * Microtweaking alert!  This double loop is one of the genuine
132     * hot spots in the code.  Even gcc doesn't seem to do enough
133     * common-subexpression chunking to make it really tense,
134     * so we'll force the issue.
135     */
136
137    /* limit(dst_col) */
138    limit_x = win->_maxx;
139    /* limit(src_col) */
140    if (limit_x > newscr->_maxx - begx)
141	limit_x = newscr->_maxx - begx;
142
143    for (src_row = 0, dst_row = begy + win->_yoffset;
144	 src_row <= win->_maxy && dst_row <= newscr->_maxy;
145	 src_row++, dst_row++) {
146	register struct ldat *nline = &newscr->_line[dst_row];
147	register struct ldat *oline = &win->_line[src_row];
148
149	if (oline->firstchar != _NOCHANGE) {
150	    int last_src = oline->lastchar;
151
152	    if (last_src > limit_x)
153		last_src = limit_x;
154
155	    src_col = oline->firstchar;
156	    dst_col = src_col + begx;
157
158	    if_WIDEC({
159		register int j;
160
161		/*
162		 * Ensure that we will copy complete multi-column characters
163		 * on the left-boundary.
164		 */
165		if (isWidecExt(oline->text[src_col])) {
166		    j = 1 + dst_col - WidecExt(oline->text[src_col]);
167		    if (j < 0)
168			j = 0;
169		    if (dst_col > j) {
170			src_col -= (dst_col - j);
171			dst_col = j;
172		    }
173		}
174
175		/*
176		 * Ensure that we will copy complete multi-column characters
177		 * on the right-boundary.
178		 */
179		j = last_src;
180		if (WidecExt(oline->text[j])) {
181		    ++j;
182		    while (j <= limit_x) {
183			if (isWidecBase(oline->text[j])) {
184			    break;
185			} else {
186			    last_src = j;
187			}
188			++j;
189		    }
190		}
191	    });
192
193	    if_WIDEC({
194		static cchar_t blank = BLANK;
195		int last_dst = begx + ((last_src < win->_maxx)
196				       ? last_src
197				       : win->_maxx);
198		int fix_left = dst_col;
199		int fix_right = last_dst;
200		register int j;
201
202		/*
203		 * Check for boundary cases where we may overwrite part of a
204		 * multi-column character.  For those, wipe the remainder of
205		 * the character to blanks.
206		 */
207		j = dst_col;
208		if (isWidecExt(nline->text[j])) {
209		    /*
210		     * On the left, we only care about multi-column characters
211		     * that extend into the changed region.
212		     */
213		    fix_left = 1 + j - WidecExt(nline->text[j]);
214		    if (fix_left < 0)
215			fix_left = 0;	/* only if cell is corrupt */
216		}
217
218		j = last_dst;
219		if (WidecExt(nline->text[j]) != 0) {
220		    /*
221		     * On the right, any multi-column character is a problem,
222		     * unless it happens to be contained in the change, and
223		     * ending at the right boundary of the change.  The
224		     * computation for 'fix_left' accounts for the left-side of
225		     * this character.  Find the end of the character.
226		     */
227		    ++j;
228		    while (j <= newscr->_maxx && isWidecExt(nline->text[j])) {
229			fix_right = j++;
230		    }
231		}
232
233		/*
234		 * The analysis is simpler if we do the clearing afterwards.
235		 * Do that now.
236		 */
237		if (fix_left < dst_col || fix_right > last_dst) {
238		    for (j = fix_left; j <= fix_right; ++j) {
239			nline->text[j] = blank;
240			CHANGED_CELL(nline, j);
241		    }
242		}
243	    });
244
245	    /*
246	     * Copy the changed text.
247	     */
248	    for (; src_col <= last_src; src_col++, dst_col++) {
249		if (!CharEq(oline->text[src_col], nline->text[dst_col])) {
250		    nline->text[dst_col] = oline->text[src_col];
251		    CHANGED_CELL(nline, dst_col);
252		}
253	    }
254
255	}
256#if USE_SCROLL_HINTS
257	if (wide) {
258	    int oind = oline->oldindex;
259
260	    nline->oldindex = ((oind == _NEWINDEX)
261			       ? _NEWINDEX
262			       : (begy + oind + win->_yoffset));
263	}
264#endif /* USE_SCROLL_HINTS */
265
266	oline->firstchar = oline->lastchar = _NOCHANGE;
267	if_USE_SCROLL_HINTS(oline->oldindex = src_row);
268    }
269
270    if (win->_clear) {
271	win->_clear = FALSE;
272	newscr->_clear = TRUE;
273    }
274
275    if (!win->_leaveok) {
276	newscr->_cury = win->_cury + win->_begy + win->_yoffset;
277	newscr->_curx = win->_curx + win->_begx;
278    }
279    newscr->_leaveok = win->_leaveok;
280
281#ifdef TRACE
282    if (USE_TRACEF(TRACE_UPDATE)) {
283	_tracedump("newscr", newscr);
284	_nc_unlock_global(tracef);
285    }
286#endif /* TRACE */
287    returnCode(OK);
288}
289