xref: /freebsd/contrib/ncurses/ncurses/base/lib_overlay.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /****************************************************************************
2  * Copyright (c) 1998-2006,2007 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.23 2007/04/07 17:13:52 tom Exp $")
44 
45 static int
46 overlap(const WINDOW *const s, WINDOW *const d, int const flag)
47 {
48     int sx1, sy1, sx2, sy2;
49     int dx1, dy1, dx2, dy2;
50     int sminrow, smincol;
51     int dminrow, dmincol;
52     int dmaxrow, dmaxcol;
53 
54     T((T_CALLED("overlap(%p,%p,%d)"), s, d, flag));
55 
56     if (s == 0 || d == 0) {
57 	returnCode(ERR);
58     } else {
59 	T(("src : begy %ld, begx %ld, maxy %ld, maxx %ld",
60 	   (long) s->_begy,
61 	   (long) s->_begx,
62 	   (long) s->_maxy,
63 	   (long) s->_maxx));
64 	T(("dst : begy %ld, begx %ld, maxy %ld, maxx %ld",
65 	   (long) d->_begy,
66 	   (long) d->_begx,
67 	   (long) d->_maxy,
68 	   (long) d->_maxx));
69 
70 	sx1 = s->_begx;
71 	sy1 = s->_begy;
72 	sx2 = sx1 + s->_maxx;
73 	sy2 = sy1 + s->_maxy;
74 
75 	dx1 = d->_begx;
76 	dy1 = d->_begy;
77 	dx2 = dx1 + d->_maxx;
78 	dy2 = dy1 + d->_maxy;
79 
80 	if (dx2 < sx1 || dx1 > sx2 || dy2 < sy1 || dy1 > sy2) {
81 	    returnCode(ERR);	/* No intersection */
82 	} else {
83 	    sminrow = max(sy1, dy1) - sy1;
84 	    smincol = max(sx1, dx1) - sx1;
85 	    dminrow = max(sy1, dy1) - dy1;
86 	    dmincol = max(sx1, dx1) - dx1;
87 	    dmaxrow = min(sy2, dy2) - dy1;
88 	    dmaxcol = min(sx2, dx2) - dx1;
89 
90 	    returnCode(copywin(s, d,
91 			       sminrow, smincol,
92 			       dminrow, dmincol,
93 			       dmaxrow, dmaxcol,
94 			       flag));
95 	}
96     }
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)"), win1, 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)"), win1, 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 sx, sy, dx, dy;
141     bool touched;
142     attr_t bk;
143     attr_t mask;
144 
145     T((T_CALLED("copywin(%p, %p, %d, %d, %d, %d, %d, %d, %d)"),
146        src, dst, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol, over));
147 
148     if (!src || !dst)
149 	returnCode(ERR);
150 
151     bk = AttrOf(dst->_nc_bkgd);
152     mask = ~(attr_t) ((bk & A_COLOR) ? A_COLOR : 0);
153 
154     /* make sure rectangle exists in source */
155     if ((sminrow + dmaxrow - dminrow) > (src->_maxy + 1) ||
156 	(smincol + dmaxcol - dmincol) > (src->_maxx + 1)) {
157 	returnCode(ERR);
158     }
159 
160     T(("rectangle exists in source"));
161 
162     /* make sure rectangle fits in destination */
163     if (dmaxrow > dst->_maxy || dmaxcol > dst->_maxx) {
164 	returnCode(ERR);
165     }
166 
167     T(("rectangle fits in destination"));
168 
169     for (dy = dminrow, sy = sminrow; dy <= dmaxrow; sy++, dy++) {
170 	touched = FALSE;
171 	for (dx = dmincol, sx = smincol; dx <= dmaxcol; sx++, dx++) {
172 	    if (over) {
173 		if ((CharOf(src->_line[sy].text[sx]) != L(' ')) &&
174 		    (!CharEq(dst->_line[dy].text[dx], src->_line[sy].text[sx]))) {
175 		    dst->_line[dy].text[dx] = src->_line[sy].text[sx];
176 		    SetAttr(dst->_line[dy].text[dx],
177 			    (AttrOf(src->_line[sy].text[sx]) & mask) | bk);
178 		    touched = TRUE;
179 		}
180 	    } else {
181 		if (!CharEq(dst->_line[dy].text[dx], src->_line[sy].text[sx])) {
182 		    dst->_line[dy].text[dx] = src->_line[sy].text[sx];
183 		    touched = TRUE;
184 		}
185 	    }
186 	}
187 	if (touched) {
188 	    touchline(dst, dminrow, (dmaxrow - dminrow + 1));
189 	}
190     }
191     T(("finished copywin"));
192     returnCode(OK);
193 }
194