xref: /netbsd-src/lib/libcurses/copywin.c (revision 6348e3f32a4431b45d07e1da16fd7b12725a45e7)
1 /*	$NetBSD: copywin.c,v 1.22 2024/12/23 02:58:03 blymn Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998-1999 Brett Lymn
5  *                         (blymn@baea.com.au, brett_lymn@yahoo.com.au)
6  * All rights reserved.
7  *
8  * This code has been donated to The NetBSD Foundation by the Author.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  *
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: copywin.c,v 1.22 2024/12/23 02:58:03 blymn Exp $");
35 #endif				/* not lint */
36 
37 #include <ctype.h>
38 #include <string.h>
39 #include "curses.h"
40 #include "curses_private.h"
41 
42 /*
43  * copywin --
44  *     Copy the box starting at (sminrow, smincol) with a size that
45  *     matches the destination box (dminrow, dmincol) by (dmaxrow, dmaxcol)
46  *     from the source window srcwin to the destination window dstwin.
47  *     All these coordindinates are relative to the relevant window.
48  *     If dooverlay is true then the copy is nondestructive otherwise the
49  *     copy is destructive.
50  */
51 int copywin(const WINDOW *srcwin, WINDOW *dstwin,
52 	    int sminrow, int smincol,
53 	    int dminrow, int dmincol, int dmaxrow, int dmaxcol, int dooverlay)
54 {
55 	int dcol;
56 	__LDATA *sp, *end;
57 #ifdef HAVE_WCHAR
58 	cchar_t cc;
59 	nschar_t *np;
60 #endif /* HAVE_WCHAR */
61 
62 	__CTRACE(__CTRACE_WINDOW,
63 	    "copywin %s mode: from (%d,%d) to (%d,%d-%d,%d)\n",
64 	    dooverlay ? "overlay" : "overwrite",
65 	    sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol);
66 
67 	if ((srcwin == NULL) || (dstwin == NULL))
68 		return ERR;
69 
70 	/* overwrite() and overlay() can come here with -ve srcwin coords */
71 	if (sminrow < 0) {
72 		dminrow -= sminrow;
73 		sminrow = 0;
74 	}
75 	if (smincol < 0) {
76 		dmincol -= smincol;
77 		smincol = 0;
78 	}
79 
80 	/* for symmetry allow dstwin coords to be -ve as well */
81 	if (dminrow < 0) {
82 		sminrow -= dminrow;
83 		dminrow = 0;
84 	}
85 	if (dmincol < 0) {
86 		smincol -= dmincol;
87 		dmincol = 0;
88 	}
89 
90 	/* Bound dmaxcol for both windows (should be ok for dstwin) */
91 	if (dmaxcol >= dstwin->maxx)
92 		dmaxcol = dstwin->maxx - 1;
93 	if (smincol + (dmaxcol - dmincol) >= srcwin->maxx)
94 		dmaxcol = srcwin->maxx + dmincol - smincol - 1;
95 	if (dmaxcol < dmincol)
96 		/* nothing in the intersection */
97 		return OK;
98 
99 	/* Bound dmaxrow for both windows (should be ok for dstwin) */
100 	if (dmaxrow >= dstwin->maxy)
101 		dmaxrow = dstwin->maxy - 1;
102 	if (sminrow + (dmaxrow - dminrow) >= srcwin->maxy)
103 		dmaxrow = srcwin->maxy + dminrow - sminrow - 1;
104 
105 	__CTRACE(__CTRACE_WINDOW,
106 	    "copywin %s mode: from (%d,%d) to (%d,%d-%d,%d)\n",
107 	    dooverlay ? "overlay" : "overwrite",
108 	    sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol);
109 
110 	for (; dminrow <= dmaxrow; sminrow++, dminrow++) {
111 		sp = &srcwin->alines[sminrow]->line[smincol];
112 		end = sp + dmaxcol - dmincol;
113 	__CTRACE(__CTRACE_WINDOW, "copywin: row %d\n", sminrow);
114 		for (dcol = dmincol; sp <= end; dcol++, sp++) {
115 			/* XXX: Perhaps this should check for the
116 			 * background character
117 			 */
118 			if ((dooverlay && !isspace(sp->ch)) || !dooverlay) {
119 				wmove(dstwin, dminrow, dcol);
120 	__CTRACE(__CTRACE_WINDOW, "copywin: dcol = %d\n", dcol);
121 #ifndef HAVE_WCHAR
122 				__waddch(dstwin, sp);
123 #else
124 				cc.vals[0] = sp->ch;
125 				cc.attributes = sp->attr;
126 				cc.elements = 1;
127 				np = sp->nsp;
128 				if (np) {
129 					while (np && cc.elements <=
130 					    CURSES_CCHAR_MAX) {
131 						cc.vals[cc.elements++] = np->ch;
132 						np = np->next;
133 					}
134 				}
135 				wadd_wch(dstwin, &cc);
136 #endif /* HAVE_WCHAR */
137 			}
138 		}
139 	}
140 	__touchwin(dstwin, 0);
141 	return OK;
142 }
143