resize.c revision 1.12.6.1 1 /* $NetBSD: resize.c,v 1.12.6.1 2007/01/21 11:38:59 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.1 2007/01/21 11:38:59 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("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("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 - win->orig->begy];
233 lp->line = &olp->line[win->ch_off];
234 #ifdef DEBUG
235 lp->sentinel = SENTINEL_VALUE;
236 #endif
237 lp->firstchp = &olp->firstch;
238 lp->lastchp = &olp->lastch;
239 lp->flags = __ISDIRTY;
240 }
241 }
242
243
244 win->cury = win->curx = 0;
245 win->maxy = nlines;
246 win->maxx = ncols;
247 win->scr_b = win->maxy - 1;
248 __swflags(win);
249
250 /*
251 * we must zot the window contents otherwise lines may pick
252 * up attributes from the previous line when the window is
253 * made smaller. The client will redraw the window anyway
254 * so this is no big deal.
255 */
256 for (i = 0; i < win->maxy; i++) {
257 lp = win->lines[i];
258 for (sp = lp->line, j = 0; j < win->maxx; j++, sp++) {
259 #ifndef HAVE_WCHAR
260 sp->ch = win->bch; /* XXX */
261 #else
262 sp->ch = ( wchar_t )btowc(( int ) win->bch );
263 if (_cursesi_copy_nsp(win->bnsp, sp) == ERR)
264 return ERR;
265 SET_WCOL( *sp, 1 );
266 #endif /* HAVE_WCHAR */
267 sp->attr = 0;
268 }
269 lp->hash = __hash((char *)(void *)lp->line,
270 (size_t) (ncols * __LDATASIZE));
271 }
272
273 #ifdef DEBUG
274 __CTRACE("resize: win->wattr = %08x\n", win->wattr);
275 __CTRACE("resize: win->flags = %#.4x\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
284 if (win->orig == NULL) {
285 /* bound subwindows to new size and fixup their pointers */
286 for (swin = win->nextp; swin != win; swin = swin->nextp) {
287 y = swin->reqy;
288 if (swin->begy + y > win->begy + win->maxy)
289 y = 0;
290 if (y <= 0)
291 y += win->begy + win->maxy - swin->begy;
292 x = swin->reqx;
293 if (swin->begx + x > win->begx + win->maxx)
294 x = 0;
295 if (x <= 0)
296 x += win->begy + win->maxx - swin->begx;
297 __resizewin(swin, y, x);
298 }
299 }
300
301 return OK;
302 }
303