xref: /netbsd-src/lib/libcurses/newwin.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: newwin.c,v 1.43 2006/01/15 11:43:54 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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)newwin.c	8.3 (Berkeley) 7/27/94";
36 #else
37 __RCSID("$NetBSD: newwin.c,v 1.43 2006/01/15 11:43:54 jdc Exp $");
38 #endif
39 #endif				/* not lint */
40 
41 #include <stdlib.h>
42 
43 #include "curses.h"
44 #include "curses_private.h"
45 
46 
47 static WINDOW *__makenew(SCREEN *screen, int nlines, int ncols, int by,
48 			 int bx, int sub, int ispad);
49 static WINDOW *__subwin(WINDOW *orig, int nlines, int ncols, int by, int bx,
50 			 int ispad);
51 
52 /*
53  * derwin --
54  *      Create a new window in the same manner as subwin but (by, bx)
55  *      are relative to the origin of window orig instead of absolute.
56  */
57 WINDOW *
58 derwin(WINDOW *orig, int nlines, int ncols, int by, int bx)
59 {
60 
61 	return __subwin(orig, nlines, ncols, orig->begy + by, orig->begx + bx,
62 	    FALSE);
63 }
64 
65 /*
66  * subpad --
67  *      Create a new pad in the same manner as subwin but (by, bx)
68  *      are relative to the origin of window orig instead of absolute.
69  */
70 WINDOW *
71 subpad(WINDOW *orig, int nlines, int ncols, int by, int bx)
72 {
73 
74 	return __subwin(orig, nlines, ncols, orig->begy + by, orig->begx + bx,
75 	    TRUE);
76 }
77 
78 /*
79  * dupwin --
80  *      Create a copy of the given window.
81  */
82 WINDOW *
83 dupwin(WINDOW *win)
84 {
85 	WINDOW *new_one;
86 
87 	if ((new_one = __newwin(_cursesi_screen, win->maxy, win->maxx,
88 				win->begy, win->begx, FALSE)) == NULL)
89 		return NULL;
90 
91 	overwrite(win, new_one);
92 	return new_one;
93 }
94 
95 /*
96  * newwin --
97  *	Allocate space for and set up defaults for a new window.
98  */
99 WINDOW *
100 newwin(int nlines, int ncols, int by, int bx)
101 {
102 	return __newwin(_cursesi_screen, nlines, ncols, by, bx, FALSE);
103 }
104 
105 /*
106  * newpad --
107  *	Allocate space for and set up defaults for a new pad.
108  */
109 WINDOW *
110 newpad(int nlines, int ncols)
111 {
112 	if (nlines < 1 || ncols < 1)
113 		return NULL;
114 	return __newwin(_cursesi_screen, nlines, ncols, 0, 0, TRUE);
115 }
116 
117 WINDOW *
118 __newwin(SCREEN *screen, int nlines, int ncols, int by, int bx, int ispad)
119 {
120 	WINDOW *win;
121 	__LINE *lp;
122 	int     i, j;
123 	int	maxy, maxx;
124 	__LDATA *sp;
125 
126 	if (by < 0 || bx < 0)
127 		return (NULL);
128 
129 	maxy = nlines > 0 ? nlines : LINES - by + nlines;
130 	maxx = ncols > 0 ? ncols : COLS - bx + ncols;
131 
132 	if ((win = __makenew(screen, maxy, maxx, by, bx, 0, ispad)) == NULL)
133 		return (NULL);
134 
135 	win->bch = ' ';
136 	if (__using_color)
137 		win->battr = __default_color;
138 	else
139 		win->battr = 0;
140 	win->nextp = win;
141 	win->ch_off = 0;
142 	win->orig = NULL;
143 	win->reqy = nlines;
144 	win->reqx = ncols;
145 
146 #ifdef DEBUG
147 	__CTRACE("newwin: win->ch_off = %d\n", win->ch_off);
148 #endif
149 
150 	for (i = 0; i < maxy; i++) {
151 		lp = win->lines[i];
152 		if (ispad)
153 			lp->flags = __ISDIRTY;
154 		else
155 			lp->flags = 0;
156 		for (sp = lp->line, j = 0; j < maxx; j++, sp++) {
157 			sp->ch = ' ';
158 			sp->attr = 0;
159 		}
160 		lp->hash = __hash((char *)(void *)lp->line,
161 		    (size_t) (ncols * __LDATASIZE));
162 	}
163 	return (win);
164 }
165 
166 WINDOW *
167 subwin(WINDOW *orig, int nlines, int ncols, int by, int bx)
168 {
169 
170 	return __subwin(orig, nlines, ncols, by, bx, FALSE);
171 }
172 
173 WINDOW *
174 __subwin(WINDOW *orig, int nlines, int ncols, int by, int bx, int ispad)
175 {
176 	int     i;
177 	__LINE *lp;
178 	WINDOW *win;
179 	int	maxy, maxx;
180 
181 #ifdef	DEBUG
182 	__CTRACE("subwin: (%p, %d, %d, %d, %d, %d)\n", orig, nlines, ncols,
183 	    by, bx, ispad);
184 #endif
185 	if (orig == NULL || orig->orig != NULL)
186 		return NULL;
187 
188 	/* Make sure window fits inside the original one. */
189 	maxy = nlines > 0 ? nlines : orig->maxy + orig->begy - by + nlines;
190 	maxx = ncols > 0 ? ncols : orig->maxx + orig->begx - bx + ncols;
191 	if (by < orig->begy || bx < orig->begx
192 	    || by + maxy > orig->maxy + orig->begy
193 	    || bx + maxx > orig->maxx + orig->begx)
194 		return (NULL);
195 	if ((win = __makenew(_cursesi_screen, maxy, maxx,
196 			     by, bx, 1, ispad)) == NULL)
197 		return (NULL);
198 	win->bch = orig->bch;
199 	win->battr = orig->battr;
200 	win->reqy = nlines;
201 	win->reqx = ncols;
202 	win->nextp = orig->nextp;
203 	orig->nextp = win;
204 	win->orig = orig;
205 
206 	/* Initialize flags here so that refresh can also use __set_subwin. */
207 	for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++)
208 		lp->flags = 0;
209 	__set_subwin(orig, win);
210 	return (win);
211 }
212 /*
213  * This code is shared with mvwin().
214  */
215 void
216 __set_subwin(WINDOW *orig, WINDOW *win)
217 {
218 	int     i;
219 	__LINE *lp, *olp;
220 
221 	win->ch_off = win->begx - orig->begx;
222 	/* Point line pointers to line space. */
223 	for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) {
224 		win->lines[i] = lp;
225 		olp = orig->lines[i + win->begy - orig->begy];
226 #ifdef DEBUG
227 		lp->sentinel = SENTINEL_VALUE;
228 #endif
229 		lp->line = &olp->line[win->ch_off];
230 		lp->firstchp = &olp->firstch;
231 		lp->lastchp = &olp->lastch;
232 		lp->hash = __hash((char *)(void *)lp->line,
233 		    (size_t) (win->maxx * __LDATASIZE));
234 	}
235 
236 #ifdef DEBUG
237 	__CTRACE("__set_subwin: win->ch_off = %d\n", win->ch_off);
238 #endif
239 }
240 /*
241  * __makenew --
242  *	Set up a window buffer and returns a pointer to it.
243  */
244 static WINDOW *
245 __makenew(SCREEN *screen, int nlines, int ncols, int by, int bx, int sub,
246 	int ispad)
247 {
248 	WINDOW			*win;
249 	__LINE			*lp;
250 	struct __winlist	*wlp, *wlp2;
251 	int			 i;
252 
253 
254 #ifdef	DEBUG
255 	__CTRACE("makenew: (%d, %d, %d, %d)\n", nlines, ncols, by, bx);
256 #endif
257 	if (nlines <= 0 || ncols <= 0)
258 		return NULL;
259 
260 	if ((win = malloc(sizeof(WINDOW))) == NULL)
261 		return (NULL);
262 #ifdef DEBUG
263 	__CTRACE("makenew: win = %p\n", win);
264 #endif
265 
266 	/* Set up line pointer array and line space. */
267 	if ((win->lines = malloc(nlines * sizeof(__LINE *))) == NULL) {
268 		free(win);
269 		return NULL;
270 	}
271 	if ((win->lspace = malloc(nlines * sizeof(__LINE))) == NULL) {
272 		free(win->lines);
273 		free(win);
274 		return NULL;
275 	}
276 	/* Don't allocate window and line space if it's a subwindow */
277 	if (sub)
278 		win->wspace = NULL;
279 	else {
280 		/*
281 		 * Allocate window space in one chunk.
282 		 */
283 		if ((win->wspace =
284 			malloc(ncols * nlines * sizeof(__LDATA))) == NULL) {
285 			free(win->lspace);
286 			free(win->lines);
287 			free(win);
288 			return NULL;
289 		}
290 		/*
291 		 * Append window to window list.
292 		 */
293 		if ((wlp = malloc(sizeof(struct __winlist))) == NULL) {
294 			free(win->wspace);
295 			free(win->lspace);
296 			free(win->lines);
297 			free(win);
298 			return NULL;
299 		}
300 		wlp->winp = win;
301 		wlp->nextp = NULL;
302 		if (screen->winlistp == NULL)
303 			screen->winlistp = wlp;
304 		else {
305 			wlp2 = screen->winlistp;
306 			while (wlp2->nextp != NULL)
307 				wlp2 = wlp2->nextp;
308 			wlp2->nextp = wlp;
309 		}
310 		/*
311 		 * Point line pointers to line space, and lines themselves into
312 		 * window space.
313 		 */
314 		for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
315 			win->lines[i] = lp;
316 			lp->line = &win->wspace[i * ncols];
317 #ifdef DEBUG
318 			lp->sentinel = SENTINEL_VALUE;
319 #endif
320 			lp->firstchp = &lp->firstch;
321 			lp->lastchp = &lp->lastch;
322 			if (ispad) {
323 				lp->firstch = 0;
324 				lp->lastch = ncols;
325 			} else {
326 				lp->firstch = ncols;
327 				lp->lastch = 0;
328 			}
329 		}
330 	}
331 #ifdef DEBUG
332 	__CTRACE("makenew: ncols = %d\n", ncols);
333 #endif
334 	win->screen = screen;
335 	win->cury = win->curx = 0;
336 	win->maxy = nlines;
337 	win->maxx = ncols;
338 	win->reqy = nlines;
339 	win->reqx = ncols;
340 
341 	win->begy = by;
342 	win->begx = bx;
343 	win->flags = (__IDLINE | __IDCHAR);
344 	win->delay = -1;
345 	win->wattr = 0;
346 	win->scr_t = 0;
347 	win->scr_b = win->maxy - 1;
348 	if (ispad) {
349 		win->flags |= __ISPAD;
350 		win->pbegy = 0;
351 		win->pbegx = 0;
352 		win->sbegy = 0;
353 		win->sbegx = 0;
354 		win->smaxy = 0;
355 		win->smaxx = 0;
356 	} else
357 		__swflags(win);
358 #ifdef DEBUG
359 	__CTRACE("makenew: win->wattr = %08x\n", win->wattr);
360 	__CTRACE("makenew: win->flags = %#.4x\n", win->flags);
361 	__CTRACE("makenew: win->maxy = %d\n", win->maxy);
362 	__CTRACE("makenew: win->maxx = %d\n", win->maxx);
363 	__CTRACE("makenew: win->begy = %d\n", win->begy);
364 	__CTRACE("makenew: win->begx = %d\n", win->begx);
365 	__CTRACE("makenew: win->scr_t = %d\n", win->scr_t);
366 	__CTRACE("makenew: win->scr_b = %d\n", win->scr_b);
367 #endif
368 	return (win);
369 }
370 
371 void
372 __swflags(WINDOW *win)
373 {
374 	win->flags &= ~(__ENDLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK);
375 	if (win->begx + win->maxx == COLS && !(win->flags & __ISPAD)) {
376 		win->flags |= __ENDLINE;
377 		if (win->begx == 0 && win->maxy == LINES && win->begy == 0)
378 			win->flags |= __FULLWIN;
379 		if (win->begy + win->maxy == LINES)
380 			win->flags |= __SCROLLWIN;
381 	}
382 }
383