resize.c revision 1.12.6.3 1 /* $NetBSD: resize.c,v 1.12.6.3 2007/02/07 09:24:22 blymn 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.12.6.3 2007/02/07 09:24:22 blymn 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(__CTRACE_WINDOW, "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(__CTRACE_WINDOW, "resize: (%p, %d, %d)\n", win, nlines, ncols);
172 __CTRACE(__CTRACE_WINDOW, "resize: win->wattr = %08x\n", win->wattr);
173 __CTRACE(__CTRACE_WINDOW, "resize: win->flags = %#.4x\n", win->flags);
174 __CTRACE(__CTRACE_WINDOW, "resize: win->maxy = %d\n", win->maxy);
175 __CTRACE(__CTRACE_WINDOW, "resize: win->maxx = %d\n", win->maxx);
176 __CTRACE(__CTRACE_WINDOW, "resize: win->begy = %d\n", win->begy);
177 __CTRACE(__CTRACE_WINDOW, "resize: win->begx = %d\n", win->begx);
178 __CTRACE(__CTRACE_WINDOW, "resize: win->scr_t = %d\n", win->scr_t);
179 __CTRACE(__CTRACE_WINDOW, "resize: win->scr_b = %d\n", win->scr_b);
180 #endif
181
182 /*
183 * free up any non-spacing storage before we lose the
184 * pointers...
185 */
186 #ifdef HAVE_WCHAR
187 __cursesi_win_free_nsp(win);
188 #endif
189
190 if (nlines <= 0 || ncols <= 0)
191 nlines = ncols = 0;
192 else {
193 /* Reallocate line pointer array and line space. */
194 newlines = realloc(win->lines, nlines * sizeof(__LINE *));
195 if (newlines == NULL)
196 return ERR;
197 win->lines = newlines;
198
199 newlspace = realloc(win->lspace, nlines * sizeof(__LINE));
200 if (newlspace == NULL)
201 return ERR;
202 win->lspace = newlspace;
203 }
204
205 /* Don't allocate window and line space if it's a subwindow */
206 if (win->orig == NULL) {
207 /*
208 * Allocate window space in one chunk.
209 */
210 if (ncols != 0) {
211 newwspace = realloc(win->wspace,
212 ncols * nlines * sizeof(__LDATA));
213 if (newwspace == NULL)
214 return ERR;
215 win->wspace = newwspace;
216 }
217
218 /*
219 * Point line pointers to line space, and lines themselves into
220 * window space.
221 */
222 for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
223 win->lines[i] = lp;
224 lp->line = &win->wspace[i * ncols];
225 #ifdef DEBUG
226 lp->sentinel = SENTINEL_VALUE;
227 #endif
228 lp->firstchp = &lp->firstch;
229 lp->lastchp = &lp->lastch;
230 lp->firstch = 0;
231 lp->lastch = ncols - 1;
232 lp->flags = __ISDIRTY;
233 }
234 } else {
235
236 win->ch_off = win->begx - win->orig->begx;
237 /* Point line pointers to line space. */
238 for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
239 win->lines[i] = lp;
240 olp = win->orig->lines[i + win->begy - win->orig->begy];
241 lp->line = &olp->line[win->ch_off];
242 #ifdef DEBUG
243 lp->sentinel = SENTINEL_VALUE;
244 #endif
245 lp->firstchp = &olp->firstch;
246 lp->lastchp = &olp->lastch;
247 lp->flags = __ISDIRTY;
248 }
249 }
250
251
252 win->cury = win->curx = 0;
253 win->maxy = nlines;
254 win->maxx = ncols;
255 win->scr_b = win->maxy - 1;
256 __swflags(win);
257
258 /*
259 * we must zot the window contents otherwise lines may pick
260 * up attributes from the previous line when the window is
261 * made smaller. The client will redraw the window anyway
262 * so this is no big deal.
263 */
264 for (i = 0; i < win->maxy; i++) {
265 lp = win->lines[i];
266 for (sp = lp->line, j = 0; j < win->maxx; j++, sp++) {
267 sp->attr = 0;
268 #ifndef HAVE_WCHAR
269 sp->ch = win->bch;
270 #else
271 sp->ch = ( wchar_t )btowc(( int ) win->bch );
272 sp->nsp = NULL;
273 if (_cursesi_copy_nsp(win->bnsp, sp) == ERR)
274 return ERR;
275 SET_WCOL( *sp, 1 );
276 #endif /* HAVE_WCHAR */
277 }
278 lp->hash = __hash((char *)(void *)lp->line,
279 (size_t) (ncols * __LDATASIZE));
280 }
281
282 #ifdef DEBUG
283 __CTRACE(__CTRACE_WINDOW, "resize: win->wattr = %08x\n", win->wattr);
284 __CTRACE(__CTRACE_WINDOW, "resize: win->flags = %#.4x\n", win->flags);
285 __CTRACE(__CTRACE_WINDOW, "resize: win->maxy = %d\n", win->maxy);
286 __CTRACE(__CTRACE_WINDOW, "resize: win->maxx = %d\n", win->maxx);
287 __CTRACE(__CTRACE_WINDOW, "resize: win->begy = %d\n", win->begy);
288 __CTRACE(__CTRACE_WINDOW, "resize: win->begx = %d\n", win->begx);
289 __CTRACE(__CTRACE_WINDOW, "resize: win->scr_t = %d\n", win->scr_t);
290 __CTRACE(__CTRACE_WINDOW, "resize: win->scr_b = %d\n", win->scr_b);
291 #endif
292
293 if (win->orig == NULL) {
294 /* bound subwindows to new size and fixup their pointers */
295 for (swin = win->nextp; swin != win; swin = swin->nextp) {
296 y = swin->reqy;
297 if (swin->begy + y > win->begy + win->maxy)
298 y = 0;
299 if (y <= 0)
300 y += win->begy + win->maxy - swin->begy;
301 x = swin->reqx;
302 if (swin->begx + x > win->begx + win->maxx)
303 x = 0;
304 if (x <= 0)
305 x += win->begy + win->maxx - swin->begx;
306 __resizewin(swin, y, x);
307 }
308 }
309
310 return OK;
311 }
312