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