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