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