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 2004 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 /*
43  * This routine writes parts of Srcwin onto Dstwin,
44  * either non-destructively (over_lay = TRUE) or destructively
45  * (over_lay = FALSE).
46  */
47 
48 #include	<string.h>
49 #include	<sys/types.h>
50 #include	"curses_inc.h"
51 
52 int
53 copywin(WINDOW *Srcwin, WINDOW *Dstwin,
54 	int minRowSrc, int minColSrc, int minRowDst,
55 	int minColDst, int maxRowDst, int maxColDst,
56 	int over_lay)
57 {
58 	int		ySrc, yDst, which_copy, t;
59 	int		height = (maxRowDst - minRowDst) + 1,
60 			width = (maxColDst - minColDst) + 1;
61 	chtype		**_yDst = Dstwin->_y, **_ySrc = Srcwin->_y,
62 			bkSrc = Srcwin->_bkgd, atDst = Dstwin->_attrs,
63 			*spSrc, *spDst, *epSrc, *epDst, *savepS,
64 			*savepD, width_bytes, numcopied;
65 
66 #ifdef	DEBUG
67 	if (outf)
68 		fprintf(outf, "copywin(%0.2o, %0.2o);\n", Srcwin, Dstwin);
69 #endif	/* DEBUG */
70 
71 	/*
72 	 * If we are going to be copying from curscr,
73 	 * first offset into curscr the offset the Dstwin knows about.
74 	 */
75 	if (Srcwin == curscr)
76 		minRowSrc += Dstwin->_yoffset;
77 
78 	/*
79 	 * There are three types of copy.
80 	 * 0 - Straight memcpy allowed
81 	 * 1 - We have to first check to see if the source character is a blank
82 	 * 2 - Dstwin has attributes or bkgd that must changed
83 	 * on a char-by-char basis.
84 	 */
85 	if ((which_copy = (over_lay) ? 1 :
86 	    (2 * ((Dstwin->_attrs != A_NORMAL) ||
87 	    (Dstwin->_bkgd != _BLNKCHAR)))) == 0)
88 		width_bytes = width * (int)sizeof (chtype);
89 
90 	/* for each Row */
91 	for (ySrc = minRowSrc, yDst = minRowDst; height-- > 0; ySrc++, yDst++) {
92 		if (which_copy) {
93 			spSrc = &_ySrc[ySrc][minColSrc];
94 			spDst = &_yDst[yDst][minColDst];
95 			numcopied = width;
96 
97 			epSrc = savepS = &_ySrc[ySrc][maxColDst];
98 			epDst = savepD = &_yDst[yDst][maxColDst];
99 		/* only copy into an area bounded by whole characters */
100 			for (; spDst <= epDst; spSrc++, spDst++)
101 				if (!ISCBIT(*spDst))
102 					break;
103 			if (spDst > epDst)
104 				continue;
105 			for (; epDst >= spDst; --epDst, --epSrc)
106 				if (!ISCBIT(*epDst))
107 					break;
108 			t = _curs_scrwidth[TYPE(RBYTE(*epDst))] - 1;
109 			if (epDst+t <= savepD)
110 				epDst += t, epSrc += t;
111 			else
112 				epDst -= 1, epSrc -= 1;
113 			if (epDst < spDst)
114 				continue;
115 			/* don't copy partial characters */
116 			for (; spSrc <= epSrc; ++spSrc, ++spDst)
117 				if (!ISCBIT(*spSrc))
118 					break;
119 			if (spSrc > epSrc)
120 				continue;
121 			for (; epSrc >= spSrc; --epSrc, --epDst)
122 				if (!ISCBIT(*epSrc))
123 					break;
124 			t = _curs_scrwidth[TYPE(RBYTE(*epSrc))] - 1;
125 			if (epSrc+t <= savepS)
126 				epSrc += t, epDst += t;
127 			else
128 				epSrc -= 1, epDst -= 1;
129 			if (epSrc < spSrc)
130 				continue;
131 		/* make sure that the copied-to place is clean */
132 			if (ISCBIT(*spDst))
133 				(void) _mbclrch(Dstwin, minRowDst,
134 				    /*LINTED*/
135 				    (intptr_t)(spDst - *_yDst[yDst]));
136 			if (ISCBIT(*epDst))
137 				(void) _mbclrch(Dstwin, minRowDst,
138 				    /*LINTED*/
139 				    (intptr_t)(epDst - *_yDst[yDst]));
140 			/*LINTED*/
141 			numcopied = (chtype) (epDst - spDst + 1);
142 
143 			if (which_copy == 1) {		/* overlay */
144 				for (; numcopied-- > 0; spSrc++, spDst++)
145 			/* Check to see if the char is a "blank/bkgd". */
146 					if (*spSrc != bkSrc)
147 						*spDst = *spSrc | atDst;
148 			} else {
149 				for (; numcopied-- > 0; spSrc++, spDst++)
150 					*spDst = *spSrc | atDst;
151 			}
152 		} else {
153 			/* ... copy all chtypes */
154 			(void) memcpy((char *)&_yDst[yDst][minColDst],
155 			    (char *)&_ySrc[ySrc][minColSrc], width_bytes);
156 		}
157 
158 		/* note that the line has changed */
159 		if (minColDst < Dstwin->_firstch[yDst])
160 			/*LINTED*/
161 			Dstwin->_firstch[yDst] = (short)minColDst;
162 		if (maxColDst > Dstwin->_lastch[yDst])
163 			/*LINTED*/
164 			Dstwin->_lastch[yDst] = (short)maxColDst;
165 	}
166 
167 #ifdef	_VR3_COMPAT_CODE
168 	if (_y16update) {
169 		(*_y16update)(Dstwin, (maxRowDst - minRowDst) + 1,
170 		    (maxColDst - minColDst) + 1, minRowDst, minColDst);
171 	}
172 #endif	/* _VR3_COMPAT_CODE */
173 
174 	/* note that something in Dstwin has changed */
175 	Dstwin->_flags |= _WINCHANGED;
176 
177 	if (Dstwin->_sync)
178 		wsyncup(Dstwin);
179 
180 	return (Dstwin->_immed ? wrefresh(Dstwin) : OK);
181 }
182