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