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