newwin.c revision 1.15 1 /* $NetBSD: newwin.c,v 1.15 2000/04/14 17:35:15 jdc Exp $ */
2
3 /*
4 * Copyright (c) 1981, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)newwin.c 8.3 (Berkeley) 7/27/94";
40 #else
41 __RCSID("$NetBSD: newwin.c,v 1.15 2000/04/14 17:35:15 jdc Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <stdlib.h>
46
47 #include "curses.h"
48 #include "curses_private.h"
49
50 #undef nl /* Don't need it here, and it interferes. */
51
52 extern struct __winlist *winlistp;
53
54 static WINDOW *__makenew __P((int, int, int, int, int));
55
56 void __set_subwin __P((WINDOW *, WINDOW *));
57
58 /*
59 * newwin --
60 * Allocate space for and set up defaults for a new window.
61 */
62 WINDOW *
63 newwin(nl, nc, by, bx)
64 int nl, nc, by, bx;
65 {
66 WINDOW *win;
67 __LINE *lp;
68 int i, j;
69 __LDATA *sp;
70
71 if (nl == 0)
72 nl = LINES - by;
73 if (nc == 0)
74 nc = COLS - bx;
75
76 if ((win = __makenew(nl, nc, by, bx, 0)) == NULL)
77 return (NULL);
78
79 win->nextp = win;
80 win->ch_off = 0;
81 win->orig = NULL;
82 win->delay = -1;
83
84 #ifdef DEBUG
85 __CTRACE("newwin: win->ch_off = %d\n", win->ch_off);
86 #endif
87
88 for (i = 0; i < nl; i++) {
89 lp = win->lines[i];
90 lp->flags = 0;
91 for (sp = lp->line, j = 0; j < nc; j++, sp++) {
92 sp->ch = ' ';
93 sp->attr = 0;
94 }
95 lp->hash = __hash((char *)(void *)lp->line,
96 (int) (nc * __LDATASIZE));
97 }
98 return (win);
99 }
100
101 WINDOW *
102 subwin(orig, nl, nc, by, bx)
103 WINDOW *orig;
104 int by, bx, nl, nc;
105 {
106 int i;
107 __LINE *lp;
108 WINDOW *win;
109
110 /* Make sure window fits inside the original one. */
111 #ifdef DEBUG
112 __CTRACE("subwin: (%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx);
113 #endif
114 if (by < orig->begy || bx < orig->begx
115 || by + nl > orig->maxy + orig->begy
116 || bx + nc > orig->maxx + orig->begx)
117 return (NULL);
118 if (nl == 0)
119 nl = orig->maxy + orig->begy - by;
120 if (nc == 0)
121 nc = orig->maxx + orig->begx - bx;
122 if ((win = __makenew(nl, nc, by, bx, 1)) == NULL)
123 return (NULL);
124 win->nextp = orig->nextp;
125 orig->nextp = win;
126 win->orig = orig;
127
128 /* Initialize flags here so that refresh can also use __set_subwin. */
129 for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++)
130 lp->flags = 0;
131 __set_subwin(orig, win);
132 return (win);
133 }
134 /*
135 * This code is shared with mvwin().
136 */
137 void
138 __set_subwin(orig, win)
139 WINDOW *orig, *win;
140 {
141 int i;
142 __LINE *lp, *olp;
143
144 win->ch_off = win->begx - orig->begx;
145 /* Point line pointers to line space. */
146 for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) {
147 win->lines[i] = lp;
148 olp = orig->lines[i + win->begy - orig->begy];
149 lp->line = &olp->line[win->begx - orig->begx];
150 lp->firstchp = &olp->firstch;
151 lp->lastchp = &olp->lastch;
152 lp->hash = __hash((char *)(void *)lp->line,
153 (int) (win->maxx * __LDATASIZE));
154 }
155
156 #ifdef DEBUG
157 __CTRACE("__set_subwin: win->ch_off = %d\n", win->ch_off);
158 #endif
159 }
160 /*
161 * __makenew --
162 * Set up a window buffer and returns a pointer to it.
163 */
164 static WINDOW *
165 __makenew(nl, nc, by, bx, sub)
166 int by, bx, nl, nc;
167 int sub;
168 {
169 WINDOW *win;
170 __LINE *lp;
171 struct __winlist *wlp, *wlp2;
172 int i;
173
174
175 #ifdef DEBUG
176 __CTRACE("makenew: (%d, %d, %d, %d)\n", nl, nc, by, bx);
177 #endif
178 if ((win = malloc(sizeof(*win))) == NULL)
179 return (NULL);
180 #ifdef DEBUG
181 __CTRACE("makenew: nl = %d\n", nl);
182 #endif
183
184 /* Set up line pointer array and line space. */
185 if ((win->lines = malloc(nl * sizeof(__LINE *))) == NULL) {
186 free(win);
187 return NULL;
188 }
189 if ((win->lspace = malloc(nl * sizeof(__LINE))) == NULL) {
190 free(win);
191 free(win->lines);
192 return NULL;
193 }
194 /* Don't allocate window and line space if it's a subwindow */
195 if (!sub) {
196 /*
197 * Allocate window space in one chunk.
198 */
199 if ((win->wspace =
200 malloc(nc * nl * sizeof(__LDATA))) == NULL) {
201 free(win->lines);
202 free(win->lspace);
203 free(win);
204 return NULL;
205 }
206 /*
207 * Append window to window list.
208 */
209 if ((wlp = malloc(sizeof(struct __winlist))) == NULL) {
210 free(win->wspace);
211 free(win->lines);
212 free(win->lspace);
213 free(win);
214 return NULL;
215 }
216 wlp->winp = win;
217 wlp->nextp = NULL;
218 if (__winlistp == NULL)
219 __winlistp = wlp;
220 else {
221 wlp2 = __winlistp;
222 while (wlp2->nextp != NULL)
223 wlp2 = wlp2->nextp;
224 wlp2->nextp = wlp;
225 }
226 /*
227 * Point line pointers to line space, and lines themselves into
228 * window space.
229 */
230 for (lp = win->lspace, i = 0; i < nl; i++, lp++) {
231 win->lines[i] = lp;
232 lp->line = &win->wspace[i * nc];
233 lp->firstchp = &lp->firstch;
234 lp->lastchp = &lp->lastch;
235 lp->firstch = 0;
236 lp->lastch = 0;
237 }
238 }
239 #ifdef DEBUG
240 __CTRACE("makenew: nc = %d\n", nc);
241 #endif
242 win->cury = win->curx = 0;
243 win->maxy = nl;
244 win->maxx = nc;
245
246 win->begy = by;
247 win->begx = bx;
248 win->flags = 0;
249 win->wattr = 0;
250 win->bchar = ' ';
251 win->battr = 0;
252 __swflags(win);
253 #ifdef DEBUG
254 __CTRACE("makenew: win->wattr = %0.2o\n", win->wattr);
255 __CTRACE("makenew: win->flags = %0.2o\n", win->flags);
256 __CTRACE("makenew: win->maxy = %d\n", win->maxy);
257 __CTRACE("makenew: win->maxx = %d\n", win->maxx);
258 __CTRACE("makenew: win->begy = %d\n", win->begy);
259 __CTRACE("makenew: win->begx = %d\n", win->begx);
260 #endif
261 return (win);
262 }
263
264 void
265 __swflags(win)
266 WINDOW *win;
267 {
268 win->flags &= ~(__ENDLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK);
269 if (win->begx + win->maxx == COLS) {
270 win->flags |= __ENDLINE;
271 if (win->begx == 0 && win->maxy == LINES && win->begy == 0)
272 win->flags |= __FULLWIN;
273 if (win->begy + win->maxy == LINES)
274 win->flags |= __SCROLLWIN;
275 }
276 }
277