xref: /netbsd-src/lib/libcurses/resize.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: resize.c,v 1.11 2004/04/29 22:28:51 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2001
5  *	Brett Lymn.
6  *
7  * This code has been donated to The NetBSD Foundation by the Author.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)resize.c   blymn 2001/08/26";
42 #else
43 __RCSID("$NetBSD: resize.c,v 1.11 2004/04/29 22:28:51 christos Exp $");
44 #endif
45 #endif				/* not lint */
46 
47 #include <stdlib.h>
48 
49 #include "curses.h"
50 #include "curses_private.h"
51 
52 static int __resizewin(WINDOW *win, int nlines, int ncols);
53 
54 /*
55  * wresize --
56  *	Resize the given window to the new size.
57  */
58 int
59 wresize(WINDOW *win, int req_nlines, int req_ncols)
60 {
61 	int	nlines = req_nlines;
62 	int	ncols = req_ncols;
63 
64 	if (win == NULL)
65 		return ERR;
66 
67 	nlines = req_nlines;
68 	ncols = req_ncols;
69 	if (win->orig == NULL) {
70 		/* bound window to screen */
71 		if (win->begy + nlines > LINES)
72 			nlines = 0;
73 		if (nlines <= 0)
74 			nlines += LINES - win->begy;
75 		if (win->begx + ncols > COLS)
76 			ncols = 0;
77 		if (ncols <= 0)
78 			ncols += COLS - win->begx;
79 	} else {
80 		/* subwins must fit inside the parent - check this */
81 		if (win->begy + nlines > win->orig->begy + win->orig->maxy)
82 			nlines = 0;
83 		if (nlines <= 0)
84 			nlines += win->orig->begy + win->orig->maxy - win->begy;
85 		if (win->begx + ncols > win->orig->begx + win->orig->maxx)
86 			ncols = 0;
87 		if (ncols <= 0)
88 			ncols += win->orig->begx + win->orig->maxx - win->begx;
89 	}
90 
91 	if ((__resizewin(win, nlines, ncols)) == ERR)
92 		return ERR;
93 
94 	win->reqy = req_nlines;
95 	win->reqx = req_ncols;
96 
97 	return OK;
98 }
99 
100 /*
101  * resizeterm --
102  *      Resize the terminal window, resizing the dependent windows.
103  */
104 int
105 resizeterm(int nlines, int ncols)
106 {
107 	WINDOW *win;
108 	struct __winlist *list;
109 	int newlines, newcols;
110 
111 	  /* don't worry if things have not changed... we would like to
112 	     do this but some bastard programs update LINES and COLS before
113 	     calling resizeterm thus negating it's effect.
114 	if ((nlines == LINES) && (ncols == COLS))
115 	return OK;*/
116 
117 #ifdef	DEBUG
118 	__CTRACE("resizeterm: (%d, %d)\n", nlines, ncols);
119 #endif
120 
121 
122 	for (list = _cursesi_screen->winlistp; list != NULL; list = list->nextp) {
123 		win = list->winp;
124 
125 		newlines = win->reqy;
126 		if (win->begy + newlines >= nlines)
127 			newlines = 0;
128 		if (newlines == 0)
129 			newlines = nlines - win->begy;
130 
131 		newcols = win->reqx;
132 		if (win->begx + newcols >= ncols)
133 			newcols = 0;
134 		if (newcols == 0)
135 			newcols = ncols - win->begx;
136 
137 		if (__resizewin(win, newlines, newcols) != OK)
138 			return ERR;
139 	}
140 
141 	LINES = nlines;
142 	COLS = ncols;
143 
144 	  /* tweak the flags now that we have updated the LINES and COLS */
145 	for (list = _cursesi_screen->winlistp; list != NULL; list = list->nextp) {
146 		win = list->winp;
147 
148 		if (!(win->flags & __ISPAD))
149 			__swflags(win);
150 	}
151 
152 	wrefresh(curscr);
153 	return OK;
154 }
155 
156 /*
157  * __resizewin --
158  *	Resize the given window.
159  */
160 static int
161 __resizewin(WINDOW *win, int nlines, int ncols)
162 {
163 	__LINE			*lp, *olp, **newlines, *newlspace;
164 	__LDATA			*sp;
165 	__LDATA                 *newwspace;
166 	int			 i, j;
167 	int			 y, x;
168 	WINDOW			*swin;
169 
170 #ifdef	DEBUG
171 	__CTRACE("resize: (%p, %d, %d)\n", win, nlines, ncols);
172 	__CTRACE("resize: win->wattr = %08x\n", win->wattr);
173 	__CTRACE("resize: win->flags = %#.4x\n", win->flags);
174 	__CTRACE("resize: win->maxy = %d\n", win->maxy);
175 	__CTRACE("resize: win->maxx = %d\n", win->maxx);
176 	__CTRACE("resize: win->begy = %d\n", win->begy);
177 	__CTRACE("resize: win->begx = %d\n", win->begx);
178 	__CTRACE("resize: win->scr_t = %d\n", win->scr_t);
179 	__CTRACE("resize: win->scr_b = %d\n", win->scr_b);
180 #endif
181 
182 	if (nlines <= 0 || ncols <= 0)
183 		nlines = ncols = 0;
184 	else {
185 		/* Reallocate line pointer array and line space. */
186 		newlines = realloc(win->lines, nlines * sizeof(__LINE *));
187 		if (newlines == NULL)
188 			return ERR;
189 		win->lines = newlines;
190 
191 		newlspace = realloc(win->lspace, nlines * sizeof(__LINE));
192 		if (newlspace == NULL)
193 			return ERR;
194 		win->lspace = newlspace;
195 	}
196 
197 	/* Don't allocate window and line space if it's a subwindow */
198 	if (win->orig == NULL) {
199 		/*
200 		 * Allocate window space in one chunk.
201 		 */
202 		if (ncols != 0) {
203 			newwspace = realloc(win->wspace,
204 					    ncols * nlines * sizeof(__LDATA));
205 			if (newwspace == NULL)
206 				return ERR;
207 			win->wspace = newwspace;
208 		}
209 
210 		/*
211 		 * Point line pointers to line space, and lines themselves into
212 		 * window space.
213 		 */
214 		for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
215 			win->lines[i] = lp;
216 			lp->line = &win->wspace[i * ncols];
217 #ifdef DEBUG
218 			lp->sentinel = SENTINEL_VALUE;
219 #endif
220 			lp->firstchp = &lp->firstch;
221 			lp->lastchp = &lp->lastch;
222 			lp->firstch = 0;
223 			lp->lastch = ncols - 1;
224 			lp->flags = __ISDIRTY;
225 		}
226 	} else {
227 
228 		win->ch_off = win->begx - win->orig->begx;
229 		  /* Point line pointers to line space. */
230 		for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
231 			win->lines[i] = lp;
232 			olp = win->orig->lines[i + win->begy
233 					      - win->orig->begy];
234 			lp->line = &olp->line[win->ch_off];
235 #ifdef DEBUG
236 			lp->sentinel = SENTINEL_VALUE;
237 #endif
238 			lp->firstchp = &olp->firstch;
239 			lp->lastchp = &olp->lastch;
240 			lp->flags = __ISDIRTY;
241 		}
242 	}
243 
244 
245 	win->cury = win->curx = 0;
246 	win->maxy = nlines;
247 	win->maxx = ncols;
248 	win->scr_b = win->maxy - 1;
249 	__swflags(win);
250 
251 	  /*
252 	   * we must zot the window contents otherwise lines may pick
253 	   * up attributes from the previous line when the window is
254 	   * made smaller.  The client will redraw the window anyway
255 	   * so this is no big deal.
256 	   */
257 	for (i = 0; i < win->maxy; i++) {
258 		lp = win->lines[i];
259 		for (sp = lp->line, j = 0; j < win->maxx; j++, sp++) {
260 			sp->ch = ' ';
261 			sp->bch = ' ';
262 			sp->attr = 0;
263 			sp->battr = 0;
264 		}
265 		lp->hash = __hash((char *)(void *)lp->line,
266 				  (size_t) (ncols * __LDATASIZE));
267 	}
268 
269 #ifdef DEBUG
270 	__CTRACE("resize: win->wattr = %08x\n", win->wattr);
271 	__CTRACE("resize: win->flags = %#.4x\n", win->flags);
272 	__CTRACE("resize: win->maxy = %d\n", win->maxy);
273 	__CTRACE("resize: win->maxx = %d\n", win->maxx);
274 	__CTRACE("resize: win->begy = %d\n", win->begy);
275 	__CTRACE("resize: win->begx = %d\n", win->begx);
276 	__CTRACE("resize: win->scr_t = %d\n", win->scr_t);
277 	__CTRACE("resize: win->scr_b = %d\n", win->scr_b);
278 #endif
279 
280 	if (win->orig == NULL) {
281 		/* bound subwindows to new size and fixup their pointers */
282 		for (swin = win->nextp; swin != win; swin = swin->nextp) {
283 			y = swin->reqy;
284 			if (swin->begy + y > win->begy + win->maxy)
285 				y = 0;
286 			if (y <= 0)
287 				y += win->begy + win->maxy - swin->begy;
288 			x = swin->reqx;
289 			if (swin->begx + x > win->begx + win->maxx)
290 				x = 0;
291 			if (x <= 0)
292 				x += win->begy + win->maxx - swin->begx;
293 			__resizewin(swin, y, x);
294 		}
295 	}
296 
297 	return OK;
298 }
299 
300