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