xref: /illumos-gate/usr/src/lib/libcurses/screen/winsdelln.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  * contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include	<sys/types.h>
43*7c478bd9Sstevel@tonic-gate #include	<string.h>
44*7c478bd9Sstevel@tonic-gate #include	"curses_inc.h"
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate /*
47*7c478bd9Sstevel@tonic-gate  * Insert/delete lines
48*7c478bd9Sstevel@tonic-gate  * id < 0 : number of lines to delete
49*7c478bd9Sstevel@tonic-gate  * id > 0 : number of lines to insert
50*7c478bd9Sstevel@tonic-gate  */
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate int
winsdelln(WINDOW * win,int id)53*7c478bd9Sstevel@tonic-gate winsdelln(WINDOW *win, int id)
54*7c478bd9Sstevel@tonic-gate {
55*7c478bd9Sstevel@tonic-gate 	int	endy, endx, to, fr, num_lines, dir;
56*7c478bd9Sstevel@tonic-gate 	chtype	*sw;
57*7c478bd9Sstevel@tonic-gate 	char	*mk;
58*7c478bd9Sstevel@tonic-gate 	bool	savimmed, savesync;
59*7c478bd9Sstevel@tonic-gate 	short	x, y, quick, *begch, *endch;
60*7c478bd9Sstevel@tonic-gate #ifdef	_VR3_COMPAT_CODE
61*7c478bd9Sstevel@tonic-gate 	/* LINTED */
62*7c478bd9Sstevel@tonic-gate 	void		(*update_ptr)();
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 	/*
65*7c478bd9Sstevel@tonic-gate 	 * Null out the update pointer so that in wclrtoeol we do not
66*7c478bd9Sstevel@tonic-gate 	 * update the _y16 area but we wait till the bottom of this
67*7c478bd9Sstevel@tonic-gate 	 * function to do it in one fell swoop.
68*7c478bd9Sstevel@tonic-gate 	 */
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	if (_y16update) {
71*7c478bd9Sstevel@tonic-gate 		update_ptr = _y16update;
72*7c478bd9Sstevel@tonic-gate 		_y16update = NULL;
73*7c478bd9Sstevel@tonic-gate 	} else
74*7c478bd9Sstevel@tonic-gate 		update_ptr = NULL;
75*7c478bd9Sstevel@tonic-gate #endif	/* _VR3_COMPAT_CODE */
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	if ((win->_cury >= win->_tmarg) && (win->_cury <= win->_bmarg))
78*7c478bd9Sstevel@tonic-gate 		endy = win->_bmarg + 1;
79*7c478bd9Sstevel@tonic-gate 	else
80*7c478bd9Sstevel@tonic-gate 		endy = win->_maxy;
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	if (id < 0) {
83*7c478bd9Sstevel@tonic-gate 		/*
84*7c478bd9Sstevel@tonic-gate 		 * Check that the amount of lines to delete aren't larger
85*7c478bd9Sstevel@tonic-gate 		 * than the window.  We save num_lines only so that we
86*7c478bd9Sstevel@tonic-gate 		 * don't have to re-compute if the if comes out true.
87*7c478bd9Sstevel@tonic-gate 		 */
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 		if ((num_lines = win->_cury - endy) > id)
90*7c478bd9Sstevel@tonic-gate 			id = num_lines;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 		/*
93*7c478bd9Sstevel@tonic-gate 		 * "fr" is the line that we are coming "fr"om and
94*7c478bd9Sstevel@tonic-gate 		 * moving "to" the new place.  This is the offset which
95*7c478bd9Sstevel@tonic-gate 		 * we have to re-align our pointers by.
96*7c478bd9Sstevel@tonic-gate 		 * We want to start setting the current line's pointer
97*7c478bd9Sstevel@tonic-gate 		 * to point to the offset's line.  We want to move line "fr"
98*7c478bd9Sstevel@tonic-gate 		 * to line "to".
99*7c478bd9Sstevel@tonic-gate 		 */
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 		to = win->_cury;
102*7c478bd9Sstevel@tonic-gate 		fr = to - id;
103*7c478bd9Sstevel@tonic-gate 		num_lines = endy - fr;
104*7c478bd9Sstevel@tonic-gate 		dir = 1;
105*7c478bd9Sstevel@tonic-gate 	} else {
106*7c478bd9Sstevel@tonic-gate 		/* can't insert more lines than are in the region */
107*7c478bd9Sstevel@tonic-gate 		if ((num_lines = endy - win->_cury) < id)
108*7c478bd9Sstevel@tonic-gate 			id = num_lines;
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 		to = endy - 1;
111*7c478bd9Sstevel@tonic-gate 		fr = to - id;
112*7c478bd9Sstevel@tonic-gate 		num_lines = fr - (win->_cury - 1);
113*7c478bd9Sstevel@tonic-gate 		dir = -1;
114*7c478bd9Sstevel@tonic-gate 	}
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	/*
117*7c478bd9Sstevel@tonic-gate 	 * If this window has no parents or children, then we can manipulate
118*7c478bd9Sstevel@tonic-gate 	 * pointers to simulate insert/delete line.  Otherwise,
119*7c478bd9Sstevel@tonic-gate 	 * to propogate the changes to parents and siblings
120*7c478bd9Sstevel@tonic-gate 	 * we have to memcpy the text around.
121*7c478bd9Sstevel@tonic-gate 	 *
122*7c478bd9Sstevel@tonic-gate 	 * Set quick to tell us which we have to do.
123*7c478bd9Sstevel@tonic-gate 	 */
124*7c478bd9Sstevel@tonic-gate 	quick = ((win->_ndescs <= 0) && (win->_parent == NULL));
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	begch = win->_firstch;
127*7c478bd9Sstevel@tonic-gate 	endch = win->_lastch;
128*7c478bd9Sstevel@tonic-gate 	endx = win->_maxx;
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	for (; num_lines > 0; num_lines--, to += dir, fr += dir) {
131*7c478bd9Sstevel@tonic-gate 		/* can be done quickly */
132*7c478bd9Sstevel@tonic-gate 		if (quick) {
133*7c478bd9Sstevel@tonic-gate 			sw = win->_y[to];
134*7c478bd9Sstevel@tonic-gate 			win->_y[to] = win->_y[fr];
135*7c478bd9Sstevel@tonic-gate 			win->_y[fr] = sw;
136*7c478bd9Sstevel@tonic-gate 			if ((win == curscr) && _MARKS != NULL) {
137*7c478bd9Sstevel@tonic-gate 				mk = _MARKS[to];
138*7c478bd9Sstevel@tonic-gate 				_MARKS[to] = _MARKS[fr];
139*7c478bd9Sstevel@tonic-gate 				_MARKS[fr] = mk;
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 				/* for color terminal do the same for */
142*7c478bd9Sstevel@tonic-gate 				/* color marks */
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 				if (_COLOR_MARKS != NULL) {
145*7c478bd9Sstevel@tonic-gate 					mk = _COLOR_MARKS[to];
146*7c478bd9Sstevel@tonic-gate 					_COLOR_MARKS[to] = _COLOR_MARKS[fr];
147*7c478bd9Sstevel@tonic-gate 					_COLOR_MARKS[fr] = mk;
148*7c478bd9Sstevel@tonic-gate 				}
149*7c478bd9Sstevel@tonic-gate 			}
150*7c478bd9Sstevel@tonic-gate 		} else
151*7c478bd9Sstevel@tonic-gate 			/* slow update */
152*7c478bd9Sstevel@tonic-gate 			(void) memcpy((char *) win->_y[to], (char *)
153*7c478bd9Sstevel@tonic-gate 			    win->_y[fr], (endx * sizeof (chtype)));
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 		/*
157*7c478bd9Sstevel@tonic-gate 		 * If this is curscr, the firstch[] and lastch[]
158*7c478bd9Sstevel@tonic-gate 		 * arrays contain blank information.
159*7c478bd9Sstevel@tonic-gate 		 */
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 		if (win == curscr) {
162*7c478bd9Sstevel@tonic-gate 			begch[to] = begch[fr];
163*7c478bd9Sstevel@tonic-gate 			endch[to] = endch[fr];
164*7c478bd9Sstevel@tonic-gate 			_CURHASH[to] = _CURHASH[fr];
165*7c478bd9Sstevel@tonic-gate 		} else {
166*7c478bd9Sstevel@tonic-gate 			/* regular window, update the change structure */
167*7c478bd9Sstevel@tonic-gate 			begch[to] = 0;
168*7c478bd9Sstevel@tonic-gate 			endch[to] = endx - 1;
169*7c478bd9Sstevel@tonic-gate 		}
170*7c478bd9Sstevel@tonic-gate 	}
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	/* clear the insert/delete lines */
173*7c478bd9Sstevel@tonic-gate 	if (id < 0)
174*7c478bd9Sstevel@tonic-gate 		num_lines = endy - to;
175*7c478bd9Sstevel@tonic-gate 	else
176*7c478bd9Sstevel@tonic-gate 		num_lines = to - (win->_cury - 1);
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	if (num_lines > 0) {		/* Is this if needed ? */
179*7c478bd9Sstevel@tonic-gate 		savimmed = win->_immed;
180*7c478bd9Sstevel@tonic-gate 		savesync = win->_sync;
181*7c478bd9Sstevel@tonic-gate 		win->_immed = win->_sync = FALSE;
182*7c478bd9Sstevel@tonic-gate 		x = win->_curx;
183*7c478bd9Sstevel@tonic-gate 		y = win->_cury;
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 		win->_curx = 0;
186*7c478bd9Sstevel@tonic-gate 		for (; num_lines > 0; --num_lines, to += dir) {
187*7c478bd9Sstevel@tonic-gate 			/* LINTED */
188*7c478bd9Sstevel@tonic-gate 			win->_cury = (short) to;
189*7c478bd9Sstevel@tonic-gate 			(void) wclrtoeol(win);
190*7c478bd9Sstevel@tonic-gate 		}
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 		win->_curx = x;
193*7c478bd9Sstevel@tonic-gate 		win->_cury = y;
194*7c478bd9Sstevel@tonic-gate 		win->_immed = savimmed;
195*7c478bd9Sstevel@tonic-gate 		win->_sync = savesync;
196*7c478bd9Sstevel@tonic-gate 	}
197*7c478bd9Sstevel@tonic-gate 	win->_flags |= (_WINCHANGED|_WINSDEL);
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate #ifdef	_VR3_COMPAT_CODE
200*7c478bd9Sstevel@tonic-gate 	if (update_ptr) {
201*7c478bd9Sstevel@tonic-gate 		_y16update = update_ptr;
202*7c478bd9Sstevel@tonic-gate 		(*_y16update)(win, endy - y, endx, y, 0);
203*7c478bd9Sstevel@tonic-gate 	}
204*7c478bd9Sstevel@tonic-gate #endif	/* _VR3_COMPAT_CODE */
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	if (win->_sync)
207*7c478bd9Sstevel@tonic-gate 		wsyncup(win);
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	return ((win != curscr && savimmed) ? wrefresh(win) : OK);
210*7c478bd9Sstevel@tonic-gate }
211