resize.c revision 1.5 1 /* $NetBSD: resize.c,v 1.5 2002/12/23 12:18:53 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.5 2002/12/23 12:18:53 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 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
195 #ifdef DEBUG
196 __CTRACE("resize: (%d, %d)\n", nlines, ncols);
197 __CTRACE("resize: win->wattr = %0.2o\n", win->wattr);
198 __CTRACE("resize: win->flags = %0.2o\n", win->flags);
199 __CTRACE("resize: win->maxy = %d\n", win->maxy);
200 __CTRACE("resize: win->maxx = %d\n", win->maxx);
201 __CTRACE("resize: win->begy = %d\n", win->begy);
202 __CTRACE("resize: win->begx = %d\n", win->begx);
203 __CTRACE("resize: win->scr_t = %d\n", win->scr_t);
204 __CTRACE("resize: win->scr_b = %d\n", win->scr_b);
205 #endif
206
207 /* Reallocate line pointer array and line space. */
208 if ((newlines = realloc(win->lines,
209 nlines * sizeof(__LINE *))) == NULL) {
210 return ERR;
211 }
212 win->lines = newlines;
213
214 if ((newlspace = realloc(win->lspace,
215 nlines * sizeof(__LINE))) == NULL) {
216 return ERR;
217 }
218 win->lspace = newlspace;
219
220 /* Don't allocate window and line space if it's a subwindow */
221 if (win->orig == NULL) {
222 /*
223 * Allocate window space in one chunk.
224 */
225 if ((newwspace =
226 realloc(win->wspace,
227 ncols * nlines * sizeof(__LDATA))) == NULL) {
228 return ERR;
229 }
230
231 win->wspace = newwspace;
232
233 /*
234 * Point line pointers to line space, and lines themselves into
235 * window space.
236 */
237 for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
238 win->lines[i] = lp;
239 lp->line = &win->wspace[i * ncols];
240 #ifdef DEBUG
241 lp->sentinel = SENTINEL_VALUE;
242 #endif
243 lp->firstchp = &lp->firstch;
244 lp->lastchp = &lp->lastch;
245 lp->firstch = 0;
246 lp->lastch = ncols - 1;
247 lp->flags = __ISDIRTY;
248 }
249 } else {
250
251 win->ch_off = win->begx - win->orig->begx;
252 /* Point line pointers to line space. */
253 for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
254 win->lines[i] = lp;
255 olp = win->orig->lines[i + win->begy
256 - win->orig->begy];
257 lp->line = &olp->line[win->ch_off];
258 lp->firstchp = &olp->firstch;
259 lp->lastchp = &olp->lastch;
260 lp->hash = __hash((char *)(void *)lp->line,
261 (size_t) (win->maxx * __LDATASIZE));
262 lp->flags = __ISDIRTY;
263 }
264 }
265
266
267 win->cury = win->curx = 0;
268 win->maxy = nlines;
269 win->maxx = ncols;
270 win->scr_b = win->maxy - 1;
271 __swflags(win);
272
273 #ifdef DEBUG
274 __CTRACE("resize: win->wattr = %0.2o\n", win->wattr);
275 __CTRACE("resize: win->flags = %0.2o\n", win->flags);
276 __CTRACE("resize: win->maxy = %d\n", win->maxy);
277 __CTRACE("resize: win->maxx = %d\n", win->maxx);
278 __CTRACE("resize: win->begy = %d\n", win->begy);
279 __CTRACE("resize: win->begx = %d\n", win->begx);
280 __CTRACE("resize: win->scr_t = %d\n", win->scr_t);
281 __CTRACE("resize: win->scr_b = %d\n", win->scr_b);
282 #endif
283 return OK;
284 }
285
286