1*51ffecc1SBen Gras /* $NetBSD: resize.c,v 1.20 2009/07/22 16:57:15 roy Exp $ */
2*51ffecc1SBen Gras
3*51ffecc1SBen Gras /*
4*51ffecc1SBen Gras * Copyright (c) 2001
5*51ffecc1SBen Gras * Brett Lymn.
6*51ffecc1SBen Gras *
7*51ffecc1SBen Gras * This code has been donated to The NetBSD Foundation by the Author.
8*51ffecc1SBen Gras *
9*51ffecc1SBen Gras * Redistribution and use in source and binary forms, with or without
10*51ffecc1SBen Gras * modification, are permitted provided that the following conditions
11*51ffecc1SBen Gras * are met:
12*51ffecc1SBen Gras * 1. Redistributions of source code must retain the above copyright
13*51ffecc1SBen Gras * notice, this list of conditions and the following disclaimer.
14*51ffecc1SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
15*51ffecc1SBen Gras * notice, this list of conditions and the following disclaimer in the
16*51ffecc1SBen Gras * documentation and/or other materials provided with the distribution.
17*51ffecc1SBen Gras * 3. All advertising materials mentioning features or use of this software
18*51ffecc1SBen Gras * must display the following acknowledgement:
19*51ffecc1SBen Gras * This product includes software developed by the University of
20*51ffecc1SBen Gras * California, Berkeley and its contributors.
21*51ffecc1SBen Gras * 4. Neither the name of the University nor the names of its contributors
22*51ffecc1SBen Gras * may be used to endorse or promote products derived from this software
23*51ffecc1SBen Gras * without specific prior written permission.
24*51ffecc1SBen Gras *
25*51ffecc1SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26*51ffecc1SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27*51ffecc1SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28*51ffecc1SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29*51ffecc1SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30*51ffecc1SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31*51ffecc1SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32*51ffecc1SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33*51ffecc1SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34*51ffecc1SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35*51ffecc1SBen Gras * SUCH DAMAGE.
36*51ffecc1SBen Gras */
37*51ffecc1SBen Gras
38*51ffecc1SBen Gras #include <sys/cdefs.h>
39*51ffecc1SBen Gras #ifndef lint
40*51ffecc1SBen Gras #if 0
41*51ffecc1SBen Gras static char sccsid[] = "@(#)resize.c blymn 2001/08/26";
42*51ffecc1SBen Gras #else
43*51ffecc1SBen Gras __RCSID("$NetBSD: resize.c,v 1.20 2009/07/22 16:57:15 roy Exp $");
44*51ffecc1SBen Gras #endif
45*51ffecc1SBen Gras #endif /* not lint */
46*51ffecc1SBen Gras
47*51ffecc1SBen Gras #include <stdlib.h>
48*51ffecc1SBen Gras
49*51ffecc1SBen Gras #include "curses.h"
50*51ffecc1SBen Gras #include "curses_private.h"
51*51ffecc1SBen Gras
52*51ffecc1SBen Gras static int __resizeterm(WINDOW *win, int nlines, int ncols);
53*51ffecc1SBen Gras static int __resizewin(WINDOW *win, int nlines, int ncols);
54*51ffecc1SBen Gras
55*51ffecc1SBen Gras /*
56*51ffecc1SBen Gras * wresize --
57*51ffecc1SBen Gras * Resize the given window to the new size.
58*51ffecc1SBen Gras */
59*51ffecc1SBen Gras int
wresize(WINDOW * win,int req_nlines,int req_ncols)60*51ffecc1SBen Gras wresize(WINDOW *win, int req_nlines, int req_ncols)
61*51ffecc1SBen Gras {
62*51ffecc1SBen Gras int nlines = req_nlines;
63*51ffecc1SBen Gras int ncols = req_ncols;
64*51ffecc1SBen Gras
65*51ffecc1SBen Gras if (win == NULL)
66*51ffecc1SBen Gras return ERR;
67*51ffecc1SBen Gras
68*51ffecc1SBen Gras #ifdef DEBUG
69*51ffecc1SBen Gras __CTRACE(__CTRACE_WINDOW, "wresize: (%p, %d, %d)\n",
70*51ffecc1SBen Gras win, nlines, ncols);
71*51ffecc1SBen Gras #endif
72*51ffecc1SBen Gras if (win->orig == NULL) {
73*51ffecc1SBen Gras /* bound "our" windows by the screen size */
74*51ffecc1SBen Gras if (win == curscr || win == __virtscr || win == stdscr) {
75*51ffecc1SBen Gras if (nlines > LINES)
76*51ffecc1SBen Gras nlines = LINES;
77*51ffecc1SBen Gras if (nlines < 1)
78*51ffecc1SBen Gras nlines = 1;
79*51ffecc1SBen Gras if (ncols > COLS)
80*51ffecc1SBen Gras ncols = COLS;
81*51ffecc1SBen Gras if (ncols < 1)
82*51ffecc1SBen Gras ncols = 1;
83*51ffecc1SBen Gras } else {
84*51ffecc1SBen Gras if (win->begy > LINES)
85*51ffecc1SBen Gras win->begy = 0;
86*51ffecc1SBen Gras if (win->begy + nlines > LINES)
87*51ffecc1SBen Gras nlines = 0;
88*51ffecc1SBen Gras if (nlines <= 0)
89*51ffecc1SBen Gras nlines += LINES - win->begy;
90*51ffecc1SBen Gras if (nlines < 1)
91*51ffecc1SBen Gras nlines = 1;
92*51ffecc1SBen Gras if (win->begx > COLS)
93*51ffecc1SBen Gras win->begx = 0;
94*51ffecc1SBen Gras if (win->begx + ncols > COLS)
95*51ffecc1SBen Gras ncols = 0;
96*51ffecc1SBen Gras if (ncols <= 0)
97*51ffecc1SBen Gras ncols += COLS - win->begx;
98*51ffecc1SBen Gras if (ncols < 1)
99*51ffecc1SBen Gras ncols = 1;
100*51ffecc1SBen Gras }
101*51ffecc1SBen Gras } else {
102*51ffecc1SBen Gras /* subwins must fit inside the parent - check this */
103*51ffecc1SBen Gras if (win->begy > win->orig->begy + win->orig->maxy)
104*51ffecc1SBen Gras win->begy = win->orig->begy + win->orig->maxy - 1;
105*51ffecc1SBen Gras if (win->begy + nlines > win->orig->begy + win->orig->maxy)
106*51ffecc1SBen Gras nlines = 0;
107*51ffecc1SBen Gras if (nlines <= 0)
108*51ffecc1SBen Gras nlines += win->orig->begy + win->orig->maxy - win->begy;
109*51ffecc1SBen Gras if (nlines < 1)
110*51ffecc1SBen Gras nlines = 1;
111*51ffecc1SBen Gras if (win->begx > win->orig->begx + win->orig->maxx)
112*51ffecc1SBen Gras win->begx = win->orig->begx + win->orig->maxx - 1;
113*51ffecc1SBen Gras if (win->begx + ncols > win->orig->begx + win->orig->maxx)
114*51ffecc1SBen Gras ncols = 0;
115*51ffecc1SBen Gras if (ncols <= 0)
116*51ffecc1SBen Gras ncols += win->orig->begx + win->orig->maxx - win->begx;
117*51ffecc1SBen Gras if (ncols < 1)
118*51ffecc1SBen Gras ncols = 1;
119*51ffecc1SBen Gras }
120*51ffecc1SBen Gras
121*51ffecc1SBen Gras if ((__resizewin(win, nlines, ncols)) == ERR)
122*51ffecc1SBen Gras return ERR;
123*51ffecc1SBen Gras
124*51ffecc1SBen Gras win->reqy = req_nlines;
125*51ffecc1SBen Gras win->reqx = req_ncols;
126*51ffecc1SBen Gras
127*51ffecc1SBen Gras /* If someone resizes curscr, we must also resize __virtscr */
128*51ffecc1SBen Gras if (win == curscr) {
129*51ffecc1SBen Gras if ((__resizewin(__virtscr, nlines, ncols)) == ERR)
130*51ffecc1SBen Gras return ERR;
131*51ffecc1SBen Gras __virtscr->reqy = req_nlines;
132*51ffecc1SBen Gras __virtscr->reqx = req_ncols;
133*51ffecc1SBen Gras }
134*51ffecc1SBen Gras
135*51ffecc1SBen Gras return OK;
136*51ffecc1SBen Gras }
137*51ffecc1SBen Gras
138*51ffecc1SBen Gras /*
139*51ffecc1SBen Gras * resizeterm --
140*51ffecc1SBen Gras * Resize the terminal window, resizing the dependent windows.
141*51ffecc1SBen Gras */
142*51ffecc1SBen Gras int
resizeterm(int nlines,int ncols)143*51ffecc1SBen Gras resizeterm(int nlines, int ncols)
144*51ffecc1SBen Gras {
145*51ffecc1SBen Gras WINDOW *win;
146*51ffecc1SBen Gras struct __winlist *list;
147*51ffecc1SBen Gras
148*51ffecc1SBen Gras #ifdef DEBUG
149*51ffecc1SBen Gras __CTRACE(__CTRACE_WINDOW, "resizeterm: (%d, %d)\n", nlines, ncols);
150*51ffecc1SBen Gras #endif
151*51ffecc1SBen Gras
152*51ffecc1SBen Gras if (__resizeterm(curscr, nlines, ncols) == ERR)
153*51ffecc1SBen Gras return ERR;
154*51ffecc1SBen Gras if (__resizeterm(__virtscr, nlines, ncols) == ERR)
155*51ffecc1SBen Gras return ERR;
156*51ffecc1SBen Gras if (__resizeterm(stdscr, nlines, ncols) == ERR)
157*51ffecc1SBen Gras return ERR;
158*51ffecc1SBen Gras
159*51ffecc1SBen Gras LINES = nlines;
160*51ffecc1SBen Gras COLS = ncols;
161*51ffecc1SBen Gras
162*51ffecc1SBen Gras /* tweak the flags now that we have updated the LINES and COLS */
163*51ffecc1SBen Gras for (list = _cursesi_screen->winlistp; list != NULL; list = list->nextp) {
164*51ffecc1SBen Gras win = list->winp;
165*51ffecc1SBen Gras
166*51ffecc1SBen Gras if (!(win->flags & __ISPAD))
167*51ffecc1SBen Gras __swflags(win);
168*51ffecc1SBen Gras }
169*51ffecc1SBen Gras
170*51ffecc1SBen Gras wrefresh(curscr);
171*51ffecc1SBen Gras return OK;
172*51ffecc1SBen Gras }
173*51ffecc1SBen Gras
174*51ffecc1SBen Gras /*
175*51ffecc1SBen Gras * __resizeterm
176*51ffecc1SBen Gras * Setup window for resizing.
177*51ffecc1SBen Gras */
178*51ffecc1SBen Gras static int
__resizeterm(WINDOW * win,int nlines,int ncols)179*51ffecc1SBen Gras __resizeterm(WINDOW *win, int nlines, int ncols)
180*51ffecc1SBen Gras {
181*51ffecc1SBen Gras int newlines, newcols;
182*51ffecc1SBen Gras
183*51ffecc1SBen Gras newlines = win->reqy;
184*51ffecc1SBen Gras if (win->begy + newlines >= nlines)
185*51ffecc1SBen Gras newlines = 0;
186*51ffecc1SBen Gras if (newlines == 0)
187*51ffecc1SBen Gras newlines = nlines - win->begy;
188*51ffecc1SBen Gras
189*51ffecc1SBen Gras newcols = win->reqx;
190*51ffecc1SBen Gras if (win->begx + newcols >= ncols)
191*51ffecc1SBen Gras newcols = 0;
192*51ffecc1SBen Gras if (newcols == 0)
193*51ffecc1SBen Gras newcols = ncols - win->begx;
194*51ffecc1SBen Gras
195*51ffecc1SBen Gras return __resizewin(win, newlines, newcols);
196*51ffecc1SBen Gras }
197*51ffecc1SBen Gras
198*51ffecc1SBen Gras /*
199*51ffecc1SBen Gras * __resizewin --
200*51ffecc1SBen Gras * Resize the given window.
201*51ffecc1SBen Gras */
202*51ffecc1SBen Gras static int
__resizewin(WINDOW * win,int nlines,int ncols)203*51ffecc1SBen Gras __resizewin(WINDOW *win, int nlines, int ncols)
204*51ffecc1SBen Gras {
205*51ffecc1SBen Gras __LINE *lp, *olp, **newlines, *newlspace;
206*51ffecc1SBen Gras __LDATA *sp;
207*51ffecc1SBen Gras __LDATA *newwspace;
208*51ffecc1SBen Gras int i, j;
209*51ffecc1SBen Gras int y, x;
210*51ffecc1SBen Gras WINDOW *swin;
211*51ffecc1SBen Gras
212*51ffecc1SBen Gras #ifdef DEBUG
213*51ffecc1SBen Gras __CTRACE(__CTRACE_WINDOW, "resize: (%p, %d, %d)\n", win, nlines, ncols);
214*51ffecc1SBen Gras __CTRACE(__CTRACE_WINDOW, "resize: win->wattr = %08x\n", win->wattr);
215*51ffecc1SBen Gras __CTRACE(__CTRACE_WINDOW, "resize: win->flags = %#.4x\n", win->flags);
216*51ffecc1SBen Gras __CTRACE(__CTRACE_WINDOW, "resize: win->maxy = %d\n", win->maxy);
217*51ffecc1SBen Gras __CTRACE(__CTRACE_WINDOW, "resize: win->maxx = %d\n", win->maxx);
218*51ffecc1SBen Gras __CTRACE(__CTRACE_WINDOW, "resize: win->begy = %d\n", win->begy);
219*51ffecc1SBen Gras __CTRACE(__CTRACE_WINDOW, "resize: win->begx = %d\n", win->begx);
220*51ffecc1SBen Gras __CTRACE(__CTRACE_WINDOW, "resize: win->scr_t = %d\n", win->scr_t);
221*51ffecc1SBen Gras __CTRACE(__CTRACE_WINDOW, "resize: win->scr_b = %d\n", win->scr_b);
222*51ffecc1SBen Gras #endif
223*51ffecc1SBen Gras
224*51ffecc1SBen Gras /*
225*51ffecc1SBen Gras * free up any non-spacing storage before we lose the
226*51ffecc1SBen Gras * pointers...
227*51ffecc1SBen Gras */
228*51ffecc1SBen Gras #ifdef HAVE_WCHAR
229*51ffecc1SBen Gras __cursesi_win_free_nsp(win);
230*51ffecc1SBen Gras #endif
231*51ffecc1SBen Gras
232*51ffecc1SBen Gras if (nlines <= 0 || ncols <= 0)
233*51ffecc1SBen Gras nlines = ncols = 0;
234*51ffecc1SBen Gras else {
235*51ffecc1SBen Gras /* Reallocate line pointer array and line space. */
236*51ffecc1SBen Gras newlines = realloc(win->alines, nlines * sizeof(__LINE *));
237*51ffecc1SBen Gras if (newlines == NULL)
238*51ffecc1SBen Gras return ERR;
239*51ffecc1SBen Gras win->alines = newlines;
240*51ffecc1SBen Gras
241*51ffecc1SBen Gras newlspace = realloc(win->lspace, nlines * sizeof(__LINE));
242*51ffecc1SBen Gras if (newlspace == NULL)
243*51ffecc1SBen Gras return ERR;
244*51ffecc1SBen Gras win->lspace = newlspace;
245*51ffecc1SBen Gras }
246*51ffecc1SBen Gras
247*51ffecc1SBen Gras /* Don't allocate window and line space if it's a subwindow */
248*51ffecc1SBen Gras if (win->orig == NULL) {
249*51ffecc1SBen Gras /*
250*51ffecc1SBen Gras * Allocate window space in one chunk.
251*51ffecc1SBen Gras */
252*51ffecc1SBen Gras if (ncols != 0) {
253*51ffecc1SBen Gras newwspace = realloc(win->wspace,
254*51ffecc1SBen Gras ncols * nlines * sizeof(__LDATA));
255*51ffecc1SBen Gras if (newwspace == NULL)
256*51ffecc1SBen Gras return ERR;
257*51ffecc1SBen Gras win->wspace = newwspace;
258*51ffecc1SBen Gras }
259*51ffecc1SBen Gras
260*51ffecc1SBen Gras /*
261*51ffecc1SBen Gras * Point line pointers to line space, and lines themselves into
262*51ffecc1SBen Gras * window space.
263*51ffecc1SBen Gras */
264*51ffecc1SBen Gras for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
265*51ffecc1SBen Gras win->alines[i] = lp;
266*51ffecc1SBen Gras lp->line = &win->wspace[i * ncols];
267*51ffecc1SBen Gras #ifdef DEBUG
268*51ffecc1SBen Gras lp->sentinel = SENTINEL_VALUE;
269*51ffecc1SBen Gras #endif
270*51ffecc1SBen Gras lp->firstchp = &lp->firstch;
271*51ffecc1SBen Gras lp->lastchp = &lp->lastch;
272*51ffecc1SBen Gras lp->firstch = 0;
273*51ffecc1SBen Gras lp->lastch = ncols - 1;
274*51ffecc1SBen Gras lp->flags = __ISDIRTY;
275*51ffecc1SBen Gras }
276*51ffecc1SBen Gras } else {
277*51ffecc1SBen Gras
278*51ffecc1SBen Gras win->ch_off = win->begx - win->orig->begx;
279*51ffecc1SBen Gras /* Point line pointers to line space. */
280*51ffecc1SBen Gras for (lp = win->lspace, i = 0; i < nlines; i++, lp++) {
281*51ffecc1SBen Gras win->alines[i] = lp;
282*51ffecc1SBen Gras olp = win->orig->alines[i + win->begy - win->orig->begy];
283*51ffecc1SBen Gras lp->line = &olp->line[win->ch_off];
284*51ffecc1SBen Gras #ifdef DEBUG
285*51ffecc1SBen Gras lp->sentinel = SENTINEL_VALUE;
286*51ffecc1SBen Gras #endif
287*51ffecc1SBen Gras lp->firstchp = &olp->firstch;
288*51ffecc1SBen Gras lp->lastchp = &olp->lastch;
289*51ffecc1SBen Gras lp->flags = __ISDIRTY;
290*51ffecc1SBen Gras }
291*51ffecc1SBen Gras }
292*51ffecc1SBen Gras
293*51ffecc1SBen Gras
294*51ffecc1SBen Gras win->cury = win->curx = 0;
295*51ffecc1SBen Gras win->maxy = nlines;
296*51ffecc1SBen Gras win->maxx = ncols;
297*51ffecc1SBen Gras win->scr_b = win->maxy - 1;
298*51ffecc1SBen Gras __swflags(win);
299*51ffecc1SBen Gras
300*51ffecc1SBen Gras /*
301*51ffecc1SBen Gras * we must zot the window contents otherwise lines may pick
302*51ffecc1SBen Gras * up attributes from the previous line when the window is
303*51ffecc1SBen Gras * made smaller. The client will redraw the window anyway
304*51ffecc1SBen Gras * so this is no big deal.
305*51ffecc1SBen Gras */
306*51ffecc1SBen Gras for (i = 0; i < win->maxy; i++) {
307*51ffecc1SBen Gras lp = win->alines[i];
308*51ffecc1SBen Gras for (sp = lp->line, j = 0; j < win->maxx; j++, sp++) {
309*51ffecc1SBen Gras sp->attr = 0;
310*51ffecc1SBen Gras #ifndef HAVE_WCHAR
311*51ffecc1SBen Gras sp->ch = win->bch;
312*51ffecc1SBen Gras #else
313*51ffecc1SBen Gras sp->ch = ( wchar_t )btowc(( int ) win->bch );
314*51ffecc1SBen Gras sp->nsp = NULL;
315*51ffecc1SBen Gras if (_cursesi_copy_nsp(win->bnsp, sp) == ERR)
316*51ffecc1SBen Gras return ERR;
317*51ffecc1SBen Gras SET_WCOL( *sp, 1 );
318*51ffecc1SBen Gras #endif /* HAVE_WCHAR */
319*51ffecc1SBen Gras }
320*51ffecc1SBen Gras lp->hash = __hash((char *)(void *)lp->line,
321*51ffecc1SBen Gras (size_t) (ncols * __LDATASIZE));
322*51ffecc1SBen Gras }
323*51ffecc1SBen Gras
324*51ffecc1SBen Gras #ifdef DEBUG
325*51ffecc1SBen Gras __CTRACE(__CTRACE_WINDOW, "resize: win->wattr = %08x\n", win->wattr);
326*51ffecc1SBen Gras __CTRACE(__CTRACE_WINDOW, "resize: win->flags = %#.4x\n", win->flags);
327*51ffecc1SBen Gras __CTRACE(__CTRACE_WINDOW, "resize: win->maxy = %d\n", win->maxy);
328*51ffecc1SBen Gras __CTRACE(__CTRACE_WINDOW, "resize: win->maxx = %d\n", win->maxx);
329*51ffecc1SBen Gras __CTRACE(__CTRACE_WINDOW, "resize: win->begy = %d\n", win->begy);
330*51ffecc1SBen Gras __CTRACE(__CTRACE_WINDOW, "resize: win->begx = %d\n", win->begx);
331*51ffecc1SBen Gras __CTRACE(__CTRACE_WINDOW, "resize: win->scr_t = %d\n", win->scr_t);
332*51ffecc1SBen Gras __CTRACE(__CTRACE_WINDOW, "resize: win->scr_b = %d\n", win->scr_b);
333*51ffecc1SBen Gras #endif
334*51ffecc1SBen Gras
335*51ffecc1SBen Gras if (win->orig == NULL) {
336*51ffecc1SBen Gras /* bound subwindows to new size and fixup their pointers */
337*51ffecc1SBen Gras for (swin = win->nextp; swin != win; swin = swin->nextp) {
338*51ffecc1SBen Gras y = swin->reqy;
339*51ffecc1SBen Gras if (swin->begy > win->begy + win->maxy)
340*51ffecc1SBen Gras swin->begy = win->begy + win->maxy - 1;
341*51ffecc1SBen Gras if (swin->begy + y > win->begy + win->maxy)
342*51ffecc1SBen Gras y = 0;
343*51ffecc1SBen Gras if (y <= 0)
344*51ffecc1SBen Gras y += win->begy + win->maxy - swin->begy;
345*51ffecc1SBen Gras if (y < 1)
346*51ffecc1SBen Gras y = 1;
347*51ffecc1SBen Gras x = swin->reqx;
348*51ffecc1SBen Gras if (swin->begx > win->begx + win->maxx)
349*51ffecc1SBen Gras swin->begx = win->begx + win->maxx - 1;
350*51ffecc1SBen Gras if (swin->begx + x > win->begx + win->maxx)
351*51ffecc1SBen Gras x = 0;
352*51ffecc1SBen Gras if (x <= 0)
353*51ffecc1SBen Gras x += win->begy + win->maxx - swin->begx;
354*51ffecc1SBen Gras if (x < 1)
355*51ffecc1SBen Gras x = 1;
356*51ffecc1SBen Gras __resizewin(swin, y, x);
357*51ffecc1SBen Gras }
358*51ffecc1SBen Gras }
359*51ffecc1SBen Gras
360*51ffecc1SBen Gras return OK;
361*51ffecc1SBen Gras }
362