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