Annotation of src/usr.bin/window/wwscroll.c, Revision 1.5
1.5 ! millert 1: /* $OpenBSD: wwscroll.c,v 1.4 2001/11/19 19:02:18 mpech 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.
1.5 ! millert 19: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 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: #ifndef lint
37: #if 0
38: static char sccsid[] = "@(#)wwscroll.c 8.1 (Berkeley) 6/6/93";
39: #else
1.5 ! millert 40: static char rcsid[] = "$OpenBSD: wwscroll.c,v 1.4 2001/11/19 19:02:18 mpech Exp $";
1.1 deraadt 41: #endif
42: #endif /* not lint */
43:
44: #include "ww.h"
45: #include "tt.h"
46:
47: wwscroll(w, n)
1.4 mpech 48: struct ww *w;
1.1 deraadt 49: int n;
50: {
1.4 mpech 51: int dir;
52: int top;
1.1 deraadt 53:
54: if (n == 0)
55: return;
56: dir = n < 0 ? -1 : 1;
57: top = w->ww_b.t - n;
58: if (top > w->ww_w.t)
59: top = w->ww_w.t;
60: else if (top + w->ww_b.nr < w->ww_w.b)
61: top = w->ww_w.b - w->ww_b.nr;
62: n = abs(top - w->ww_b.t);
63: if (n < w->ww_i.nr) {
64: while (--n >= 0) {
65: (void) wwscroll1(w, w->ww_i.t, w->ww_i.b, dir, 0);
66: w->ww_buf += dir;
67: w->ww_b.t -= dir;
68: w->ww_b.b -= dir;
69: }
70: } else {
71: w->ww_buf -= top - w->ww_b.t;
72: w->ww_b.t = top;
73: w->ww_b.b = top + w->ww_b.nr;
74: wwredrawwin(w);
75: }
76: }
77:
78: /*
79: * Scroll one line, between 'row1' and 'row2', in direction 'dir'.
80: * Don't adjust ww_scroll.
81: * And don't redraw 'leaveit' lines.
82: */
83: wwscroll1(w, row1, row2, dir, leaveit)
1.4 mpech 84: struct ww *w;
1.1 deraadt 85: int row1, row2, dir;
86: int leaveit;
87: {
1.4 mpech 88: int i;
1.1 deraadt 89: int row1x, row2x;
90: int nvis;
91: int nvismax;
92: int scrolled = 0;
93:
94: /*
95: * See how many lines on the screen are affected.
96: * And calculate row1x, row2x, and left at the same time.
97: */
98: for (i = row1; i < row2 && w->ww_nvis[i] == 0; i++)
99: ;
100: if (i >= row2) /* can't do any fancy stuff */
101: goto out;
102: row1x = i;
103: for (i = row2 - 1; i >= row1 && w->ww_nvis[i] == 0; i--)
104: ;
105: if (i <= row1x)
106: goto out; /* just one line is easy */
107: row2x = i + 1;
108:
109: /*
110: * See how much of this window is visible.
111: */
112: nvismax = wwncol * (row2x - row1x);
113: nvis = 0;
114: for (i = row1x; i < row2x; i++)
115: nvis += w->ww_nvis[i];
116:
117: /*
118: * If it's a good idea to scroll and the terminal can, then do it.
119: */
120: if (nvis < nvismax / 2)
121: goto no_scroll; /* not worth it */
122: if ((dir > 0 ? tt.tt_scroll_down == 0 : tt.tt_scroll_up == 0) ||
123: (tt.tt_scroll_top != row1x || tt.tt_scroll_bot != row2x - 1) &&
124: tt.tt_setscroll == 0)
125: if (tt.tt_delline == 0 || tt.tt_insline == 0)
126: goto no_scroll;
127: xxscroll(dir, row1x, row2x);
128: scrolled = 1;
129: /*
130: * Fix up the old screen.
131: */
132: {
1.4 mpech 133: union ww_char *tmp;
134: union ww_char **cpp, **cqq;
1.1 deraadt 135:
136: if (dir > 0) {
137: cpp = &wwos[row1x];
138: cqq = cpp + 1;
139: tmp = *cpp;
140: for (i = row2x - row1x; --i > 0;)
141: *cpp++ = *cqq++;
142: *cpp = tmp;
143: } else {
144: cpp = &wwos[row2x];
145: cqq = cpp - 1;
146: tmp = *cqq;
147: for (i = row2x - row1x; --i > 0;)
148: *--cpp = *--cqq;
149: *cqq = tmp;
150: }
151: for (i = wwncol; --i >= 0;)
152: tmp++->c_w = ' ';
153: }
154:
155: no_scroll:
156: /*
157: * Fix the new screen.
158: */
159: if (nvis == nvismax) {
160: /*
161: * Can shift whole lines.
162: */
163: if (dir > 0) {
164: {
1.4 mpech 165: union ww_char *tmp;
166: union ww_char **cpp, **cqq;
1.1 deraadt 167:
168: cpp = &wwns[row1x];
169: cqq = cpp + 1;
170: tmp = *cpp;
171: for (i = row2x - row1x; --i > 0;)
172: *cpp++ = *cqq++;
173: *cpp = tmp;
174: }
175: if (scrolled) {
1.4 mpech 176: char *p, *q;
1.1 deraadt 177:
178: p = &wwtouched[row1x];
179: q = p + 1;
180: for (i = row2x - row1x; --i > 0;)
181: *p++ = *q++;
182: *p |= WWU_TOUCHED;
183: } else {
1.4 mpech 184: char *p;
1.1 deraadt 185:
186: p = &wwtouched[row1x];
187: for (i = row2x - row1x; --i >= 0;)
188: *p++ |= WWU_TOUCHED;
189: }
190: wwredrawwin1(w, row1, row1x, dir);
191: wwredrawwin1(w, row2x - 1, row2 - leaveit, dir);
192: } else {
193: {
1.4 mpech 194: union ww_char *tmp;
195: union ww_char **cpp, **cqq;
1.1 deraadt 196:
197: cpp = &wwns[row2x];
198: cqq = cpp - 1;
199: tmp = *cqq;
200: for (i = row2x - row1x; --i > 0;)
201: *--cpp = *--cqq;
202: *cqq = tmp;
203: }
204: if (scrolled) {
1.4 mpech 205: char *p, *q;
1.1 deraadt 206:
207: p = &wwtouched[row2x];
208: q = p - 1;
209: for (i = row2x - row1x; --i > 0;)
210: *--p = *--q;
211: *q |= WWU_TOUCHED;
212: } else {
1.4 mpech 213: char *p;
1.1 deraadt 214:
215: p = &wwtouched[row1x];
216: for (i = row2x - row1x; --i >= 0;)
217: *p++ |= WWU_TOUCHED;
218: }
219: wwredrawwin1(w, row1 + leaveit, row1x + 1, dir);
220: wwredrawwin1(w, row2x, row2, dir);
221: }
222: } else {
223: if (scrolled) {
1.4 mpech 224: char *p;
1.1 deraadt 225:
226: p = &wwtouched[row1x];
227: for (i = row2x - row1x; --i >= 0;)
228: *p++ |= WWU_TOUCHED;
229: }
230: out:
231: if (dir > 0)
232: wwredrawwin1(w, row1, row2 - leaveit, dir);
233: else
234: wwredrawwin1(w, row1 + leaveit, row2, dir);
235: }
236: return scrolled;
237: }