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