Home | History | Annotate | Line # | Download | only in libcurses
newwin.c revision 1.66
      1 /*	$NetBSD: newwin.c,v 1.66 2022/04/12 07:03:04 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. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)newwin.c	8.3 (Berkeley) 7/27/94";
     36 #else
     37 __RCSID("$NetBSD: newwin.c,v 1.66 2022/04/12 07:03:04 blymn Exp $");
     38 #endif
     39 #endif				/* not lint */
     40 
     41 #include <stdlib.h>
     42 
     43 #include "curses.h"
     44 #include "curses_private.h"
     45 
     46 
     47 static WINDOW *__makenew(SCREEN *screen, int nlines, int ncols, int by,
     48 			 int bx, int sub, int ispad);
     49 static WINDOW *__subwin(WINDOW *orig, int nlines, int ncols, int by, int bx,
     50 			 int ispad);
     51 
     52 /*
     53  * derwin --
     54  *      Create a new window in the same manner as subwin but (by, bx)
     55  *      are relative to the origin of window orig instead of absolute.
     56  */
     57 WINDOW *
     58 derwin(WINDOW *orig, int nlines, int ncols, int by, int bx)
     59 {
     60 
     61 	return __subwin(orig, nlines, ncols, orig->begy + by, orig->begx + bx,
     62 	    FALSE);
     63 }
     64 
     65 /*
     66  * subpad --
     67  *      Create a new pad in the same manner as subwin but (by, bx)
     68  *      are relative to the origin of window orig instead of absolute.
     69  */
     70 WINDOW *
     71 subpad(WINDOW *orig, int nlines, int ncols, int by, int bx)
     72 {
     73 
     74 	return __subwin(orig, nlines, ncols, orig->begy + by, orig->begx + bx,
     75 	    TRUE);
     76 }
     77 
     78 /*
     79  * dupwin --
     80  *      Create a copy of the given window.
     81  */
     82 WINDOW *
     83 dupwin(WINDOW *win)
     84 {
     85 	WINDOW *new_one;
     86 
     87 	if ((new_one = __newwin(_cursesi_screen, win->maxy, win->maxx,
     88 				win->begy, win->begx, FALSE,
     89 				win == stdscr ? TRUE : FALSE)) == NULL)
     90 		return NULL;
     91 
     92 	overwrite(win, new_one);
     93 	return new_one;
     94 }
     95 
     96 /*
     97  * newwin --
     98  *	Allocate space for and set up defaults for a new window.
     99  */
    100 WINDOW *
    101 newwin(int nlines, int ncols, int by, int bx)
    102 {
    103 
    104 	return __newwin(_cursesi_screen, nlines, ncols, by, bx, FALSE, FALSE);
    105 }
    106 
    107 /*
    108  * newpad --
    109  *	Allocate space for and set up defaults for a new pad.
    110  */
    111 WINDOW *
    112 newpad(int nlines, int ncols)
    113 {
    114 
    115 	if (nlines < 1 || ncols < 1)
    116 		return NULL;
    117 	return __newwin(_cursesi_screen, nlines, ncols, 0, 0, TRUE, FALSE);
    118 }
    119 
    120 WINDOW *
    121 __newwin(SCREEN *screen, int nlines, int ncols, int by, int bx, int ispad,
    122     int isstdscr)
    123 {
    124 	WINDOW *win;
    125 	__LINE *lp;
    126 	int     i, j;
    127 	int	ry, maxy, maxx;
    128 	__LDATA *sp;
    129 
    130 	if (by < 0 || bx < 0)
    131 		return NULL;
    132 
    133 	if (isstdscr) {
    134 		ry = __rippedlines(screen, -1);
    135 		by += __rippedlines(screen, 1);
    136 	} else
    137 		ry = 0;
    138 
    139 	maxy = nlines > 0 ? nlines : LINES - by - ry + nlines;
    140 	maxx = ncols > 0 ? ncols : COLS - bx + ncols;
    141 
    142 	if ((win = __makenew(screen, maxy, maxx, by, bx, 0, ispad)) == NULL)
    143 		return NULL;
    144 
    145 #ifdef HAVE_WCHAR
    146 	win->bch = (wchar_t) btowc((int) ' ');
    147 #else
    148 	win->bch = ' ';
    149 #endif
    150 
    151 	if (__using_color)
    152 		win->battr |= __default_color;
    153 	win->nextp = win;
    154 	win->ch_off = 0;
    155 	win->orig = NULL;
    156 	win->reqy = nlines;
    157 	win->reqx = ncols;
    158 
    159 	__CTRACE(__CTRACE_WINDOW, "newwin: win->ch_off = %d\n", win->ch_off);
    160 
    161 	for (i = 0; i < maxy; i++) {
    162 		lp = win->alines[i];
    163 		if (ispad)
    164 			lp->flags = __ISDIRTY;
    165 		else
    166 			lp->flags = 0;
    167 		for (sp = lp->line, j = 0; j < maxx; j++, sp++) {
    168 			sp->attr = 0;
    169 			sp->ch = win->bch;
    170 #ifdef HAVE_WCHAR
    171 			sp->nsp = NULL;
    172 			sp->wcols = 1;
    173 #endif /* HAVE_WCHAR */
    174 		}
    175 		lp->hash = __hash_line(lp->line, maxx);
    176 	}
    177 	return (win);
    178 }
    179 
    180 WINDOW *
    181 subwin(WINDOW *orig, int nlines, int ncols, int by, int bx)
    182 {
    183 
    184 	return __subwin(orig, nlines, ncols, by, bx, FALSE);
    185 }
    186 
    187 static WINDOW *
    188 __subwin(WINDOW *orig, int nlines, int ncols, int by, int bx, int ispad)
    189 {
    190 	int     i;
    191 	__LINE *lp;
    192 	WINDOW *win;
    193 	int	maxy, maxx;
    194 
    195 	__CTRACE(__CTRACE_WINDOW, "subwin: (%p, %d, %d, %d, %d, %d)\n",
    196 	    orig, nlines, ncols, by, bx, ispad);
    197 	if (orig == NULL)
    198 		return NULL;
    199 
    200 	/* Make sure window fits inside the original one. */
    201 	maxy = nlines > 0 ? nlines : orig->maxy + orig->begy - by + nlines;
    202 	maxx = ncols > 0 ? ncols : orig->maxx + orig->begx - bx + ncols;
    203 	if (by < orig->begy || bx < orig->begx
    204 	    || by + maxy > orig->maxy + orig->begy
    205 	    || bx + maxx > orig->maxx + orig->begx)
    206 		return NULL;
    207 	if ((win = __makenew(_cursesi_screen, maxy, maxx,
    208 			     by, bx, 1, ispad)) == NULL)
    209 		return NULL;
    210 	win->bch = orig->bch;
    211 	win->battr = orig->battr;
    212 	win->reqy = nlines;
    213 	win->reqx = ncols;
    214 	win->nextp = orig->nextp;
    215 	orig->nextp = win;
    216 	win->orig = orig;
    217 
    218 	/* Initialize flags here so that refresh can also use __set_subwin. */
    219 	for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++)
    220 		lp->flags = 0;
    221 	__set_subwin(orig, win);
    222 	return win;
    223 }
    224 /*
    225  * This code is shared with mvwin().
    226  */
    227 void
    228 __set_subwin(WINDOW *orig, WINDOW *win)
    229 {
    230 	int     i;
    231 	__LINE *lp, *olp;
    232 
    233 	win->ch_off = win->begx - orig->begx;
    234 	/* Point line pointers to line space. */
    235 	for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) {
    236 		win->alines[i] = lp;
    237 		olp = orig->alines[i + win->begy - orig->begy];
    238 #ifdef DEBUG
    239 		lp->sentinel = SENTINEL_VALUE;
    240 #endif
    241 		lp->line = &olp->line[win->ch_off];
    242 		lp->firstchp = &olp->firstch;
    243 		lp->lastchp = &olp->lastch;
    244 		lp->hash = __hash_line(lp->line, win->maxx);
    245 	}
    246 
    247 	__CTRACE(__CTRACE_WINDOW, "__set_subwin: win->ch_off = %d\n",
    248 	    win->ch_off);
    249 }
    250 /*
    251  * __makenew --
    252  *	Set up a window buffer and returns a pointer to it.
    253  */
    254 static WINDOW *
    255 __makenew(SCREEN *screen, int nlines, int ncols, int by, int bx, int sub,
    256 	int ispad)
    257 {
    258 	WINDOW			*win;
    259 	__LINE			*lp;
    260 	struct __winlist	*wlp, *wlp2;
    261 	int			 i;
    262 
    263 
    264 	__CTRACE(__CTRACE_WINDOW, "makenew: (%d, %d, %d, %d)\n",
    265 	    nlines, ncols, by, bx);
    266 	if (nlines <= 0 || ncols <= 0)
    267 		return NULL;
    268 
    269 	if ((win = malloc(sizeof(WINDOW))) == NULL)
    270 		return NULL;
    271 	__CTRACE(__CTRACE_WINDOW, "makenew: win = %p\n", win);
    272 	win->fp = NULL;
    273 	win->buf = NULL;
    274 	win->buflen = 0;
    275 
    276 	/* Set up line pointer array and line space. */
    277 	if ((win->alines = malloc(nlines * sizeof(__LINE *))) == NULL) {
    278 		free(win);
    279 		return NULL;
    280 	}
    281 	if ((win->lspace = calloc(nlines, sizeof(__LINE))) == NULL) {
    282 		free(win->alines);
    283 		free(win);
    284 		return NULL;
    285 	}
    286 	/* Don't allocate window and line space if it's a subwindow */
    287 	if (sub)
    288 		win->wspace = NULL;
    289 	else {
    290 		/*
    291 		 * Allocate window space in one chunk.
    292 		 */
    293 		if ((win->wspace =
    294 			calloc(ncols * nlines, sizeof(__LDATA))) == NULL) {
    295 			free(win->lspace);
    296 			free(win->alines);
    297 			free(win);
    298 			return NULL;
    299 		}
    300 		/*
    301 		 * Append window to window list.
    302 		 */
    303 		if ((wlp = malloc(sizeof(struct __winlist))) == NULL) {
    304 			free(win->wspace);
    305 			free(win->lspace);
    306 			free(win->alines);
    307 			free(win);
    308 			return NULL;
    309 		}
    310 		wlp->winp = win;
    311 		wlp->nextp = NULL;
    312 		if (screen->winlistp == NULL)
    313 			screen->winlistp = wlp;
    314 		else {
    315 			wlp2 = screen->winlistp;
    316 			while (wlp2->nextp != NULL)
    317 				wlp2 = wlp2->nextp;
    318 			wlp2->nextp = wlp;
    319 		}
    320 		/*
    321 		 * Point line pointers to line space, and lines themselves into
    322 		 * window space.
    323 		 */
    324 		for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
    325 			win->alines[i] = lp;
    326 			lp->line = &win->wspace[i * ncols];
    327 #ifdef DEBUG
    328 			lp->sentinel = SENTINEL_VALUE;
    329 #endif
    330 			lp->firstchp = &lp->firstch;
    331 			lp->lastchp = &lp->lastch;
    332 			if (ispad) {
    333 				lp->firstch = 0;
    334 				lp->lastch = ncols;
    335 			} else {
    336 				lp->firstch = ncols;
    337 				lp->lastch = 0;
    338 			}
    339 		}
    340 	}
    341 	__CTRACE(__CTRACE_WINDOW, "makenew: ncols = %d\n", ncols);
    342 	win->screen = screen;
    343 	win->cury = win->curx = 0;
    344 	win->maxy = nlines;
    345 	win->maxx = ncols;
    346 	win->reqy = nlines;
    347 	win->reqx = ncols;
    348 
    349 	win->begy = by;
    350 	win->begx = bx;
    351 	win->flags = (__IDLINE | __IDCHAR);
    352 	win->delay = -1;
    353 	win->wattr = 0;
    354 	win->battr = 0;
    355 #ifdef HAVE_WCHAR
    356 	win->bnsp = NULL;
    357 	win->wcols = 1;
    358 #endif /* HAVE_WCHAR */
    359 	win->scr_t = 0;
    360 	win->scr_b = win->maxy - 1;
    361 	if (ispad) {
    362 		win->flags |= __ISPAD;
    363 		win->pbegy = 0;
    364 		win->pbegx = 0;
    365 		win->sbegy = 0;
    366 		win->sbegx = 0;
    367 		win->smaxy = 0;
    368 		win->smaxx = 0;
    369 	} else
    370 		__swflags(win);
    371 	__CTRACE(__CTRACE_WINDOW, "makenew: sub = %d\n", sub);
    372 	__CTRACE(__CTRACE_WINDOW, "makenew: ispad = %d\n", ispad);
    373 	__CTRACE(__CTRACE_WINDOW, "makenew: win->wattr = %08x\n", win->wattr);
    374 	__CTRACE(__CTRACE_WINDOW, "makenew: win->flags = %#.4x\n", win->flags);
    375 	__CTRACE(__CTRACE_WINDOW, "makenew: win->maxy = %d\n", win->maxy);
    376 	__CTRACE(__CTRACE_WINDOW, "makenew: win->maxx = %d\n", win->maxx);
    377 	__CTRACE(__CTRACE_WINDOW, "makenew: win->begy = %d\n", win->begy);
    378 	__CTRACE(__CTRACE_WINDOW, "makenew: win->begx = %d\n", win->begx);
    379 	__CTRACE(__CTRACE_WINDOW, "makenew: win->scr_t = %d\n", win->scr_t);
    380 	__CTRACE(__CTRACE_WINDOW, "makenew: win->scr_b = %d\n", win->scr_b);
    381 	return win;
    382 }
    383 
    384 void
    385 __swflags(WINDOW *win)
    386 {
    387 	TERMINAL *term = win->screen->term;
    388 
    389 	win->flags &= ~(__ENDLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK);
    390 	if (win->begx + win->maxx == win->screen->COLS &&
    391 	    !(win->flags & __ISPAD))
    392 	{
    393 		win->flags |= __ENDLINE;
    394 		if (win->begx == 0 &&
    395 		    win->maxy == win->screen->LINES &&
    396 		    win->begy == 0)
    397 			win->flags |= __FULLWIN;
    398 		if (win->begy + win->maxy == win->screen->LINES &&
    399 		    t_auto_right_margin(term) &&
    400 		    !(t_insert_character(term) != NULL ||
    401 		    t_parm_ich(term) != NULL ||
    402 		    (t_enter_insert_mode(term) != NULL &&
    403 		     t_exit_insert_mode(term) != NULL)))
    404 			win->flags |= __SCROLLWIN;
    405 	}
    406 }
    407 
    408 /*
    409  * is_pad --
    410  *	Return true if window was created by newpad.
    411  */
    412 bool
    413 is_pad(const WINDOW *win)
    414 {
    415 
    416 	return win->flags & __ISPAD ? true : false;
    417 }
    418