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