newwin.c revision 1.3 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, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 /*static char sccsid[] = "from: @(#)newwin.c 5.4 (Berkeley) 6/1/90";*/
36 static char rcsid[] = "$Id: newwin.c,v 1.3 1993/08/01 18:35:30 mycroft Exp $";
37 #endif /* not lint */
38
39 /*
40 * allocate space for and set up defaults for a new window
41 *
42 */
43
44 # include "curses.ext"
45
46 char *malloc();
47
48 # define SMALLOC (short *) malloc
49
50 static WINDOW *makenew();
51
52 # undef nl /* don't need it here, and it interferes */
53
54 WINDOW *
55 newwin(num_lines, num_cols, begy, begx)
56 int num_lines, num_cols, begy, begx;
57 {
58 reg WINDOW *win;
59 reg chtype *sp;
60 reg int i, by, bx, nl, nc;
61 reg int j;
62
63 by = begy;
64 bx = begx;
65 nl = num_lines;
66 nc = num_cols;
67
68 if (nl == 0)
69 nl = LINES - by;
70 if (nc == 0)
71 nc = COLS - bx;
72 if ((win = makenew(nl, nc, by, bx)) == NULL)
73 return ERR;
74 if ((win->_firstch = SMALLOC(nl * sizeof win->_firstch[0])) == NULL) {
75 free(win->_y);
76 free(win);
77 return NULL;
78 }
79 if ((win->_lastch = SMALLOC(nl * sizeof win->_lastch[0])) == NULL) {
80 free(win->_y);
81 free(win->_firstch);
82 free(win);
83 return NULL;
84 }
85 win->_nextp = win;
86 for (i = 0; i < nl; i++) {
87 win->_firstch[i] = _NOCHANGE;
88 win->_lastch[i] = _NOCHANGE;
89 }
90 for (i = 0; i < nl; i++)
91 if ((win->_y[i] = (chtype *) malloc(nc * sizeof(chtype))) == NULL) {
92 for (j = 0; j < i; j++)
93 free(win->_y[j]);
94 free(win->_firstch);
95 free(win->_lastch);
96 free(win->_y);
97 free(win);
98 return ERR;
99 }
100 else
101 for (sp = win->_y[i]; sp < win->_y[i] + nc; )
102 *sp++ = ' ';
103 win->_ch_off = 0;
104 # ifdef DEBUG
105 fprintf(outf, "NEWWIN: win->_ch_off = %d\n", win->_ch_off);
106 # endif
107 return win;
108 }
109
110 WINDOW *
111 subwin(orig, num_lines, num_cols, begy, begx)
112 reg WINDOW *orig;
113 int num_lines, num_cols, begy, begx;
114 {
115 reg int i;
116 reg WINDOW *win;
117 reg int by, bx, nl, nc;
118
119 by = begy;
120 bx = begx;
121 nl = num_lines;
122 nc = num_cols;
123
124 /*
125 * make sure window fits inside the original one
126 */
127 # ifdef DEBUG
128 fprintf(outf, "SUBWIN(%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx);
129 # endif
130 if (by < orig->_begy || bx < orig->_begx
131 || by + nl > orig->_maxy + orig->_begy
132 || bx + nc > orig->_maxx + orig->_begx)
133 return ERR;
134 if (nl == 0)
135 nl = orig->_maxy + orig->_begy - by;
136 if (nc == 0)
137 nc = orig->_maxx + orig->_begx - bx;
138 if ((win = makenew(nl, nc, by, bx)) == NULL)
139 return ERR;
140 win->_nextp = orig->_nextp;
141 orig->_nextp = win;
142 win->_orig = orig;
143 _set_subwin_(orig, win);
144 return win;
145 }
146
147 /*
148 * this code is shared with mvwin()
149 */
150 _set_subwin_(orig, win)
151 register WINDOW *orig, *win;
152 {
153 register int i, j, k;
154
155 j = win->_begy - orig->_begy;
156 k = win->_begx - orig->_begx;
157 win->_ch_off = k;
158 # ifdef DEBUG
159 fprintf(outf, "_SET_SUBWIN_: win->_ch_off = %d\n", win->_ch_off);
160 # endif
161 win->_firstch = &orig->_firstch[j];
162 win->_lastch = &orig->_lastch[j];
163 for (i = 0; i < win->_maxy; i++, j++)
164 win->_y[i] = &orig->_y[j][k];
165
166 }
167
168 /*
169 * This routine sets up a window buffer and returns a pointer to it.
170 */
171 static WINDOW *
172 makenew(num_lines, num_cols, begy, begx)
173 int num_lines, num_cols, begy, begx; {
174
175 reg int i;
176 reg WINDOW *win;
177 reg int by, bx, nl, nc;
178
179 by = begy;
180 bx = begx;
181 nl = num_lines;
182 nc = num_cols;
183
184 # ifdef DEBUG
185 fprintf(outf, "MAKENEW(%d, %d, %d, %d)\n", nl, nc, by, bx);
186 # endif
187 if ((win = (WINDOW *) malloc(sizeof *win)) == NULL)
188 return NULL;
189 # ifdef DEBUG
190 fprintf(outf, "MAKENEW: nl = %d\n", nl);
191 # endif
192 if ((win->_y = (chtype **) malloc(nl * sizeof(chtype *))) == NULL) {
193 free(win);
194 return NULL;
195 }
196 # ifdef DEBUG
197 fprintf(outf, "MAKENEW: nc = %d\n", nc);
198 # endif
199 win->_cury = win->_curx = 0;
200 win->_clear = FALSE;
201 win->_maxy = nl;
202 win->_maxx = nc;
203 win->_begy = by;
204 win->_begx = bx;
205 win->_flags = 0;
206 win->_scroll = win->_leave = FALSE;
207 _swflags_(win);
208 # ifdef DEBUG
209 fprintf(outf, "MAKENEW: win->_clear = %d\n", win->_clear);
210 fprintf(outf, "MAKENEW: win->_leave = %d\n", win->_leave);
211 fprintf(outf, "MAKENEW: win->_scroll = %d\n", win->_scroll);
212 fprintf(outf, "MAKENEW: win->_flags = %0.2o\n", win->_flags);
213 fprintf(outf, "MAKENEW: win->_maxy = %d\n", win->_maxy);
214 fprintf(outf, "MAKENEW: win->_maxx = %d\n", win->_maxx);
215 fprintf(outf, "MAKENEW: win->_begy = %d\n", win->_begy);
216 fprintf(outf, "MAKENEW: win->_begx = %d\n", win->_begx);
217 # endif
218 return win;
219 }
220
221 _swflags_(win)
222 register WINDOW *win;
223 {
224 win->_flags &= ~(_ENDLINE|_FULLLINE|_FULLWIN|_SCROLLWIN);
225 if (win->_begx + win->_maxx == COLS) {
226 win->_flags |= _ENDLINE;
227 if (win->_begx == 0) {
228 if (AL && DL)
229 win->_flags |= _FULLLINE;
230 if (win->_maxy == LINES && win->_begy == 0)
231 win->_flags |= _FULLWIN;
232 }
233 if (win->_begy + win->_maxy == LINES)
234 win->_flags |= _SCROLLWIN;
235 }
236 }
237