Home | History | Annotate | Line # | Download | only in libcurses
newwin.c revision 1.30
      1 /*	$NetBSD: newwin.c,v 1.30 2002/10/22 11:37:34 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.30 2002/10/22 11:37:34 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 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 			sp->battr = 0;
    129 		}
    130 		lp->hash = __hash((char *)(void *)lp->line,
    131 		    (size_t) (ncols * __LDATASIZE));
    132 	}
    133 	return (win);
    134 }
    135 
    136 WINDOW *
    137 subwin(WINDOW *orig, int nlines, int ncols, int by, int bx)
    138 {
    139 	int     i;
    140 	__LINE *lp;
    141 	WINDOW *win;
    142 
    143 	/* Make sure window fits inside the original one. */
    144 #ifdef	DEBUG
    145 	__CTRACE("subwin: (%0.2o, %d, %d, %d, %d)\n", orig, nlines, ncols, by, bx);
    146 #endif
    147 	if (by < orig->begy || bx < orig->begx
    148 	    || by + nlines > orig->maxy + orig->begy
    149 	    || bx + ncols > orig->maxx + orig->begx)
    150 		return (NULL);
    151 	if (nlines == 0)
    152 		nlines = orig->maxy + orig->begy - by;
    153 	if (ncols == 0)
    154 		ncols = orig->maxx + orig->begx - bx;
    155 	if ((win = __makenew(_cursesi_screen, nlines, ncols,
    156 			     by, bx, 1)) == NULL)
    157 		return (NULL);
    158 	win->nextp = orig->nextp;
    159 	orig->nextp = win;
    160 	win->orig = orig;
    161 
    162 	/* Initialize flags here so that refresh can also use __set_subwin. */
    163 	for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++)
    164 		lp->flags = 0;
    165 	__set_subwin(orig, win);
    166 	return (win);
    167 }
    168 /*
    169  * This code is shared with mvwin().
    170  */
    171 void
    172 __set_subwin(WINDOW *orig, WINDOW *win)
    173 {
    174 	int     i;
    175 	__LINE *lp, *olp;
    176 
    177 	win->ch_off = win->begx - orig->begx;
    178 	/* Point line pointers to line space. */
    179 	for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) {
    180 		win->lines[i] = lp;
    181 		olp = orig->lines[i + win->begy - orig->begy];
    182 #ifdef DEBUG
    183 		lp->sentinel = SENTINEL_VALUE;
    184 #endif
    185 		lp->line = &olp->line[win->ch_off];
    186 		lp->firstchp = &olp->firstch;
    187 		lp->lastchp = &olp->lastch;
    188 		lp->hash = __hash((char *)(void *)lp->line,
    189 		    (size_t) (win->maxx * __LDATASIZE));
    190 	}
    191 
    192 #ifdef DEBUG
    193 	__CTRACE("__set_subwin: win->ch_off = %d\n", win->ch_off);
    194 #endif
    195 }
    196 /*
    197  * __makenew --
    198  *	Set up a window buffer and returns a pointer to it.
    199  */
    200 static WINDOW *
    201 __makenew(SCREEN *screen, int nlines, int ncols, int by, int bx, int sub)
    202 {
    203 	WINDOW			*win;
    204 	__LINE			*lp;
    205 	struct __winlist	*wlp, *wlp2;
    206 	int			 i;
    207 
    208 
    209 #ifdef	DEBUG
    210 	__CTRACE("makenew: (%d, %d, %d, %d)\n", nlines, ncols, by, bx);
    211 #endif
    212 	if ((win = malloc(sizeof(WINDOW))) == NULL)
    213 		return (NULL);
    214 #ifdef DEBUG
    215 	__CTRACE("makenew: nlines = %d\n", nlines);
    216 #endif
    217 
    218 	/* Set up line pointer array and line space. */
    219 	if ((win->lines = malloc(nlines * sizeof(__LINE *))) == NULL) {
    220 		free(win);
    221 		return NULL;
    222 	}
    223 	if ((win->lspace = malloc(nlines * sizeof(__LINE))) == NULL) {
    224 		free(win);
    225 		free(win->lines);
    226 		return NULL;
    227 	}
    228 	/* Don't allocate window and line space if it's a subwindow */
    229 	if (!sub) {
    230 		/*
    231 		 * Allocate window space in one chunk.
    232 		 */
    233 		if ((win->wspace =
    234 			malloc(ncols * nlines * sizeof(__LDATA))) == NULL) {
    235 			free(win->lines);
    236 			free(win->lspace);
    237 			free(win);
    238 			return NULL;
    239 		}
    240 		/*
    241 		 * Append window to window list.
    242 		 */
    243 		if ((wlp = malloc(sizeof(struct __winlist))) == NULL) {
    244 			free(win->wspace);
    245 			free(win->lines);
    246 			free(win->lspace);
    247 			free(win);
    248 			return NULL;
    249 		}
    250 		wlp->winp = win;
    251 		wlp->nextp = NULL;
    252 		if (screen->winlistp == NULL)
    253 			screen->winlistp = wlp;
    254 		else {
    255 			wlp2 = screen->winlistp;
    256 			while (wlp2->nextp != NULL)
    257 				wlp2 = wlp2->nextp;
    258 			wlp2->nextp = wlp;
    259 		}
    260 		/*
    261 		 * Point line pointers to line space, and lines themselves into
    262 		 * window space.
    263 		 */
    264 		for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
    265 			win->lines[i] = lp;
    266 			lp->line = &win->wspace[i * ncols];
    267 #ifdef DEBUG
    268 			lp->sentinel = SENTINEL_VALUE;
    269 #endif
    270 			lp->firstchp = &lp->firstch;
    271 			lp->lastchp = &lp->lastch;
    272 			lp->firstch = 0;
    273 			lp->lastch = 0;
    274 		}
    275 	}
    276 #ifdef DEBUG
    277 	__CTRACE("makenew: ncols = %d\n", ncols);
    278 #endif
    279 	win->cury = win->curx = 0;
    280 	win->maxy = nlines;
    281 	win->maxx = ncols;
    282 
    283 	win->begy = by;
    284 	win->begx = bx;
    285 	win->flags = (__IDLINE | __IDCHAR);
    286 	win->delay = -1;
    287 	win->wattr = 0;
    288 	win->bch = ' ';
    289 	win->battr = 0;
    290 	win->scr_t = 0;
    291 	win->scr_b = win->maxy - 1;
    292 	__swflags(win);
    293 #ifdef DEBUG
    294 	__CTRACE("makenew: win->wattr = %0.2o\n", win->wattr);
    295 	__CTRACE("makenew: win->flags = %0.2o\n", win->flags);
    296 	__CTRACE("makenew: win->maxy = %d\n", win->maxy);
    297 	__CTRACE("makenew: win->maxx = %d\n", win->maxx);
    298 	__CTRACE("makenew: win->begy = %d\n", win->begy);
    299 	__CTRACE("makenew: win->begx = %d\n", win->begx);
    300 	__CTRACE("makenew: win->scr_t = %d\n", win->scr_t);
    301 	__CTRACE("makenew: win->scr_b = %d\n", win->scr_b);
    302 #endif
    303 	return (win);
    304 }
    305 
    306 void
    307 __swflags(WINDOW *win)
    308 {
    309 	win->flags &= ~(__ENDLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK);
    310 	if (win->begx + win->maxx == COLS) {
    311 		win->flags |= __ENDLINE;
    312 		if (win->begx == 0 && win->maxy == LINES && win->begy == 0)
    313 			win->flags |= __FULLWIN;
    314 		if (win->begy + win->maxy == LINES)
    315 			win->flags |= __SCROLLWIN;
    316 	}
    317 }
    318