Home | History | Annotate | Line # | Download | only in libcurses
newwin.c revision 1.4
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1981 Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1      cgd  * modification, are permitted provided that the following conditions
      7  1.1      cgd  * are met:
      8  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1      cgd  *    must display the following acknowledgement:
     15  1.1      cgd  *	This product includes software developed by the University of
     16  1.1      cgd  *	California, Berkeley and its contributors.
     17  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1      cgd  *    may be used to endorse or promote products derived from this software
     19  1.1      cgd  *    without specific prior written permission.
     20  1.1      cgd  *
     21  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1      cgd  * SUCH DAMAGE.
     32  1.1      cgd  */
     33  1.1      cgd 
     34  1.1      cgd #ifndef lint
     35  1.4  mycroft /*static char sccsid[] = "from: @(#)newwin.c	5.6 (Berkeley) 8/23/92";*/
     36  1.3  mycroft static char rcsid[] = "$Id: newwin.c,v 1.4 1993/08/07 05:49:00 mycroft Exp $";
     37  1.4  mycroft #endif	/* not lint */
     38  1.1      cgd 
     39  1.4  mycroft #include <curses.h>
     40  1.4  mycroft #include <stdlib.h>
     41  1.1      cgd 
     42  1.4  mycroft #undef	nl		/* Don't need it here, and it interferes. */
     43  1.1      cgd 
     44  1.4  mycroft static WINDOW *makenew __P((int, int, int, int));
     45  1.1      cgd 
     46  1.4  mycroft /*
     47  1.4  mycroft  * newwin --
     48  1.4  mycroft  *	Allocate space for and set up defaults for a new window.
     49  1.4  mycroft  */
     50  1.1      cgd WINDOW *
     51  1.1      cgd newwin(num_lines, num_cols, begy, begx)
     52  1.4  mycroft 	int num_lines, num_cols, begy, begx;
     53  1.1      cgd {
     54  1.4  mycroft 	register WINDOW *win;
     55  1.4  mycroft 	register int by, bx, i, j, nl, nc;
     56  1.4  mycroft 	register char *sp;
     57  1.1      cgd 
     58  1.1      cgd 	by = begy;
     59  1.1      cgd 	bx = begx;
     60  1.1      cgd 	nl = num_lines;
     61  1.1      cgd 	nc = num_cols;
     62  1.1      cgd 
     63  1.1      cgd 	if (nl == 0)
     64  1.1      cgd 		nl = LINES - by;
     65  1.1      cgd 	if (nc == 0)
     66  1.1      cgd 		nc = COLS - bx;
     67  1.1      cgd 	if ((win = makenew(nl, nc, by, bx)) == NULL)
     68  1.4  mycroft 		return (NULL);
     69  1.4  mycroft 	if ((win->_firstch = malloc(nl * sizeof(win->_firstch[0]))) == NULL) {
     70  1.1      cgd 		free(win->_y);
     71  1.1      cgd 		free(win);
     72  1.4  mycroft 		return (NULL);
     73  1.1      cgd 	}
     74  1.4  mycroft 	if ((win->_lastch = malloc(nl * sizeof(win->_lastch[0]))) == NULL) {
     75  1.1      cgd 		free(win->_y);
     76  1.1      cgd 		free(win->_firstch);
     77  1.1      cgd 		free(win);
     78  1.4  mycroft 		return (NULL);
     79  1.1      cgd 	}
     80  1.1      cgd 	win->_nextp = win;
     81  1.1      cgd 	for (i = 0; i < nl; i++) {
     82  1.1      cgd 		win->_firstch[i] = _NOCHANGE;
     83  1.1      cgd 		win->_lastch[i] = _NOCHANGE;
     84  1.1      cgd 	}
     85  1.1      cgd 	for (i = 0; i < nl; i++)
     86  1.4  mycroft 		if ((win->_y[i] = malloc(nc * sizeof(win->_y[0]))) == NULL) {
     87  1.1      cgd 			for (j = 0; j < i; j++)
     88  1.1      cgd 				free(win->_y[j]);
     89  1.1      cgd 			free(win->_firstch);
     90  1.1      cgd 			free(win->_lastch);
     91  1.1      cgd 			free(win->_y);
     92  1.1      cgd 			free(win);
     93  1.4  mycroft 			return (NULL);
     94  1.4  mycroft 		} else
     95  1.4  mycroft 			for (sp = win->_y[i]; sp < win->_y[i] + nc;)
     96  1.1      cgd 				*sp++ = ' ';
     97  1.1      cgd 	win->_ch_off = 0;
     98  1.4  mycroft #ifdef DEBUG
     99  1.4  mycroft 	__TRACE("newwin: win->_ch_off = %d\n", win->_ch_off);
    100  1.4  mycroft #endif
    101  1.4  mycroft 	return (win);
    102  1.1      cgd }
    103  1.1      cgd 
    104  1.1      cgd WINDOW *
    105  1.1      cgd subwin(orig, num_lines, num_cols, begy, begx)
    106  1.4  mycroft 	register WINDOW *orig;
    107  1.4  mycroft 	int num_lines, num_cols, begy, begx;
    108  1.1      cgd {
    109  1.4  mycroft 	register WINDOW *win;
    110  1.4  mycroft 	register int by, bx, nl, nc;
    111  1.1      cgd 
    112  1.1      cgd 	by = begy;
    113  1.1      cgd 	bx = begx;
    114  1.1      cgd 	nl = num_lines;
    115  1.1      cgd 	nc = num_cols;
    116  1.1      cgd 
    117  1.4  mycroft 	/* Make sure window fits inside the original one. */
    118  1.4  mycroft #ifdef	DEBUG
    119  1.4  mycroft 	__TRACE("subwin: (%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx);
    120  1.4  mycroft #endif
    121  1.1      cgd 	if (by < orig->_begy || bx < orig->_begx
    122  1.1      cgd 	    || by + nl > orig->_maxy + orig->_begy
    123  1.1      cgd 	    || bx + nc > orig->_maxx + orig->_begx)
    124  1.4  mycroft 		return (NULL);
    125  1.1      cgd 	if (nl == 0)
    126  1.1      cgd 		nl = orig->_maxy + orig->_begy - by;
    127  1.1      cgd 	if (nc == 0)
    128  1.1      cgd 		nc = orig->_maxx + orig->_begx - bx;
    129  1.1      cgd 	if ((win = makenew(nl, nc, by, bx)) == NULL)
    130  1.4  mycroft 		return (NULL);
    131  1.1      cgd 	win->_nextp = orig->_nextp;
    132  1.1      cgd 	orig->_nextp = win;
    133  1.1      cgd 	win->_orig = orig;
    134  1.4  mycroft 	__set_subwin(orig, win);
    135  1.4  mycroft 	return (win);
    136  1.1      cgd }
    137  1.1      cgd 
    138  1.1      cgd /*
    139  1.4  mycroft  * This code is shared with mvwin().
    140  1.1      cgd  */
    141  1.4  mycroft void
    142  1.4  mycroft __set_subwin(orig, win)
    143  1.4  mycroft 	register WINDOW *orig, *win;
    144  1.1      cgd {
    145  1.4  mycroft 	register int i, j, k;
    146  1.1      cgd 
    147  1.1      cgd 	j = win->_begy - orig->_begy;
    148  1.1      cgd 	k = win->_begx - orig->_begx;
    149  1.1      cgd 	win->_ch_off = k;
    150  1.4  mycroft #ifdef DEBUG
    151  1.4  mycroft 	__TRACE("__set_subwin: win->_ch_off = %d\n", win->_ch_off);
    152  1.4  mycroft #endif
    153  1.1      cgd 	win->_firstch = &orig->_firstch[j];
    154  1.1      cgd 	win->_lastch = &orig->_lastch[j];
    155  1.1      cgd 	for (i = 0; i < win->_maxy; i++, j++)
    156  1.1      cgd 		win->_y[i] = &orig->_y[j][k];
    157  1.1      cgd }
    158  1.1      cgd 
    159  1.1      cgd /*
    160  1.4  mycroft  * makenew --
    161  1.4  mycroft  *	Set up a window buffer and returns a pointer to it.
    162  1.1      cgd  */
    163  1.1      cgd static WINDOW *
    164  1.1      cgd makenew(num_lines, num_cols, begy, begx)
    165  1.4  mycroft 	int num_lines, num_cols, begy, begx;
    166  1.4  mycroft {
    167  1.4  mycroft 	register WINDOW *win;
    168  1.4  mycroft 	register int by, bx, nl, nc;
    169  1.1      cgd 
    170  1.1      cgd 	by = begy;
    171  1.1      cgd 	bx = begx;
    172  1.1      cgd 	nl = num_lines;
    173  1.1      cgd 	nc = num_cols;
    174  1.1      cgd 
    175  1.4  mycroft #ifdef	DEBUG
    176  1.4  mycroft 	__TRACE("makenew: (%d, %d, %d, %d)\n", nl, nc, by, bx);
    177  1.4  mycroft #endif
    178  1.4  mycroft 	if ((win = malloc(sizeof(*win))) == NULL)
    179  1.4  mycroft 		return (NULL);
    180  1.4  mycroft #ifdef DEBUG
    181  1.4  mycroft 	__TRACE("makenew: nl = %d\n", nl);
    182  1.4  mycroft #endif
    183  1.4  mycroft 	if ((win->_y = malloc(nl * sizeof(win->_y[0]))) == NULL) {
    184  1.1      cgd 		free(win);
    185  1.4  mycroft 		return (NULL);
    186  1.1      cgd 	}
    187  1.4  mycroft #ifdef DEBUG
    188  1.4  mycroft 	__TRACE("makenew: nc = %d\n", nc);
    189  1.4  mycroft #endif
    190  1.1      cgd 	win->_cury = win->_curx = 0;
    191  1.4  mycroft 	win->_clear = 0;
    192  1.1      cgd 	win->_maxy = nl;
    193  1.1      cgd 	win->_maxx = nc;
    194  1.1      cgd 	win->_begy = by;
    195  1.1      cgd 	win->_begx = bx;
    196  1.1      cgd 	win->_flags = 0;
    197  1.4  mycroft 	win->_scroll = win->_leave = 0;
    198  1.4  mycroft 	__swflags(win);
    199  1.4  mycroft #ifdef DEBUG
    200  1.4  mycroft 	__TRACE("makenew: win->_clear = %d\n", win->_clear);
    201  1.4  mycroft 	__TRACE("makenew: win->_leave = %d\n", win->_leave);
    202  1.4  mycroft 	__TRACE("makenew: win->_scroll = %d\n", win->_scroll);
    203  1.4  mycroft 	__TRACE("makenew: win->_flags = %0.2o\n", win->_flags);
    204  1.4  mycroft 	__TRACE("makenew: win->_maxy = %d\n", win->_maxy);
    205  1.4  mycroft 	__TRACE("makenew: win->_maxx = %d\n", win->_maxx);
    206  1.4  mycroft 	__TRACE("makenew: win->_begy = %d\n", win->_begy);
    207  1.4  mycroft 	__TRACE("makenew: win->_begx = %d\n", win->_begx);
    208  1.4  mycroft #endif
    209  1.4  mycroft 	return (win);
    210  1.1      cgd }
    211  1.1      cgd 
    212  1.4  mycroft void
    213  1.4  mycroft __swflags(win)
    214  1.4  mycroft 	register WINDOW *win;
    215  1.1      cgd {
    216  1.4  mycroft 	win->_flags &= ~(_ENDLINE | _FULLLINE | _FULLWIN | _SCROLLWIN);
    217  1.1      cgd 	if (win->_begx + win->_maxx == COLS) {
    218  1.1      cgd 		win->_flags |= _ENDLINE;
    219  1.1      cgd 		if (win->_begx == 0) {
    220  1.1      cgd 			if (AL && DL)
    221  1.1      cgd 				win->_flags |= _FULLLINE;
    222  1.1      cgd 			if (win->_maxy == LINES && win->_begy == 0)
    223  1.1      cgd 				win->_flags |= _FULLWIN;
    224  1.1      cgd 		}
    225  1.1      cgd 		if (win->_begy + win->_maxy == LINES)
    226  1.1      cgd 			win->_flags |= _SCROLLWIN;
    227  1.1      cgd 	}
    228  1.1      cgd }
    229