lib_overlay.c revision 178866
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
43MODULE_ID("$Id: lib_overlay.c,v 1.25 2008/04/12 17:21:59 tom Exp $")
44
45static int
46overlap(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)"), src, dst, flag));
56
57    if (src != 0 && dst != 0) {
58	_nc_lock_window(src);
59	_nc_lock_window(dst);
60
61	T(("src : begy %ld, begx %ld, maxy %ld, maxx %ld",
62	   (long) src->_begy,
63	   (long) src->_begx,
64	   (long) src->_maxy,
65	   (long) src->_maxx));
66	T(("dst : begy %ld, begx %ld, maxy %ld, maxx %ld",
67	   (long) dst->_begy,
68	   (long) dst->_begx,
69	   (long) dst->_maxy,
70	   (long) dst->_maxx));
71
72	sx1 = src->_begx;
73	sy1 = src->_begy;
74	sx2 = sx1 + src->_maxx;
75	sy2 = sy1 + src->_maxy;
76
77	dx1 = dst->_begx;
78	dy1 = dst->_begy;
79	dx2 = dx1 + dst->_maxx;
80	dy2 = dy1 + dst->_maxy;
81
82	if (dx2 >= sx1 && dx1 <= sx2 && dy2 >= sy1 && dy1 <= sy2) {
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	    rc = copywin(src, dst,
91			 sminrow, smincol,
92			 dminrow, dmincol,
93			 dmaxrow, dmaxcol,
94			 flag);
95	}
96	_nc_unlock_window(dst);
97	_nc_unlock_window(src);
98    }
99    returnCode(rc);
100}
101
102/*
103**
104**	overlay(win1, win2)
105**
106**
107**	overlay() writes the overlapping area of win1 behind win2
108**	on win2 non-destructively.
109**
110**/
111
112NCURSES_EXPORT(int)
113overlay(const WINDOW *win1, WINDOW *win2)
114{
115    T((T_CALLED("overlay(%p,%p)"), win1, win2));
116    returnCode(overlap(win1, win2, TRUE));
117}
118
119/*
120**
121**	overwrite(win1, win2)
122**
123**
124**	overwrite() writes the overlapping area of win1 behind win2
125**	on win2 destructively.
126**
127**/
128
129NCURSES_EXPORT(int)
130overwrite(const WINDOW *win1, WINDOW *win2)
131{
132    T((T_CALLED("overwrite(%p,%p)"), win1, win2));
133    returnCode(overlap(win1, win2, FALSE));
134}
135
136NCURSES_EXPORT(int)
137copywin(const WINDOW *src, WINDOW *dst,
138	int sminrow, int smincol,
139	int dminrow, int dmincol,
140	int dmaxrow, int dmaxcol,
141	int over)
142{
143    int rc = ERR;
144    int sx, sy, dx, dy;
145    bool touched;
146    attr_t bk;
147    attr_t mask;
148
149    T((T_CALLED("copywin(%p, %p, %d, %d, %d, %d, %d, %d, %d)"),
150       src, dst, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol, over));
151
152    if (src && dst) {
153
154	_nc_lock_window(src);
155	_nc_lock_window(dst);
156
157	bk = AttrOf(dst->_nc_bkgd);
158	mask = ~(attr_t) ((bk & A_COLOR) ? A_COLOR : 0);
159
160	/* make sure rectangle exists in source */
161	if ((sminrow + dmaxrow - dminrow) <= (src->_maxy + 1) &&
162	    (smincol + dmaxcol - dmincol) <= (src->_maxx + 1)) {
163
164	    T(("rectangle exists in source"));
165
166	    /* make sure rectangle fits in destination */
167	    if (dmaxrow <= dst->_maxy && dmaxcol <= dst->_maxx) {
168
169		T(("rectangle fits in destination"));
170
171		for (dy = dminrow, sy = sminrow;
172		     dy <= dmaxrow;
173		     sy++, dy++) {
174
175		    touched = FALSE;
176		    for (dx = dmincol, sx = smincol;
177			 dx <= dmaxcol;
178			 sx++, dx++) {
179			if (over) {
180			    if ((CharOf(src->_line[sy].text[sx]) != L(' ')) &&
181				(!CharEq(dst->_line[dy].text[dx],
182					 src->_line[sy].text[sx]))) {
183				dst->_line[dy].text[dx] =
184				    src->_line[sy].text[sx];
185				SetAttr(dst->_line[dy].text[dx],
186					((AttrOf(src->_line[sy].text[sx]) &
187					  mask) | bk));
188				touched = TRUE;
189			    }
190			} else {
191			    if (!CharEq(dst->_line[dy].text[dx],
192					src->_line[sy].text[sx])) {
193				dst->_line[dy].text[dx] =
194				    src->_line[sy].text[sx];
195				touched = TRUE;
196			    }
197			}
198		    }
199		    if (touched) {
200			touchline(dst, dminrow, (dmaxrow - dminrow + 1));
201		    }
202		}
203		T(("finished copywin"));
204		rc = OK;
205	    }
206	}
207	_nc_unlock_window(dst);
208	_nc_unlock_window(src);
209    }
210    returnCode(rc);
211}
212