1 /****************************************************************************
2 * Copyright 2020 Thomas E. Dickey *
3 * Copyright 1998-2013,2016 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 ****************************************************************************/
38 ** The routines overlay(), copywin(), and overwrite().
42 #include <curses.priv.h>
44 MODULE_ID("$Id: lib_overlay.c,v 1.33 2020/02/02 23:34:34 tom Exp $")
47 overlap(const WINDOW *const src, WINDOW *const dst, int const flag)
51 T((T_CALLED("overlap(%p,%p,%d)"), (const void *) src, (void *) dst, flag));
53 if (src != 0 && dst != 0) {
54 int sx1, sy1, sx2, sy2;
55 int dx1, dy1, dx2, dy2;
57 _nc_lock_global(curses);
59 T(("src : begy %ld, begx %ld, maxy %ld, maxx %ld",
64 T(("dst : begy %ld, begx %ld, maxy %ld, maxx %ld",
72 sx2 = sx1 + src->_maxx;
73 sy2 = sy1 + src->_maxy;
77 dx2 = dx1 + dst->_maxx;
78 dy2 = dy1 + dst->_maxy;
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;
88 rc = copywin(src, dst,
94 _nc_unlock_global(curses);
101 ** overlay(win1, win2)
104 ** overlay() writes the overlapping area of win1 behind win2
105 ** on win2 non-destructively.
110 overlay(const WINDOW *win1, WINDOW *win2)
112 T((T_CALLED("overlay(%p,%p)"), (const void *) win1, (void *) win2));
113 returnCode(overlap(win1, win2, TRUE));
118 ** overwrite(win1, win2)
121 ** overwrite() writes the overlapping area of win1 behind win2
122 ** on win2 destructively.
127 overwrite(const WINDOW *win1, WINDOW *win2)
129 T((T_CALLED("overwrite(%p,%p)"), (const void *) win1, (void *) win2));
130 returnCode(overlap(win1, win2, FALSE));
134 copywin(const WINDOW *src, WINDOW *dst,
135 int sminrow, int smincol,
136 int dminrow, int dmincol,
137 int dmaxrow, int dmaxcol,
142 T((T_CALLED("copywin(%p, %p, %d, %d, %d, %d, %d, %d, %d)"),
147 dmaxrow, dmaxcol, over));
151 && dmaxrow >= dminrow
152 && dmaxcol >= dmincol) {
156 _nc_lock_global(curses);
158 bk = AttrOf(dst->_nc_bkgd);
159 mask = ~(attr_t) ((bk & A_COLOR) ? A_COLOR : 0);
161 /* make sure rectangle exists in source */
162 if ((sminrow + dmaxrow - dminrow) <= (src->_maxy + 1) &&
163 (smincol + dmaxcol - dmincol) <= (src->_maxx + 1)) {
165 T(("rectangle exists in source"));
167 /* make sure rectangle fits in destination */
168 if (dmaxrow <= dst->_maxy && dmaxcol <= dst->_maxx) {
172 T(("rectangle fits in destination"));
174 for (dy = dminrow, sy = sminrow;
179 if (dy < 0 || sy < 0)
183 for (dx = dmincol, sx = smincol;
187 if (dx < 0 || sx < 0)
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]) &
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];
212 touchline(dst, dminrow, (dmaxrow - dminrow + 1));
215 T(("finished copywin"));
220 _nc_unlock_global(curses);