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