resize.c revision 1.10 1 /* $NetBSD: resize.c,v 1.10 2004/03/16 07:49:01 jdc 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.10 2004/03/16 07:49:01 jdc 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 = NULL; /* XXX gcc -Wuninitialized */
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 if (!(win->flags & __ISPAD))
147 __swflags(list->winp);
148 }
149
150 wrefresh(curscr);
151 return OK;
152 }
153
154 /*
155 * __resizewin --
156 * Resize the given window.
157 */
158 static int
159 __resizewin(WINDOW *win, int nlines, int ncols)
160 {
161 __LINE *lp, *olp, **newlines, *newlspace;
162 __LDATA *sp;
163 __LDATA *newwspace;
164 int i, j;
165 int y, x;
166 WINDOW *swin;
167
168 #ifdef DEBUG
169 __CTRACE("resize: (%p, %d, %d)\n", win, nlines, ncols);
170 __CTRACE("resize: win->wattr = %08x\n", win->wattr);
171 __CTRACE("resize: win->flags = %#.4x\n", win->flags);
172 __CTRACE("resize: win->maxy = %d\n", win->maxy);
173 __CTRACE("resize: win->maxx = %d\n", win->maxx);
174 __CTRACE("resize: win->begy = %d\n", win->begy);
175 __CTRACE("resize: win->begx = %d\n", win->begx);
176 __CTRACE("resize: win->scr_t = %d\n", win->scr_t);
177 __CTRACE("resize: win->scr_b = %d\n", win->scr_b);
178 #endif
179
180 if (nlines <= 0 || ncols <= 0)
181 nlines = ncols = 0;
182 else {
183 /* Reallocate line pointer array and line space. */
184 newlines = realloc(win->lines, nlines * sizeof(__LINE *));
185 if (newlines == NULL)
186 return ERR;
187 win->lines = newlines;
188
189 newlspace = realloc(win->lspace, nlines * sizeof(__LINE));
190 if (newlspace == NULL)
191 return ERR;
192 win->lspace = newlspace;
193 }
194
195 /* Don't allocate window and line space if it's a subwindow */
196 if (win->orig == NULL) {
197 /*
198 * Allocate window space in one chunk.
199 */
200 if (ncols != 0) {
201 newwspace = realloc(win->wspace,
202 ncols * nlines * sizeof(__LDATA));
203 if (newwspace == NULL)
204 return ERR;
205 win->wspace = newwspace;
206 }
207
208 /*
209 * Point line pointers to line space, and lines themselves into
210 * window space.
211 */
212 for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
213 win->lines[i] = lp;
214 lp->line = &win->wspace[i * ncols];
215 #ifdef DEBUG
216 lp->sentinel = SENTINEL_VALUE;
217 #endif
218 lp->firstchp = &lp->firstch;
219 lp->lastchp = &lp->lastch;
220 lp->firstch = 0;
221 lp->lastch = ncols - 1;
222 lp->flags = __ISDIRTY;
223 }
224 } else {
225
226 win->ch_off = win->begx - win->orig->begx;
227 /* Point line pointers to line space. */
228 for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
229 win->lines[i] = lp;
230 olp = win->orig->lines[i + win->begy
231 - win->orig->begy];
232 lp->line = &olp->line[win->ch_off];
233 #ifdef DEBUG
234 lp->sentinel = SENTINEL_VALUE;
235 #endif
236 lp->firstchp = &olp->firstch;
237 lp->lastchp = &olp->lastch;
238 lp->flags = __ISDIRTY;
239 }
240 }
241
242
243 win->cury = win->curx = 0;
244 win->maxy = nlines;
245 win->maxx = ncols;
246 win->scr_b = win->maxy - 1;
247 __swflags(win);
248
249 /*
250 * we must zot the window contents otherwise lines may pick
251 * up attributes from the previous line when the window is
252 * made smaller. The client will redraw the window anyway
253 * so this is no big deal.
254 */
255 for (i = 0; i < win->maxy; i++) {
256 lp = win->lines[i];
257 for (sp = lp->line, j = 0; j < win->maxx; j++, sp++) {
258 sp->ch = ' ';
259 sp->bch = ' ';
260 sp->attr = 0;
261 sp->battr = 0;
262 }
263 lp->hash = __hash((char *)(void *)lp->line,
264 (size_t) (ncols * __LDATASIZE));
265 }
266
267 #ifdef DEBUG
268 __CTRACE("resize: win->wattr = %08x\n", win->wattr);
269 __CTRACE("resize: win->flags = %#.4x\n", win->flags);
270 __CTRACE("resize: win->maxy = %d\n", win->maxy);
271 __CTRACE("resize: win->maxx = %d\n", win->maxx);
272 __CTRACE("resize: win->begy = %d\n", win->begy);
273 __CTRACE("resize: win->begx = %d\n", win->begx);
274 __CTRACE("resize: win->scr_t = %d\n", win->scr_t);
275 __CTRACE("resize: win->scr_b = %d\n", win->scr_b);
276 #endif
277
278 if (win->orig == NULL) {
279 /* bound subwindows to new size and fixup their pointers */
280 for (swin = win->nextp; swin != win; swin = swin->nextp) {
281 y = swin->reqy;
282 if (swin->begy + y > win->begy + win->maxy)
283 y = 0;
284 if (y <= 0)
285 y += win->begy + win->maxy - swin->begy;
286 x = swin->reqx;
287 if (swin->begx + x > win->begx + win->maxx)
288 x = 0;
289 if (x <= 0)
290 x += win->begy + win->maxx - swin->begx;
291 __resizewin(swin, y, x);
292 }
293 }
294
295 return OK;
296 }
297
298