150276Speter/****************************************************************************
2262685Sdelphij * Copyright (c) 1998-2010,2011 Free Software Foundation, Inc.              *
350276Speter *                                                                          *
450276Speter * Permission is hereby granted, free of charge, to any person obtaining a  *
550276Speter * copy of this software and associated documentation files (the            *
650276Speter * "Software"), to deal in the Software without restriction, including      *
750276Speter * without limitation the rights to use, copy, modify, merge, publish,      *
850276Speter * distribute, distribute with modifications, sublicense, and/or sell       *
950276Speter * copies of the Software, and to permit persons to whom the Software is    *
1050276Speter * furnished to do so, subject to the following conditions:                 *
1150276Speter *                                                                          *
1250276Speter * The above copyright notice and this permission notice shall be included  *
1350276Speter * in all copies or substantial portions of the Software.                   *
1450276Speter *                                                                          *
1550276Speter * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1650276Speter * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
1750276Speter * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
1850276Speter * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
1950276Speter * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2050276Speter * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2150276Speter * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2250276Speter *                                                                          *
2350276Speter * Except as contained in this notice, the name(s) of the above copyright   *
2450276Speter * holders shall not be used in advertising or otherwise to promote the     *
2550276Speter * sale, use or other dealings in this Software without prior written       *
2650276Speter * authorization.                                                           *
2750276Speter ****************************************************************************/
2850276Speter
2950276Speter/****************************************************************************
30262629Sdelphij *  Author: Thomas E. Dickey 1996-on                                        *
31262629Sdelphij *     and: Juergen Pfeifer                                                 *
3250276Speter ****************************************************************************/
3350276Speter
3450276Speter#include <curses.priv.h>
3550276Speter
36262685SdelphijMODULE_ID("$Id: wresize.c,v 1.35 2011/05/21 18:55:07 tom Exp $")
3750276Speter
38166124Srafanstatic int
39166124Srafancleanup_lines(struct ldat *data, int length)
40166124Srafan{
41166124Srafan    while (--length >= 0)
42174993Srafan	free(data[length].text);
43166124Srafan    free(data);
44166124Srafan    return ERR;
45166124Srafan}
46166124Srafan
4750276Speter/*
48166124Srafan * If we have reallocated the ldat structs, we will have to repair pointers
49166124Srafan * used in subwindows.
50166124Srafan */
51166124Srafanstatic void
52166124Srafanrepair_subwindows(WINDOW *cmp)
53166124Srafan{
54166124Srafan    WINDOWLIST *wp;
55166124Srafan    struct ldat *pline = cmp->_line;
56166124Srafan    int row;
57262629Sdelphij#ifdef USE_SP_WINDOWLIST
58262629Sdelphij    SCREEN *sp = _nc_screen_of(cmp);
59262629Sdelphij#endif
60166124Srafan
61184989Srafan    _nc_lock_global(curses);
62174993Srafan
63262629Sdelphij    for (each_window(SP_PARM, wp)) {
64166124Srafan	WINDOW *tst = &(wp->win);
65166124Srafan
66166124Srafan	if (tst->_parent == cmp) {
67166124Srafan
68166124Srafan	    if (tst->_pary > cmp->_maxy)
69166124Srafan		tst->_pary = cmp->_maxy;
70166124Srafan	    if (tst->_parx > cmp->_maxx)
71166124Srafan		tst->_parx = cmp->_maxx;
72166124Srafan
73166124Srafan	    if (tst->_maxy + tst->_pary > cmp->_maxy)
74262629Sdelphij		tst->_maxy = (NCURSES_SIZE_T) (cmp->_maxy - tst->_pary);
75166124Srafan	    if (tst->_maxx + tst->_parx > cmp->_maxx)
76262629Sdelphij		tst->_maxx = (NCURSES_SIZE_T) (cmp->_maxx - tst->_parx);
77166124Srafan
78166124Srafan	    for (row = 0; row <= tst->_maxy; ++row) {
79166124Srafan		tst->_line[row].text = &pline[tst->_pary + row].text[tst->_parx];
80166124Srafan	    }
81166124Srafan	    repair_subwindows(tst);
82166124Srafan	}
83166124Srafan    }
84184989Srafan    _nc_unlock_global(curses);
85166124Srafan}
86166124Srafan
87166124Srafan/*
8850276Speter * Reallocate a curses WINDOW struct to either shrink or grow to the specified
8950276Speter * new lines/columns.  If it grows, the new character cells are filled with
9050276Speter * blanks.  The application is responsible for repainting the blank area.
9150276Speter */
9276726SpeterNCURSES_EXPORT(int)
9350276Speterwresize(WINDOW *win, int ToLines, int ToCols)
9450276Speter{
95166124Srafan    int col, row, size_x, size_y;
9662449Speter    struct ldat *pline;
97166124Srafan    struct ldat *new_lines = 0;
9850276Speter
9950276Speter#ifdef TRACE
100262629Sdelphij    T((T_CALLED("wresize(%p,%d,%d)"), (void *) win, ToLines, ToCols));
10162449Speter    if (win) {
102166124Srafan	TR(TRACE_UPDATE, ("...beg (%ld, %ld), max(%ld,%ld), reg(%ld,%ld)",
103166124Srafan			  (long) win->_begy, (long) win->_begx,
104166124Srafan			  (long) win->_maxy, (long) win->_maxx,
105166124Srafan			  (long) win->_regtop, (long) win->_regbottom));
106174993Srafan	if (USE_TRACEF(TRACE_UPDATE)) {
10750276Speter	    _tracedump("...before", win);
108174993Srafan	    _nc_unlock_global(tracef);
109174993Srafan	}
11062449Speter    }
11150276Speter#endif
11250276Speter
11362449Speter    if (!win || --ToLines < 0 || --ToCols < 0)
11462449Speter	returnCode(ERR);
11550276Speter
11662449Speter    size_x = win->_maxx;
11762449Speter    size_y = win->_maxy;
11850276Speter
11962449Speter    if (ToLines == size_y
12062449Speter	&& ToCols == size_x)
12162449Speter	returnCode(OK);
12250276Speter
12362449Speter    if ((win->_flags & _SUBWIN)) {
12450276Speter	/*
12562449Speter	 * Check if the new limits will fit into the parent window's size.  If
12662449Speter	 * not, do not resize.  We could adjust the location of the subwindow,
12762449Speter	 * but the application may not like that.
12850276Speter	 */
12962449Speter	if (win->_pary + ToLines > win->_parent->_maxy
13062449Speter	    || win->_parx + ToCols > win->_parent->_maxx) {
13162449Speter	    returnCode(ERR);
13262449Speter	}
13362449Speter	pline = win->_parent->_line;
13462449Speter    } else {
13562449Speter	pline = 0;
13662449Speter    }
13750276Speter
13862449Speter    /*
139166124Srafan     * Allocate new memory as needed.  Do the allocations without modifying
140166124Srafan     * the original window, in case an allocation fails.  Always allocate
141166124Srafan     * (at least temporarily) the array pointing to the individual lines.
14262449Speter     */
143166124Srafan    new_lines = typeCalloc(struct ldat, (unsigned) (ToLines + 1));
144166124Srafan    if (new_lines == 0)
145166124Srafan	returnCode(ERR);
14650276Speter
14762449Speter    /*
148166124Srafan     * For each line in the target, allocate or adjust pointers for the
149166124Srafan     * corresponding text, depending on whether this is a window or a
150166124Srafan     * subwindow.
15162449Speter     */
152166124Srafan    for (row = 0; row <= ToLines; ++row) {
153166124Srafan	int begin = (row > size_y) ? 0 : (size_x + 1);
15462449Speter	int end = ToCols;
155166124Srafan	NCURSES_CH_T *s;
15650276Speter
157166124Srafan	if (!(win->_flags & _SUBWIN)) {
158166124Srafan	    if (row <= size_y) {
159166124Srafan		if (ToCols != size_x) {
160262629Sdelphij		    s = typeMalloc(NCURSES_CH_T, (unsigned) ToCols + 1);
161262629Sdelphij		    if (s == 0)
162166124Srafan			returnCode(cleanup_lines(new_lines, row));
163166124Srafan		    for (col = 0; col <= ToCols; ++col) {
164166124Srafan			s[col] = (col <= size_x
165166124Srafan				  ? win->_line[row].text[col]
166166124Srafan				  : win->_nc_bkgd);
167166124Srafan		    }
168166124Srafan		} else {
169166124Srafan		    s = win->_line[row].text;
170166124Srafan		}
17197049Speter	    } else {
172262629Sdelphij		s = typeMalloc(NCURSES_CH_T, (unsigned) ToCols + 1);
173262629Sdelphij		if (s == 0)
174166124Srafan		    returnCode(cleanup_lines(new_lines, row));
175166124Srafan		for (col = 0; col <= ToCols; ++col)
176166124Srafan		    s[col] = win->_nc_bkgd;
17762449Speter	    }
178262685Sdelphij	} else if (pline != 0 && pline[win->_pary + row].text != 0) {
179262685Sdelphij	    s = &pline[win->_pary + row].text[win->_parx];
180166124Srafan	} else {
181262685Sdelphij	    s = 0;
182166124Srafan	}
18350276Speter
184166124Srafan	if_USE_SCROLL_HINTS(new_lines[row].oldindex = row);
185166124Srafan	if (row <= size_y) {
186166124Srafan	    new_lines[row].firstchar = win->_line[row].firstchar;
187166124Srafan	    new_lines[row].lastchar = win->_line[row].lastchar;
188166124Srafan	}
189166124Srafan	if ((ToCols != size_x) || (row > size_y)) {
19062449Speter	    if (end >= begin) {	/* growing */
191166124Srafan		if (new_lines[row].firstchar < begin)
192262629Sdelphij		    new_lines[row].firstchar = (NCURSES_SIZE_T) begin;
19362449Speter	    } else {		/* shrinking */
194166124Srafan		new_lines[row].firstchar = 0;
19562449Speter	    }
196262629Sdelphij	    new_lines[row].lastchar = (NCURSES_SIZE_T) ToCols;
19750276Speter	}
198166124Srafan	new_lines[row].text = s;
19962449Speter    }
20050276Speter
20162449Speter    /*
202166124Srafan     * Dispose of unwanted memory.
203166124Srafan     */
204166124Srafan    if (!(win->_flags & _SUBWIN)) {
205166124Srafan	if (ToCols == size_x) {
206166124Srafan	    for (row = ToLines + 1; row <= size_y; row++) {
207166124Srafan		free(win->_line[row].text);
208166124Srafan	    }
209166124Srafan	} else {
210166124Srafan	    for (row = 0; row <= size_y; row++) {
211166124Srafan		free(win->_line[row].text);
212166124Srafan	    }
213166124Srafan	}
214166124Srafan    }
215166124Srafan
216166124Srafan    free(win->_line);
217166124Srafan    win->_line = new_lines;
218166124Srafan
219166124Srafan    /*
22062449Speter     * Finally, adjust the parameters showing screen size and cursor
22162449Speter     * position:
22262449Speter     */
223262629Sdelphij    win->_maxx = (NCURSES_SIZE_T) ToCols;
224262629Sdelphij    win->_maxy = (NCURSES_SIZE_T) ToLines;
22550276Speter
22662449Speter    if (win->_regtop > win->_maxy)
22762449Speter	win->_regtop = win->_maxy;
22862449Speter    if (win->_regbottom > win->_maxy
22962449Speter	|| win->_regbottom == size_y)
23062449Speter	win->_regbottom = win->_maxy;
23150276Speter
23262449Speter    if (win->_curx > win->_maxx)
23362449Speter	win->_curx = win->_maxx;
23462449Speter    if (win->_cury > win->_maxy)
23562449Speter	win->_cury = win->_maxy;
23650276Speter
237166124Srafan    /*
238166124Srafan     * Check for subwindows of this one, and readjust pointers to our text,
239166124Srafan     * if needed.
240166124Srafan     */
241166124Srafan    repair_subwindows(win);
242166124Srafan
24350276Speter#ifdef TRACE
244166124Srafan    TR(TRACE_UPDATE, ("...beg (%ld, %ld), max(%ld,%ld), reg(%ld,%ld)",
245166124Srafan		      (long) win->_begy, (long) win->_begx,
246166124Srafan		      (long) win->_maxy, (long) win->_maxx,
247166124Srafan		      (long) win->_regtop, (long) win->_regbottom));
248174993Srafan    if (USE_TRACEF(TRACE_UPDATE)) {
24962449Speter	_tracedump("...after:", win);
250174993Srafan	_nc_unlock_global(tracef);
251174993Srafan    }
25250276Speter#endif
25362449Speter    returnCode(OK);
25450276Speter}
255