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