xref: /netbsd-src/lib/libcurses/insdelln.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: insdelln.c,v 1.18 2017/01/06 13:53:18 roy 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.18 2017/01/06 13:53:18 roy 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 #ifdef DEBUG
79 	__CTRACE(__CTRACE_LINE,
80 	    "winsdelln: (%p) cury=%d lines=%d\n", win, win->cury, nlines);
81 #endif
82 
83 	if (!nlines)
84 		return OK;
85 
86 	if (__using_color && win != curscr)
87 		attr = win->battr & __COLOR;
88 	else
89 		attr = 0;
90 
91 	if (nlines > 0) {
92 		/* Insert lines */
93 		if (win->cury < win->scr_t || win->cury > win->scr_b) {
94 			/*  Outside scrolling region */
95 			if (nlines > win->maxy - win->cury)
96 				nlines = win->maxy - win->cury;
97 			last = win->maxy - 1;
98 		} else {
99 			/* Inside scrolling region */
100 			if (nlines > win->scr_b + 1 - win->cury)
101 				nlines = win->scr_b + 1 - win->cury;
102 			last = win->scr_b;
103 		}
104 		for (y = last - nlines; y >= win->cury; --y) {
105 			win->alines[y]->flags &= ~__ISPASTEOL;
106 			win->alines[y + nlines]->flags &= ~__ISPASTEOL;
107 			if (win->orig == NULL) {
108 				temp = win->alines[y + nlines];
109 				win->alines[y + nlines] = win->alines[y];
110 				win->alines[y] = temp;
111 			} else {
112 				(void)memcpy(win->alines[y + nlines]->line,
113 				    win->alines[y]->line,
114 				    (size_t)win->maxx * __LDATASIZE);
115 			}
116 		}
117 		for (y = win->cury - 1 + nlines; y >= win->cury; --y)
118 			for (i = 0; i < win->maxx; i++) {
119 				win->alines[y]->line[i].ch = win->bch;
120 				win->alines[y]->line[i].attr = attr;
121 #ifndef HAVE_WCHAR
122 				win->alines[y]->line[i].ch = win->bch;
123 #else
124 				win->alines[y]->line[i].ch
125 					= (wchar_t)btowc((int)win->bch );
126 				lp = &win->alines[y]->line[i];
127 				if (_cursesi_copy_nsp(win->bnsp, lp) == ERR)
128 					return ERR;
129 				SET_WCOL(*lp, 1);
130 #endif /* HAVE_WCHAR */
131 			}
132 		for (y = last; y >= win->cury; --y)
133 			__touchline(win, y, 0, (int)win->maxx - 1);
134 	} else {
135 		/* Delete nlines */
136 		nlines = 0 - nlines;
137 		if (win->cury < win->scr_t || win->cury > win->scr_b) {
138 			/*  Outside scrolling region */
139 			if (nlines > win->maxy - win->cury)
140 				nlines = win->maxy - win->cury;
141 			last = win->maxy;
142 		} else {
143 			/* Inside scrolling region */
144 			if (nlines > win->scr_b + 1 - win->cury)
145 				nlines = win->scr_b + 1 - win->cury;
146 			last = win->scr_b + 1;
147 		}
148 		for (y = win->cury; y < last - nlines; y++) {
149 			win->alines[y]->flags &= ~__ISPASTEOL;
150 			win->alines[y + nlines]->flags &= ~__ISPASTEOL;
151 			if (win->orig == NULL) {
152 				temp = win->alines[y];
153 				win->alines[y] = win->alines[y + nlines];
154 				win->alines[y + nlines] = temp;
155 			} else {
156 				(void)memcpy(win->alines[y]->line,
157 				    win->alines[y + nlines]->line,
158 				    (size_t)win->maxx * __LDATASIZE);
159 			}
160 		}
161 		for (y = last - nlines; y < last; y++)
162 			for (i = 0; i < win->maxx; i++) {
163 				win->alines[y]->line[i].ch = win->bch;
164 				win->alines[y]->line[i].attr = attr;
165 #ifndef HAVE_WCHAR
166 				win->alines[y]->line[i].ch = win->bch;
167 #else
168 				win->alines[y]->line[i].ch
169 					= (wchar_t)btowc((int)win->bch);
170 				lp = &win->alines[y]->line[i];
171 				SET_WCOL( *lp, 1 );
172 				if (_cursesi_copy_nsp(win->bnsp, lp) == ERR)
173 					return ERR;
174 #endif /* HAVE_WCHAR */
175 			}
176 		for (y = win->cury; y < last; y++)
177 			__touchline(win, y, 0, (int)win->maxx - 1);
178 	}
179 	if (win->orig != NULL)
180 		__id_subwins(win->orig);
181 	__sync(win);
182 	return OK;
183 }
184