xref: /netbsd-src/lib/libcurses/newwin.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: newwin.c,v 1.9 1998/02/03 19:12:30 perry 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.9 1998/02/03 19:12:30 perry 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 
80 #ifdef DEBUG
81 	__CTRACE("newwin: win->ch_off = %d\n", win->ch_off);
82 #endif
83 
84 	for (i = 0; i < nl; i++) {
85 		lp = win->lines[i];
86 		lp->flags = 0;
87 		for (sp = lp->line, j = 0; j < nc; j++, sp++) {
88 			sp->ch = ' ';
89 			sp->attr = 0;
90 		}
91 		lp->hash = __hash((char *) lp->line, nc * __LDATASIZE);
92 	}
93 	return (win);
94 }
95 
96 WINDOW *
97 subwin(orig, nl, nc, by, bx)
98 	WINDOW *orig;
99 	int by, bx, nl, nc;
100 {
101 	int i;
102 	__LINE *lp;
103 	WINDOW *win;
104 
105 	/* Make sure window fits inside the original one. */
106 #ifdef	DEBUG
107 	__CTRACE("subwin: (%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx);
108 #endif
109 	if (by < orig->begy || bx < orig->begx
110 	    || by + nl > orig->maxy + orig->begy
111 	    || bx + nc > orig->maxx + orig->begx)
112 		return (NULL);
113 	if (nl == 0)
114 		nl = orig->maxy + orig->begy - by;
115 	if (nc == 0)
116 		nc = orig->maxx + orig->begx - bx;
117 	if ((win = __makenew(nl, nc, by, bx, 1)) == NULL)
118 		return (NULL);
119 	win->nextp = orig->nextp;
120 	orig->nextp = win;
121 	win->orig = orig;
122 
123 	/* Initialize flags here so that refresh can also use __set_subwin. */
124 	for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++)
125 		lp->flags = 0;
126 	__set_subwin(orig, win);
127 	return (win);
128 }
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, 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 /*
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 	int i;
168 
169 
170 #ifdef	DEBUG
171 	__CTRACE("makenew: (%d, %d, %d, %d)\n", nl, nc, by, bx);
172 #endif
173 	if ((win = malloc(sizeof(*win))) == NULL)
174 		return (NULL);
175 #ifdef DEBUG
176 	__CTRACE("makenew: nl = %d\n", nl);
177 #endif
178 
179 	/*
180 	 * Set up line pointer array and line space.
181 	 */
182 	if ((win->lines = malloc (nl * sizeof(__LINE *))) == NULL) {
183 		free(win);
184 		return NULL;
185 	}
186 	if ((win->lspace = malloc (nl * sizeof(__LINE))) == NULL) {
187 		free (win);
188 		free (win->lines);
189 		return NULL;
190 	}
191 
192 	/* Don't allocate window and line space if it's a subwindow */
193 	if (!sub) {
194 		/*
195 		 * Allocate window space in one chunk.
196 		 */
197 		if ((win->wspace =
198 		    malloc(nc * nl * sizeof(__LDATA))) == NULL) {
199 			free(win->lines);
200 			free(win->lspace);
201 			free(win);
202 			return NULL;
203 		}
204 
205 		/*
206 		 * Point line pointers to line space, and lines themselves into
207 		 * window space.
208 		 */
209 		for (lp = win->lspace, i = 0; i < nl; i++, lp++) {
210 			win->lines[i] = lp;
211 			lp->line = &win->wspace[i * nc];
212 			lp->firstchp = &lp->firstch;
213 			lp->lastchp = &lp->lastch;
214 			lp->firstch = 0;
215 			lp->lastch = 0;
216 		}
217 	}
218 #ifdef DEBUG
219 	__CTRACE("makenew: nc = %d\n", nc);
220 #endif
221 	win->cury = win->curx = 0;
222 	win->maxy = nl;
223 	win->maxx = nc;
224 
225 	win->begy = by;
226 	win->begx = bx;
227 	win->flags = 0;
228 	__swflags(win);
229 #ifdef DEBUG
230 	__CTRACE("makenew: win->flags = %0.2o\n", win->flags);
231 	__CTRACE("makenew: win->maxy = %d\n", win->maxy);
232 	__CTRACE("makenew: win->maxx = %d\n", win->maxx);
233 	__CTRACE("makenew: win->begy = %d\n", win->begy);
234 	__CTRACE("makenew: win->begx = %d\n", win->begx);
235 #endif
236 	return (win);
237 }
238 
239 void
240 __swflags(win)
241 	WINDOW *win;
242 {
243 	win->flags &= ~(__ENDLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK);
244 	if (win->begx + win->maxx == COLS) {
245 		win->flags |= __ENDLINE;
246 		if (win->begx == 0 && win->maxy == LINES && win->begy == 0)
247 			win->flags |= __FULLWIN;
248 		if (win->begy + win->maxy == LINES)
249 			win->flags |= __SCROLLWIN;
250 	}
251 }
252