xref: /netbsd-src/lib/libcurses/newwin.c (revision d9158b13b5dfe46201430699a3f7a235ecf28df3)
1 /*
2  * Copyright (c) 1981, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 /* from: static char sccsid[] = "@(#)newwin.c	8.1 (Berkeley) 7/20/93"; */
36 static char *rcsid = "$Id: newwin.c,v 1.6 1993/11/09 04:08:22 cgd Exp $";
37 #endif	/* not lint */
38 
39 #include <curses.h>
40 #include <stdlib.h>
41 
42 #undef	nl		/* Don't need it here, and it interferes. */
43 
44 static WINDOW 	*__makenew __P((int, int, int, int, int));
45 
46 void	 __set_subwin __P((WINDOW *, WINDOW *));
47 
48 /*
49  * newwin --
50  *	Allocate space for and set up defaults for a new window.
51  */
52 WINDOW *
53 newwin(nl, nc, by, bx)
54 	register int nl, nc, by, bx;
55 {
56 	register WINDOW *win;
57 	register __LINE *lp;
58 	register int  i, j;
59 	register __LDATA *sp;
60 
61 	if (nl == 0)
62 		nl = LINES - by;
63 	if (nc == 0)
64 		nc = COLS - bx;
65 
66 	if ((win = __makenew(nl, nc, by, bx, 0)) == NULL)
67 		return (NULL);
68 
69 	win->nextp = win;
70 	win->ch_off = 0;
71 	win->orig = NULL;
72 
73 #ifdef DEBUG
74 	__CTRACE("newwin: win->ch_off = %d\n", win->ch_off);
75 #endif
76 
77 	for (i = 0; i < nl; i++) {
78 		lp = win->lines[i];
79 		lp->flags = 0;
80 		for (sp = lp->line, j = 0; j < nc; j++, sp++) {
81 			sp->ch = ' ';
82 			sp->attr = 0;
83 		}
84 		lp->hash = __hash((char *) lp->line, nc * __LDATASIZE);
85 	}
86 	return (win);
87 }
88 
89 WINDOW *
90 subwin(orig, nl, nc, by, bx)
91 	register WINDOW *orig;
92 	register int by, bx, nl, nc;
93 {
94 	int i;
95 	__LINE *lp;
96 	register WINDOW *win;
97 
98 	/* Make sure window fits inside the original one. */
99 #ifdef	DEBUG
100 	__CTRACE("subwin: (%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx);
101 #endif
102 	if (by < orig->begy || bx < orig->begx
103 	    || by + nl > orig->maxy + orig->begy
104 	    || bx + nc > orig->maxx + orig->begx)
105 		return (NULL);
106 	if (nl == 0)
107 		nl = orig->maxy + orig->begy - by;
108 	if (nc == 0)
109 		nc = orig->maxx + orig->begx - bx;
110 	if ((win = __makenew(nl, nc, by, bx, 1)) == NULL)
111 		return (NULL);
112 	win->nextp = orig->nextp;
113 	orig->nextp = win;
114 	win->orig = orig;
115 
116 	/* Initialize flags here so that refresh can also use __set_subwin. */
117 	for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++)
118 		lp->flags = 0;
119 	__set_subwin(orig, win);
120 	return (win);
121 }
122 
123 /*
124  * This code is shared with mvwin().
125  */
126 void
127 __set_subwin(orig, win)
128 	register WINDOW *orig, *win;
129 {
130 	int i;
131 	__LINE *lp, *olp;
132 
133 	win->ch_off = win->begx - orig->begx;
134 	/*  Point line pointers to line space. */
135 	for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) {
136 		win->lines[i] = lp;
137 		olp = orig->lines[i + win->begy];
138 		lp->line = &olp->line[win->begx];
139 		lp->firstchp = &olp->firstch;
140 		lp->lastchp = &olp->lastch;
141 		lp->hash = __hash((char *) lp->line, win->maxx * __LDATASIZE);
142 	}
143 
144 #ifdef DEBUG
145 	__CTRACE("__set_subwin: win->ch_off = %d\n", win->ch_off);
146 #endif
147 }
148 
149 /*
150  * __makenew --
151  *	Set up a window buffer and returns a pointer to it.
152  */
153 static WINDOW *
154 __makenew(nl, nc, by, bx, sub)
155 	register int by, bx, nl, nc;
156 	int sub;
157 {
158 	register WINDOW *win;
159 	register __LINE *lp;
160 	int i;
161 
162 
163 #ifdef	DEBUG
164 	__CTRACE("makenew: (%d, %d, %d, %d)\n", nl, nc, by, bx);
165 #endif
166 	if ((win = malloc(sizeof(*win))) == NULL)
167 		return (NULL);
168 #ifdef DEBUG
169 	__CTRACE("makenew: nl = %d\n", nl);
170 #endif
171 
172 	/*
173 	 * Set up line pointer array and line space.
174 	 */
175 	if ((win->lines = malloc (nl * sizeof(__LINE *))) == NULL) {
176 		free(win);
177 		return NULL;
178 	}
179 	if ((win->lspace = malloc (nl * sizeof(__LINE))) == NULL) {
180 		free (win);
181 		free (win->lines);
182 		return NULL;
183 	}
184 
185 	/* Don't allocate window and line space if it's a subwindow */
186 	if (!sub) {
187 		/*
188 		 * Allocate window space in one chunk.
189 		 */
190 		if ((win->wspace =
191 		    malloc(nc * nl * sizeof(__LDATA))) == NULL) {
192 			free(win->lines);
193 			free(win->lspace);
194 			free(win);
195 			return NULL;
196 		}
197 
198 		/*
199 		 * Point line pointers to line space, and lines themselves into
200 		 * window space.
201 		 */
202 		for (lp = win->lspace, i = 0; i < nl; i++, lp++) {
203 			win->lines[i] = lp;
204 			lp->line = &win->wspace[i * nc];
205 			lp->firstchp = &lp->firstch;
206 			lp->lastchp = &lp->lastch;
207 			lp->firstch = 0;
208 			lp->lastch = 0;
209 		}
210 	}
211 #ifdef DEBUG
212 	__CTRACE("makenew: nc = %d\n", nc);
213 #endif
214 	win->cury = win->curx = 0;
215 	win->maxy = nl;
216 	win->maxx = nc;
217 
218 	win->begy = by;
219 	win->begx = bx;
220 	win->flags = 0;
221 	__swflags(win);
222 #ifdef DEBUG
223 	__CTRACE("makenew: win->flags = %0.2o\n", win->flags);
224 	__CTRACE("makenew: win->maxy = %d\n", win->maxy);
225 	__CTRACE("makenew: win->maxx = %d\n", win->maxx);
226 	__CTRACE("makenew: win->begy = %d\n", win->begy);
227 	__CTRACE("makenew: win->begx = %d\n", win->begx);
228 #endif
229 	return (win);
230 }
231 
232 void
233 __swflags(win)
234 	register WINDOW *win;
235 {
236 	win->flags &=
237 	    ~(__ENDLINE | __FULLLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK);
238 	if (win->begx + win->maxx == COLS) {
239 		win->flags |= __ENDLINE;
240 		if (win->begx == 0) {
241 			if (AL && DL)
242 				win->flags |= __FULLLINE;
243 			if (win->maxy == LINES && win->begy == 0)
244 				win->flags |= __FULLWIN;
245 		}
246 		if (win->begy + win->maxy == LINES)
247 			win->flags |= __SCROLLWIN;
248 	}
249 }
250