Home | History | Annotate | Line # | Download | only in libcurses
newwin.c revision 1.17
      1 /*	$NetBSD: newwin.c,v 1.17 2000/04/15 22:53:46 jdc 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.17 2000/04/15 22:53:46 jdc 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 extern struct __winlist	*winlistp;
     53 
     54 static WINDOW *__makenew __P((int, int, int, int, int));
     55 
     56 void __set_subwin __P((WINDOW *, WINDOW *));
     57 
     58 /*
     59  * newwin --
     60  *	Allocate space for and set up defaults for a new window.
     61  */
     62 WINDOW *
     63 newwin(int nl, int nc, int by, int bx)
     64 {
     65 	WINDOW *win;
     66 	__LINE *lp;
     67 	int     i, j;
     68 	__LDATA *sp;
     69 
     70 	if (nl == 0)
     71 		nl = LINES - by;
     72 	if (nc == 0)
     73 		nc = COLS - bx;
     74 
     75 	if ((win = __makenew(nl, nc, by, bx, 0)) == NULL)
     76 		return (NULL);
     77 
     78 	win->nextp = win;
     79 	win->ch_off = 0;
     80 	win->orig = NULL;
     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(WINDOW *orig, int nl, int nc, int by, int bx)
    101 {
    102 	int     i;
    103 	__LINE *lp;
    104 	WINDOW *win;
    105 
    106 	/* Make sure window fits inside the original one. */
    107 #ifdef	DEBUG
    108 	__CTRACE("subwin: (%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx);
    109 #endif
    110 	if (by < orig->begy || bx < orig->begx
    111 	    || by + nl > orig->maxy + orig->begy
    112 	    || bx + nc > orig->maxx + orig->begx)
    113 		return (NULL);
    114 	if (nl == 0)
    115 		nl = orig->maxy + orig->begy - by;
    116 	if (nc == 0)
    117 		nc = orig->maxx + orig->begx - bx;
    118 	if ((win = __makenew(nl, nc, by, bx, 1)) == NULL)
    119 		return (NULL);
    120 	win->nextp = orig->nextp;
    121 	orig->nextp = win;
    122 	win->orig = orig;
    123 
    124 	/* Initialize flags here so that refresh can also use __set_subwin. */
    125 	for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++)
    126 		lp->flags = 0;
    127 	__set_subwin(orig, win);
    128 	return (win);
    129 }
    130 /*
    131  * This code is shared with mvwin().
    132  */
    133 void
    134 __set_subwin(WINDOW *orig, WINDOW *win)
    135 {
    136 	int     i;
    137 	__LINE *lp, *olp;
    138 
    139 	win->ch_off = win->begx - orig->begx;
    140 	/* Point line pointers to line space. */
    141 	for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) {
    142 		win->lines[i] = lp;
    143 		olp = orig->lines[i + win->begy - orig->begy];
    144 		lp->line = &olp->line[win->begx - orig->begx];
    145 		lp->firstchp = &olp->firstch;
    146 		lp->lastchp = &olp->lastch;
    147 		lp->hash = __hash((char *)(void *)lp->line,
    148 		    (int) (win->maxx * __LDATASIZE));
    149 	}
    150 
    151 #ifdef DEBUG
    152 	__CTRACE("__set_subwin: win->ch_off = %d\n", win->ch_off);
    153 #endif
    154 }
    155 /*
    156  * __makenew --
    157  *	Set up a window buffer and returns a pointer to it.
    158  */
    159 static WINDOW *
    160 __makenew(nl, nc, by, bx, sub)
    161 	int     by, bx, nl, nc;
    162 	int     sub;
    163 {
    164 	WINDOW			*win;
    165 	__LINE			*lp;
    166 	struct __winlist	*wlp, *wlp2;
    167 	int			 i;
    168 
    169 
    170 #ifdef	DEBUG
    171 	__CTRACE("makenew: (%d, %d, %d, %d)\n", nl, nc, by, bx);
    172 #endif
    173 	if ((win = malloc(sizeof(*win))) == NULL)
    174 		return (NULL);
    175 #ifdef DEBUG
    176 	__CTRACE("makenew: nl = %d\n", nl);
    177 #endif
    178 
    179 	/* Set up line pointer array and line space. */
    180 	if ((win->lines = malloc(nl * sizeof(__LINE *))) == NULL) {
    181 		free(win);
    182 		return NULL;
    183 	}
    184 	if ((win->lspace = malloc(nl * sizeof(__LINE))) == NULL) {
    185 		free(win);
    186 		free(win->lines);
    187 		return NULL;
    188 	}
    189 	/* Don't allocate window and line space if it's a subwindow */
    190 	if (!sub) {
    191 		/*
    192 		 * Allocate window space in one chunk.
    193 		 */
    194 		if ((win->wspace =
    195 			malloc(nc * nl * sizeof(__LDATA))) == NULL) {
    196 			free(win->lines);
    197 			free(win->lspace);
    198 			free(win);
    199 			return NULL;
    200 		}
    201 		/*
    202 		 * Append window to window list.
    203 		 */
    204 		if ((wlp = malloc(sizeof(struct __winlist))) == NULL) {
    205 			free(win->wspace);
    206 			free(win->lines);
    207 			free(win->lspace);
    208 			free(win);
    209 			return NULL;
    210 		}
    211 		wlp->winp = win;
    212 		wlp->nextp = NULL;
    213 		if (__winlistp == NULL)
    214 			__winlistp = wlp;
    215 		else {
    216 			wlp2 = __winlistp;
    217 			while (wlp2->nextp != NULL)
    218 				wlp2 = wlp2->nextp;
    219 			wlp2->nextp = wlp;
    220 		}
    221 		/*
    222 		 * Point line pointers to line space, and lines themselves into
    223 		 * window space.
    224 		 */
    225 		for (lp = win->lspace, i = 0; i < nl; i++, lp++) {
    226 			win->lines[i] = lp;
    227 			lp->line = &win->wspace[i * nc];
    228 			lp->firstchp = &lp->firstch;
    229 			lp->lastchp = &lp->lastch;
    230 			lp->firstch = 0;
    231 			lp->lastch = 0;
    232 		}
    233 	}
    234 #ifdef DEBUG
    235 	__CTRACE("makenew: nc = %d\n", nc);
    236 #endif
    237 	win->cury = win->curx = 0;
    238 	win->maxy = nl;
    239 	win->maxx = nc;
    240 
    241 	win->begy = by;
    242 	win->begx = bx;
    243 	win->flags = 0;
    244 	win->delay = -1;
    245 	win->wattr = 0;
    246 	win->bchar = ' ';
    247 	win->battr = 0;
    248 	__swflags(win);
    249 #ifdef DEBUG
    250 	__CTRACE("makenew: win->wattr = %0.2o\n", win->wattr);
    251 	__CTRACE("makenew: win->flags = %0.2o\n", win->flags);
    252 	__CTRACE("makenew: win->maxy = %d\n", win->maxy);
    253 	__CTRACE("makenew: win->maxx = %d\n", win->maxx);
    254 	__CTRACE("makenew: win->begy = %d\n", win->begy);
    255 	__CTRACE("makenew: win->begx = %d\n", win->begx);
    256 #endif
    257 	return (win);
    258 }
    259 
    260 void
    261 __swflags(WINDOW *win)
    262 {
    263 	win->flags &= ~(__ENDLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK);
    264 	if (win->begx + win->maxx == COLS) {
    265 		win->flags |= __ENDLINE;
    266 		if (win->begx == 0 && win->maxy == LINES && win->begy == 0)
    267 			win->flags |= __FULLWIN;
    268 		if (win->begy + win->maxy == LINES)
    269 			win->flags |= __SCROLLWIN;
    270 	}
    271 }
    272