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