]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_overlay.c
ncurses 6.1 - patch 20180923
[ncurses.git] / ncurses / base / lib_overlay.c
1 /****************************************************************************
2  * Copyright (c) 1998-2013,2016 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.32 2016/05/28 23:11:26 tom Exp $")
44
45 static int
46 overlap(const WINDOW *const src, WINDOW *const dst, int const flag)
47 {
48     int rc = ERR;
49
50     T((T_CALLED("overlap(%p,%p,%d)"), (const void *) src, (void *) dst, flag));
51
52     if (src != 0 && dst != 0) {
53         int sx1, sy1, sx2, sy2;
54         int dx1, dy1, dx2, dy2;
55
56         _nc_lock_global(curses);
57
58         T(("src : begy %ld, begx %ld, maxy %ld, maxx %ld",
59            (long) src->_begy,
60            (long) src->_begx,
61            (long) src->_maxy,
62            (long) src->_maxx));
63         T(("dst : begy %ld, begx %ld, maxy %ld, maxx %ld",
64            (long) dst->_begy,
65            (long) dst->_begx,
66            (long) dst->_maxy,
67            (long) dst->_maxx));
68
69         sx1 = src->_begx;
70         sy1 = src->_begy;
71         sx2 = sx1 + src->_maxx;
72         sy2 = sy1 + src->_maxy;
73
74         dx1 = dst->_begx;
75         dy1 = dst->_begy;
76         dx2 = dx1 + dst->_maxx;
77         dy2 = dy1 + dst->_maxy;
78
79         if (dx2 >= sx1 && dx1 <= sx2 && dy2 >= sy1 && dy1 <= sy2) {
80             int sminrow = max(sy1, dy1) - sy1;
81             int smincol = max(sx1, dx1) - sx1;
82             int dminrow = max(sy1, dy1) - dy1;
83             int dmincol = max(sx1, dx1) - dx1;
84             int dmaxrow = min(sy2, dy2) - dy1;
85             int dmaxcol = min(sx2, dx2) - dx1;
86
87             rc = copywin(src, dst,
88                          sminrow, smincol,
89                          dminrow, dmincol,
90                          dmaxrow, dmaxcol,
91                          flag);
92         }
93         _nc_unlock_global(curses);
94     }
95     returnCode(rc);
96 }
97
98 /*
99 **
100 **      overlay(win1, win2)
101 **
102 **
103 **      overlay() writes the overlapping area of win1 behind win2
104 **      on win2 non-destructively.
105 **
106 **/
107
108 NCURSES_EXPORT(int)
109 overlay(const WINDOW *win1, WINDOW *win2)
110 {
111     T((T_CALLED("overlay(%p,%p)"), (const void *) win1, (void *) win2));
112     returnCode(overlap(win1, win2, TRUE));
113 }
114
115 /*
116 **
117 **      overwrite(win1, win2)
118 **
119 **
120 **      overwrite() writes the overlapping area of win1 behind win2
121 **      on win2 destructively.
122 **
123 **/
124
125 NCURSES_EXPORT(int)
126 overwrite(const WINDOW *win1, WINDOW *win2)
127 {
128     T((T_CALLED("overwrite(%p,%p)"), (const void *) win1, (void *) win2));
129     returnCode(overlap(win1, win2, FALSE));
130 }
131
132 NCURSES_EXPORT(int)
133 copywin(const WINDOW *src, WINDOW *dst,
134         int sminrow, int smincol,
135         int dminrow, int dmincol,
136         int dmaxrow, int dmaxcol,
137         int over)
138 {
139     int rc = ERR;
140
141     T((T_CALLED("copywin(%p, %p, %d, %d, %d, %d, %d, %d, %d)"),
142        (const void *) src,
143        (void *) dst,
144        sminrow, smincol,
145        dminrow, dmincol,
146        dmaxrow, dmaxcol, over));
147
148     if (src != 0
149         && dst != 0
150         && dmaxrow >= dminrow
151         && dmaxcol >= dmincol) {
152         attr_t bk;
153         attr_t mask;
154
155         _nc_lock_global(curses);
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                 int sx, sy, dx, dy;
169                 bool copied = FALSE;
170
171                 T(("rectangle fits in destination"));
172
173                 for (dy = dminrow, sy = sminrow;
174                      dy <= dmaxrow;
175                      sy++, dy++) {
176                     bool touched;
177
178                     if (dy < 0 || sy < 0)
179                         continue;
180
181                     touched = FALSE;
182                     for (dx = dmincol, sx = smincol;
183                          dx <= dmaxcol;
184                          sx++, dx++) {
185
186                         if (dx < 0 || sx < 0)
187                             continue;
188                         copied = TRUE;
189
190                         if (over) {
191                             if ((CharOf(src->_line[sy].text[sx]) != L(' ')) &&
192                                 (!CharEq(dst->_line[dy].text[dx],
193                                          src->_line[sy].text[sx]))) {
194                                 dst->_line[dy].text[dx] =
195                                     src->_line[sy].text[sx];
196                                 SetAttr(dst->_line[dy].text[dx],
197                                         ((AttrOf(src->_line[sy].text[sx]) &
198                                           mask) | bk));
199                                 touched = TRUE;
200                             }
201                         } else {
202                             if (!CharEq(dst->_line[dy].text[dx],
203                                         src->_line[sy].text[sx])) {
204                                 dst->_line[dy].text[dx] =
205                                     src->_line[sy].text[sx];
206                                 touched = TRUE;
207                             }
208                         }
209                     }
210                     if (touched) {
211                         touchline(dst, dminrow, (dmaxrow - dminrow + 1));
212                     }
213                 }
214                 T(("finished copywin"));
215                 if (copied)
216                     rc = OK;
217             }
218         }
219         _nc_unlock_global(curses);
220     }
221     returnCode(rc);
222 }