xref: /netbsd-src/lib/libcurses/insdelln.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: insdelln.c,v 1.14 2007/05/28 15:01:56 blymn 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.14 2007/05/28 15:01:56 blymn 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 #include <stdlib.h>
52 
53 #include "curses.h"
54 #include "curses_private.h"
55 
56 #ifndef _CURSES_USE_MACROS
57 
58 /*
59  * insdelln --
60  *	Insert or delete lines on stdscr, leaving (cury, curx) unchanged.
61  */
62 int
63 insdelln(int lines)
64 {
65 	return winsdelln(stdscr, lines);
66 }
67 
68 #endif
69 
70 /*
71  * winsdelln --
72  *	Insert or delete lines on the window, leaving (cury, curx) unchanged.
73  */
74 int
75 winsdelln(WINDOW *win, int lines)
76 {
77 	int     y, i, last;
78 	__LINE *temp;
79 #ifdef HAVE_WCHAR
80 	__LDATA *lp;
81 #endif /* HAVE_WCHAR */
82 	attr_t	attr;
83 
84 #ifdef DEBUG
85 	__CTRACE(__CTRACE_LINE,
86 	    "winsdelln: (%p) cury=%d lines=%d\n", win, win->cury, lines);
87 #endif
88 
89 	if (!lines)
90 		return(OK);
91 
92 	if (__using_color && win != curscr)
93 		attr = win->battr & __COLOR;
94 	else
95 		attr = 0;
96 
97 	if (lines > 0) {
98 		/* Insert lines */
99 		if (win->cury < win->scr_t || win->cury > win->scr_b) {
100 			/*  Outside scrolling region */
101 			if (lines > win->maxy - win->cury)
102 				lines = win->maxy - win->cury;
103 			last = win->maxy - 1;
104 		} else {
105 			/* Inside scrolling region */
106 			if (lines > win->scr_b + 1 - win->cury)
107 				lines = win->scr_b + 1 - win->cury;
108 			last = win->scr_b;
109 		}
110 		for (y = last - lines; y >= win->cury; --y) {
111 			win->lines[y]->flags &= ~__ISPASTEOL;
112 			win->lines[y + lines]->flags &= ~__ISPASTEOL;
113 			if (win->orig == NULL) {
114 				temp = win->lines[y + lines];
115 				win->lines[y + lines] = win->lines[y];
116 				win->lines[y] = temp;
117 			} else {
118 				(void) memcpy(win->lines[y + lines]->line,
119 				    win->lines[y]->line,
120 				    (size_t) win->maxx * __LDATASIZE);
121 			}
122 		}
123 		for (y = win->cury - 1 + lines; y >= win->cury; --y)
124 			for (i = 0; i < win->maxx; i++) {
125 				win->lines[y]->line[i].ch = win->bch;
126 				win->lines[y]->line[i].attr = attr;
127 #ifndef HAVE_WCHAR
128 				win->lines[y]->line[i].ch = win->bch;
129 #else
130 				win->lines[y]->line[i].ch
131 					= ( wchar_t )btowc(( int ) win->bch );
132 				lp = &win->lines[y]->line[i];
133 				if (_cursesi_copy_nsp(win->bnsp, lp) == ERR)
134 					return ERR;
135 				SET_WCOL( *lp, 1 );
136 #endif /* HAVE_WCHAR */
137 			}
138 		for (y = last; y >= win->cury; --y)
139 			__touchline(win, y, 0, (int) win->maxx - 1);
140 	} else {
141 		/* Delete lines */
142 		lines = 0 - lines;
143 		if (win->cury < win->scr_t || win->cury > win->scr_b) {
144 			/*  Outside scrolling region */
145 			if (lines > win->maxy - win->cury)
146 				lines = win->maxy - win->cury;
147 			last = win->maxy;
148 		} else {
149 			/* Inside scrolling region */
150 			if (lines > win->scr_b + 1 - win->cury)
151 				lines = win->scr_b + 1 - win->cury;
152 			last = win->scr_b + 1;
153 		}
154 		for (y = win->cury; y < last - lines; y++) {
155 			win->lines[y]->flags &= ~__ISPASTEOL;
156 			win->lines[y + lines]->flags &= ~__ISPASTEOL;
157 			if (win->orig == NULL) {
158 				temp = win->lines[y];
159 				win->lines[y] = win->lines[y + lines];
160 				win->lines[y + lines] = temp;
161 			} else {
162 				(void) memcpy(win->lines[y]->line,
163 				    win->lines[y + lines]->line,
164 				    (size_t) win->maxx * __LDATASIZE);
165 			}
166 		}
167 		for (y = last - lines; y < last; y++)
168 			for (i = 0; i < win->maxx; i++) {
169 				win->lines[y]->line[i].ch = win->bch;
170 				win->lines[y]->line[i].attr = attr;
171 #ifndef HAVE_WCHAR
172 				win->lines[y]->line[i].ch = win->bch;
173 #else
174 				win->lines[y]->line[i].ch
175 					= (wchar_t)btowc((int) win->bch);
176 				lp = &win->lines[y]->line[i];
177 				SET_WCOL( *lp, 1 );
178 				if (_cursesi_copy_nsp(win->bnsp, lp) == ERR)
179 					return ERR;
180 #endif /* HAVE_WCHAR */
181 			}
182 		for (y = win->cury; y < last; y++)
183 			__touchline(win, y, 0, (int) win->maxx - 1);
184 	}
185 	if (win->orig != NULL)
186 		__id_subwins(win->orig);
187 	return (OK);
188 }
189