1 /* @(#)wwscroll.c 8.1 (Berkeley) 6/6/93 */ 2 /* $NetBSD: wwscroll.c,v 1.7 2003/08/07 11:17:44 agc Exp $ */ 3 4 /* 5 * Copyright (c) 1983, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * This code is derived from software contributed to Berkeley by 9 * Edward Wang at The University of California, Berkeley. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <stdlib.h> 37 38 #include "ww.h" 39 #include "tt.h" 40 #include "xx.h" 41 42 void 43 wwscroll(struct ww *w, int n) 44 { 45 int dir; 46 int top; 47 48 if (n == 0) 49 return; 50 dir = n < 0 ? -1 : 1; 51 top = w->ww_b.t - n; 52 if (top > w->ww_w.t) 53 top = w->ww_w.t; 54 else if (top + w->ww_b.nr < w->ww_w.b) 55 top = w->ww_w.b - w->ww_b.nr; 56 n = abs(top - w->ww_b.t); 57 if (n < w->ww_i.nr) { 58 while (--n >= 0) { 59 (void) wwscroll1(w, w->ww_i.t, w->ww_i.b, dir, 0); 60 w->ww_buf += dir; 61 w->ww_b.t -= dir; 62 w->ww_b.b -= dir; 63 } 64 } else { 65 w->ww_buf -= top - w->ww_b.t; 66 w->ww_b.t = top; 67 w->ww_b.b = top + w->ww_b.nr; 68 wwredrawwin(w); 69 } 70 } 71 72 /* 73 * Scroll one line, between 'row1' and 'row2', in direction 'dir'. 74 * Don't adjust ww_scroll. 75 * And don't redraw 'leaveit' lines. 76 */ 77 int 78 wwscroll1(struct ww *w, int row1, int row2, int dir, int leaveit) 79 { 80 int i; 81 int row1x, row2x; 82 int nvis; 83 int nvismax; 84 int scrolled = 0; 85 86 /* 87 * See how many lines on the screen are affected. 88 * And calculate row1x, row2x, and left at the same time. 89 */ 90 for (i = row1; i < row2 && w->ww_nvis[i] == 0; i++) 91 ; 92 if (i >= row2) /* can't do any fancy stuff */ 93 goto out; 94 row1x = i; 95 for (i = row2 - 1; i >= row1 && w->ww_nvis[i] == 0; i--) 96 ; 97 if (i <= row1x) 98 goto out; /* just one line is easy */ 99 row2x = i + 1; 100 101 /* 102 * See how much of this window is visible. 103 */ 104 nvismax = wwncol * (row2x - row1x); 105 nvis = 0; 106 for (i = row1x; i < row2x; i++) 107 nvis += w->ww_nvis[i]; 108 109 /* 110 * If it's a good idea to scroll and the terminal can, then do it. 111 */ 112 if (nvis < nvismax / 2) 113 goto no_scroll; /* not worth it */ 114 if ((dir > 0 ? tt.tt_scroll_down == 0 : tt.tt_scroll_up == 0) || 115 ((tt.tt_scroll_top != row1x || tt.tt_scroll_bot != row2x - 1) && 116 tt.tt_setscroll == 0)) 117 if (tt.tt_delline == 0 || tt.tt_insline == 0) 118 goto no_scroll; 119 xxscroll(dir, row1x, row2x); 120 scrolled = 1; 121 /* 122 * Fix up the old screen. 123 */ 124 { 125 union ww_char *tmp; 126 union ww_char **cpp, **cqq; 127 128 if (dir > 0) { 129 cpp = &wwos[row1x]; 130 cqq = cpp + 1; 131 tmp = *cpp; 132 for (i = row2x - row1x; --i > 0;) 133 *cpp++ = *cqq++; 134 *cpp = tmp; 135 } else { 136 cpp = &wwos[row2x]; 137 cqq = cpp - 1; 138 tmp = *cqq; 139 for (i = row2x - row1x; --i > 0;) 140 *--cpp = *--cqq; 141 *cqq = tmp; 142 } 143 for (i = wwncol; --i >= 0;) 144 tmp++->c_w = ' '; 145 } 146 147 no_scroll: 148 /* 149 * Fix the new screen. 150 */ 151 if (nvis == nvismax) { 152 /* 153 * Can shift whole lines. 154 */ 155 if (dir > 0) { 156 { 157 union ww_char *tmp; 158 union ww_char **cpp, **cqq; 159 160 cpp = &wwns[row1x]; 161 cqq = cpp + 1; 162 tmp = *cpp; 163 for (i = row2x - row1x; --i > 0;) 164 *cpp++ = *cqq++; 165 *cpp = tmp; 166 } 167 if (scrolled) { 168 char *p, *q; 169 170 p = &wwtouched[row1x]; 171 q = p + 1; 172 for (i = row2x - row1x; --i > 0;) 173 *p++ = *q++; 174 *p |= WWU_TOUCHED; 175 } else { 176 char *p; 177 178 p = &wwtouched[row1x]; 179 for (i = row2x - row1x; --i >= 0;) 180 *p++ |= WWU_TOUCHED; 181 } 182 wwredrawwin1(w, row1, row1x, dir); 183 wwredrawwin1(w, row2x - 1, row2 - leaveit, dir); 184 } else { 185 { 186 union ww_char *tmp; 187 union ww_char **cpp, **cqq; 188 189 cpp = &wwns[row2x]; 190 cqq = cpp - 1; 191 tmp = *cqq; 192 for (i = row2x - row1x; --i > 0;) 193 *--cpp = *--cqq; 194 *cqq = tmp; 195 } 196 if (scrolled) { 197 char *p, *q; 198 199 p = &wwtouched[row2x]; 200 q = p - 1; 201 for (i = row2x - row1x; --i > 0;) 202 *--p = *--q; 203 *q |= WWU_TOUCHED; 204 } else { 205 char *p; 206 207 p = &wwtouched[row1x]; 208 for (i = row2x - row1x; --i >= 0;) 209 *p++ |= WWU_TOUCHED; 210 } 211 wwredrawwin1(w, row1 + leaveit, row1x + 1, dir); 212 wwredrawwin1(w, row2x, row2, dir); 213 } 214 } else { 215 if (scrolled) { 216 char *p; 217 218 p = &wwtouched[row1x]; 219 for (i = row2x - row1x; --i >= 0;) 220 *p++ |= WWU_TOUCHED; 221 } 222 out: 223 if (dir > 0) 224 wwredrawwin1(w, row1, row2 - leaveit, dir); 225 else 226 wwredrawwin1(w, row1 + leaveit, row2, dir); 227 } 228 return scrolled; 229 } 230