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