Home | History | Annotate | Line # | Download | only in libcurses
newwin.c revision 1.35
      1  1.35       jdc /*	$NetBSD: newwin.c,v 1.35 2003/04/08 05:56:01 jdc Exp $	*/
      2   1.8     mikel 
      3   1.1       cgd /*
      4   1.7       cgd  * Copyright (c) 1981, 1993, 1994
      5   1.5       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36   1.8     mikel #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38   1.8     mikel #if 0
     39   1.7       cgd static char sccsid[] = "@(#)newwin.c	8.3 (Berkeley) 7/27/94";
     40   1.8     mikel #else
     41  1.35       jdc __RCSID("$NetBSD: newwin.c,v 1.35 2003/04/08 05:56:01 jdc Exp $");
     42   1.8     mikel #endif
     43  1.10       mrg #endif				/* not lint */
     44   1.1       cgd 
     45   1.4   mycroft #include <stdlib.h>
     46   1.1       cgd 
     47   1.7       cgd #include "curses.h"
     48  1.13     blymn #include "curses_private.h"
     49   1.7       cgd 
     50  1.15       jdc extern struct __winlist	*winlistp;
     51  1.15       jdc 
     52  1.26     blymn static WINDOW *__makenew(SCREEN *screen, int nlines, int ncols, int by,
     53  1.33       jdc 			 int bx, int sub, int ispad);
     54  1.33       jdc static WINDOW *__subwin(WINDOW *orig, int nlines, int ncols, int by, int bx,
     55  1.33       jdc 			 int ispad);
     56  1.20     blymn 
     57  1.20     blymn /*
     58  1.20     blymn  * derwin --
     59  1.20     blymn  *      Create a new window in the same manner as subwin but (by, bx)
     60  1.20     blymn  *      are relative to the origin of window orig instead of absolute.
     61  1.20     blymn  */
     62  1.20     blymn WINDOW *
     63  1.20     blymn derwin(WINDOW *orig, int nlines, int ncols, int by, int bx)
     64  1.20     blymn {
     65  1.20     blymn 	if (orig == NULL)
     66  1.33       jdc 		return NULL;
     67  1.33       jdc 
     68  1.33       jdc 	return __subwin(orig, nlines, ncols, orig->begy + by, orig->begx + bx,
     69  1.33       jdc 	    FALSE);
     70  1.33       jdc }
     71  1.33       jdc 
     72  1.33       jdc /*
     73  1.33       jdc  * subpad --
     74  1.33       jdc  *      Create a new pad in the same manner as subwin but (by, bx)
     75  1.33       jdc  *      are relative to the origin of window orig instead of absolute.
     76  1.33       jdc  */
     77  1.33       jdc WINDOW *
     78  1.33       jdc subpad(WINDOW *orig, int nlines, int ncols, int by, int bx)
     79  1.33       jdc {
     80  1.33       jdc 	if (orig == NULL)
     81  1.33       jdc 		return NULL;
     82  1.28     blymn 
     83  1.33       jdc 	return __subwin(orig, nlines, ncols, orig->begy + by, orig->begx + bx,
     84  1.33       jdc 	    TRUE);
     85  1.20     blymn }
     86   1.5       cgd 
     87  1.22     blymn /*
     88  1.22     blymn  * dupwin --
     89  1.22     blymn  *      Create a copy of the given window.
     90  1.22     blymn  */
     91  1.22     blymn WINDOW *
     92  1.22     blymn dupwin(WINDOW *win)
     93  1.22     blymn {
     94  1.22     blymn 	WINDOW *new_one;
     95  1.22     blymn 
     96  1.34       dsl 	if ((new_one = __newwin(_cursesi_screen, win->maxy, win->maxx,
     97  1.34       dsl 				win->begy, win->begx, FALSE)) == NULL)
     98  1.22     blymn 		return NULL;
     99  1.22     blymn 
    100  1.22     blymn 	overwrite(win, new_one);
    101  1.22     blymn 	return new_one;
    102  1.22     blymn }
    103  1.22     blymn 
    104   1.4   mycroft /*
    105   1.4   mycroft  * newwin --
    106   1.4   mycroft  *	Allocate space for and set up defaults for a new window.
    107   1.4   mycroft  */
    108   1.1       cgd WINDOW *
    109  1.19     blymn newwin(int nlines, int ncols, int by, int bx)
    110   1.1       cgd {
    111  1.33       jdc 	return __newwin(_cursesi_screen, nlines, ncols, by, bx, FALSE);
    112  1.26     blymn }
    113  1.26     blymn 
    114  1.33       jdc /*
    115  1.33       jdc  * newpad --
    116  1.33       jdc  *	Allocate space for and set up defaults for a new pad.
    117  1.33       jdc  */
    118  1.26     blymn WINDOW *
    119  1.33       jdc newpad(int nlines, int ncols)
    120  1.33       jdc {
    121  1.33       jdc 	if (nlines < 1 || ncols < 1)
    122  1.33       jdc 		return NULL;
    123  1.33       jdc 	return __newwin(_cursesi_screen, nlines, ncols, 0, 0, TRUE);
    124  1.33       jdc }
    125  1.33       jdc 
    126  1.33       jdc WINDOW *
    127  1.33       jdc __newwin(SCREEN *screen, int nlines, int ncols, int by, int bx, int ispad)
    128  1.26     blymn {
    129   1.9     perry 	WINDOW *win;
    130   1.9     perry 	__LINE *lp;
    131  1.10       mrg 	int     i, j;
    132  1.34       dsl 	int	maxy, maxx;
    133   1.9     perry 	__LDATA *sp;
    134  1.35       jdc 
    135  1.35       jdc 	if (by < 0 || bx < 0)
    136  1.35       jdc 		return (NULL);
    137   1.1       cgd 
    138  1.34       dsl 	maxy = nlines > 0 ? nlines : LINES - by + nlines;
    139  1.34       dsl 	maxx = ncols > 0 ? ncols : COLS - bx + ncols;
    140   1.5       cgd 
    141  1.34       dsl 	if ((win = __makenew(screen, maxy, maxx, by, bx, 0, ispad)) == NULL)
    142   1.4   mycroft 		return (NULL);
    143   1.5       cgd 
    144   1.5       cgd 	win->nextp = win;
    145   1.5       cgd 	win->ch_off = 0;
    146   1.5       cgd 	win->orig = NULL;
    147  1.34       dsl 	win->reqy = nlines;
    148  1.34       dsl 	win->reqx = ncols;
    149   1.5       cgd 
    150   1.5       cgd #ifdef DEBUG
    151   1.5       cgd 	__CTRACE("newwin: win->ch_off = %d\n", win->ch_off);
    152   1.5       cgd #endif
    153   1.5       cgd 
    154  1.34       dsl 	for (i = 0; i < maxy; i++) {
    155   1.5       cgd 		lp = win->lines[i];
    156   1.5       cgd 		lp->flags = 0;
    157  1.34       dsl 		for (sp = lp->line, j = 0; j < maxx; j++, sp++) {
    158   1.5       cgd 			sp->ch = ' ';
    159  1.21       jdc 			sp->bch = ' ';
    160   1.5       cgd 			sp->attr = 0;
    161  1.31       jdc 			if (__using_color)
    162  1.31       jdc 				sp->battr = __default_color;
    163  1.31       jdc 			else
    164  1.31       jdc 				sp->battr = 0;
    165   1.5       cgd 		}
    166  1.12  christos 		lp->hash = __hash((char *)(void *)lp->line,
    167  1.30     blymn 		    (size_t) (ncols * __LDATASIZE));
    168   1.1       cgd 	}
    169   1.4   mycroft 	return (win);
    170   1.1       cgd }
    171   1.1       cgd 
    172   1.1       cgd WINDOW *
    173  1.19     blymn subwin(WINDOW *orig, int nlines, int ncols, int by, int bx)
    174   1.1       cgd {
    175  1.34       dsl 	if (orig == NULL || orig->orig != NULL)
    176  1.33       jdc 		return NULL;
    177  1.33       jdc 
    178  1.34       dsl 	return __subwin(orig, nlines, ncols, by, bx, TRUE);
    179  1.33       jdc }
    180  1.33       jdc 
    181  1.33       jdc WINDOW *
    182  1.33       jdc __subwin(WINDOW *orig, int nlines, int ncols, int by, int bx, int ispad)
    183  1.33       jdc {
    184  1.10       mrg 	int     i;
    185   1.5       cgd 	__LINE *lp;
    186   1.9     perry 	WINDOW *win;
    187  1.34       dsl 	int	maxy, maxx;
    188   1.1       cgd 
    189   1.4   mycroft 	/* Make sure window fits inside the original one. */
    190   1.4   mycroft #ifdef	DEBUG
    191  1.34       dsl 	__CTRACE("subwin: (%p, %d, %d, %d, %d, %d)\n", orig, nlines, ncols,
    192  1.33       jdc 	    by, bx, ispad);
    193   1.4   mycroft #endif
    194  1.34       dsl 	maxy = nlines > 0 ? nlines : orig->maxy + orig->begy - by + nlines;
    195  1.34       dsl 	maxx = ncols > 0 ? ncols : orig->maxx + orig->begx - bx + ncols;
    196   1.5       cgd 	if (by < orig->begy || bx < orig->begx
    197  1.34       dsl 	    || by + maxy > orig->maxy + orig->begy
    198  1.34       dsl 	    || bx + maxx > orig->maxx + orig->begx)
    199   1.4   mycroft 		return (NULL);
    200  1.34       dsl 	if ((win = __makenew(_cursesi_screen, maxy, maxx,
    201  1.33       jdc 			     by, bx, 1, ispad)) == NULL)
    202   1.4   mycroft 		return (NULL);
    203  1.34       dsl 	win->reqy = nlines;
    204  1.34       dsl 	win->reqx = ncols;
    205   1.5       cgd 	win->nextp = orig->nextp;
    206   1.5       cgd 	orig->nextp = win;
    207   1.5       cgd 	win->orig = orig;
    208   1.5       cgd 
    209   1.5       cgd 	/* Initialize flags here so that refresh can also use __set_subwin. */
    210   1.5       cgd 	for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++)
    211   1.5       cgd 		lp->flags = 0;
    212   1.4   mycroft 	__set_subwin(orig, win);
    213   1.4   mycroft 	return (win);
    214   1.1       cgd }
    215   1.1       cgd /*
    216   1.4   mycroft  * This code is shared with mvwin().
    217   1.1       cgd  */
    218   1.4   mycroft void
    219  1.16     blymn __set_subwin(WINDOW *orig, WINDOW *win)
    220   1.1       cgd {
    221  1.10       mrg 	int     i;
    222   1.5       cgd 	__LINE *lp, *olp;
    223   1.5       cgd 
    224   1.5       cgd 	win->ch_off = win->begx - orig->begx;
    225  1.10       mrg 	/* Point line pointers to line space. */
    226   1.5       cgd 	for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) {
    227   1.5       cgd 		win->lines[i] = lp;
    228  1.11   mycroft 		olp = orig->lines[i + win->begy - orig->begy];
    229  1.29     blymn #ifdef DEBUG
    230  1.29     blymn 		lp->sentinel = SENTINEL_VALUE;
    231  1.29     blymn #endif
    232  1.18   mycroft 		lp->line = &olp->line[win->ch_off];
    233   1.5       cgd 		lp->firstchp = &olp->firstch;
    234   1.5       cgd 		lp->lastchp = &olp->lastch;
    235  1.12  christos 		lp->hash = __hash((char *)(void *)lp->line,
    236  1.30     blymn 		    (size_t) (win->maxx * __LDATASIZE));
    237   1.5       cgd 	}
    238   1.1       cgd 
    239   1.4   mycroft #ifdef DEBUG
    240  1.33       jdc 	__CTRACE("__set_subwin: win->ch_off = %d\n", win->ch_off);
    241   1.4   mycroft #endif
    242   1.1       cgd }
    243   1.1       cgd /*
    244   1.5       cgd  * __makenew --
    245   1.4   mycroft  *	Set up a window buffer and returns a pointer to it.
    246   1.1       cgd  */
    247   1.1       cgd static WINDOW *
    248  1.33       jdc __makenew(SCREEN *screen, int nlines, int ncols, int by, int bx, int sub,
    249  1.33       jdc 	int ispad)
    250   1.4   mycroft {
    251  1.15       jdc 	WINDOW			*win;
    252  1.15       jdc 	__LINE			*lp;
    253  1.15       jdc 	struct __winlist	*wlp, *wlp2;
    254  1.15       jdc 	int			 i;
    255  1.10       mrg 
    256   1.1       cgd 
    257   1.4   mycroft #ifdef	DEBUG
    258  1.19     blymn 	__CTRACE("makenew: (%d, %d, %d, %d)\n", nlines, ncols, by, bx);
    259   1.4   mycroft #endif
    260  1.34       dsl 	if (nlines <= 0 || ncols <= 0)
    261  1.34       dsl 		return NULL;
    262  1.34       dsl 
    263  1.27     blymn 	if ((win = malloc(sizeof(WINDOW))) == NULL)
    264   1.4   mycroft 		return (NULL);
    265   1.4   mycroft #ifdef DEBUG
    266  1.34       dsl 	__CTRACE("makenew: win = %p\n", win);
    267   1.4   mycroft #endif
    268   1.5       cgd 
    269  1.10       mrg 	/* Set up line pointer array and line space. */
    270  1.19     blymn 	if ((win->lines = malloc(nlines * sizeof(__LINE *))) == NULL) {
    271   1.1       cgd 		free(win);
    272   1.5       cgd 		return NULL;
    273   1.5       cgd 	}
    274  1.19     blymn 	if ((win->lspace = malloc(nlines * sizeof(__LINE))) == NULL) {
    275  1.10       mrg 		free(win);
    276  1.10       mrg 		free(win->lines);
    277   1.5       cgd 		return NULL;
    278   1.5       cgd 	}
    279   1.5       cgd 	/* Don't allocate window and line space if it's a subwindow */
    280   1.5       cgd 	if (!sub) {
    281   1.5       cgd 		/*
    282   1.5       cgd 		 * Allocate window space in one chunk.
    283   1.5       cgd 		 */
    284  1.10       mrg 		if ((win->wspace =
    285  1.19     blymn 			malloc(ncols * nlines * sizeof(__LDATA))) == NULL) {
    286   1.5       cgd 			free(win->lines);
    287   1.5       cgd 			free(win->lspace);
    288   1.5       cgd 			free(win);
    289   1.5       cgd 			return NULL;
    290  1.15       jdc 		}
    291  1.15       jdc 		/*
    292  1.15       jdc 		 * Append window to window list.
    293  1.15       jdc 		 */
    294  1.15       jdc 		if ((wlp = malloc(sizeof(struct __winlist))) == NULL) {
    295  1.15       jdc 			free(win->wspace);
    296  1.15       jdc 			free(win->lines);
    297  1.15       jdc 			free(win->lspace);
    298  1.15       jdc 			free(win);
    299  1.15       jdc 			return NULL;
    300  1.15       jdc 		}
    301  1.15       jdc 		wlp->winp = win;
    302  1.15       jdc 		wlp->nextp = NULL;
    303  1.26     blymn 		if (screen->winlistp == NULL)
    304  1.26     blymn 			screen->winlistp = wlp;
    305  1.15       jdc 		else {
    306  1.26     blymn 			wlp2 = screen->winlistp;
    307  1.15       jdc 			while (wlp2->nextp != NULL)
    308  1.15       jdc 				wlp2 = wlp2->nextp;
    309  1.15       jdc 			wlp2->nextp = wlp;
    310   1.5       cgd 		}
    311   1.5       cgd 		/*
    312   1.5       cgd 		 * Point line pointers to line space, and lines themselves into
    313   1.5       cgd 		 * window space.
    314   1.5       cgd 		 */
    315  1.19     blymn 		for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
    316   1.5       cgd 			win->lines[i] = lp;
    317  1.19     blymn 			lp->line = &win->wspace[i * ncols];
    318  1.29     blymn #ifdef DEBUG
    319  1.29     blymn 			lp->sentinel = SENTINEL_VALUE;
    320  1.29     blymn #endif
    321   1.5       cgd 			lp->firstchp = &lp->firstch;
    322   1.5       cgd 			lp->lastchp = &lp->lastch;
    323  1.32       jdc 			lp->firstch = ncols;
    324   1.5       cgd 			lp->lastch = 0;
    325   1.5       cgd 		}
    326   1.1       cgd 	}
    327   1.4   mycroft #ifdef DEBUG
    328  1.19     blymn 	__CTRACE("makenew: ncols = %d\n", ncols);
    329   1.4   mycroft #endif
    330   1.5       cgd 	win->cury = win->curx = 0;
    331  1.19     blymn 	win->maxy = nlines;
    332  1.19     blymn 	win->maxx = ncols;
    333  1.34       dsl 	win->reqy = nlines;
    334  1.34       dsl 	win->reqx = ncols;
    335   1.5       cgd 
    336   1.5       cgd 	win->begy = by;
    337   1.5       cgd 	win->begx = bx;
    338  1.29     blymn 	win->flags = (__IDLINE | __IDCHAR);
    339  1.17       jdc 	win->delay = -1;
    340  1.13     blymn 	win->wattr = 0;
    341  1.21       jdc 	win->bch = ' ';
    342  1.31       jdc 	if (__using_color)
    343  1.31       jdc 		win->battr = __default_color;
    344  1.31       jdc 	else
    345  1.31       jdc 		win->battr = 0;
    346  1.25       jdc 	win->scr_t = 0;
    347  1.24       jdc 	win->scr_b = win->maxy - 1;
    348  1.33       jdc 	if (ispad)
    349  1.33       jdc 		win->flags |= __ISPAD;
    350  1.33       jdc 	else
    351  1.33       jdc 		__swflags(win);
    352   1.4   mycroft #ifdef DEBUG
    353  1.31       jdc 	__CTRACE("makenew: win->wattr = %08x\n", win->wattr);
    354  1.34       dsl 	__CTRACE("makenew: win->flags = %#.4x\n", win->flags);
    355   1.5       cgd 	__CTRACE("makenew: win->maxy = %d\n", win->maxy);
    356   1.5       cgd 	__CTRACE("makenew: win->maxx = %d\n", win->maxx);
    357   1.5       cgd 	__CTRACE("makenew: win->begy = %d\n", win->begy);
    358   1.5       cgd 	__CTRACE("makenew: win->begx = %d\n", win->begx);
    359  1.31       jdc 	__CTRACE("makenew: win->bch = %s\n", unctrl(win->bch));
    360  1.31       jdc 	__CTRACE("makenew: win->battr = %08x\n", win->battr);
    361  1.24       jdc 	__CTRACE("makenew: win->scr_t = %d\n", win->scr_t);
    362  1.24       jdc 	__CTRACE("makenew: win->scr_b = %d\n", win->scr_b);
    363   1.4   mycroft #endif
    364   1.4   mycroft 	return (win);
    365   1.1       cgd }
    366   1.1       cgd 
    367   1.4   mycroft void
    368  1.16     blymn __swflags(WINDOW *win)
    369   1.1       cgd {
    370   1.7       cgd 	win->flags &= ~(__ENDLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK);
    371  1.33       jdc 	if (win->begx + win->maxx == COLS && !(win->flags & __ISPAD)) {
    372   1.5       cgd 		win->flags |= __ENDLINE;
    373   1.7       cgd 		if (win->begx == 0 && win->maxy == LINES && win->begy == 0)
    374   1.7       cgd 			win->flags |= __FULLWIN;
    375   1.5       cgd 		if (win->begy + win->maxy == LINES)
    376   1.5       cgd 			win->flags |= __SCROLLWIN;
    377   1.1       cgd 	}
    378   1.1       cgd }
    379