xref: /netbsd-src/lib/libcurses/insdelln.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: insdelln.c,v 1.10 2003/07/29 16:42:55 dsl 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.10 2003/07/29 16:42:55 dsl 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: (%p) 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);
111 			}
112 		}
113 		for (y = win->cury - 1 + lines; y >= win->cury; --y)
114 			for (i = 0; i < win->maxx; i++) {
115 				win->lines[y]->line[i].ch = ' ';
116 				win->lines[y]->line[i].bch = win->bch;
117 				win->lines[y]->line[i].attr = 0;
118 				win->lines[y]->line[i].battr = win->battr;
119 			}
120 		for (y = last; y >= win->cury; --y)
121 			__touchline(win, y, 0, (int) win->maxx - 1);
122 	} else {
123 		/* Delete lines */
124 		lines = 0 - lines;
125 		if (win->cury < win->scr_t || win->cury > win->scr_b) {
126 			/*  Outside scrolling region */
127 			if (lines > win->maxy - win->cury)
128 				lines = win->maxy - win->cury;
129 			last = win->maxy;
130 		} else {
131 			/* Inside scrolling region */
132 			if (lines > win->scr_b + 1 - win->cury)
133 				lines = win->scr_b + 1 - win->cury;
134 			last = win->scr_b + 1;
135 		}
136 		for (y = win->cury; y < last - lines; y++) {
137 			win->lines[y]->flags &= ~__ISPASTEOL;
138 			win->lines[y + lines]->flags &= ~__ISPASTEOL;
139 			if (win->orig == NULL) {
140 				temp = win->lines[y];
141 				win->lines[y] = win->lines[y + lines];
142 				win->lines[y + lines] = temp;
143 			} else {
144 				(void) memcpy(win->lines[y]->line,
145 				    win->lines[y + lines]->line,
146 				    (size_t) win->maxx * __LDATASIZE);
147 			}
148 		}
149 		for (y = last - lines; y < last; y++)
150 			for (i = 0; i < win->maxx; i++) {
151 				win->lines[y]->line[i].ch = ' ';
152 				win->lines[y]->line[i].bch = win->bch;
153 				win->lines[y]->line[i].attr = 0;
154 				win->lines[y]->line[i].battr = win->battr;
155 			}
156 		for (y = win->cury; y < last; y++)
157 			__touchline(win, y, 0, (int) win->maxx - 1);
158 	}
159 	if (win->orig != NULL)
160 		__id_subwins(win->orig);
161 	return (OK);
162 }
163