xref: /386bsd/usr/src/lib/libcurses/newwin.c (revision a2142627)
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 static char sccsid[] = "@(#)newwin.c	8.1 (Berkeley) 7/20/93";
36 #endif	/* not lint */
37 
38 #include <curses.h>
39 #include <stdlib.h>
40 
41 #undef	nl		/* Don't need it here, and it interferes. */
42 
43 static WINDOW 	*__makenew __P((int, int, int, int, int));
44 
45 void	 __set_subwin __P((WINDOW *, WINDOW *));
46 
47 /*
48  * newwin --
49  *	Allocate space for and set up defaults for a new window.
50  */
51 WINDOW *
newwin(nl,nc,by,bx)52 newwin(nl, nc, by, bx)
53 	register int nl, nc, by, bx;
54 {
55 	register WINDOW *win;
56 	register __LINE *lp;
57 	register int  i, j;
58 	register __LDATA *sp;
59 
60 	if (nl == 0)
61 		nl = LINES - by;
62 	if (nc == 0)
63 		nc = COLS - bx;
64 
65 	if ((win = __makenew(nl, nc, by, bx, 0)) == NULL)
66 		return (NULL);
67 
68 	win->nextp = win;
69 	win->ch_off = 0;
70 	win->orig = NULL;
71 
72 #ifdef DEBUG
73 	__CTRACE("newwin: win->ch_off = %d\n", win->ch_off);
74 #endif
75 
76 	for (i = 0; i < nl; i++) {
77 		lp = win->lines[i];
78 		lp->flags = 0;
79 		for (sp = lp->line, j = 0; j < nc; j++, sp++) {
80 			sp->ch = ' ';
81 			sp->attr = 0;
82 		}
83 		lp->hash = __hash((char *) lp->line, nc * __LDATASIZE);
84 	}
85 	return (win);
86 }
87 
88 WINDOW *
subwin(orig,nl,nc,by,bx)89 subwin(orig, nl, nc, by, bx)
90 	register WINDOW *orig;
91 	register int by, bx, nl, nc;
92 {
93 	int i;
94 	__LINE *lp;
95 	register WINDOW *win;
96 
97 	/* Make sure window fits inside the original one. */
98 #ifdef	DEBUG
99 	__CTRACE("subwin: (%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx);
100 #endif
101 	if (by < orig->begy || bx < orig->begx
102 	    || by + nl > orig->maxy + orig->begy
103 	    || bx + nc > orig->maxx + orig->begx)
104 		return (NULL);
105 	if (nl == 0)
106 		nl = orig->maxy + orig->begy - by;
107 	if (nc == 0)
108 		nc = orig->maxx + orig->begx - bx;
109 	if ((win = __makenew(nl, nc, by, bx, 1)) == NULL)
110 		return (NULL);
111 	win->nextp = orig->nextp;
112 	orig->nextp = win;
113 	win->orig = orig;
114 
115 	/* Initialize flags here so that refresh can also use __set_subwin. */
116 	for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++)
117 		lp->flags = 0;
118 	__set_subwin(orig, win);
119 	return (win);
120 }
121 
122 /*
123  * This code is shared with mvwin().
124  */
125 void
__set_subwin(orig,win)126 __set_subwin(orig, win)
127 	register WINDOW *orig, *win;
128 {
129 	int i;
130 	__LINE *lp, *olp;
131 
132 	win->ch_off = win->begx - orig->begx;
133 	/*  Point line pointers to line space. */
134 	for (lp = win->lspace, i = 0; i < win->maxy; i++, lp++) {
135 		win->lines[i] = lp;
136 		olp = orig->lines[i + win->begy];
137 		lp->line = &olp->line[win->begx];
138 		lp->firstchp = &olp->firstch;
139 		lp->lastchp = &olp->lastch;
140 		lp->hash = __hash((char *) lp->line, win->maxx * __LDATASIZE);
141 	}
142 
143 #ifdef DEBUG
144 	__CTRACE("__set_subwin: win->ch_off = %d\n", win->ch_off);
145 #endif
146 }
147 
148 /*
149  * __makenew --
150  *	Set up a window buffer and returns a pointer to it.
151  */
152 static WINDOW *
__makenew(nl,nc,by,bx,sub)153 __makenew(nl, nc, by, bx, sub)
154 	register int by, bx, nl, nc;
155 	int sub;
156 {
157 	register WINDOW *win;
158 	register __LINE *lp;
159 	int i;
160 
161 
162 #ifdef	DEBUG
163 	__CTRACE("makenew: (%d, %d, %d, %d)\n", nl, nc, by, bx);
164 #endif
165 	if ((win = malloc(sizeof(*win))) == NULL)
166 		return (NULL);
167 #ifdef DEBUG
168 	__CTRACE("makenew: nl = %d\n", nl);
169 #endif
170 
171 	/*
172 	 * Set up line pointer array and line space.
173 	 */
174 	if ((win->lines = malloc (nl * sizeof(__LINE *))) == NULL) {
175 		free(win);
176 		return NULL;
177 	}
178 	if ((win->lspace = malloc (nl * sizeof(__LINE))) == NULL) {
179 		free (win);
180 		free (win->lines);
181 		return NULL;
182 	}
183 
184 	/* Don't allocate window and line space if it's a subwindow */
185 	if (!sub) {
186 		/*
187 		 * Allocate window space in one chunk.
188 		 */
189 		if ((win->wspace =
190 		    malloc(nc * nl * sizeof(__LDATA))) == NULL) {
191 			free(win->lines);
192 			free(win->lspace);
193 			free(win);
194 			return NULL;
195 		}
196 
197 		/*
198 		 * Point line pointers to line space, and lines themselves into
199 		 * window space.
200 		 */
201 		for (lp = win->lspace, i = 0; i < nl; i++, lp++) {
202 			win->lines[i] = lp;
203 			lp->line = &win->wspace[i * nc];
204 			lp->firstchp = &lp->firstch;
205 			lp->lastchp = &lp->lastch;
206 			lp->firstch = 0;
207 			lp->lastch = 0;
208 		}
209 	}
210 #ifdef DEBUG
211 	__CTRACE("makenew: nc = %d\n", nc);
212 #endif
213 	win->cury = win->curx = 0;
214 	win->maxy = nl;
215 	win->maxx = nc;
216 
217 	win->begy = by;
218 	win->begx = bx;
219 	win->flags = 0;
220 	__swflags(win);
221 #ifdef DEBUG
222 	__CTRACE("makenew: win->flags = %0.2o\n", win->flags);
223 	__CTRACE("makenew: win->maxy = %d\n", win->maxy);
224 	__CTRACE("makenew: win->maxx = %d\n", win->maxx);
225 	__CTRACE("makenew: win->begy = %d\n", win->begy);
226 	__CTRACE("makenew: win->begx = %d\n", win->begx);
227 #endif
228 	return (win);
229 }
230 
231 void
__swflags(win)232 __swflags(win)
233 	register WINDOW *win;
234 {
235 	win->flags &=
236 	    ~(__ENDLINE | __FULLLINE | __FULLWIN | __SCROLLWIN | __LEAVEOK);
237 	if (win->begx + win->maxx == COLS) {
238 		win->flags |= __ENDLINE;
239 		if (win->begx == 0) {
240 			if (AL && DL)
241 				win->flags |= __FULLLINE;
242 			if (win->maxy == LINES && win->begy == 0)
243 				win->flags |= __FULLWIN;
244 		}
245 		if (win->begy + win->maxy == LINES)
246 			win->flags |= __SCROLLWIN;
247 	}
248 }
249