add mpool
[unix-history] / usr / src / lib / libcurses / newwin.c
CommitLineData
87c6fcf8 1/*
2f14f200
KB
2 * Copyright (c) 1981 Regents of the University of California.
3 * All rights reserved.
4 *
e21c485a 5 * %sccs.include.redist.c%
87c6fcf8
DF
6 */
7
8#ifndef lint
e21c485a 9static char sccsid[] = "@(#)newwin.c 5.4 (Berkeley) %G%";
2f14f200 10#endif /* not lint */
87c6fcf8 11
f1d89179
KA
12/*
13 * allocate space for and set up defaults for a new window
14 *
f1d89179
KA
15 */
16
17# include "curses.ext"
18
52f06ac3
JB
19char *malloc();
20
21# define SMALLOC (short *) malloc
f1d89179
KA
22
23static WINDOW *makenew();
24
25# undef nl /* don't need it here, and it interferes */
26
27WINDOW *
28newwin(num_lines, num_cols, begy, begx)
29int num_lines, num_cols, begy, begx;
30{
31 reg WINDOW *win;
32 reg char *sp;
33 reg int i, by, bx, nl, nc;
52f06ac3 34 reg int j;
f1d89179
KA
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;
52f06ac3
JB
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 }
f1d89179 63 for (i = 0; i < nl; i++)
52f06ac3 64 if ((win->_y[i] = malloc(nc * sizeof win->_y[0])) == NULL) {
f1d89179 65 for (j = 0; j < i; j++)
52f06ac3
JB
66 free(win->_y[j]);
67 free(win->_firstch);
68 free(win->_lastch);
69 free(win->_y);
70 free(win);
f1d89179
KA
71 return ERR;
72 }
73 else
74 for (sp = win->_y[i]; sp < win->_y[i] + nc; )
75 *sp++ = ' ';
52f06ac3
JB
76 win->_ch_off = 0;
77# ifdef DEBUG
78 fprintf(outf, "NEWWIN: win->_ch_off = %d\n", win->_ch_off);
79# endif
f1d89179
KA
80 return win;
81}
82
83WINDOW *
84subwin(orig, num_lines, num_cols, begy, begx)
85reg WINDOW *orig;
52f06ac3
JB
86int num_lines, num_cols, begy, begx;
87{
f1d89179
KA
88 reg int i;
89 reg WINDOW *win;
90 reg int by, bx, nl, nc;
f1d89179
KA
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
ab41d2db 104 || by + nl > orig->_maxy + orig->_begy
52f06ac3 105 || bx + nc > orig->_maxx + orig->_begx)
f1d89179
KA
106 return ERR;
107 if (nl == 0)
ab41d2db 108 nl = orig->_maxy + orig->_begy - by;
f1d89179 109 if (nc == 0)
ab41d2db 110 nc = orig->_maxx + orig->_begx - bx;
52f06ac3 111 if ((win = makenew(nl, nc, by, bx)) == NULL)
f1d89179 112 return ERR;
60d72089
KA
113 win->_nextp = orig->_nextp;
114 orig->_nextp = win;
115 win->_orig = orig;
52f06ac3 116 _set_subwin_(orig, win);
f1d89179
KA
117 return win;
118}
119
52f06ac3
JB
120/*
121 * this code is shared with mvwin()
122 */
123_set_subwin_(orig, win)
124register 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
f1d89179
KA
141/*
142 * This routine sets up a window buffer and returns a pointer to it.
143 */
144static WINDOW *
145makenew(num_lines, num_cols, begy, begx)
146int 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
52f06ac3 160 if ((win = (WINDOW *) malloc(sizeof *win)) == NULL)
f1d89179
KA
161 return NULL;
162# ifdef DEBUG
163 fprintf(outf, "MAKENEW: nl = %d\n", nl);
164# endif
52f06ac3
JB
165 if ((win->_y = (char **) malloc(nl * sizeof win->_y[0])) == NULL) {
166 free(win);
95cc59cf 167 return NULL;
f1d89179
KA
168 }
169# ifdef DEBUG
170 fprintf(outf, "MAKENEW: nc = %d\n", nc);
171# endif
172 win->_cury = win->_curx = 0;
52f06ac3 173 win->_clear = FALSE;
f1d89179
KA
174 win->_maxy = nl;
175 win->_maxx = nc;
176 win->_begy = by;
177 win->_begx = bx;
ab41d2db 178 win->_flags = 0;
f1d89179 179 win->_scroll = win->_leave = FALSE;
52f06ac3 180 _swflags_(win);
f1d89179
KA
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}
52f06ac3
JB
193
194_swflags_(win)
195register 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}