resize.c revision 1.7 1 /* $NetBSD: resize.c,v 1.7 2003/02/17 11:07:21 dsl 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.7 2003/02/17 11:07:21 dsl Exp $");
44 #endif
45 #endif /* not lint */
46
47 #include <stdlib.h>
48
49 #include "curses.h"
50 #include "curses_private.h"
51
52 extern struct __winlist *winlistp;
53
54 static int __resizewin(WINDOW *win, int nlines, int ncols);
55
56 /*
57 * wresize --
58 * Resize the given window to the new size.
59 */
60 int
61 wresize(WINDOW *win, int req_nlines, int req_ncols)
62 {
63 int nlines = req_nlines;
64 int ncols = req_ncols;
65
66 if (win == NULL)
67 return ERR;
68
69 nlines = req_nlines;
70 ncols = req_ncols;
71 if (win->orig == NULL) {
72 /* bound window to screen */
73 if (win->begy + nlines > LINES)
74 nlines = 0;
75 if (nlines <= 0)
76 nlines += LINES - win->begy;
77 if (win->begx + ncols > COLS)
78 ncols = 0;
79 if (ncols <= 0)
80 ncols += COLS - win->begx;
81 } else {
82 /* subwins must fit inside the parent - check this */
83 if (win->begy + nlines > win->orig->begy + win->orig->maxy)
84 nlines = 0;
85 if (nlines <= 0)
86 nlines += win->orig->begy + win->orig->maxy - win->begy;
87 if (win->begx + ncols > win->orig->begx + win->orig->maxx)
88 ncols = 0;
89 if (ncols <= 0)
90 ncols += win->orig->begx + win->orig->maxx - win->begx;
91 }
92
93 if ((__resizewin(win, nlines, ncols)) == ERR)
94 return ERR;
95
96 win->reqy = req_nlines;
97 win->reqx = req_ncols;
98
99 return OK;
100 }
101
102 /*
103 * resizeterm --
104 * Resize the terminal window, resizing the dependent windows.
105 */
106 int
107 resizeterm(int nlines, int ncols)
108 {
109 WINDOW *win;
110 struct __winlist *list;
111 int newlines, newcols;
112
113 /* don't worry if things have not changed... we would like to
114 do this but some bastard programs update LINES and COLS before
115 calling resizeterm thus negating it's effect.
116 if ((nlines == LINES) && (ncols == COLS))
117 return OK;*/
118
119 #ifdef DEBUG
120 __CTRACE("resizeterm: (%d, %d)\n", nlines, ncols);
121 #endif
122
123
124 for (list = __winlistp; list != NULL; list = list->nextp) {
125 win = list->winp;
126
127 newlines = win->reqy;
128 if (win->begy + newlines >= nlines)
129 newlines = 0;
130 if (newlines == 0)
131 newlines = nlines - win->begy;
132
133 newcols = win->reqx;
134 if (win->begx + newcols >= ncols)
135 newcols = 0;
136 if (newcols == 0)
137 newcols = ncols - win->begx;
138
139 if (__resizewin(win, newlines, newcols) != OK)
140 return ERR;
141 }
142
143 LINES = nlines;
144 COLS = ncols;
145
146 /* tweak the flags now that we have updated the LINES and COLS */
147 for (list = __winlistp; list != NULL; list = list->nextp) {
148 if (!win->flags & __ISPAD)
149 __swflags(list->winp);
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