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