xref: /freebsd/contrib/ncurses/ncurses/base/lib_overlay.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /****************************************************************************
2  * Copyright 2020 Thomas E. Dickey                                          *
3  * Copyright 1998-2013,2016 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 
30 /****************************************************************************
31  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
32  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
33  ****************************************************************************/
34 
35 /*
36 **	lib_overlay.c
37 **
38 **	The routines overlay(), copywin(), and overwrite().
39 **
40 */
41 
42 #include <curses.priv.h>
43 
44 MODULE_ID("$Id: lib_overlay.c,v 1.33 2020/02/02 23:34:34 tom Exp $")
45 
46 static int
47 overlap(const WINDOW *const src, WINDOW *const dst, int const flag)
48 {
49     int rc = ERR;
50 
51     T((T_CALLED("overlap(%p,%p,%d)"), (const void *) src, (void *) dst, flag));
52 
53     if (src != 0 && dst != 0) {
54 	int sx1, sy1, sx2, sy2;
55 	int dx1, dy1, dx2, dy2;
56 
57 	_nc_lock_global(curses);
58 
59 	T(("src : begy %ld, begx %ld, maxy %ld, maxx %ld",
60 	   (long) src->_begy,
61 	   (long) src->_begx,
62 	   (long) src->_maxy,
63 	   (long) src->_maxx));
64 	T(("dst : begy %ld, begx %ld, maxy %ld, maxx %ld",
65 	   (long) dst->_begy,
66 	   (long) dst->_begx,
67 	   (long) dst->_maxy,
68 	   (long) dst->_maxx));
69 
70 	sx1 = src->_begx;
71 	sy1 = src->_begy;
72 	sx2 = sx1 + src->_maxx;
73 	sy2 = sy1 + src->_maxy;
74 
75 	dx1 = dst->_begx;
76 	dy1 = dst->_begy;
77 	dx2 = dx1 + dst->_maxx;
78 	dy2 = dy1 + dst->_maxy;
79 
80 	if (dx2 >= sx1 && dx1 <= sx2 && dy2 >= sy1 && dy1 <= sy2) {
81 	    int sminrow = max(sy1, dy1) - sy1;
82 	    int smincol = max(sx1, dx1) - sx1;
83 	    int dminrow = max(sy1, dy1) - dy1;
84 	    int dmincol = max(sx1, dx1) - dx1;
85 	    int dmaxrow = min(sy2, dy2) - dy1;
86 	    int dmaxcol = min(sx2, dx2) - dx1;
87 
88 	    rc = copywin(src, dst,
89 			 sminrow, smincol,
90 			 dminrow, dmincol,
91 			 dmaxrow, dmaxcol,
92 			 flag);
93 	}
94 	_nc_unlock_global(curses);
95     }
96     returnCode(rc);
97 }
98 
99 /*
100 **
101 **	overlay(win1, win2)
102 **
103 **
104 **	overlay() writes the overlapping area of win1 behind win2
105 **	on win2 non-destructively.
106 **
107 **/
108 
109 NCURSES_EXPORT(int)
110 overlay(const WINDOW *win1, WINDOW *win2)
111 {
112     T((T_CALLED("overlay(%p,%p)"), (const void *) win1, (void *) win2));
113     returnCode(overlap(win1, win2, TRUE));
114 }
115 
116 /*
117 **
118 **	overwrite(win1, win2)
119 **
120 **
121 **	overwrite() writes the overlapping area of win1 behind win2
122 **	on win2 destructively.
123 **
124 **/
125 
126 NCURSES_EXPORT(int)
127 overwrite(const WINDOW *win1, WINDOW *win2)
128 {
129     T((T_CALLED("overwrite(%p,%p)"), (const void *) win1, (void *) win2));
130     returnCode(overlap(win1, win2, FALSE));
131 }
132 
133 NCURSES_EXPORT(int)
134 copywin(const WINDOW *src, WINDOW *dst,
135 	int sminrow, int smincol,
136 	int dminrow, int dmincol,
137 	int dmaxrow, int dmaxcol,
138 	int over)
139 {
140     int rc = ERR;
141 
142     T((T_CALLED("copywin(%p, %p, %d, %d, %d, %d, %d, %d, %d)"),
143        (const void *) src,
144        (void *) dst,
145        sminrow, smincol,
146        dminrow, dmincol,
147        dmaxrow, dmaxcol, over));
148 
149     if (src != 0
150 	&& dst != 0
151 	&& dmaxrow >= dminrow
152 	&& dmaxcol >= dmincol) {
153 	attr_t bk;
154 	attr_t mask;
155 
156 	_nc_lock_global(curses);
157 
158 	bk = AttrOf(dst->_nc_bkgd);
159 	mask = ~(attr_t) ((bk & A_COLOR) ? A_COLOR : 0);
160 
161 	/* make sure rectangle exists in source */
162 	if ((sminrow + dmaxrow - dminrow) <= (src->_maxy + 1) &&
163 	    (smincol + dmaxcol - dmincol) <= (src->_maxx + 1)) {
164 
165 	    T(("rectangle exists in source"));
166 
167 	    /* make sure rectangle fits in destination */
168 	    if (dmaxrow <= dst->_maxy && dmaxcol <= dst->_maxx) {
169 		int sx, sy, dx, dy;
170 		bool copied = FALSE;
171 
172 		T(("rectangle fits in destination"));
173 
174 		for (dy = dminrow, sy = sminrow;
175 		     dy <= dmaxrow;
176 		     sy++, dy++) {
177 		    bool touched;
178 
179 		    if (dy < 0 || sy < 0)
180 			continue;
181 
182 		    touched = FALSE;
183 		    for (dx = dmincol, sx = smincol;
184 			 dx <= dmaxcol;
185 			 sx++, dx++) {
186 
187 			if (dx < 0 || sx < 0)
188 			    continue;
189 			copied = TRUE;
190 
191 			if (over) {
192 			    if ((CharOf(src->_line[sy].text[sx]) != L(' ')) &&
193 				(!CharEq(dst->_line[dy].text[dx],
194 					 src->_line[sy].text[sx]))) {
195 				dst->_line[dy].text[dx] =
196 				    src->_line[sy].text[sx];
197 				SetAttr(dst->_line[dy].text[dx],
198 					((AttrOf(src->_line[sy].text[sx]) &
199 					  mask) | bk));
200 				touched = TRUE;
201 			    }
202 			} else {
203 			    if (!CharEq(dst->_line[dy].text[dx],
204 					src->_line[sy].text[sx])) {
205 				dst->_line[dy].text[dx] =
206 				    src->_line[sy].text[sx];
207 				touched = TRUE;
208 			    }
209 			}
210 		    }
211 		    if (touched) {
212 			touchline(dst, dminrow, (dmaxrow - dminrow + 1));
213 		    }
214 		}
215 		T(("finished copywin"));
216 		if (copied)
217 		    rc = OK;
218 	    }
219 	}
220 	_nc_unlock_global(curses);
221     }
222     returnCode(rc);
223 }
224