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