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