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