copywin.c revision 1.2 1 /* $NetBSD: copywin.c,v 1.2 2000/04/18 22:15:55 jdc 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 = sminrow + dmaxrow - dminrow;
53 smaxcol = smincol + dmaxcol - dmincol;
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 if (overlay == TRUE) {
62 /* non destructive copy */
63 #ifdef DEBUG
64 __CTRACE("copywin overlay mode: from (%d,%d) to (%d,%d)\n",
65 starty, startx, endy, endx);
66 #endif
67 y1 = starty - sminrow;
68 y2 = starty - dminrow;
69 for (y = starty; y < endy; y++, y1++, y2++) {
70 end = &srcwin->lines[y1]->line[endx - srcwin->begx];
71 x = startx - dstwin->begx;
72 for (sp = &srcwin->lines[y1]->line[startx - srcwin->begx];
73 sp < end; sp++) {
74 if (!isspace(sp->ch)) {
75 wmove(dstwin, y2, x);
76 __waddch(dstwin, sp);
77 }
78 x++;
79 }
80 }
81 return (OK);
82 } else {
83 /* destructive copy */
84 #ifdef DEBUG
85 __CTRACE("copywin overwrite mode: from (%d,%d) to (%d,%d)\n",
86 starty, startx, endy, endx);
87 #endif
88 x = endx - startx;
89 for (y = starty; y < endy; y++) {
90 (void) memcpy(
91 &dstwin->lines[y - dstwin->begy]->line[startx - dstwin->begx],
92 &srcwin->lines[y - srcwin->begy]->line[startx - srcwin->begx],
93 (size_t) x * __LDATASIZE);
94 __touchline(dstwin, y, (int) (startx - dstwin->begx),
95 (int) (endx - dstwin->begx), 0);
96 }
97 return (OK);
98 }
99 }
100
101