]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ncurses/ncurses/base/lib_overlay.c
This commit was generated by cvs2svn to compensate for changes in r157191,
[FreeBSD/FreeBSD.git] / contrib / ncurses / ncurses / base / lib_overlay.c
1 /****************************************************************************
2  * Copyright (c) 1998,2000,2001 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.20 2001/12/19 01:06:37 tom Exp $")
44
45 static int
46 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 NCURSES_EXPORT(int)
80 overlay(const WINDOW *win1, WINDOW *win2)
81 {
82     T((T_CALLED("overlay(%p,%p)"), win1, win2));
83     returnCode(overlap(win1, win2, TRUE));
84 }
85
86 /*
87 **
88 **      overwrite(win1, win2)
89 **
90 **
91 **      overwrite() writes the overlapping area of win1 behind win2
92 **      on win2 destructively.
93 **
94 **/
95
96 NCURSES_EXPORT(int)
97 overwrite(const WINDOW *win1, WINDOW *win2)
98 {
99     T((T_CALLED("overwrite(%p,%p)"), win1, win2));
100     returnCode(overlap(win1, win2, FALSE));
101 }
102
103 NCURSES_EXPORT(int)
104 copywin
105 (const WINDOW *src, WINDOW *dst,
106  int sminrow, int smincol,
107  int dminrow, int dmincol, int dmaxrow, int dmaxcol,
108  int over)
109 {
110     int sx, sy, dx, dy;
111     bool touched;
112     attr_t bk = AttrOf(dst->_nc_bkgd);
113     attr_t mask = ~(attr_t) ((bk & A_COLOR) ? A_COLOR : 0);
114
115     T((T_CALLED("copywin(%p, %p, %d, %d, %d, %d, %d, %d, %d)"),
116        src, dst, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol, over));
117
118     if (!src || !dst)
119         returnCode(ERR);
120
121     /* make sure rectangle exists in source */
122     if ((sminrow + dmaxrow - dminrow) > (src->_maxy + 1) ||
123         (smincol + dmaxcol - dmincol) > (src->_maxx + 1)) {
124         returnCode(ERR);
125     }
126
127     T(("rectangle exists in source"));
128
129     /* make sure rectangle fits in destination */
130     if (dmaxrow > dst->_maxy || dmaxcol > dst->_maxx) {
131         returnCode(ERR);
132     }
133
134     T(("rectangle fits in destination"));
135
136     for (dy = dminrow, sy = sminrow; dy <= dmaxrow; sy++, dy++) {
137         touched = FALSE;
138         for (dx = dmincol, sx = smincol; dx <= dmaxcol; sx++, dx++) {
139             if (over) {
140                 if ((CharOf(src->_line[sy].text[sx]) != L(' ')) &&
141                     (!CharEq(dst->_line[dy].text[dx], src->_line[sy].text[sx]))) {
142                     dst->_line[dy].text[dx] = src->_line[sy].text[sx];
143                     SetAttr(dst->_line[dy].text[dx],
144                             (AttrOf(src->_line[sy].text[sx]) & mask) | bk);
145                     touched = TRUE;
146                 }
147             } else {
148                 if (!CharEq(dst->_line[dy].text[dx], src->_line[sy].text[sx])) {
149                     dst->_line[dy].text[dx] = src->_line[sy].text[sx];
150                     touched = TRUE;
151                 }
152             }
153         }
154         if (touched) {
155             touchline(dst, 0, getmaxy(dst));
156         }
157     }
158     T(("finished copywin"));
159     returnCode(OK);
160 }