Home | History | Annotate | Line # | Download | only in libcurses
newwin.c revision 1.31
      1 /*	$NetBSD: newwin.c,v 1.31 2002/11/25 09:11:26 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.31 2002/11/25 09:11:26 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 extern struct __winlist	*winlistp;
     51 
     52 static WINDOW *__makenew(SCREEN *screen, int nlines, int ncols, int by,
     53 			 int bx, int sub);
     54 
     55 /*
     56  * derwin --
     57  *      Create a new window in the same manner as subwin but (by, bx)
     58  *      are relative to the origin of window orig instead of absolute.
     59  */
     60 WINDOW *
     61 derwin(WINDOW *orig, int nlines, int ncols, int by, int bx)
     62 {
     63 	if (orig == NULL)
     64 		return ERR;
     65 
     66 	return subwin(orig, nlines, ncols, orig->begy + by, orig->begx + bx);
     67 }
     68 
     69 /*
     70  * dupwin --
     71  *      Create a copy of the given window.
     72  */
     73 WINDOW *
     74 dupwin(WINDOW *win)
     75 {
     76 	WINDOW *new_one;
     77 
     78 	if ((new_one =
     79 	     newwin(win->maxy, win->maxx, win->begy, win->begx)) == NULL)
     80 		return NULL;
     81 
     82 	overwrite(win, new_one);
     83 	return new_one;
     84 }
     85 
     86 
     87 /*
     88  * newwin --
     89  *	Allocate space for and set up defaults for a new window.
     90  */
     91 WINDOW *
     92 newwin(int nlines, int ncols, int by, int bx)
     93 {
     94 	return __newwin(_cursesi_screen, nlines, ncols, by, bx);
     95 }
     96 
     97 WINDOW *
     98 __newwin(SCREEN *screen, int nlines, int ncols, int by, int bx)
     99 {
    100 	WINDOW *win;
    101 	__LINE *lp;
    102 	int     i, j;
    103 	__LDATA *sp;
    104 
    105 	if (nlines == 0)
    106 		nlines = LINES - by;
    107 	if (ncols == 0)
    108 		ncols = COLS - bx;
    109 
    110 	if ((win = __makenew(screen, nlines, ncols, by, bx, 0)) == NULL)
    111 		return (NULL);
    112 
    113 	win->nextp = win;
    114 	win->ch_off = 0;
    115 	win->orig = NULL;
    116 
    117 #ifdef DEBUG
    118 	__CTRACE("newwin: win->ch_off = %d\n", win->ch_off);
    119 #endif
    120 
    121 	for (i = 0; i < nlines; i++) {
    122 		lp = win->lines[i];
    123 		lp->flags = 0;
    124 		for (sp = lp->line, j = 0; j < ncols; j++, sp++) {
    125 			sp->ch = ' ';
    126 			sp->bch = ' ';
    127 			sp->attr = 0;
    128 			if (__using_color)
    129 				sp->battr = __default_color;
    130 			else
    131 				sp->battr = 0;
    132 		}
    133 		lp->hash = __hash((char *)(void *)lp->line,
    134 		    (size_t) (ncols * __LDATASIZE));
    135 	}
    136 	return (win);
    137 }
    138 
    139 WINDOW *
    140 subwin(WINDOW *orig, int nlines, int ncols, int by, int bx)
    141 {
    142 	int     i;
    143 	__LINE *lp;
    144 	WINDOW *win;
    145 
    146 	/* Make sure window fits inside the original one. */
    147 #ifdef	DEBUG
    148 	__CTRACE("subwin: (%0.2o, %d, %d, %d, %d)\n", orig, nlines, ncols, by, bx);
    149 #endif
    150 	if (by < orig->begy || bx < orig->begx
    151 	    || by + nlines > orig->maxy + orig->begy
    152 	    || bx + ncols > orig->maxx + orig->begx)
    153 		return (NULL);
    154 	if (nlines == 0)
    155 		nlines = orig->maxy + orig->begy - by;
    156 	if (ncols == 0)
    157 		ncols = orig->maxx + orig->begx - bx;
    158 	if ((win = __makenew(_cursesi_screen, nlines, ncols,
    159 			     by, bx, 1)) == NULL)
    160 		return (NULL);
    161 	win->nextp = orig->nextp;
    162 	orig->nextp = win;
    163 	win->orig = orig;
    164 
    165 	/* Initialize flags here so that refresh can also use __set_subwin. */
    166 	for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++)
    167 		lp->flags = 0;
    168 	__set_subwin(orig, win);
    169 	return (win);
    170 }
    171 /*
    172  * This code is shared with mvwin().
    173  */
    174 void
    175 __set_subwin(WINDOW *orig, WINDOW *win)
    176 {
    177 	int     i;
    178 	__LINE *lp, *olp;
    179 
    180 	win->ch_off = win->begx - orig->begx;
    181 	/* Point line pointers to line space. */
    182 	for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) {
    183 		win->lines[i] = lp;
    184 		olp = orig->lines[i + win->begy - orig->begy];
    185 #ifdef DEBUG
    186 		lp->sentinel = SENTINEL_VALUE;
    187 #endif
    188 		lp->line = &olp->line[win->ch_off];
    189 		lp->firstchp = &olp->firstch;
    190 		lp->lastchp = &olp->lastch;
    191 		lp->hash = __hash((char *)(void *)lp->line,
    192 		    (size_t) (win->maxx * __LDATASIZE));
    193 	}
    194 
    195 #ifdef DEBUG
    196 	__CTRACE("__set_subwin: win->ch_off = %d\n", win->ch_off);
    197 #endif
    198 }
    199 /*
    200  * __makenew --
    201  *	Set up a window buffer and returns a pointer to it.
    202  */
    203 static WINDOW *
    204 __makenew(SCREEN *screen, int nlines, int ncols, int by, int bx, int sub)
    205 {
    206 	WINDOW			*win;
    207 	__LINE			*lp;
    208 	struct __winlist	*wlp, *wlp2;
    209 	int			 i;
    210 
    211 
    212 #ifdef	DEBUG
    213 	__CTRACE("makenew: (%d, %d, %d, %d)\n", nlines, ncols, by, bx);
    214 #endif
    215 	if ((win = malloc(sizeof(WINDOW))) == NULL)
    216 		return (NULL);
    217 #ifdef DEBUG
    218 	__CTRACE("makenew: nlines = %d\n", nlines);
    219 #endif
    220 
    221 	/* Set up line pointer array and line space. */
    222 	if ((win->lines = malloc(nlines * sizeof(__LINE *))) == NULL) {
    223 		free(win);
    224 		return NULL;
    225 	}
    226 	if ((win->lspace = malloc(nlines * sizeof(__LINE))) == NULL) {
    227 		free(win);
    228 		free(win->lines);
    229 		return NULL;
    230 	}
    231 	/* Don't allocate window and line space if it's a subwindow */
    232 	if (!sub) {
    233 		/*
    234 		 * Allocate window space in one chunk.
    235 		 */
    236 		if ((win->wspace =
    237 			malloc(ncols * nlines * sizeof(__LDATA))) == NULL) {
    238 			free(win->lines);
    239 			free(win->lspace);
    240 			free(win);
    241 			return NULL;
    242 		}
    243 		/*
    244 		 * Append window to window list.
    245 		 */
    246 		if ((wlp = malloc(sizeof(struct __winlist))) == NULL) {
    247 			free(win->wspace);
    248 			free(win->lines);
    249 			free(win->lspace);
    250 			free(win);
    251 			return NULL;
    252 		}
    253 		wlp->winp = win;
    254 		wlp->nextp = NULL;
    255 		if (screen->winlistp == NULL)
    256 			screen->winlistp = wlp;
    257 		else {
    258 			wlp2 = screen->winlistp;
    259 			while (wlp2->nextp != NULL)
    260 				wlp2 = wlp2->nextp;
    261 			wlp2->nextp = wlp;
    262 		}
    263 		/*
    264 		 * Point line pointers to line space, and lines themselves into
    265 		 * window space.
    266 		 */
    267 		for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
    268 			win->lines[i] = lp;
    269 			lp->line = &win->wspace[i * ncols];
    270 #ifdef DEBUG
    271 			lp->sentinel = SENTINEL_VALUE;
    272 #endif
    273 			lp->firstchp = &lp->firstch;
    274 			lp->lastchp = &lp->lastch;
    275 			lp->firstch = 0;
    276 			lp->lastch = 0;
    277 		}
    278 	}
    279 #ifdef DEBUG
    280 	__CTRACE("makenew: ncols = %d\n", ncols);
    281 #endif
    282 	win->cury = win->curx = 0;
    283 	win->maxy = nlines;
    284 	win->maxx = ncols;
    285 
    286 	win->begy = by;
    287 	win->begx = bx;
    288 	win->flags = (__IDLINE | __IDCHAR);
    289 	win->delay = -1;
    290 	win->wattr = 0;
    291 	win->bch = ' ';
    292 	if (__using_color)
    293 		win->battr = __default_color;
    294 	else
    295 		win->battr = 0;
    296 	win->scr_t = 0;
    297 	win->scr_b = win->maxy - 1;
    298 	__swflags(win);
    299 #ifdef DEBUG
    300 	__CTRACE("makenew: win->wattr = %08x\n", win->wattr);
    301 	__CTRACE("makenew: win->flags = %0.2o\n", win->flags);
    302 	__CTRACE("makenew: win->maxy = %d\n", win->maxy);
    303 	__CTRACE("makenew: win->maxx = %d\n", win->maxx);
    304 	__CTRACE("makenew: win->begy = %d\n", win->begy);
    305 	__CTRACE("makenew: win->begx = %d\n", win->begx);
    306 	__CTRACE("makenew: win->bch = %s\n", unctrl(win->bch));
    307 	__CTRACE("makenew: win->battr = %08x\n", win->battr);
    308 	__CTRACE("makenew: win->scr_t = %d\n", win->scr_t);
    309 	__CTRACE("makenew: win->scr_b = %d\n", win->scr_b);
    310 #endif
    311 	return (win);
    312 }
    313 
    314 void
    315 __swflags(WINDOW *win)
    316 {
    317 	win->flags &= ~(__ENDLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK);
    318 	if (win->begx + win->maxx == COLS) {
    319 		win->flags |= __ENDLINE;
    320 		if (win->begx == 0 && win->maxy == LINES && win->begy == 0)
    321 			win->flags |= __FULLWIN;
    322 		if (win->begy + win->maxy == LINES)
    323 			win->flags |= __SCROLLWIN;
    324 	}
    325 }
    326