blob: a77bc487f21c4f8cd4d7dfc0677b473cda93fa60 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
Steve Kondikae271bc2015-11-15 02:50:53 +01002 * Copyright (c) 1998-2010,2011 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05303 * *
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 *
Steve Kondikae271bc2015-11-15 02:50:53 +010033 * and: Juergen Pfeifer *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053034 ****************************************************************************/
35
36/*
37 * lib_refresh.c
38 *
39 * The routines wrefresh() and wnoutrefresh().
40 *
41 */
42
43#include <curses.priv.h>
44
Steve Kondikae271bc2015-11-15 02:50:53 +010045MODULE_ID("$Id: lib_refresh.c,v 1.45 2011/06/25 19:02:22 Vassili.Courzakis Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053046
47NCURSES_EXPORT(int)
48wrefresh(WINDOW *win)
49{
50 int code;
Steve Kondikae271bc2015-11-15 02:50:53 +010051#if NCURSES_SP_FUNCS
52 SCREEN *SP_PARM = _nc_screen_of(win);
53#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053054
Steve Kondikae271bc2015-11-15 02:50:53 +010055 T((T_CALLED("wrefresh(%p)"), (void *) win));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053056
57 if (win == 0) {
58 code = ERR;
Steve Kondikae271bc2015-11-15 02:50:53 +010059 } else if (win == CurScreen(SP_PARM)) {
60 CurScreen(SP_PARM)->_clear = TRUE;
61 code = NCURSES_SP_NAME(doupdate) (NCURSES_SP_ARG);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053062 } else if ((code = wnoutrefresh(win)) == OK) {
63 if (win->_clear)
Steve Kondikae271bc2015-11-15 02:50:53 +010064 NewScreen(SP_PARM)->_clear = TRUE;
65 code = NCURSES_SP_NAME(doupdate) (NCURSES_SP_ARG);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053066 /*
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{
Steve Kondikae271bc2015-11-15 02:50:53 +010080 int limit_x;
81 int src_row, src_col;
82 int begx;
83 int begy;
84 int dst_row, dst_col;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053085#if USE_SCROLL_HINTS
86 bool wide;
87#endif
Steve Kondikae271bc2015-11-15 02:50:53 +010088#if NCURSES_SP_FUNCS
89 SCREEN *SP_PARM = _nc_screen_of(win);
90#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053091
Steve Kondikae271bc2015-11-15 02:50:53 +010092 T((T_CALLED("wnoutrefresh(%p)"), (void *) win));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053093
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
Steve Kondikae271bc2015-11-15 02:50:53 +0100101#ifdef TRACE
102 if (USE_TRACEF(TRACE_UPDATE)) {
103 _tracedump("...win", win);
104 _nc_unlock_global(tracef);
105 }
106#endif /* TRACE */
107
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530108 /* put them here so "win == 0" won't break our code */
109 begx = win->_begx;
110 begy = win->_begy;
111
Steve Kondikae271bc2015-11-15 02:50:53 +0100112 NewScreen(SP_PARM)->_nc_bkgd = win->_nc_bkgd;
113 WINDOW_ATTRS(NewScreen(SP_PARM)) = WINDOW_ATTRS(win);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530114
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 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100133 wide = (begx <= 1 && win->_maxx >= (NewScreen(SP_PARM)->_maxx - 1));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530134#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) */
Steve Kondikae271bc2015-11-15 02:50:53 +0100148 if (limit_x > NewScreen(SP_PARM)->_maxx - begx)
149 limit_x = NewScreen(SP_PARM)->_maxx - begx;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530150
151 for (src_row = 0, dst_row = begy + win->_yoffset;
Steve Kondikae271bc2015-11-15 02:50:53 +0100152 src_row <= win->_maxy && dst_row <= NewScreen(SP_PARM)->_maxy;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530153 src_row++, dst_row++) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100154 struct ldat *nline = &(NewScreen(SP_PARM)->_line[dst_row]);
155 struct ldat *oline = &win->_line[src_row];
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530156
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({
Steve Kondikae271bc2015-11-15 02:50:53 +0100167 int j;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530168
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;
Steve Kondikae271bc2015-11-15 02:50:53 +0100208 int j;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530209
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;
Steve Kondikae271bc2015-11-15 02:50:53 +0100236 while (j <= NewScreen(SP_PARM)->_maxx &&
237 isWidecExt(nline->text[j])) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530238 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;
Steve Kondikae271bc2015-11-15 02:50:53 +0100281 NewScreen(SP_PARM)->_clear = TRUE;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530282 }
283
284 if (!win->_leaveok) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100285 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);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530288 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100289 NewScreen(SP_PARM)->_leaveok = win->_leaveok;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530290
291#ifdef TRACE
292 if (USE_TRACEF(TRACE_UPDATE)) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100293 _tracedump("newscr", NewScreen(SP_PARM));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530294 _nc_unlock_global(tracef);
295 }
296#endif /* TRACE */
297 returnCode(OK);
298}