1 /****************************************************************************
2  * Copyright (c) 1998 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 /*
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.12 1998/02/11 12:13:59 tom Exp $")
45 
46 static int overlap(const WINDOW *const s, WINDOW *const d, int const flag)
47 {
48 int sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol;
49 
50 	T(("overlap : sby %d, sbx %d, smy %d, smx %d, dby %d, dbx %d, dmy %d, dmx %d",
51 		s->_begy, s->_begx, s->_maxy, s->_maxx,
52 		d->_begy, d->_begx, d->_maxy, d->_maxx));
53 
54 	if (!s || !d)
55 		returnCode(ERR);
56 
57 	sminrow = max(s->_begy, d->_begy) - s->_begy;
58 	smincol = max(s->_begx, d->_begx) - s->_begx;
59 	dminrow = max(s->_begy, d->_begy) - d->_begy;
60 	dmincol = max(s->_begx, d->_begx) - d->_begx;
61 	dmaxrow = min(s->_maxy+s->_begy, d->_maxy+d->_begy) - d->_begy;
62 	dmaxcol = min(s->_maxx+s->_begx, d->_maxx+d->_begx) - d->_begx;
63 
64 	return(copywin(s, d,
65 		       sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol,
66 		       flag));
67 }
68 
69 /*
70 **
71 **	overlay(win1, win2)
72 **
73 **
74 **	overlay() writes the overlapping area of win1 behind win2
75 **	on win2 non-destructively.
76 **
77 **/
78 
79 int overlay(const WINDOW *win1, WINDOW *win2)
80 {
81 	T((T_CALLED("overlay(%p,%p)"), win1, win2));
82 	returnCode(overlap(win1, win2, TRUE));
83 }
84 
85 /*
86 **
87 **	overwrite(win1, win2)
88 **
89 **
90 **	overwrite() writes the overlapping area of win1 behind win2
91 **	on win2 destructively.
92 **
93 **/
94 
95 int overwrite(const WINDOW *win1, WINDOW *win2)
96 {
97 	T((T_CALLED("overwrite(%p,%p)"), win1, win2));
98 	returnCode(overlap(win1, win2, FALSE));
99 }
100 
101 int copywin(const WINDOW *src, WINDOW *dst,
102 	int sminrow, int smincol,
103 	int dminrow, int dmincol, int dmaxrow, int dmaxcol,
104 	int over)
105 {
106 int sx, sy, dx, dy;
107 bool touched;
108 chtype bk = AttrOf(dst->_bkgd);
109 chtype mask = ~(chtype)((bk&A_COLOR) ? A_COLOR : 0);
110 
111 	T((T_CALLED("copywin(%p, %p, %d, %d, %d, %d, %d, %d, %d)"),
112 		src, dst, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol, over));
113 
114 	if (!src || !dst)
115 	  returnCode(ERR);
116 
117 	/* make sure rectangle exists in source */
118 	if ((sminrow + dmaxrow - dminrow) > (src->_maxy + 1) ||
119 	    (smincol + dmaxcol - dmincol) > (src->_maxx + 1)) {
120 		returnCode(ERR);
121 	}
122 
123 	T(("rectangle exists in source"));
124 
125 	/* make sure rectangle fits in destination */
126 	if (dmaxrow > dst->_maxy || dmaxcol > dst->_maxx) {
127 		returnCode(ERR);
128 	}
129 
130 	T(("rectangle fits in destination"));
131 
132 	for (dy = dminrow, sy = sminrow; dy <= dmaxrow; sy++, dy++) {
133 	   touched = FALSE;
134 	   for(dx=dmincol, sx=smincol; dx <= dmaxcol; sx++, dx++)
135 	   {
136 		if (over)
137 		{
138 		   if ((TextOf(src->_line[sy].text[sx]) != ' ') &&
139                        (dst->_line[dy].text[dx]!=src->_line[sy].text[sx]))
140 		   {
141 			dst->_line[dy].text[dx] =
142 					(src->_line[sy].text[sx] & mask) | bk;
143 			touched = TRUE;
144 		   }
145 	        }
146 		else {
147 		   if (dst->_line[dy].text[dx] != src->_line[sy].text[sx])
148 		   {
149 			dst->_line[dy].text[dx] = src->_line[sy].text[sx];
150 			touched = TRUE;
151                    }
152                 }
153            }
154 	   if (touched)
155 	   {
156 	      touchline(dst,0,getmaxy(dst));
157 	   }
158 	}
159 	T(("finished copywin"));
160 	returnCode(OK);
161 }
162