resize.c revision 1.6 1 /* $NetBSD: resize.c,v 1.6 2003/02/10 23:24:27 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.6 2003/02/10 23:24:27 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 nlines, int ncols)
62 {
63 __LINE *lp;
64 int i, j;
65 __LDATA *sp;
66
67 if ((win == NULL) || (nlines < 0) || (ncols < 0))
68 return ERR;
69
70 if (win->orig == NULL) {
71 if (nlines == 0)
72 nlines = LINES - win->begy;
73 if (ncols == 0)
74 ncols = COLS - win->begx;
75 } else {
76 /* subwins must fit inside the parent - check this */
77 if ((nlines == 0)
78 || (nlines > (win->orig->maxy + win->orig->begy
79 - win->begy)))
80 nlines = win->orig->maxy + win->orig->begy - win->begy;
81
82 if ((ncols == 0)
83 || (ncols > (win->orig->maxx + win->orig->begx
84 - win->begx)))
85 ncols = win->orig->maxx + win->orig->begx - win->begx;
86 }
87
88 if ((__resizewin(win, nlines, ncols)) == ERR)
89 return ERR;
90
91 /*
92 * we must zot the window contents otherwise lines may pick
93 * up attributes from the previous line when the window is
94 * made smaller. The client will redraw the window anyway
95 * so this is no big deal.
96 */
97 for (i = 0; i < win->maxy; i++) {
98 lp = win->lines[i];
99 for (sp = lp->line, j = 0; j < win->maxx;
100 j++, sp++) {
101 sp->ch = ' ';
102 sp->bch = ' ';
103 sp->attr = 0;
104 sp->battr = 0;
105 }
106 lp->hash = __hash((char *)(void *)lp->line,
107 (size_t) (ncols * __LDATASIZE));
108 }
109
110 return OK;
111 }
112
113 /*
114 * resizeterm --
115 * Resize the terminal window, resizing the dependent windows.
116 */
117 int
118 resizeterm(int nlines, int ncols)
119 {
120 WINDOW *win;
121 struct __winlist *list;
122 int newlines, newcols, ldelta, cdelta;
123
124 /* don't worry if things have not changed... we would like to
125 do this but some bastard programs update LINES and COLS before
126 calling resizeterm thus negating it's effect.
127 if ((nlines == LINES) && (ncols == COLS))
128 return OK;*/
129
130 #ifdef DEBUG
131 __CTRACE("resizeterm: (%d, %d)\n", nlines, ncols);
132 #endif
133
134 ldelta = nlines - __virtscr->maxy;
135 cdelta = ncols - __virtscr->maxx;
136
137 for (list = __winlistp; list != NULL; list = list->nextp) {
138 win = list->winp;
139 newlines = win->maxy;
140 newcols = win->maxx;
141
142 if (win->begy >= (nlines - 1)) {
143 win->begy = nlines - win->maxx - 1;
144 if (win->begy < 0)
145 win->begy = 0;
146 }
147
148
149 if ((newlines + win->begy + ldelta) == nlines)
150 newlines = nlines;
151
152 if (newlines > nlines) {
153 newlines = nlines - win->begy;
154 if (newlines < 0)
155 newlines = 1;
156 }
157
158 if ((newcols + win->begx + cdelta) == ncols)
159 newcols = ncols;
160
161 if (newcols > ncols) {
162 newcols = ncols - win->begx;
163 if (newcols < 0)
164 newcols = 1;
165 }
166
167 if (wresize(win, newlines, newcols) != OK)
168 return ERR;
169 }
170
171 LINES = nlines;
172 COLS = ncols;
173
174 /* tweak the flags now that we have updated the LINES and COLS */
175 for (list = __winlistp; list != NULL; list = list->nextp) {
176 if (!win->flags & __ISPAD)
177 __swflags(list->winp);
178 }
179
180 wrefresh(curscr);
181 return OK;
182 }
183
184 /*
185 * __resizewin --
186 * Resize the given window.
187 */
188 static int
189 __resizewin(WINDOW *win, int nlines, int ncols)
190 {
191 __LINE *lp, *olp, **newlines, *newlspace;
192 __LDATA *newwspace;
193 int i;
194 WINDOW *swin;
195
196 #ifdef DEBUG
197 __CTRACE("resize: (%d, %d)\n", nlines, ncols);
198 __CTRACE("resize: win->wattr = %0.2o\n", win->wattr);
199 __CTRACE("resize: win->flags = %0.2o\n", win->flags);
200 __CTRACE("resize: win->maxy = %d\n", win->maxy);
201 __CTRACE("resize: win->maxx = %d\n", win->maxx);
202 __CTRACE("resize: win->begy = %d\n", win->begy);
203 __CTRACE("resize: win->begx = %d\n", win->begx);
204 __CTRACE("resize: win->scr_t = %d\n", win->scr_t);
205 __CTRACE("resize: win->scr_b = %d\n", win->scr_b);
206 #endif
207
208 /* Reallocate line pointer array and line space. */
209 if ((newlines = realloc(win->lines,
210 nlines * sizeof(__LINE *))) == NULL) {
211 return ERR;
212 }
213 win->lines = newlines;
214
215 if ((newlspace = realloc(win->lspace,
216 nlines * sizeof(__LINE))) == NULL) {
217 return ERR;
218 }
219 win->lspace = newlspace;
220
221 /* Don't allocate window and line space if it's a subwindow */
222 if (win->orig == NULL) {
223 /*
224 * Allocate window space in one chunk.
225 */
226 if ((newwspace =
227 realloc(win->wspace,
228 ncols * nlines * sizeof(__LDATA))) == NULL) {
229 return ERR;
230 }
231
232 win->wspace = newwspace;
233
234 /*
235 * Point line pointers to line space, and lines themselves into
236 * window space.
237 */
238 for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
239 win->lines[i] = lp;
240 lp->line = &win->wspace[i * ncols];
241 #ifdef DEBUG
242 lp->sentinel = SENTINEL_VALUE;
243 #endif
244 lp->firstchp = &lp->firstch;
245 lp->lastchp = &lp->lastch;
246 lp->firstch = 0;
247 lp->lastch = ncols - 1;
248 lp->flags = __ISDIRTY;
249 }
250 } else {
251
252 win->ch_off = win->begx - win->orig->begx;
253 /* Point line pointers to line space. */
254 for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
255 win->lines[i] = lp;
256 olp = win->orig->lines[i + win->begy
257 - win->orig->begy];
258 lp->line = &olp->line[win->ch_off];
259 #ifdef DEBUG
260 lp->sentinel = SENTINEL_VALUE;
261 #endif
262 lp->firstchp = &olp->firstch;
263 lp->lastchp = &olp->lastch;
264 lp->hash = __hash((char *)(void *)lp->line,
265 (size_t) (win->maxx * __LDATASIZE));
266 lp->flags = __ISDIRTY;
267 }
268 }
269
270
271 win->cury = win->curx = 0;
272 win->maxy = nlines;
273 win->maxx = ncols;
274 win->scr_b = win->maxy - 1;
275 __swflags(win);
276
277 #ifdef DEBUG
278 __CTRACE("resize: win->wattr = %0.2o\n", win->wattr);
279 __CTRACE("resize: win->flags = %0.2o\n", win->flags);
280 __CTRACE("resize: win->maxy = %d\n", win->maxy);
281 __CTRACE("resize: win->maxx = %d\n", win->maxx);
282 __CTRACE("resize: win->begy = %d\n", win->begy);
283 __CTRACE("resize: win->begx = %d\n", win->begx);
284 __CTRACE("resize: win->scr_t = %d\n", win->scr_t);
285 __CTRACE("resize: win->scr_b = %d\n", win->scr_b);
286 #endif
287 if (win->orig == NULL) {
288 for (swin = win->nextp; swin != win; swin = swin->nextp)
289 wresize(swin, nlines - swin->begy, ncols - swin->begx);
290 }
291 return OK;
292 }
293
294