1 /****************************************************************************
2 * Copyright 2019,2020 Thomas E. Dickey *
3 * Copyright 1998-2010,2011 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
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 ****************************************************************************/
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)
45 #include <curses.priv.h>
47 MODULE_ID("$Id: lib_scroll.c,v 1.32 2020/02/02 23:34:34 tom Exp $")
50 _nc_scroll_window(WINDOW *win,
59 size_t to_copy = (sizeof(NCURSES_CH_T) * (size_t) (win->_maxx + 1));
61 TR(TRACE_MOVE, ("_nc_scroll_window(%p, %d, %ld, %ld)",
62 (void *) win, n, (long) top, (long) bottom));
66 || bottom > win->_maxy) {
67 TR(TRACE_MOVE, ("nothing to scroll"));
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
81 #define BottomLimit(n) ((n) >= 0 && (n) >= top)
82 #define TopLimit(n) ((n) <= win->_maxy && (n) <= bottom)
84 /* shift n lines downwards */
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,
92 if_USE_SCROLL_HINTS(win->_line[line].oldindex =
93 win->_line[line + n].oldindex);
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);
103 /* shift n lines upwards */
106 for (line = top; line <= limit && TopLimit(line); line++) {
107 memcpy(win->_line[line].text,
108 win->_line[line + n].text,
110 if_USE_SCROLL_HINTS(win->_line[line].oldindex =
111 win->_line[line + n].oldindex);
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);
119 touchline(win, top, bottom - top + 1);
122 if (WINDOW_EXT(win, addch_used) != 0) {
123 int next = WINDOW_EXT(win, addch_y) + n;
124 if (next < 0 || next > win->_maxy) {
126 ("Alert discarded multibyte on scroll"));
127 WINDOW_EXT(win, addch_y) = 0;
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;
139 wscrl(WINDOW *win, int n)
141 T((T_CALLED("wscrl(%p,%d)"), (void *) win, n));
143 if (!win || !win->_scroll) {
144 TR(TRACE_MOVE, ("...scrollok is false"));
149 _nc_scroll_window(win, n, win->_regtop, win->_regbottom, win->_nc_bkgd);