xref: /netbsd/lib/libcurses/insdelln.c (revision bf9ec67e)
1 /*	$NetBSD: insdelln.c,v 1.8 2001/04/20 12:56:08 jdc Exp $	*/
2 
3 /*
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Julian Coleman.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 #ifndef lint
41 __RCSID("$NetBSD: insdelln.c,v 1.8 2001/04/20 12:56:08 jdc Exp $");
42 #endif				/* not lint */
43 
44 /*
45  * Based on deleteln.c and insertln.c -
46  * Copyright (c) 1981, 1993, 1994
47  *	The Regents of the University of California.  All rights reserved.
48  */
49 
50 #include <string.h>
51 
52 #include "curses.h"
53 #include "curses_private.h"
54 
55 #ifndef _CURSES_USE_MACROS
56 
57 /*
58  * insdelln --
59  *	Insert or delete lines on stdscr, leaving (cury, curx) unchanged.
60  */
61 int
62 insdelln(int lines)
63 {
64 	return winsdelln(stdscr, lines);
65 }
66 
67 #endif
68 
69 /*
70  * winsdelln --
71  *	Insert or delete lines on the window, leaving (cury, curx) unchanged.
72  */
73 int
74 winsdelln(WINDOW *win, int lines)
75 {
76 	int     y, i, last;
77 	__LINE *temp;
78 
79 #ifdef DEBUG
80 	__CTRACE("winsdelln: (%0.2o) cury=%d lines=%d\n", win, win->cury,
81 	    lines);
82 #endif
83 
84 	if (!lines)
85 		return(OK);
86 
87 	if (lines > 0) {
88 		/* Insert lines */
89 		if (win->cury < win->scr_t || win->cury > win->scr_b) {
90 			/*  Outside scrolling region */
91 			if (lines > win->maxy - win->cury)
92 				lines = win->maxy - win->cury;
93 			last = win->maxy - 1;
94 		} else {
95 			/* Inside scrolling region */
96 			if (lines > win->scr_b + 1 - win->cury)
97 				lines = win->scr_b + 1 - win->cury;
98 			last = win->scr_b;
99 		}
100 		for (y = last - lines; y >= win->cury; --y) {
101 			win->lines[y]->flags &= ~__ISPASTEOL;
102 			win->lines[y + lines]->flags &= ~__ISPASTEOL;
103 			if (win->orig == NULL) {
104 				temp = win->lines[y + lines];
105 				win->lines[y + lines] = win->lines[y];
106 				win->lines[y] = temp;
107 			} else {
108 				(void) memcpy(win->lines[y + lines]->line,
109 				    win->lines[y]->line,
110 				    (size_t) win->maxx * __LDATASIZE * lines);
111 				temp = win->lines[y];
112 			}
113 		}
114 		for (y = win->cury - 1 + lines; y >= win->cury; --y)
115 			for (i = 0; i < win->maxx; i++) {
116 				win->lines[y]->line[i].ch = ' ';
117 				win->lines[y]->line[i].bch = win->bch;
118 				win->lines[y]->line[i].attr = 0;
119 				win->lines[y]->line[i].battr = win->battr;
120 			}
121 		for (y = last; y >= win->cury; --y)
122 			__touchline(win, y, 0, (int) win->maxx - 1);
123 	} else {
124 		/* Delete lines */
125 		lines = 0 - lines;
126 		if (win->cury < win->scr_t || win->cury > win->scr_b) {
127 			/*  Outside scrolling region */
128 			if (lines > win->maxy - win->cury)
129 				lines = win->maxy - win->cury;
130 			last = win->maxy;
131 		} else {
132 			/* Inside scrolling region */
133 			if (lines > win->scr_b + 1 - win->cury)
134 				lines = win->scr_b + 1 - win->cury;
135 			last = win->scr_b + 1;
136 		}
137 		for (y = win->cury; y < last - lines; y++) {
138 			win->lines[y]->flags &= ~__ISPASTEOL;
139 			win->lines[y + lines]->flags &= ~__ISPASTEOL;
140 			if (win->orig == NULL) {
141 				temp = win->lines[y];
142 				win->lines[y] = win->lines[y + lines];
143 				win->lines[y + lines] = temp;
144 			} else {
145 				(void) memcpy(win->lines[y]->line,
146 				    win->lines[y + lines]->line,
147 				    (size_t) win->maxx * __LDATASIZE * lines);
148 				temp = win->lines[y + lines];
149 			}
150 		}
151 		for (y = last - lines; y < last; y++)
152 			for (i = 0; i < win->maxx; i++) {
153 				win->lines[y]->line[i].ch = ' ';
154 				win->lines[y]->line[i].bch = win->bch;
155 				win->lines[y]->line[i].attr = 0;
156 				win->lines[y]->line[i].battr = win->battr;
157 			}
158 		for (y = win->cury; y < last; y++)
159 			__touchline(win, y, 0, (int) win->maxx - 1);
160 	}
161 	if (win->orig != NULL)
162 		__id_subwins(win->orig);
163 	return (OK);
164 }
165