Home | History | Annotate | Line # | Download | only in libcurses
copywin.c revision 1.13.4.1
      1  1.13.4.1   matt /*	$NetBSD: copywin.c,v 1.13.4.1 2008/01/09 01:36:22 matt Exp $	*/
      2       1.1  blymn 
      3       1.1  blymn /*-
      4       1.1  blymn  * Copyright (c) 1998-1999 Brett Lymn
      5       1.1  blymn  *                         (blymn (at) baea.com.au, brett_lymn (at) yahoo.com.au)
      6       1.1  blymn  * All rights reserved.
      7       1.1  blymn  *
      8       1.1  blymn  * This code has been donated to The NetBSD Foundation by the Author.
      9       1.1  blymn  *
     10       1.1  blymn  * Redistribution and use in source and binary forms, with or without
     11       1.1  blymn  * modification, are permitted provided that the following conditions
     12       1.1  blymn  * are met:
     13       1.1  blymn  * 1. Redistributions of source code must retain the above copyright
     14       1.1  blymn  *    notice, this list of conditions and the following disclaimer.
     15       1.1  blymn  * 2. The name of the author may not be used to endorse or promote products
     16       1.7    wiz  *    derived from this software without specific prior written permission
     17       1.1  blymn  *
     18       1.1  blymn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19       1.1  blymn  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20       1.1  blymn  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21       1.1  blymn  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22       1.1  blymn  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23       1.1  blymn  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24       1.1  blymn  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25       1.1  blymn  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26       1.1  blymn  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27       1.1  blymn  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28       1.1  blymn  *
     29       1.1  blymn  *
     30       1.1  blymn  */
     31       1.5  blymn 
     32       1.5  blymn #include <sys/cdefs.h>
     33       1.5  blymn #ifndef lint
     34  1.13.4.1   matt __RCSID("$NetBSD: copywin.c,v 1.13.4.1 2008/01/09 01:36:22 matt Exp $");
     35       1.5  blymn #endif				/* not lint */
     36       1.1  blymn 
     37       1.1  blymn #include <ctype.h>
     38       1.2    jdc #include <string.h>
     39       1.1  blymn #include "curses.h"
     40       1.1  blymn #include "curses_private.h"
     41       1.1  blymn 
     42       1.1  blymn /*
     43       1.1  blymn  * copywin --
     44       1.1  blymn  *     Copy the box starting at (sminrow, smincol) with a size that
     45       1.1  blymn  *     matches the destination box (dminrow, dmincol) by (dmaxrow, dmaxcol)
     46       1.1  blymn  *     from the source window srcwin to the destination window dstwin.
     47      1.10    dsl  *     All these coordindinates are relative to the relevant window.
     48       1.8  lukem  *     If dooverlay is true then the copy is nondestructive otherwise the
     49       1.1  blymn  *     copy is destructive.
     50       1.1  blymn  */
     51      1.10    dsl int copywin(const WINDOW *srcwin, WINDOW *dstwin,
     52      1.10    dsl 	    int sminrow, int smincol,
     53      1.10    dsl 	    int dminrow, int dmincol, int dmaxrow, int dmaxcol, int dooverlay)
     54       1.1  blymn {
     55      1.10    dsl 	int dcol;
     56       1.1  blymn 	__LDATA *sp, *end;
     57      1.13  blymn #ifdef HAVE_WCHAR
     58      1.13  blymn 	cchar_t cc;
     59      1.13  blymn 	nschar_t *np;
     60      1.13  blymn #endif /* HAVE_WCHAR */
     61       1.9  blymn 
     62      1.10    dsl 	/* overwrite() and overlay() can come here with -ve srcwin coords */
     63      1.10    dsl 	if (sminrow < 0) {
     64      1.10    dsl 		dminrow -= sminrow;
     65      1.10    dsl 		sminrow = 0;
     66      1.10    dsl 	}
     67      1.10    dsl 	if (smincol < 0) {
     68      1.10    dsl 		dmincol -= smincol;
     69      1.10    dsl 		smincol = 0;
     70      1.10    dsl 	}
     71       1.9  blymn 
     72      1.10    dsl 	/* for symmetry allow dstwin coords to be -ve as well */
     73      1.10    dsl 	if (dminrow < 0) {
     74      1.10    dsl 		sminrow -= dminrow;
     75      1.10    dsl 		dminrow = 0;
     76      1.10    dsl 	}
     77      1.10    dsl 	if (dmincol < 0) {
     78      1.10    dsl 		smincol -= dmincol;
     79      1.10    dsl 		dmincol = 0;
     80       1.4  blymn 	}
     81       1.9  blymn 
     82      1.10    dsl 	/* Bound dmaxcol for both windows (should be ok for dstwin) */
     83      1.10    dsl 	if (dmaxcol >= dstwin->maxx)
     84      1.10    dsl 		dmaxcol = dstwin->maxx - 1;
     85      1.10    dsl 	if (smincol + (dmaxcol - dmincol) >= srcwin->maxx)
     86      1.11    dsl 		dmaxcol = srcwin->maxx + dmincol - smincol - 1;
     87      1.10    dsl 	if (dmaxcol < dmincol)
     88      1.10    dsl 		/* nothing in the intersection */
     89      1.10    dsl 		return OK;
     90      1.10    dsl 
     91      1.10    dsl 	/* Bound dmaxrow for both windows (should be ok for dstwin) */
     92      1.11    dsl 	if (dmaxrow >= dstwin->maxy)
     93      1.10    dsl 		dmaxrow = dstwin->maxy - 1;
     94      1.10    dsl 	if (sminrow + (dmaxrow - dminrow) >= srcwin->maxy)
     95      1.11    dsl 		dmaxrow = srcwin->maxy + dminrow - sminrow - 1;
     96      1.10    dsl 
     97      1.10    dsl #ifdef DEBUG
     98      1.12    jdc 	__CTRACE(__CTRACE_WINDOW,
     99      1.12    jdc 	    "copywin %s mode: from (%d,%d) to (%d,%d-%d,%d)\n",
    100      1.12    jdc 	    dooverlay ? "overlay" : "overwrite",
    101      1.12    jdc 	    sminrow, smincol, dminrow, dmincol, dmaxrow, dmaxcol);
    102       1.1  blymn #endif
    103      1.10    dsl 
    104      1.10    dsl 	for (; dminrow <= dmaxrow; sminrow++, dminrow++) {
    105      1.10    dsl 		sp = &srcwin->lines[sminrow]->line[smincol];
    106      1.10    dsl 		end = sp + dmaxcol - dmincol;
    107  1.13.4.1   matt 		for (dcol = dmincol; sp <= end; dcol++, sp++) {
    108  1.13.4.1   matt 			/* XXX: Perhaps this should check for the
    109  1.13.4.1   matt 			 * background character
    110  1.13.4.1   matt 			 */
    111  1.13.4.1   matt 			if ((dooverlay && !isspace(sp->ch)) || !dooverlay) {
    112  1.13.4.1   matt 				wmove(dstwin, dminrow, dcol);
    113      1.13  blymn #ifndef HAVE_WCHAR
    114       1.4  blymn 				__waddch(dstwin, sp);
    115      1.13  blymn #else
    116  1.13.4.1   matt 				cc.vals[0] = sp->ch;
    117  1.13.4.1   matt 				cc.attributes = sp->attr;
    118  1.13.4.1   matt 				cc.elements = 1;
    119  1.13.4.1   matt 				np = sp->nsp;
    120  1.13.4.1   matt 				if (np) {
    121  1.13.4.1   matt 					while (np && cc.elements <=
    122  1.13.4.1   matt 					    CURSES_CCHAR_MAX) {
    123  1.13.4.1   matt 						cc.vals[cc.elements++] = np->ch;
    124  1.13.4.1   matt 						np = np->next;
    125  1.13.4.1   matt 					}
    126  1.13.4.1   matt 				}
    127  1.13.4.1   matt 				wadd_wch(dstwin, &cc);
    128      1.13  blymn #endif /* HAVE_WCHAR */
    129       1.4  blymn 			}
    130       1.1  blymn 		}
    131       1.1  blymn 	}
    132  1.13.4.1   matt 	__touchwin(dstwin);
    133      1.10    dsl 	return OK;
    134       1.1  blymn }
    135       1.1  blymn 
    136