xref: /original-bsd/lib/libcurses/newwin.c (revision f1656be1)
1 /*
2  * Copyright (c) 1981 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)newwin.c	5.2 (Berkeley) 06/08/88";
15 #endif /* not lint */
16 
17 /*
18  * allocate space for and set up defaults for a new window
19  *
20  */
21 
22 # include	"curses.ext"
23 
24 char	*malloc();
25 
26 # define	SMALLOC	(short *) malloc
27 
28 static WINDOW	*makenew();
29 
30 # undef		nl	/* don't need it here, and it interferes	*/
31 
32 WINDOW *
33 newwin(num_lines, num_cols, begy, begx)
34 int	num_lines, num_cols, begy, begx;
35 {
36 	reg WINDOW	*win;
37 	reg char	*sp;
38 	reg int		i, by, bx, nl, nc;
39 	reg int		j;
40 
41 	by = begy;
42 	bx = begx;
43 	nl = num_lines;
44 	nc = num_cols;
45 
46 	if (nl == 0)
47 		nl = LINES - by;
48 	if (nc == 0)
49 		nc = COLS - bx;
50 	if ((win = makenew(nl, nc, by, bx)) == NULL)
51 		return ERR;
52 	if ((win->_firstch = SMALLOC(nl * sizeof win->_firstch[0])) == NULL) {
53 		free(win->_y);
54 		free(win);
55 		return NULL;
56 	}
57 	if ((win->_lastch = SMALLOC(nl * sizeof win->_lastch[0])) == NULL) {
58 		free(win->_y);
59 		free(win->_firstch);
60 		free(win);
61 		return NULL;
62 	}
63 	win->_nextp = win;
64 	for (i = 0; i < nl; i++) {
65 		win->_firstch[i] = _NOCHANGE;
66 		win->_lastch[i] = _NOCHANGE;
67 	}
68 	for (i = 0; i < nl; i++)
69 		if ((win->_y[i] = malloc(nc * sizeof win->_y[0])) == NULL) {
70 			for (j = 0; j < i; j++)
71 				free(win->_y[j]);
72 			free(win->_firstch);
73 			free(win->_lastch);
74 			free(win->_y);
75 			free(win);
76 			return ERR;
77 		}
78 		else
79 			for (sp = win->_y[i]; sp < win->_y[i] + nc; )
80 				*sp++ = ' ';
81 	win->_ch_off = 0;
82 # ifdef DEBUG
83 	fprintf(outf, "NEWWIN: win->_ch_off = %d\n", win->_ch_off);
84 # endif
85 	return win;
86 }
87 
88 WINDOW *
89 subwin(orig, num_lines, num_cols, begy, begx)
90 reg WINDOW	*orig;
91 int		num_lines, num_cols, begy, begx;
92 {
93 	reg int		i;
94 	reg WINDOW	*win;
95 	reg int		by, bx, nl, nc;
96 
97 	by = begy;
98 	bx = begx;
99 	nl = num_lines;
100 	nc = num_cols;
101 
102 	/*
103 	 * make sure window fits inside the original one
104 	 */
105 # ifdef	DEBUG
106 	fprintf(outf, "SUBWIN(%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx);
107 # endif
108 	if (by < orig->_begy || bx < orig->_begx
109 	    || by + nl > orig->_maxy + orig->_begy
110 	    || bx + nc > orig->_maxx + orig->_begx)
111 		return ERR;
112 	if (nl == 0)
113 		nl = orig->_maxy + orig->_begy - by;
114 	if (nc == 0)
115 		nc = orig->_maxx + orig->_begx - bx;
116 	if ((win = makenew(nl, nc, by, bx)) == NULL)
117 		return ERR;
118 	win->_nextp = orig->_nextp;
119 	orig->_nextp = win;
120 	win->_orig = orig;
121 	_set_subwin_(orig, win);
122 	return win;
123 }
124 
125 /*
126  * this code is shared with mvwin()
127  */
128 _set_subwin_(orig, win)
129 register WINDOW	*orig, *win;
130 {
131 	register int	i, j, k;
132 
133 	j = win->_begy - orig->_begy;
134 	k = win->_begx - orig->_begx;
135 	win->_ch_off = k;
136 # ifdef DEBUG
137 	fprintf(outf, "_SET_SUBWIN_: win->_ch_off = %d\n", win->_ch_off);
138 # endif
139 	win->_firstch = &orig->_firstch[j];
140 	win->_lastch = &orig->_lastch[j];
141 	for (i = 0; i < win->_maxy; i++, j++)
142 		win->_y[i] = &orig->_y[j][k];
143 
144 }
145 
146 /*
147  *	This routine sets up a window buffer and returns a pointer to it.
148  */
149 static WINDOW *
150 makenew(num_lines, num_cols, begy, begx)
151 int	num_lines, num_cols, begy, begx; {
152 
153 	reg int		i;
154 	reg WINDOW	*win;
155 	reg int		by, bx, nl, nc;
156 
157 	by = begy;
158 	bx = begx;
159 	nl = num_lines;
160 	nc = num_cols;
161 
162 # ifdef	DEBUG
163 	fprintf(outf, "MAKENEW(%d, %d, %d, %d)\n", nl, nc, by, bx);
164 # endif
165 	if ((win = (WINDOW *) malloc(sizeof *win)) == NULL)
166 		return NULL;
167 # ifdef DEBUG
168 	fprintf(outf, "MAKENEW: nl = %d\n", nl);
169 # endif
170 	if ((win->_y = (char **) malloc(nl * sizeof win->_y[0])) == NULL) {
171 		free(win);
172 		return NULL;
173 	}
174 # ifdef DEBUG
175 	fprintf(outf, "MAKENEW: nc = %d\n", nc);
176 # endif
177 	win->_cury = win->_curx = 0;
178 	win->_clear = FALSE;
179 	win->_maxy = nl;
180 	win->_maxx = nc;
181 	win->_begy = by;
182 	win->_begx = bx;
183 	win->_flags = 0;
184 	win->_scroll = win->_leave = FALSE;
185 	_swflags_(win);
186 # ifdef DEBUG
187 	fprintf(outf, "MAKENEW: win->_clear = %d\n", win->_clear);
188 	fprintf(outf, "MAKENEW: win->_leave = %d\n", win->_leave);
189 	fprintf(outf, "MAKENEW: win->_scroll = %d\n", win->_scroll);
190 	fprintf(outf, "MAKENEW: win->_flags = %0.2o\n", win->_flags);
191 	fprintf(outf, "MAKENEW: win->_maxy = %d\n", win->_maxy);
192 	fprintf(outf, "MAKENEW: win->_maxx = %d\n", win->_maxx);
193 	fprintf(outf, "MAKENEW: win->_begy = %d\n", win->_begy);
194 	fprintf(outf, "MAKENEW: win->_begx = %d\n", win->_begx);
195 # endif
196 	return win;
197 }
198 
199 _swflags_(win)
200 register WINDOW	*win;
201 {
202 	win->_flags &= ~(_ENDLINE|_FULLLINE|_FULLWIN|_SCROLLWIN);
203 	if (win->_begx + win->_maxx == COLS) {
204 		win->_flags |= _ENDLINE;
205 		if (win->_begx == 0) {
206 			if (AL && DL)
207 				win->_flags |= _FULLLINE;
208 			if (win->_maxy == LINES && win->_begy == 0)
209 				win->_flags |= _FULLWIN;
210 		}
211 		if (win->_begy + win->_maxy == LINES)
212 			win->_flags |= _SCROLLWIN;
213 	}
214 }
215