ncurses 4.2
[ncurses.git] / ncurses / lib_refresh.c
1 /****************************************************************************
2  * Copyright (c) 1998 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  ****************************************************************************/
33
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.19 1998/02/11 12:13:59 tom Exp $")
46
47 int wrefresh(WINDOW *win)
48 {
49 int code;
50
51         T((T_CALLED("wrefresh(%p)"), win));
52
53         if (win == curscr) {
54                 curscr->_clear = TRUE;
55                 code = doupdate();
56         } else if ((code = wnoutrefresh(win)) == OK) {
57                 if (win->_clear)
58                         newscr->_clear = TRUE;
59                 code = doupdate();
60                 /*
61                  * Reset the clearok() flag in case it was set for the special
62                  * case in hardscroll.c (if we don't reset it here, we'll get 2
63                  * refreshes because the flag is copied from stdscr to newscr).
64                  * Resetting the flag shouldn't do any harm, anyway.
65                  */
66                 win->_clear = FALSE;
67         }
68         returnCode(code);
69 }
70
71 int wnoutrefresh(WINDOW *win)
72 {
73 short   i, j;
74 short   begx;
75 short   begy;
76 short   m, n;
77 bool    wide;
78
79         T((T_CALLED("wnoutrefresh(%p)"), win));
80 #ifdef TRACE
81         if (_nc_tracing & TRACE_UPDATE)
82             _tracedump("...win", win);
83 #endif /* TRACE */
84
85         /*
86          * This function will break badly if we try to refresh a pad.
87          */
88         if ((win == 0)
89          || (win->_flags & _ISPAD))
90                 returnCode(ERR);
91
92         /* put them here so "win == 0" won't break our code */
93         begx = win->_begx;
94         begy = win->_begy;
95
96         /*
97          * If 'newscr' has a different background than the window that we're
98          * trying to refresh, we'll have to copy the whole thing.
99          */
100         if (win->_bkgd != newscr->_bkgd) {
101                 touchwin(win);
102                 newscr->_bkgd = win->_bkgd;
103         }
104         newscr->_attrs = win->_attrs;
105
106         /* merge in change information from all subwindows of this window */
107         wsyncdown(win);
108
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
125         win->_flags &= ~_HASMOVED;
126
127         /*
128          * Microtweaking alert!  This double loop is one of the genuine
129          * hot spots in the code.  Even gcc doesn't seem to do enough
130          * common-subexpression chunking to make it really tense,
131          * so we'll force the issue.
132          */
133         for (i = 0, m = begy + win->_yoffset;
134              i <= win->_maxy && m <= newscr->_maxy;
135              i++, m++) {
136                 register struct ldat    *nline = &newscr->_line[m];
137                 register struct ldat    *oline = &win->_line[i];
138
139                 if (oline->firstchar != _NOCHANGE) {
140                         int last = oline->lastchar;
141
142                         /* limit(j) */
143                         if (last > win->_maxx)
144                                 last = win->_maxx;
145                         /* limit(n) */
146                         if (last > newscr->_maxx - begx)
147                                 last = newscr->_maxx - begx;
148
149                         for (j = oline->firstchar, n = j + begx; j <= last; j++, n++) {
150                                 if (oline->text[j] != nline->text[n]) {
151                                         nline->text[n] = oline->text[j];
152
153                                         if (nline->firstchar == _NOCHANGE)
154                                                 nline->firstchar = nline->lastchar = n;
155                                         else if (n < nline->firstchar)
156                                                 nline->firstchar = n;
157                                         else if (n > nline->lastchar)
158                                                 nline->lastchar = n;
159                                 }
160                         }
161
162                 }
163
164 #if USE_SCROLL_HINTS
165                 if (wide) {
166                     int oind = oline->oldindex;
167
168                     nline->oldindex = (oind == _NEWINDEX) ? _NEWINDEX : begy + oind + win->_yoffset;
169                 }
170 #endif /* USE_SCROLL_HINTS */
171
172                 oline->firstchar = oline->lastchar = _NOCHANGE;
173                 if_USE_SCROLL_HINTS(oline->oldindex = i);
174         }
175
176         if (win->_clear) {
177                 win->_clear = FALSE;
178                 newscr->_clear = TRUE;
179         }
180
181         if (! win->_leaveok) {
182                 newscr->_cury = win->_cury + win->_begy + win->_yoffset;
183                 newscr->_curx = win->_curx + win->_begx;
184         }
185 #ifdef TRACE
186         if (_nc_tracing & TRACE_UPDATE)
187             _tracedump("newscr", newscr);
188 #endif /* TRACE */
189         returnCode(OK);
190 }