Back to index

tetex-bin  3.0
lib_overlay.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 1998-2001,2002 Free Software Foundation, Inc.              *
00003  *                                                                          *
00004  * Permission is hereby granted, free of charge, to any person obtaining a  *
00005  * copy of this software and associated documentation files (the            *
00006  * "Software"), to deal in the Software without restriction, including      *
00007  * without limitation the rights to use, copy, modify, merge, publish,      *
00008  * distribute, distribute with modifications, sublicense, and/or sell       *
00009  * copies of the Software, and to permit persons to whom the Software is    *
00010  * furnished to do so, subject to the following conditions:                 *
00011  *                                                                          *
00012  * The above copyright notice and this permission notice shall be included  *
00013  * in all copies or substantial portions of the Software.                   *
00014  *                                                                          *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
00018  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
00019  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
00020  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
00021  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
00022  *                                                                          *
00023  * Except as contained in this notice, the name(s) of the above copyright   *
00024  * holders shall not be used in advertising or otherwise to promote the     *
00025  * sale, use or other dealings in this Software without prior written       *
00026  * authorization.                                                           *
00027  ****************************************************************************/
00028 
00029 /****************************************************************************
00030  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
00031  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
00032  ****************************************************************************/
00033 
00034 /*
00035 **     lib_overlay.c
00036 **
00037 **     The routines overlay(), copywin(), and overwrite().
00038 **
00039 */
00040 
00041 #include <curses.priv.h>
00042 
00043 MODULE_ID("$Id: lib_overlay.c,v 1.21 2002/09/21 23:03:32 tom Exp $")
00044 
00045 static int
00046 overlap(const WINDOW *const s, WINDOW *const d, int const flag)
00047 {
00048     int sx1, sy1, sx2, sy2;
00049     int dx1, dy1, dx2, dy2;
00050     int sminrow, smincol;
00051     int dminrow, dmincol;
00052     int dmaxrow, dmaxcol;
00053 
00054     T((T_CALLED("overlap(%p,%p,%d)"), s, d, flag));
00055 
00056     if (s == 0 || d == 0) {
00057        returnCode(ERR);
00058     } else {
00059        T(("src : begy %d, begx %d, maxy %d, maxx %d",
00060           s->_begy, s->_begx, s->_maxy, s->_maxx));
00061        T(("dst : begy %d, begx %d, maxy %d, maxx %d",
00062           d->_begy, d->_begx, d->_maxy, d->_maxx));
00063 
00064        sx1 = s->_begx;
00065        sy1 = s->_begy;
00066        sx2 = sx1 + s->_maxx;
00067        sy2 = sy1 + s->_maxy;
00068 
00069        dx1 = d->_begx;
00070        dy1 = d->_begy;
00071        dx2 = dx1 + d->_maxx;
00072        dy2 = dy1 + d->_maxy;
00073 
00074        if (dx2 < sx1 || dx1 > sx2 || dy2 < sy1 || dy1 > sy2) {
00075            returnCode(ERR); /* No intersection */
00076        } else {
00077            sminrow = max(sy1, dy1) - sy1;
00078            smincol = max(sx1, dx1) - sx1;
00079            dminrow = max(sy1, dy1) - dy1;
00080            dmincol = max(sx1, dx1) - dx1;
00081            dmaxrow = min(sy2, dy2) - dy1;
00082            dmaxcol = min(sx2, dx2) - dx1;
00083 
00084            returnCode(copywin(s, d,
00085                             sminrow, smincol,
00086                             dminrow, dmincol,
00087                             dmaxrow, dmaxcol,
00088                             flag));
00089        }
00090     }
00091 }
00092 
00093 /*
00094 **
00095 **     overlay(win1, win2)
00096 **
00097 **
00098 **     overlay() writes the overlapping area of win1 behind win2
00099 **     on win2 non-destructively.
00100 **
00101 **/
00102 
00103 NCURSES_EXPORT(int)
00104 overlay(const WINDOW *win1, WINDOW *win2)
00105 {
00106     T((T_CALLED("overlay(%p,%p)"), win1, win2));
00107     returnCode(overlap(win1, win2, TRUE));
00108 }
00109 
00110 /*
00111 **
00112 **     overwrite(win1, win2)
00113 **
00114 **
00115 **     overwrite() writes the overlapping area of win1 behind win2
00116 **     on win2 destructively.
00117 **
00118 **/
00119 
00120 NCURSES_EXPORT(int)
00121 overwrite(const WINDOW *win1, WINDOW *win2)
00122 {
00123     T((T_CALLED("overwrite(%p,%p)"), win1, win2));
00124     returnCode(overlap(win1, win2, FALSE));
00125 }
00126 
00127 NCURSES_EXPORT(int)
00128 copywin(const WINDOW *src, WINDOW *dst,
00129        int sminrow, int smincol,
00130        int dminrow, int dmincol,
00131        int dmaxrow, int dmaxcol,
00132        int over)
00133 {
00134     int sx, sy, dx, dy;
00135     bool touched;
00136     attr_t bk = AttrOf(dst->_nc_bkgd);
00137     attr_t mask = ~(attr_t) ((bk & A_COLOR) ? A_COLOR : 0);
00138 
00139     T((T_CALLED("copywin(%p, %p, %d, %d, %d, %d, %d, %d, %d)"),
00140        src, dst, sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol, over));
00141 
00142     if (!src || !dst)
00143        returnCode(ERR);
00144 
00145     /* make sure rectangle exists in source */
00146     if ((sminrow + dmaxrow - dminrow) > (src->_maxy + 1) ||
00147        (smincol + dmaxcol - dmincol) > (src->_maxx + 1)) {
00148        returnCode(ERR);
00149     }
00150 
00151     T(("rectangle exists in source"));
00152 
00153     /* make sure rectangle fits in destination */
00154     if (dmaxrow > dst->_maxy || dmaxcol > dst->_maxx) {
00155        returnCode(ERR);
00156     }
00157 
00158     T(("rectangle fits in destination"));
00159 
00160     for (dy = dminrow, sy = sminrow; dy <= dmaxrow; sy++, dy++) {
00161        touched = FALSE;
00162        for (dx = dmincol, sx = smincol; dx <= dmaxcol; sx++, dx++) {
00163            if (over) {
00164               if ((CharOf(src->_line[sy].text[sx]) != L(' ')) &&
00165                   (!CharEq(dst->_line[dy].text[dx], src->_line[sy].text[sx]))) {
00166                   dst->_line[dy].text[dx] = src->_line[sy].text[sx];
00167                   SetAttr(dst->_line[dy].text[dx],
00168                          (AttrOf(src->_line[sy].text[sx]) & mask) | bk);
00169                   touched = TRUE;
00170               }
00171            } else {
00172               if (!CharEq(dst->_line[dy].text[dx], src->_line[sy].text[sx])) {
00173                   dst->_line[dy].text[dx] = src->_line[sy].text[sx];
00174                   touched = TRUE;
00175               }
00176            }
00177        }
00178        if (touched) {
00179            touchline(dst, dminrow, (dmaxrow - dminrow + 1));
00180        }
00181     }
00182     T(("finished copywin"));
00183     returnCode(OK);
00184 }