lib_overlay.c revision 166124
1/****************************************************************************
2 * Copyright (c) 1998-2002,2006 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.22 2006/10/14 20:43:31 tom Exp $")
44
45static int
46overlap(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
109NCURSES_EXPORT(int)
110overlay(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
126NCURSES_EXPORT(int)
127overwrite(const WINDOW *win1, WINDOW *win2)
128{
129    T((T_CALLED("overwrite(%p,%p)"), win1, win2));
130    returnCode(overlap(win1, win2, FALSE));
131}
132
133NCURSES_EXPORT(int)
134copywin(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 = AttrOf(dst->_nc_bkgd);
143    attr_t mask = ~(attr_t) ((bk & A_COLOR) ? A_COLOR : 0);
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    /* make sure rectangle exists in source */
152    if ((sminrow + dmaxrow - dminrow) > (src->_maxy + 1) ||
153	(smincol + dmaxcol - dmincol) > (src->_maxx + 1)) {
154	returnCode(ERR);
155    }
156
157    T(("rectangle exists in source"));
158
159    /* make sure rectangle fits in destination */
160    if (dmaxrow > dst->_maxy || dmaxcol > dst->_maxx) {
161	returnCode(ERR);
162    }
163
164    T(("rectangle fits in destination"));
165
166    for (dy = dminrow, sy = sminrow; dy <= dmaxrow; sy++, dy++) {
167	touched = FALSE;
168	for (dx = dmincol, sx = smincol; dx <= dmaxcol; sx++, dx++) {
169	    if (over) {
170		if ((CharOf(src->_line[sy].text[sx]) != L(' ')) &&
171		    (!CharEq(dst->_line[dy].text[dx], src->_line[sy].text[sx]))) {
172		    dst->_line[dy].text[dx] = src->_line[sy].text[sx];
173		    SetAttr(dst->_line[dy].text[dx],
174			    (AttrOf(src->_line[sy].text[sx]) & mask) | bk);
175		    touched = TRUE;
176		}
177	    } else {
178		if (!CharEq(dst->_line[dy].text[dx], src->_line[sy].text[sx])) {
179		    dst->_line[dy].text[dx] = src->_line[sy].text[sx];
180		    touched = TRUE;
181		}
182	    }
183	}
184	if (touched) {
185	    touchline(dst, dminrow, (dmaxrow - dminrow + 1));
186	}
187    }
188    T(("finished copywin"));
189    returnCode(OK);
190}
191