]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_scroll.c
ncurses 6.2 - patch 20200523
[ncurses.git] / ncurses / base / lib_scroll.c
1 /****************************************************************************
2  * Copyright 2019,2020 Thomas E. Dickey                                     *
3  * Copyright 1998-2010,2011 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29
30 /****************************************************************************
31  *  Author: Thomas E. Dickey 1996-2003                                      *
32  *     and: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34  ****************************************************************************/
35
36 /*
37 **      lib_scroll.c
38 **
39 **      The routine wscrl(win, n).
40 **  positive n scroll the window up (ie. move lines down)
41 **  negative n scroll the window down (ie. move lines up)
42 **
43 */
44
45 #include <curses.priv.h>
46
47 MODULE_ID("$Id: lib_scroll.c,v 1.32 2020/02/02 23:34:34 tom Exp $")
48
49 NCURSES_EXPORT(void)
50 _nc_scroll_window(WINDOW *win,
51                   int const n,
52                   int const top,
53                   int const bottom,
54                   NCURSES_CH_T blank)
55 {
56     int limit;
57     int line;
58     int j;
59     size_t to_copy = (sizeof(NCURSES_CH_T) * (size_t) (win->_maxx + 1));
60
61     TR(TRACE_MOVE, ("_nc_scroll_window(%p, %d, %ld, %ld)",
62                     (void *) win, n, (long) top, (long) bottom));
63
64     if (top < 0
65         || bottom < top
66         || bottom > win->_maxy) {
67         TR(TRACE_MOVE, ("nothing to scroll"));
68         return;
69     }
70
71     /*
72      * This used to do a line-text pointer-shuffle instead of text copies.
73      * That (a) doesn't work when the window is derived and doesn't have
74      * its own storage, (b) doesn't save you a lot on modern machines
75      * anyway.  Your typical memcpy implementations are coded in
76      * assembler using a tight BLT loop; for the size of copies we're
77      * talking here, the total execution time is dominated by the one-time
78      * setup cost.  So there is no point in trying to be excessively
79      * clever -- esr.
80      */
81 #define BottomLimit(n) ((n) >= 0 && (n) >= top)
82 #define TopLimit(n)    ((n) <= win->_maxy && (n) <= bottom)
83
84     /* shift n lines downwards */
85     if (n < 0) {
86         limit = top - n;
87         for (line = bottom; line >= limit && BottomLimit(line); line--) {
88             TR(TRACE_MOVE, ("...copying %d to %d", line + n, line));
89             memcpy(win->_line[line].text,
90                    win->_line[line + n].text,
91                    to_copy);
92             if_USE_SCROLL_HINTS(win->_line[line].oldindex =
93                                 win->_line[line + n].oldindex);
94         }
95         for (line = top; line < limit && TopLimit(line); line++) {
96             TR(TRACE_MOVE, ("...filling %d", line));
97             for (j = 0; j <= win->_maxx; j++)
98                 win->_line[line].text[j] = blank;
99             if_USE_SCROLL_HINTS(win->_line[line].oldindex = _NEWINDEX);
100         }
101     }
102
103     /* shift n lines upwards */
104     if (n > 0) {
105         limit = bottom - n;
106         for (line = top; line <= limit && TopLimit(line); line++) {
107             memcpy(win->_line[line].text,
108                    win->_line[line + n].text,
109                    to_copy);
110             if_USE_SCROLL_HINTS(win->_line[line].oldindex =
111                                 win->_line[line + n].oldindex);
112         }
113         for (line = bottom; line > limit && BottomLimit(line); line--) {
114             for (j = 0; j <= win->_maxx; j++)
115                 win->_line[line].text[j] = blank;
116             if_USE_SCROLL_HINTS(win->_line[line].oldindex = _NEWINDEX);
117         }
118     }
119     touchline(win, top, bottom - top + 1);
120
121     if_WIDEC({
122         if (WINDOW_EXT(win, addch_used) != 0) {
123             int next = WINDOW_EXT(win, addch_y) + n;
124             if (next < 0 || next > win->_maxy) {
125                 TR(TRACE_VIRTPUT,
126                    ("Alert discarded multibyte on scroll"));
127                 WINDOW_EXT(win, addch_y) = 0;
128             } else {
129                 TR(TRACE_VIRTPUT, ("scrolled working position to %d,%d",
130                                    WINDOW_EXT(win, addch_y),
131                                    WINDOW_EXT(win, addch_x)));
132                 WINDOW_EXT(win, addch_y) = next;
133             }
134         }
135     })
136 }
137
138 NCURSES_EXPORT(int)
139 wscrl(WINDOW *win, int n)
140 {
141     T((T_CALLED("wscrl(%p,%d)"), (void *) win, n));
142
143     if (!win || !win->_scroll) {
144         TR(TRACE_MOVE, ("...scrollok is false"));
145         returnCode(ERR);
146     }
147
148     if (n != 0) {
149         _nc_scroll_window(win, n, win->_regtop, win->_regbottom, win->_nc_bkgd);
150         _nc_synchook(win);
151     }
152     returnCode(OK);
153 }