newwin.c revision 1.16 1 /* $NetBSD: newwin.c,v 1.16 2000/04/15 13:17:04 blymn 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.16 2000/04/15 13:17:04 blymn 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(int nl, int nc, int by, int bx)
64 {
65 WINDOW *win;
66 __LINE *lp;
67 int i, j;
68 __LDATA *sp;
69
70 if (nl == 0)
71 nl = LINES - by;
72 if (nc == 0)
73 nc = COLS - bx;
74
75 if ((win = __makenew(nl, nc, by, bx, 0)) == NULL)
76 return (NULL);
77
78 win->nextp = win;
79 win->ch_off = 0;
80 win->orig = NULL;
81 win->delay = -1;
82
83 #ifdef DEBUG
84 __CTRACE("newwin: win->ch_off = %d\n", win->ch_off);
85 #endif
86
87 for (i = 0; i < nl; i++) {
88 lp = win->lines[i];
89 lp->flags = 0;
90 for (sp = lp->line, j = 0; j < nc; j++, sp++) {
91 sp->ch = ' ';
92 sp->attr = 0;
93 }
94 lp->hash = __hash((char *)(void *)lp->line,
95 (int) (nc * __LDATASIZE));
96 }
97 return (win);
98 }
99
100 WINDOW *
101 subwin(WINDOW *orig, int nl, int nc, int by, int bx)
102 {
103 int i;
104 __LINE *lp;
105 WINDOW *win;
106
107 /* Make sure window fits inside the original one. */
108 #ifdef DEBUG
109 __CTRACE("subwin: (%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx);
110 #endif
111 if (by < orig->begy || bx < orig->begx
112 || by + nl > orig->maxy + orig->begy
113 || bx + nc > orig->maxx + orig->begx)
114 return (NULL);
115 if (nl == 0)
116 nl = orig->maxy + orig->begy - by;
117 if (nc == 0)
118 nc = orig->maxx + orig->begx - bx;
119 if ((win = __makenew(nl, nc, by, bx, 1)) == NULL)
120 return (NULL);
121 win->nextp = orig->nextp;
122 orig->nextp = win;
123 win->orig = orig;
124
125 /* Initialize flags here so that refresh can also use __set_subwin. */
126 for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++)
127 lp->flags = 0;
128 __set_subwin(orig, win);
129 return (win);
130 }
131 /*
132 * This code is shared with mvwin().
133 */
134 void
135 __set_subwin(WINDOW *orig, WINDOW *win)
136 {
137 int i;
138 __LINE *lp, *olp;
139
140 win->ch_off = win->begx - orig->begx;
141 /* Point line pointers to line space. */
142 for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) {
143 win->lines[i] = lp;
144 olp = orig->lines[i + win->begy - orig->begy];
145 lp->line = &olp->line[win->begx - orig->begx];
146 lp->firstchp = &olp->firstch;
147 lp->lastchp = &olp->lastch;
148 lp->hash = __hash((char *)(void *)lp->line,
149 (int) (win->maxx * __LDATASIZE));
150 }
151
152 #ifdef DEBUG
153 __CTRACE("__set_subwin: win->ch_off = %d\n", win->ch_off);
154 #endif
155 }
156 /*
157 * __makenew --
158 * Set up a window buffer and returns a pointer to it.
159 */
160 static WINDOW *
161 __makenew(nl, nc, by, bx, sub)
162 int by, bx, nl, nc;
163 int sub;
164 {
165 WINDOW *win;
166 __LINE *lp;
167 struct __winlist *wlp, *wlp2;
168 int i;
169
170
171 #ifdef DEBUG
172 __CTRACE("makenew: (%d, %d, %d, %d)\n", nl, nc, by, bx);
173 #endif
174 if ((win = malloc(sizeof(*win))) == NULL)
175 return (NULL);
176 #ifdef DEBUG
177 __CTRACE("makenew: nl = %d\n", nl);
178 #endif
179
180 /* Set up line pointer array and line space. */
181 if ((win->lines = malloc(nl * sizeof(__LINE *))) == NULL) {
182 free(win);
183 return NULL;
184 }
185 if ((win->lspace = malloc(nl * sizeof(__LINE))) == NULL) {
186 free(win);
187 free(win->lines);
188 return NULL;
189 }
190 /* Don't allocate window and line space if it's a subwindow */
191 if (!sub) {
192 /*
193 * Allocate window space in one chunk.
194 */
195 if ((win->wspace =
196 malloc(nc * nl * sizeof(__LDATA))) == NULL) {
197 free(win->lines);
198 free(win->lspace);
199 free(win);
200 return NULL;
201 }
202 /*
203 * Append window to window list.
204 */
205 if ((wlp = malloc(sizeof(struct __winlist))) == NULL) {
206 free(win->wspace);
207 free(win->lines);
208 free(win->lspace);
209 free(win);
210 return NULL;
211 }
212 wlp->winp = win;
213 wlp->nextp = NULL;
214 if (__winlistp == NULL)
215 __winlistp = wlp;
216 else {
217 wlp2 = __winlistp;
218 while (wlp2->nextp != NULL)
219 wlp2 = wlp2->nextp;
220 wlp2->nextp = wlp;
221 }
222 /*
223 * Point line pointers to line space, and lines themselves into
224 * window space.
225 */
226 for (lp = win->lspace, i = 0; i < nl; i++, lp++) {
227 win->lines[i] = lp;
228 lp->line = &win->wspace[i * nc];
229 lp->firstchp = &lp->firstch;
230 lp->lastchp = &lp->lastch;
231 lp->firstch = 0;
232 lp->lastch = 0;
233 }
234 }
235 #ifdef DEBUG
236 __CTRACE("makenew: nc = %d\n", nc);
237 #endif
238 win->cury = win->curx = 0;
239 win->maxy = nl;
240 win->maxx = nc;
241
242 win->begy = by;
243 win->begx = bx;
244 win->flags = 0;
245 win->wattr = 0;
246 win->bchar = ' ';
247 win->battr = 0;
248 __swflags(win);
249 #ifdef DEBUG
250 __CTRACE("makenew: win->wattr = %0.2o\n", win->wattr);
251 __CTRACE("makenew: win->flags = %0.2o\n", win->flags);
252 __CTRACE("makenew: win->maxy = %d\n", win->maxy);
253 __CTRACE("makenew: win->maxx = %d\n", win->maxx);
254 __CTRACE("makenew: win->begy = %d\n", win->begy);
255 __CTRACE("makenew: win->begx = %d\n", win->begx);
256 #endif
257 return (win);
258 }
259
260 void
261 __swflags(WINDOW *win)
262 {
263 win->flags &= ~(__ENDLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK);
264 if (win->begx + win->maxx == COLS) {
265 win->flags |= __ENDLINE;
266 if (win->begx == 0 && win->maxy == LINES && win->begy == 0)
267 win->flags |= __FULLWIN;
268 if (win->begy + win->maxy == LINES)
269 win->flags |= __SCROLLWIN;
270 }
271 }
272