xref: /netbsd-src/lib/libcurses/insdelln.c (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /*	$NetBSD: insdelln.c,v 1.12 2006/02/05 17:39:52 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.12 2006/02/05 17:39:52 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 	attr_t	attr;
79 
80 #ifdef DEBUG
81 	__CTRACE("winsdelln: (%p) cury=%d lines=%d\n", win, win->cury,
82 	    lines);
83 #endif
84 
85 	if (!lines)
86 		return(OK);
87 
88 	if (__using_color && win != curscr)
89 		attr = win->battr & __COLOR;
90 	else
91 		attr = 0;
92 
93 	if (lines > 0) {
94 		/* Insert lines */
95 		if (win->cury < win->scr_t || win->cury > win->scr_b) {
96 			/*  Outside scrolling region */
97 			if (lines > win->maxy - win->cury)
98 				lines = win->maxy - win->cury;
99 			last = win->maxy - 1;
100 		} else {
101 			/* Inside scrolling region */
102 			if (lines > win->scr_b + 1 - win->cury)
103 				lines = win->scr_b + 1 - win->cury;
104 			last = win->scr_b;
105 		}
106 		for (y = last - lines; y >= win->cury; --y) {
107 			win->lines[y]->flags &= ~__ISPASTEOL;
108 			win->lines[y + lines]->flags &= ~__ISPASTEOL;
109 			if (win->orig == NULL) {
110 				temp = win->lines[y + lines];
111 				win->lines[y + lines] = win->lines[y];
112 				win->lines[y] = temp;
113 			} else {
114 				(void) memcpy(win->lines[y + lines]->line,
115 				    win->lines[y]->line,
116 				    (size_t) win->maxx * __LDATASIZE);
117 			}
118 		}
119 		for (y = win->cury - 1 + lines; y >= win->cury; --y)
120 			for (i = 0; i < win->maxx; i++) {
121 				win->lines[y]->line[i].ch = win->bch;
122 				win->lines[y]->line[i].attr = attr;
123 			}
124 		for (y = last; y >= win->cury; --y)
125 			__touchline(win, y, 0, (int) win->maxx - 1);
126 	} else {
127 		/* Delete lines */
128 		lines = 0 - lines;
129 		if (win->cury < win->scr_t || win->cury > win->scr_b) {
130 			/*  Outside scrolling region */
131 			if (lines > win->maxy - win->cury)
132 				lines = win->maxy - win->cury;
133 			last = win->maxy;
134 		} else {
135 			/* Inside scrolling region */
136 			if (lines > win->scr_b + 1 - win->cury)
137 				lines = win->scr_b + 1 - win->cury;
138 			last = win->scr_b + 1;
139 		}
140 		for (y = win->cury; y < last - lines; y++) {
141 			win->lines[y]->flags &= ~__ISPASTEOL;
142 			win->lines[y + lines]->flags &= ~__ISPASTEOL;
143 			if (win->orig == NULL) {
144 				temp = win->lines[y];
145 				win->lines[y] = win->lines[y + lines];
146 				win->lines[y + lines] = temp;
147 			} else {
148 				(void) memcpy(win->lines[y]->line,
149 				    win->lines[y + lines]->line,
150 				    (size_t) win->maxx * __LDATASIZE);
151 			}
152 		}
153 		for (y = last - lines; y < last; y++)
154 			for (i = 0; i < win->maxx; i++) {
155 				win->lines[y]->line[i].ch = win->bch;
156 				win->lines[y]->line[i].attr = attr;
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