BSD 4_4 release
[unix-history] / usr / src / lib / libcurses / newwin.c
index 014faec..a2f577a 100644 (file)
@@ -1,12 +1,38 @@
 /*
 /*
- * Copyright (c) 1981 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1981, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)newwin.c   5.13 (Berkeley) %G%";
+static char sccsid[] = "@(#)newwin.c   8.1 (Berkeley) 7/20/93";
 #endif /* not lint */
 
 #include <curses.h>
 #endif /* not lint */
 
 #include <curses.h>
@@ -41,18 +67,20 @@ newwin(nl, nc, by, bx)
 
        win->nextp = win;
        win->ch_off = 0;
 
        win->nextp = win;
        win->ch_off = 0;
+       win->orig = NULL;
 
 #ifdef DEBUG
 
 #ifdef DEBUG
-       __TRACE("newwin: win->ch_off = %d\n", win->ch_off);
+       __CTRACE("newwin: win->ch_off = %d\n", win->ch_off);
 #endif
 
 #endif
 
-       for (lp = win->lines[0], i = 0; i < nl; i++, lp = win->lines[i]) {
+       for (i = 0; i < nl; i++) {
+               lp = win->lines[i];
                lp->flags = 0;
                for (sp = lp->line, j = 0; j < nc; j++, sp++) {
                        sp->ch = ' ';
                        sp->attr = 0;
                }
                lp->flags = 0;
                for (sp = lp->line, j = 0; j < nc; j++, sp++) {
                        sp->ch = ' ';
                        sp->attr = 0;
                }
-               lp->hash = __hash(lp->line, nc * __LDATASIZE);
+               lp->hash = __hash((char *) lp->line, nc * __LDATASIZE);
        }
        return (win);
 }
        }
        return (win);
 }
@@ -62,11 +90,13 @@ subwin(orig, nl, nc, by, bx)
        register WINDOW *orig;
        register int by, bx, nl, nc;
 {
        register WINDOW *orig;
        register int by, bx, nl, nc;
 {
+       int i;
+       __LINE *lp;
        register WINDOW *win;
 
        /* Make sure window fits inside the original one. */
 #ifdef DEBUG
        register WINDOW *win;
 
        /* Make sure window fits inside the original one. */
 #ifdef DEBUG
-       __TRACE("subwin: (%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx);
+       __CTRACE("subwin: (%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx);
 #endif
        if (by < orig->begy || bx < orig->begx
            || by + nl > orig->maxy + orig->begy
 #endif
        if (by < orig->begy || bx < orig->begx
            || by + nl > orig->maxy + orig->begy
@@ -81,6 +111,10 @@ subwin(orig, nl, nc, by, bx)
        win->nextp = orig->nextp;
        orig->nextp = win;
        win->orig = orig;
        win->nextp = orig->nextp;
        orig->nextp = win;
        win->orig = orig;
+
+       /* Initialize flags here so that refresh can also use __set_subwin. */
+       for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++)
+               lp->flags = 0;
        __set_subwin(orig, win);
        return (win);
 }
        __set_subwin(orig, win);
        return (win);
 }
@@ -92,11 +126,22 @@ void
 __set_subwin(orig, win)
        register WINDOW *orig, *win;
 {
 __set_subwin(orig, win)
        register WINDOW *orig, *win;
 {
+       int i;
+       __LINE *lp, *olp;
+
        win->ch_off = win->begx - orig->begx;
        win->ch_off = win->begx - orig->begx;
-       win->lines = &orig->lines[win->begy];
+       /*  Point line pointers to line space. */
+       for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) {
+               win->lines[i] = lp;
+               olp = orig->lines[i + win->begy];
+               lp->line = &olp->line[win->begx];
+               lp->firstchp = &olp->firstch;
+               lp->lastchp = &olp->lastch;
+               lp->hash = __hash((char *) lp->line, win->maxx * __LDATASIZE);
+       }
 
 #ifdef DEBUG
 
 #ifdef DEBUG
-       __TRACE("__set_subwin: win->ch_off = %d\n", win->ch_off);
+       __CTRACE("__set_subwin: win->ch_off = %d\n", win->ch_off);
 #endif
 }
 
 #endif
 }
 
@@ -115,29 +160,29 @@ __makenew(nl, nc, by, bx, sub)
        
 
 #ifdef DEBUG
        
 
 #ifdef DEBUG
-       __TRACE("makenew: (%d, %d, %d, %d)\n", nl, nc, by, bx);
+       __CTRACE("makenew: (%d, %d, %d, %d)\n", nl, nc, by, bx);
 #endif
        if ((win = malloc(sizeof(*win))) == NULL)
                return (NULL);
 #ifdef DEBUG
 #endif
        if ((win = malloc(sizeof(*win))) == NULL)
                return (NULL);
 #ifdef DEBUG
-       __TRACE("makenew: nl = %d\n", nl);
+       __CTRACE("makenew: nl = %d\n", nl);
 #endif
 
 #endif
 
-       /* Don't allocate space if it's a subwindow */
+       /* 
+        * Set up line pointer array and line space.
+        */
+       if ((win->lines = malloc (nl * sizeof(__LINE *))) == NULL) {
+               free(win);
+               return NULL;
+       }
+       if ((win->lspace = malloc (nl * sizeof(__LINE))) == NULL) {
+               free (win);
+               free (win->lines);
+               return NULL;
+       }
+
+       /* Don't allocate window and line space if it's a subwindow */
        if (!sub) {
        if (!sub) {
-               /* 
-                * Set up line pointer array and line space.
-                */
-               if ((win->lines = malloc (nl * sizeof(__LINE *))) == NULL) {
-                       free(win);
-                       return NULL;
-               }
-               if ((win->lspace = malloc (nl * sizeof(__LINE))) == NULL) {
-                       free (win);
-                       free (win->lines);
-                       return NULL;
-               }
-               
                /*
                 * Allocate window space in one chunk.
                 */
                /*
                 * Allocate window space in one chunk.
                 */
@@ -163,7 +208,7 @@ __makenew(nl, nc, by, bx, sub)
                }
        }
 #ifdef DEBUG
                }
        }
 #ifdef DEBUG
-       __TRACE("makenew: nc = %d\n", nc);
+       __CTRACE("makenew: nc = %d\n", nc);
 #endif
        win->cury = win->curx = 0;
        win->maxy = nl;
 #endif
        win->cury = win->curx = 0;
        win->maxy = nl;
@@ -174,16 +219,16 @@ __makenew(nl, nc, by, bx, sub)
        win->flags = 0;
        __swflags(win);
 #ifdef DEBUG
        win->flags = 0;
        __swflags(win);
 #ifdef DEBUG
-       __TRACE("makenew: win->flags = %0.2o\n", win->flags);
-       __TRACE("makenew: win->maxy = %d\n", win->maxy);
-       __TRACE("makenew: win->maxx = %d\n", win->maxx);
-       __TRACE("makenew: win->begy = %d\n", win->begy);
-       __TRACE("makenew: win->begx = %d\n", win->begx);
+       __CTRACE("makenew: win->flags = %0.2o\n", win->flags);
+       __CTRACE("makenew: win->maxy = %d\n", win->maxy);
+       __CTRACE("makenew: win->maxx = %d\n", win->maxx);
+       __CTRACE("makenew: win->begy = %d\n", win->begy);
+       __CTRACE("makenew: win->begx = %d\n", win->begx);
 #endif
        return (win);
 }
 
 #endif
        return (win);
 }
 
-static void
+void
 __swflags(win)
        register WINDOW *win;
 {
 __swflags(win)
        register WINDOW *win;
 {