Home | History | Annotate | Line # | Download | only in libcurses
newwin.c revision 1.13
      1 /*	$NetBSD: newwin.c,v 1.13 2000/04/11 13:57:10 blymn Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1981, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)newwin.c	8.3 (Berkeley) 7/27/94";
     40 #else
     41 __RCSID("$NetBSD: newwin.c,v 1.13 2000/04/11 13:57:10 blymn Exp $");
     42 #endif
     43 #endif				/* not lint */
     44 
     45 #include <stdlib.h>
     46 
     47 #include "curses.h"
     48 #include "curses_private.h"
     49 
     50 #undef	nl			/* Don't need it here, and it interferes. */
     51 
     52 static WINDOW *__makenew __P((int, int, int, int, int));
     53 
     54 void __set_subwin __P((WINDOW *, WINDOW *));
     55 
     56 /*
     57  * newwin --
     58  *	Allocate space for and set up defaults for a new window.
     59  */
     60 WINDOW *
     61 newwin(nl, nc, by, bx)
     62 	int     nl, nc, by, bx;
     63 {
     64 	WINDOW *win;
     65 	__LINE *lp;
     66 	int     i, j;
     67 	__LDATA *sp;
     68 
     69 	if (nl == 0)
     70 		nl = LINES - by;
     71 	if (nc == 0)
     72 		nc = COLS - bx;
     73 
     74 	if ((win = __makenew(nl, nc, by, bx, 0)) == NULL)
     75 		return (NULL);
     76 
     77 	win->nextp = win;
     78 	win->ch_off = 0;
     79 	win->orig = NULL;
     80 	win->delay = -1;
     81 
     82 #ifdef DEBUG
     83 	__CTRACE("newwin: win->ch_off = %d\n", win->ch_off);
     84 #endif
     85 
     86 	for (i = 0; i < nl; i++) {
     87 		lp = win->lines[i];
     88 		lp->flags = 0;
     89 		for (sp = lp->line, j = 0; j < nc; j++, sp++) {
     90 			sp->ch = ' ';
     91 			sp->attr = 0;
     92 		}
     93 		lp->hash = __hash((char *)(void *)lp->line,
     94 		    (int) (nc * __LDATASIZE));
     95 	}
     96 	return (win);
     97 }
     98 
     99 WINDOW *
    100 subwin(orig, nl, nc, by, bx)
    101 	WINDOW *orig;
    102 	int     by, bx, nl, nc;
    103 {
    104 	int     i;
    105 	__LINE *lp;
    106 	WINDOW *win;
    107 
    108 	/* Make sure window fits inside the original one. */
    109 #ifdef	DEBUG
    110 	__CTRACE("subwin: (%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx);
    111 #endif
    112 	if (by < orig->begy || bx < orig->begx
    113 	    || by + nl > orig->maxy + orig->begy
    114 	    || bx + nc > orig->maxx + orig->begx)
    115 		return (NULL);
    116 	if (nl == 0)
    117 		nl = orig->maxy + orig->begy - by;
    118 	if (nc == 0)
    119 		nc = orig->maxx + orig->begx - bx;
    120 	if ((win = __makenew(nl, nc, by, bx, 1)) == NULL)
    121 		return (NULL);
    122 	win->nextp = orig->nextp;
    123 	orig->nextp = win;
    124 	win->orig = orig;
    125 
    126 	/* Initialize flags here so that refresh can also use __set_subwin. */
    127 	for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++)
    128 		lp->flags = 0;
    129 	__set_subwin(orig, win);
    130 	return (win);
    131 }
    132 /*
    133  * This code is shared with mvwin().
    134  */
    135 void
    136 __set_subwin(orig, win)
    137 	WINDOW *orig, *win;
    138 {
    139 	int     i;
    140 	__LINE *lp, *olp;
    141 
    142 	win->ch_off = win->begx - orig->begx;
    143 	/* Point line pointers to line space. */
    144 	for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) {
    145 		win->lines[i] = lp;
    146 		olp = orig->lines[i + win->begy - orig->begy];
    147 		lp->line = &olp->line[win->begx - orig->begx];
    148 		lp->firstchp = &olp->firstch;
    149 		lp->lastchp = &olp->lastch;
    150 		lp->hash = __hash((char *)(void *)lp->line,
    151 		    (int) (win->maxx * __LDATASIZE));
    152 	}
    153 
    154 #ifdef DEBUG
    155 	__CTRACE("__set_subwin: win->ch_off = %d\n", win->ch_off);
    156 #endif
    157 }
    158 /*
    159  * __makenew --
    160  *	Set up a window buffer and returns a pointer to it.
    161  */
    162 static WINDOW *
    163 __makenew(nl, nc, by, bx, sub)
    164 	int     by, bx, nl, nc;
    165 	int     sub;
    166 {
    167 	WINDOW *win;
    168 	__LINE *lp;
    169 	int     i;
    170 
    171 
    172 #ifdef	DEBUG
    173 	__CTRACE("makenew: (%d, %d, %d, %d)\n", nl, nc, by, bx);
    174 #endif
    175 	if ((win = malloc(sizeof(*win))) == NULL)
    176 		return (NULL);
    177 #ifdef DEBUG
    178 	__CTRACE("makenew: nl = %d\n", nl);
    179 #endif
    180 
    181 	/* Set up line pointer array and line space. */
    182 	if ((win->lines = malloc(nl * sizeof(__LINE *))) == NULL) {
    183 		free(win);
    184 		return NULL;
    185 	}
    186 	if ((win->lspace = malloc(nl * sizeof(__LINE))) == NULL) {
    187 		free(win);
    188 		free(win->lines);
    189 		return NULL;
    190 	}
    191 	/* Don't allocate window and line space if it's a subwindow */
    192 	if (!sub) {
    193 		/*
    194 		 * Allocate window space in one chunk.
    195 		 */
    196 		if ((win->wspace =
    197 			malloc(nc * nl * sizeof(__LDATA))) == NULL) {
    198 			free(win->lines);
    199 			free(win->lspace);
    200 			free(win);
    201 			return NULL;
    202 		}
    203 		/*
    204 		 * Point line pointers to line space, and lines themselves into
    205 		 * window space.
    206 		 */
    207 		for (lp = win->lspace, i = 0; i < nl; i++, lp++) {
    208 			win->lines[i] = lp;
    209 			lp->line = &win->wspace[i * nc];
    210 			lp->firstchp = &lp->firstch;
    211 			lp->lastchp = &lp->lastch;
    212 			lp->firstch = 0;
    213 			lp->lastch = 0;
    214 		}
    215 	}
    216 #ifdef DEBUG
    217 	__CTRACE("makenew: nc = %d\n", nc);
    218 #endif
    219 	win->cury = win->curx = 0;
    220 	win->maxy = nl;
    221 	win->maxx = nc;
    222 
    223 	win->begy = by;
    224 	win->begx = bx;
    225 	win->flags = 0;
    226 	win->wattr = 0;
    227 	__swflags(win);
    228 #ifdef DEBUG
    229 	__CTRACE("makenew: win->wattr = %0.2o\n", win->wattr);
    230 	__CTRACE("makenew: win->flags = %0.2o\n", win->flags);
    231 	__CTRACE("makenew: win->maxy = %d\n", win->maxy);
    232 	__CTRACE("makenew: win->maxx = %d\n", win->maxx);
    233 	__CTRACE("makenew: win->begy = %d\n", win->begy);
    234 	__CTRACE("makenew: win->begx = %d\n", win->begx);
    235 #endif
    236 	return (win);
    237 }
    238 
    239 void
    240 __swflags(win)
    241 	WINDOW *win;
    242 {
    243 	win->flags &= ~(__ENDLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK);
    244 	if (win->begx + win->maxx == COLS) {
    245 		win->flags |= __ENDLINE;
    246 		if (win->begx == 0 && win->maxy == LINES && win->begy == 0)
    247 			win->flags |= __FULLWIN;
    248 		if (win->begy + win->maxy == LINES)
    249 			win->flags |= __SCROLLWIN;
    250 	}
    251 }
    252