Home | History | Annotate | Line # | Download | only in libcurses
copywin.c revision 1.4
      1 /*	$NetBSD: copywin.c,v 1.4 2000/04/23 14:14:49 blymn Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998-1999 Brett Lymn
      5  *                         (blymn (at) baea.com.au, brett_lymn (at) yahoo.com.au)
      6  * All rights reserved.
      7  *
      8  * This code has been donated to The NetBSD Foundation by the Author.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. The name of the author may not be used to endorse or promote products
     16  *    derived from this software withough specific prior written permission
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  *
     29  *
     30  */
     31 
     32 #include <ctype.h>
     33 #include <string.h>
     34 #include "curses.h"
     35 #include "curses_private.h"
     36 
     37 /*
     38  * copywin --
     39  *     Copy the box starting at (sminrow, smincol) with a size that
     40  *     matches the destination box (dminrow, dmincol) by (dmaxrow, dmaxcol)
     41  *     from the source window srcwin to the destination window dstwin.
     42  *     If overlay is true then the copy is nondestructive otherwise the
     43  *     copy is destructive.
     44  */
     45 int copywin(const WINDOW *srcwin, WINDOW *dstwin, int sminrow,
     46             int smincol, int dminrow, int dmincol, int dmaxrow,
     47             int dmaxcol, int overlay)
     48 {
     49 	int starty, startx, endy, endx, x, y, y1, y2, smaxrow, smaxcol;
     50 	__LDATA *sp, *end;
     51 
     52 	smaxrow = min(sminrow + dmaxrow - dminrow, srcwin->maxy - sminrow);
     53 	smaxcol = min(smincol + dmaxcol - dmincol, srcwin->maxx - smincol);
     54 	starty = max(sminrow, dminrow);
     55 	startx = max(smincol, dmincol);
     56 	endy = min(sminrow + smaxrow, dminrow + dmaxrow);
     57 	endx = min(smincol + smaxcol, dmincol + dmaxcol);
     58 	if (starty >= endy || startx >= endx)
     59 		return (OK);
     60 
     61 #ifdef DEBUG
     62 	if (overlay == TRUE) {
     63 		__CTRACE("copywin overlay mode: from (%d,%d) to (%d,%d)\n",
     64 			 starty, startx, endy, endx);
     65 	} else {
     66 		__CTRACE("copywin overwrite mode: from (%d,%d) to (%d,%d)\n",
     67 			 starty, startx, endy, endx);
     68 	}
     69 
     70 #endif
     71 	y1 = starty - sminrow;
     72 	y2 = starty - dminrow;
     73 	for (y = starty; y < endy; y++, y1++, y2++) {
     74 		end = &srcwin->lines[y1]->line[endx - srcwin->begx];
     75 		x = startx - dstwin->begx;
     76 		for (sp = &srcwin->lines[y1]->line[startx - srcwin->begx];
     77 		     sp < end; sp++) {
     78 			if ((!isspace(sp->ch)) || (overlay == FALSE)){
     79 				wmove(dstwin, y2, x);
     80 				__waddch(dstwin, sp);
     81 			}
     82 			x++;
     83 		}
     84 	}
     85 	return (OK);
     86 }
     87 
     88