insdelln.c revision 1.15
1/*	$NetBSD: insdelln.c,v 1.15 2008/04/28 20:23:01 martin 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.15 2008/04/28 20:23:01 martin 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 */
55int
56insdelln(int lines)
57{
58	return winsdelln(stdscr, lines);
59}
60
61#endif
62
63/*
64 * winsdelln --
65 *	Insert or delete lines on the window, leaving (cury, curx) unchanged.
66 */
67int
68winsdelln(WINDOW *win, int lines)
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, lines);
80#endif
81
82	if (!lines)
83		return(OK);
84
85	if (__using_color && win != curscr)
86		attr = win->battr & __COLOR;
87	else
88		attr = 0;
89
90	if (lines > 0) {
91		/* Insert lines */
92		if (win->cury < win->scr_t || win->cury > win->scr_b) {
93			/*  Outside scrolling region */
94			if (lines > win->maxy - win->cury)
95				lines = win->maxy - win->cury;
96			last = win->maxy - 1;
97		} else {
98			/* Inside scrolling region */
99			if (lines > win->scr_b + 1 - win->cury)
100				lines = win->scr_b + 1 - win->cury;
101			last = win->scr_b;
102		}
103		for (y = last - lines; y >= win->cury; --y) {
104			win->lines[y]->flags &= ~__ISPASTEOL;
105			win->lines[y + lines]->flags &= ~__ISPASTEOL;
106			if (win->orig == NULL) {
107				temp = win->lines[y + lines];
108				win->lines[y + lines] = win->lines[y];
109				win->lines[y] = temp;
110			} else {
111				(void) memcpy(win->lines[y + lines]->line,
112				    win->lines[y]->line,
113				    (size_t) win->maxx * __LDATASIZE);
114			}
115		}
116		for (y = win->cury - 1 + lines; y >= win->cury; --y)
117			for (i = 0; i < win->maxx; i++) {
118				win->lines[y]->line[i].ch = win->bch;
119				win->lines[y]->line[i].attr = attr;
120#ifndef HAVE_WCHAR
121				win->lines[y]->line[i].ch = win->bch;
122#else
123				win->lines[y]->line[i].ch
124					= ( wchar_t )btowc(( int ) win->bch );
125				lp = &win->lines[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 lines */
135		lines = 0 - lines;
136		if (win->cury < win->scr_t || win->cury > win->scr_b) {
137			/*  Outside scrolling region */
138			if (lines > win->maxy - win->cury)
139				lines = win->maxy - win->cury;
140			last = win->maxy;
141		} else {
142			/* Inside scrolling region */
143			if (lines > win->scr_b + 1 - win->cury)
144				lines = win->scr_b + 1 - win->cury;
145			last = win->scr_b + 1;
146		}
147		for (y = win->cury; y < last - lines; y++) {
148			win->lines[y]->flags &= ~__ISPASTEOL;
149			win->lines[y + lines]->flags &= ~__ISPASTEOL;
150			if (win->orig == NULL) {
151				temp = win->lines[y];
152				win->lines[y] = win->lines[y + lines];
153				win->lines[y + lines] = temp;
154			} else {
155				(void) memcpy(win->lines[y]->line,
156				    win->lines[y + lines]->line,
157				    (size_t) win->maxx * __LDATASIZE);
158			}
159		}
160		for (y = last - lines; y < last; y++)
161			for (i = 0; i < win->maxx; i++) {
162				win->lines[y]->line[i].ch = win->bch;
163				win->lines[y]->line[i].attr = attr;
164#ifndef HAVE_WCHAR
165				win->lines[y]->line[i].ch = win->bch;
166#else
167				win->lines[y]->line[i].ch
168					= (wchar_t)btowc((int) win->bch);
169				lp = &win->lines[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