Home | History | Annotate | Line # | Download | only in libcurses
copywin.c revision 1.2
      1  1.2    jdc /*	$NetBSD: copywin.c,v 1.2 2000/04/18 22:15:55 jdc 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.1  blymn  *    derived from this software withough 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.1  blymn 
     32  1.1  blymn #include <ctype.h>
     33  1.2    jdc #include <string.h>
     34  1.1  blymn #include "curses.h"
     35  1.1  blymn #include "curses_private.h"
     36  1.1  blymn 
     37  1.1  blymn /*
     38  1.1  blymn  * copywin --
     39  1.1  blymn  *     Copy the box starting at (sminrow, smincol) with a size that
     40  1.1  blymn  *     matches the destination box (dminrow, dmincol) by (dmaxrow, dmaxcol)
     41  1.1  blymn  *     from the source window srcwin to the destination window dstwin.
     42  1.1  blymn  *     If overlay is true then the copy is nondestructive otherwise the
     43  1.1  blymn  *     copy is destructive.
     44  1.1  blymn  */
     45  1.1  blymn int copywin(const WINDOW *srcwin, WINDOW *dstwin, int sminrow,
     46  1.1  blymn             int smincol, int dminrow, int dmincol, int dmaxrow,
     47  1.1  blymn             int dmaxcol, int overlay)
     48  1.1  blymn {
     49  1.1  blymn 	int starty, startx, endy, endx, x, y, y1, y2, smaxrow, smaxcol;
     50  1.1  blymn 	__LDATA *sp, *end;
     51  1.1  blymn 
     52  1.1  blymn 	smaxrow = sminrow + dmaxrow - dminrow;
     53  1.1  blymn 	smaxcol = smincol + dmaxcol - dmincol;
     54  1.1  blymn 	starty = max(sminrow, dminrow);
     55  1.1  blymn 	startx = max(smincol, dmincol);
     56  1.1  blymn 	endy = min(sminrow + smaxrow, dminrow + dmaxrow);
     57  1.1  blymn 	endx = min(smincol + smaxcol, dmincol + dmaxcol);
     58  1.1  blymn 	if (starty >= endy || startx >= endx)
     59  1.1  blymn 		return (OK);
     60  1.1  blymn 
     61  1.1  blymn 	if (overlay == TRUE) {
     62  1.1  blymn 		  /* non destructive copy */
     63  1.1  blymn #ifdef DEBUG
     64  1.1  blymn 		__CTRACE("copywin overlay mode: from (%d,%d) to (%d,%d)\n",
     65  1.1  blymn 			 starty, startx, endy, endx);
     66  1.1  blymn #endif
     67  1.1  blymn 		y1 = starty - sminrow;
     68  1.1  blymn 		y2 = starty - dminrow;
     69  1.1  blymn 		for (y = starty; y < endy; y++, y1++, y2++) {
     70  1.1  blymn 			end = &srcwin->lines[y1]->line[endx - srcwin->begx];
     71  1.1  blymn 			x = startx - dstwin->begx;
     72  1.1  blymn 			for (sp = &srcwin->lines[y1]->line[startx - srcwin->begx];
     73  1.1  blymn 			     sp < end; sp++) {
     74  1.1  blymn 				if (!isspace(sp->ch)) {
     75  1.1  blymn 					wmove(dstwin, y2, x);
     76  1.1  blymn 					__waddch(dstwin, sp);
     77  1.1  blymn 				}
     78  1.1  blymn 				x++;
     79  1.1  blymn 			}
     80  1.1  blymn 		}
     81  1.1  blymn 		return (OK);
     82  1.1  blymn 	} else {
     83  1.1  blymn 		  /* destructive copy */
     84  1.1  blymn #ifdef DEBUG
     85  1.1  blymn 		__CTRACE("copywin overwrite mode: from (%d,%d) to (%d,%d)\n",
     86  1.1  blymn 			 starty, startx, endy, endx);
     87  1.1  blymn #endif
     88  1.1  blymn 		x = endx - startx;
     89  1.1  blymn 		for (y = starty; y < endy; y++) {
     90  1.1  blymn 			(void) memcpy(
     91  1.1  blymn 				&dstwin->lines[y - dstwin->begy]->line[startx - dstwin->begx],
     92  1.1  blymn 				&srcwin->lines[y - srcwin->begy]->line[startx - srcwin->begx],
     93  1.1  blymn 				(size_t) x * __LDATASIZE);
     94  1.1  blymn 			__touchline(dstwin, y, (int) (startx - dstwin->begx),
     95  1.1  blymn 				    (int) (endx - dstwin->begx), 0);
     96  1.1  blymn 		}
     97  1.1  blymn 		return (OK);
     98  1.1  blymn 	}
     99  1.1  blymn }
    100  1.1  blymn 
    101