newwin.c revision 1.10 1 /* $NetBSD: newwin.c,v 1.10 1999/04/13 14:08:18 mrg 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.10 1999/04/13 14:08:18 mrg Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <stdlib.h>
46
47 #include "curses.h"
48
49 #undef nl /* Don't need it here, and it interferes. */
50
51 static WINDOW *__makenew __P((int, int, int, int, int));
52
53 void __set_subwin __P((WINDOW *, WINDOW *));
54
55 /*
56 * newwin --
57 * Allocate space for and set up defaults for a new window.
58 */
59 WINDOW *
60 newwin(nl, nc, by, bx)
61 int nl, nc, by, bx;
62 {
63 WINDOW *win;
64 __LINE *lp;
65 int i, j;
66 __LDATA *sp;
67
68 if (nl == 0)
69 nl = LINES - by;
70 if (nc == 0)
71 nc = COLS - bx;
72
73 if ((win = __makenew(nl, nc, by, bx, 0)) == NULL)
74 return (NULL);
75
76 win->nextp = win;
77 win->ch_off = 0;
78 win->orig = NULL;
79 win->delay = -1;
80
81 #ifdef DEBUG
82 __CTRACE("newwin: win->ch_off = %d\n", win->ch_off);
83 #endif
84
85 for (i = 0; i < nl; i++) {
86 lp = win->lines[i];
87 lp->flags = 0;
88 for (sp = lp->line, j = 0; j < nc; j++, sp++) {
89 sp->ch = ' ';
90 sp->attr = 0;
91 }
92 lp->hash = __hash((char *) lp->line, (int) (nc * __LDATASIZE));
93 }
94 return (win);
95 }
96
97 WINDOW *
98 subwin(orig, nl, nc, by, bx)
99 WINDOW *orig;
100 int by, bx, nl, nc;
101 {
102 int i;
103 __LINE *lp;
104 WINDOW *win;
105
106 /* Make sure window fits inside the original one. */
107 #ifdef DEBUG
108 __CTRACE("subwin: (%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx);
109 #endif
110 if (by < orig->begy || bx < orig->begx
111 || by + nl > orig->maxy + orig->begy
112 || bx + nc > orig->maxx + orig->begx)
113 return (NULL);
114 if (nl == 0)
115 nl = orig->maxy + orig->begy - by;
116 if (nc == 0)
117 nc = orig->maxx + orig->begx - bx;
118 if ((win = __makenew(nl, nc, by, bx, 1)) == NULL)
119 return (NULL);
120 win->nextp = orig->nextp;
121 orig->nextp = win;
122 win->orig = orig;
123
124 /* Initialize flags here so that refresh can also use __set_subwin. */
125 for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++)
126 lp->flags = 0;
127 __set_subwin(orig, win);
128 return (win);
129 }
130 /*
131 * This code is shared with mvwin().
132 */
133 void
134 __set_subwin(orig, win)
135 WINDOW *orig, *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];
145 lp->line = &olp->line[win->begx];
146 lp->firstchp = &olp->firstch;
147 lp->lastchp = &olp->lastch;
148 lp->hash = __hash((char *) lp->line, (int) (win->maxx * __LDATASIZE));
149 }
150
151 #ifdef DEBUG
152 __CTRACE("__set_subwin: win->ch_off = %d\n", win->ch_off);
153 #endif
154 }
155 /*
156 * __makenew --
157 * Set up a window buffer and returns a pointer to it.
158 */
159 static WINDOW *
160 __makenew(nl, nc, by, bx, sub)
161 int by, bx, nl, nc;
162 int sub;
163 {
164 WINDOW *win;
165 __LINE *lp;
166 int i;
167
168
169 #ifdef DEBUG
170 __CTRACE("makenew: (%d, %d, %d, %d)\n", nl, nc, by, bx);
171 #endif
172 if ((win = malloc(sizeof(*win))) == NULL)
173 return (NULL);
174 #ifdef DEBUG
175 __CTRACE("makenew: nl = %d\n", nl);
176 #endif
177
178 /* Set up line pointer array and line space. */
179 if ((win->lines = malloc(nl * sizeof(__LINE *))) == NULL) {
180 free(win);
181 return NULL;
182 }
183 if ((win->lspace = malloc(nl * sizeof(__LINE))) == NULL) {
184 free(win);
185 free(win->lines);
186 return NULL;
187 }
188 /* Don't allocate window and line space if it's a subwindow */
189 if (!sub) {
190 /*
191 * Allocate window space in one chunk.
192 */
193 if ((win->wspace =
194 malloc(nc * nl * sizeof(__LDATA))) == NULL) {
195 free(win->lines);
196 free(win->lspace);
197 free(win);
198 return NULL;
199 }
200 /*
201 * Point line pointers to line space, and lines themselves into
202 * window space.
203 */
204 for (lp = win->lspace, i = 0; i < nl; i++, lp++) {
205 win->lines[i] = lp;
206 lp->line = &win->wspace[i * nc];
207 lp->firstchp = &lp->firstch;
208 lp->lastchp = &lp->lastch;
209 lp->firstch = 0;
210 lp->lastch = 0;
211 }
212 }
213 #ifdef DEBUG
214 __CTRACE("makenew: nc = %d\n", nc);
215 #endif
216 win->cury = win->curx = 0;
217 win->maxy = nl;
218 win->maxx = nc;
219
220 win->begy = by;
221 win->begx = bx;
222 win->flags = 0;
223 __swflags(win);
224 #ifdef DEBUG
225 __CTRACE("makenew: win->flags = %0.2o\n", win->flags);
226 __CTRACE("makenew: win->maxy = %d\n", win->maxy);
227 __CTRACE("makenew: win->maxx = %d\n", win->maxx);
228 __CTRACE("makenew: win->begy = %d\n", win->begy);
229 __CTRACE("makenew: win->begx = %d\n", win->begx);
230 #endif
231 return (win);
232 }
233
234 void
235 __swflags(win)
236 WINDOW *win;
237 {
238 win->flags &= ~(__ENDLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK);
239 if (win->begx + win->maxx == COLS) {
240 win->flags |= __ENDLINE;
241 if (win->begx == 0 && win->maxy == LINES && win->begy == 0)
242 win->flags |= __FULLWIN;
243 if (win->begy + win->maxy == LINES)
244 win->flags |= __SCROLLWIN;
245 }
246 }
247