Home | History | Annotate | Download | only in libcurses
      1 /*
      2  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
      3  * Use is subject to license terms.
      4  */
      5 
      6 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
      7 /*	  All Rights Reserved  	*/
      8 
      9 /*
     10  * Copyright (c) 1980 Regents of the University of California.
     11  * All rights reserved.  The Berkeley software License Agreement
     12  * specifies the terms and conditions for redistribution.
     13  */
     14 
     15 #pragma ident	"%Z%%M%	%I%	%E% SMI"
     16 
     17 /*LINTLIBRARY*/
     18 
     19 #ifndef lint
     20 static char
     21 sccsid[] = "@(#)newwin.c 1.7 89/07/13 SMI"; /* from UCB 5.1 85/06/07 */
     22 #endif	/* not lint */
     23 
     24 /*
     25  * allocate space for and set up defaults for a new window
     26  *
     27  */
     28 
     29 #include	"curses.ext"
     30 #include 	<malloc.h>
     31 
     32 #define	SMALLOC	(short *)malloc
     33 
     34 /* forward declaration */
     35 static WINDOW *makenew(int, int, int, int);
     36 
     37 #undef		nl	/* don't need it here, and it interferes	*/
     38 
     39 WINDOW *
     40 newwin(int num_lines, int num_cols, int begy, int begx)
     41 {
     42 	WINDOW	*win;
     43 	char	*sp;
     44 	int	i, by, bx, nl, nc;
     45 	int	j;
     46 
     47 	by = begy;
     48 	bx = begx;
     49 	nl = num_lines;
     50 	nc = num_cols;
     51 
     52 	if (nl == 0)
     53 		nl = LINES - by;
     54 	if (nc == 0)
     55 		nc = COLS - bx;
     56 	if ((win = makenew(nl, nc, by, bx)) == NULL)
     57 		return (ERR);
     58 	if ((win->_firstch = SMALLOC(nl * sizeof (win->_firstch[0]))) == NULL) {
     59 		free(win->_y);
     60 		free(win);
     61 		return (NULL);
     62 	}
     63 	if ((win->_lastch = SMALLOC(nl * sizeof (win->_lastch[0]))) == NULL) {
     64 		free(win->_y);
     65 		free(win->_firstch);
     66 		free(win);
     67 		return (NULL);
     68 	}
     69 	win->_nextp = win;
     70 	for (i = 0; i < nl; i++) {
     71 		win->_firstch[i] = _NOCHANGE;
     72 		win->_lastch[i] = _NOCHANGE;
     73 	}
     74 	for (i = 0; i < nl; i++)
     75 		if ((win->_y[i] = malloc(nc * sizeof (win->_y[0]))) == NULL) {
     76 			for (j = 0; j < i; j++)
     77 				free(win->_y[j]);
     78 			free(win->_firstch);
     79 			free(win->_lastch);
     80 			free(win->_y);
     81 			free(win);
     82 			return (ERR);
     83 		}
     84 		else
     85 			for (sp = win->_y[i]; sp < win->_y[i] + nc; )
     86 				*sp++ = ' ';
     87 	win->_ch_off = 0;
     88 #ifdef DEBUG
     89 	fprintf(outf, "NEWWIN: win->_ch_off = %d\n", win->_ch_off);
     90 #endif
     91 	return (win);
     92 }
     93 
     94 WINDOW *
     95 subwin(WINDOW *orig, int num_lines, int num_cols, int begy, int begx)
     96 {
     97 	WINDOW	*win;
     98 	int	by, bx, nl, nc;
     99 
    100 	by = begy;
    101 	bx = begx;
    102 	nl = num_lines;
    103 	nc = num_cols;
    104 
    105 	/*
    106 	 * make sure window fits inside the original one
    107 	 */
    108 #ifdef	DEBUG
    109 	fprintf(outf, "SUBWIN(%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx);
    110 #endif
    111 	if (by < orig->_begy || bx < orig->_begx ||
    112 	    by + nl > orig->_maxy + orig->_begy ||
    113 	    bx + nc > orig->_maxx + orig->_begx)
    114 		return (ERR);
    115 	if (nl == 0)
    116 		nl = orig->_maxy + orig->_begy - by;
    117 	if (nc == 0)
    118 		nc = orig->_maxx + orig->_begx - bx;
    119 	if ((win = makenew(nl, nc, by, bx)) == NULL)
    120 		return (ERR);
    121 	win->_nextp = orig->_nextp;
    122 	orig->_nextp = win;
    123 	win->_orig = orig;
    124 	_set_subwin_(orig, win);
    125 	return (win);
    126 }
    127 
    128 /*
    129  * this code is shared with mvwin()
    130  */
    131 
    132 void
    133 _set_subwin_(WINDOW *orig, WINDOW *win)
    134 {
    135 	int	i, j, k;
    136 
    137 	j = win->_begy - orig->_begy;
    138 	k = win->_begx - orig->_begx;
    139 	win->_ch_off = (short)k;
    140 #ifdef DEBUG
    141 	fprintf(outf, "_SET_SUBWIN_: win->_ch_off = %d\n", win->_ch_off);
    142 #endif
    143 	win->_firstch = &orig->_firstch[j];
    144 	win->_lastch = &orig->_lastch[j];
    145 	for (i = 0; i < win->_maxy; i++, j++)
    146 		win->_y[i] = &orig->_y[j][k];
    147 
    148 }
    149 
    150 /*
    151  *	This routine sets up a window buffer and returns a pointer to it.
    152  */
    153 
    154 static WINDOW *
    155 makenew(int num_lines, int num_cols, int begy, int begx)
    156 {
    157 	WINDOW	*win;
    158 	int	by, bx, nl, nc;
    159 
    160 	by = begy;
    161 	bx = begx;
    162 	nl = num_lines;
    163 	nc = num_cols;
    164 
    165 #ifdef	DEBUG
    166 	fprintf(outf, "MAKENEW(%d, %d, %d, %d)\n", nl, nc, by, bx);
    167 #endif
    168 	if ((win = (WINDOW *) malloc(sizeof (*win))) == NULL)
    169 		return (NULL);
    170 #ifdef DEBUG
    171 	fprintf(outf, "MAKENEW: nl = %d\n", nl);
    172 #endif
    173 	if ((win->_y = (char **)malloc(nl * sizeof (win->_y[0]))) == NULL) {
    174 		free(win);
    175 		return (NULL);
    176 	}
    177 #ifdef DEBUG
    178 	fprintf(outf, "MAKENEW: nc = %d\n", nc);
    179 #endif
    180 	win->_cury = win->_curx = 0;
    181 	win->_clear = FALSE;
    182 	win->_maxy = (short)nl;
    183 	win->_maxx = (short)nc;
    184 	win->_begy = (short)by;
    185 	win->_begx = (short)bx;
    186 	win->_flags = 0;
    187 	win->_scroll = win->_leave = FALSE;
    188 	_swflags_(win);
    189 	win->_orig = NULL;
    190 #ifdef DEBUG
    191 	fprintf(outf, "MAKENEW: win->_clear = %d\n", win->_clear);
    192 	fprintf(outf, "MAKENEW: win->_leave = %d\n", win->_leave);
    193 	fprintf(outf, "MAKENEW: win->_scroll = %d\n", win->_scroll);
    194 	fprintf(outf, "MAKENEW: win->_flags = %0.2o\n", win->_flags);
    195 	fprintf(outf, "MAKENEW: win->_maxy = %d\n", win->_maxy);
    196 	fprintf(outf, "MAKENEW: win->_maxx = %d\n", win->_maxx);
    197 	fprintf(outf, "MAKENEW: win->_begy = %d\n", win->_begy);
    198 	fprintf(outf, "MAKENEW: win->_begx = %d\n", win->_begx);
    199 #endif
    200 	return (win);
    201 }
    202 
    203 void
    204 _swflags_(WINDOW *win)
    205 {
    206 	win->_flags &= ~(_ENDLINE|_FULLLINE|_FULLWIN|_SCROLLWIN);
    207 	if (win->_begx + win->_maxx == COLS) {
    208 		win->_flags |= _ENDLINE;
    209 		if (win->_begx == 0) {
    210 			if (AL && DL)
    211 				win->_flags |= _FULLLINE;
    212 			if (win->_maxy == LINES && win->_begy == 0)
    213 				win->_flags |= _FULLWIN;
    214 		}
    215 		if (win->_begy + win->_maxy == LINES)
    216 			win->_flags |= _SCROLLWIN;
    217 	}
    218 }
    219