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