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