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