xref: /netbsd-src/lib/libcurses/insdelln.c (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: insdelln.c,v 1.19 2021/09/06 07:03:49 rin 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: insdelln.c,v 1.19 2021/09/06 07:03:49 rin Exp $");
35 #endif				/* not lint */
36 
37 /*
38  * Based on deleteln.c and insertln.c -
39  * Copyright (c) 1981, 1993, 1994
40  *	The Regents of the University of California.  All rights reserved.
41  */
42 
43 #include <string.h>
44 #include <stdlib.h>
45 
46 #include "curses.h"
47 #include "curses_private.h"
48 
49 #ifndef _CURSES_USE_MACROS
50 
51 /*
52  * insdelln --
53  *	Insert or delete lines on stdscr, leaving (cury, curx) unchanged.
54  */
55 int
56 insdelln(int nlines)
57 {
58 
59 	return winsdelln(stdscr, nlines);
60 }
61 
62 #endif
63 
64 /*
65  * winsdelln --
66  *	Insert or delete lines on the window, leaving (cury, curx) unchanged.
67  */
68 int
69 winsdelln(WINDOW *win, int nlines)
70 {
71 	int     y, i, last;
72 	__LINE *temp;
73 #ifdef HAVE_WCHAR
74 	__LDATA *lp;
75 #endif /* HAVE_WCHAR */
76 	attr_t	attr;
77 
78 	__CTRACE(__CTRACE_LINE,
79 	    "winsdelln: (%p) cury=%d lines=%d\n", win, win->cury, nlines);
80 
81 	if (!nlines)
82 		return OK;
83 
84 	if (__using_color && win != curscr)
85 		attr = win->battr & __COLOR;
86 	else
87 		attr = 0;
88 
89 	if (nlines > 0) {
90 		/* Insert lines */
91 		if (win->cury < win->scr_t || win->cury > win->scr_b) {
92 			/*  Outside scrolling region */
93 			if (nlines > win->maxy - win->cury)
94 				nlines = win->maxy - win->cury;
95 			last = win->maxy - 1;
96 		} else {
97 			/* Inside scrolling region */
98 			if (nlines > win->scr_b + 1 - win->cury)
99 				nlines = win->scr_b + 1 - win->cury;
100 			last = win->scr_b;
101 		}
102 		for (y = last - nlines; y >= win->cury; --y) {
103 			win->alines[y]->flags &= ~__ISPASTEOL;
104 			win->alines[y + nlines]->flags &= ~__ISPASTEOL;
105 			if (win->orig == NULL) {
106 				temp = win->alines[y + nlines];
107 				win->alines[y + nlines] = win->alines[y];
108 				win->alines[y] = temp;
109 			} else {
110 				(void)memcpy(win->alines[y + nlines]->line,
111 				    win->alines[y]->line,
112 				    (size_t)win->maxx * __LDATASIZE);
113 			}
114 		}
115 		for (y = win->cury - 1 + nlines; y >= win->cury; --y)
116 			for (i = 0; i < win->maxx; i++) {
117 				win->alines[y]->line[i].ch = win->bch;
118 				win->alines[y]->line[i].attr = attr;
119 #ifndef HAVE_WCHAR
120 				win->alines[y]->line[i].ch = win->bch;
121 #else
122 				win->alines[y]->line[i].ch
123 					= (wchar_t)btowc((int)win->bch );
124 				lp = &win->alines[y]->line[i];
125 				if (_cursesi_copy_nsp(win->bnsp, lp) == ERR)
126 					return ERR;
127 				SET_WCOL(*lp, 1);
128 #endif /* HAVE_WCHAR */
129 			}
130 		for (y = last; y >= win->cury; --y)
131 			__touchline(win, y, 0, (int)win->maxx - 1);
132 	} else {
133 		/* Delete nlines */
134 		nlines = 0 - nlines;
135 		if (win->cury < win->scr_t || win->cury > win->scr_b) {
136 			/*  Outside scrolling region */
137 			if (nlines > win->maxy - win->cury)
138 				nlines = win->maxy - win->cury;
139 			last = win->maxy;
140 		} else {
141 			/* Inside scrolling region */
142 			if (nlines > win->scr_b + 1 - win->cury)
143 				nlines = win->scr_b + 1 - win->cury;
144 			last = win->scr_b + 1;
145 		}
146 		for (y = win->cury; y < last - nlines; y++) {
147 			win->alines[y]->flags &= ~__ISPASTEOL;
148 			win->alines[y + nlines]->flags &= ~__ISPASTEOL;
149 			if (win->orig == NULL) {
150 				temp = win->alines[y];
151 				win->alines[y] = win->alines[y + nlines];
152 				win->alines[y + nlines] = temp;
153 			} else {
154 				(void)memcpy(win->alines[y]->line,
155 				    win->alines[y + nlines]->line,
156 				    (size_t)win->maxx * __LDATASIZE);
157 			}
158 		}
159 		for (y = last - nlines; y < last; y++)
160 			for (i = 0; i < win->maxx; i++) {
161 				win->alines[y]->line[i].ch = win->bch;
162 				win->alines[y]->line[i].attr = attr;
163 #ifndef HAVE_WCHAR
164 				win->alines[y]->line[i].ch = win->bch;
165 #else
166 				win->alines[y]->line[i].ch
167 					= (wchar_t)btowc((int)win->bch);
168 				lp = &win->alines[y]->line[i];
169 				SET_WCOL( *lp, 1 );
170 				if (_cursesi_copy_nsp(win->bnsp, lp) == ERR)
171 					return ERR;
172 #endif /* HAVE_WCHAR */
173 			}
174 		for (y = win->cury; y < last; y++)
175 			__touchline(win, y, 0, (int)win->maxx - 1);
176 	}
177 	if (win->orig != NULL)
178 		__id_subwins(win->orig);
179 	__sync(win);
180 	return OK;
181 }
182