wresize.c revision 50276
1184588Sdfr/****************************************************************************
2184588Sdfr * Copyright (c) 1998 Free Software Foundation, Inc.                        *
3184588Sdfr *                                                                          *
4184588Sdfr * Permission is hereby granted, free of charge, to any person obtaining a  *
5184588Sdfr * copy of this software and associated documentation files (the            *
6184588Sdfr * "Software"), to deal in the Software without restriction, including      *
7184588Sdfr * without limitation the rights to use, copy, modify, merge, publish,      *
8184588Sdfr * distribute, distribute with modifications, sublicense, and/or sell       *
9184588Sdfr * copies of the Software, and to permit persons to whom the Software is    *
10184588Sdfr * furnished to do so, subject to the following conditions:                 *
11184588Sdfr *                                                                          *
12184588Sdfr * The above copyright notice and this permission notice shall be included  *
13184588Sdfr * in all copies or substantial portions of the Software.                   *
14184588Sdfr *                                                                          *
15184588Sdfr * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16184588Sdfr * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17184588Sdfr * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18184588Sdfr * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19184588Sdfr * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20184588Sdfr * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21184588Sdfr * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22184588Sdfr *                                                                          *
23184588Sdfr * Except as contained in this notice, the name(s) of the above copyright   *
24184588Sdfr * holders shall not be used in advertising or otherwise to promote the     *
25184588Sdfr * sale, use or other dealings in this Software without prior written       *
26184588Sdfr * authorization.                                                           *
27184588Sdfr ****************************************************************************/
28184588Sdfr
29184588Sdfr/****************************************************************************
30184588Sdfr *  Author: Thomas E. Dickey <dickey@clark.net> 1996,1997                   *
31184588Sdfr ****************************************************************************/
32184588Sdfr
33184588Sdfr#include <curses.priv.h>
34184588Sdfr
35184588SdfrMODULE_ID("$Id: wresize.c,v 1.12 1999/02/27 18:57:31 tom Exp $")
36184588Sdfr
37184588Sdfr/*
38184588Sdfr * Reallocate a curses WINDOW struct to either shrink or grow to the specified
39184588Sdfr * new lines/columns.  If it grows, the new character cells are filled with
40184588Sdfr * blanks.  The application is responsible for repainting the blank area.
41184588Sdfr */
42184588Sdfr
43184588Sdfr#define DOALLOC(p,t,n)  typeRealloc(t, n, p)
44184588Sdfr#define	ld_ALLOC(p,n)	DOALLOC(p,struct ldat,n)
45184588Sdfr#define	c_ALLOC(p,n)	DOALLOC(p,chtype,n)
46184588Sdfr
47184588Sdfrint
48184588Sdfrwresize(WINDOW *win, int ToLines, int ToCols)
49184588Sdfr{
50184588Sdfr	register int row;
51184588Sdfr	int size_x, size_y;
52184588Sdfr	struct ldat *pline;
53184588Sdfr	chtype blank;
54184588Sdfr
55184588Sdfr#ifdef TRACE
56184588Sdfr	T((T_CALLED("wresize(%p,%d,%d)"), win, ToLines, ToCols));
57184588Sdfr	if (win) {
58184588Sdfr	  TR(TRACE_UPDATE, ("...beg (%d, %d), max(%d,%d), reg(%d,%d)",
59184588Sdfr			    win->_begy, win->_begx,
60184588Sdfr			    win->_maxy, win->_maxx,
61184588Sdfr			    win->_regtop, win->_regbottom));
62184588Sdfr	  if (_nc_tracing & TRACE_UPDATE)
63184588Sdfr	    _tracedump("...before", win);
64184588Sdfr	}
65184588Sdfr#endif
66184588Sdfr
67184588Sdfr	if (!win || --ToLines < 0 || --ToCols < 0)
68184588Sdfr		returnCode(ERR);
69184588Sdfr
70184588Sdfr	size_x = win->_maxx;
71184588Sdfr	size_y = win->_maxy;
72184588Sdfr
73184588Sdfr	if (ToLines == size_y
74184588Sdfr	 && ToCols  == size_x)
75184588Sdfr		returnCode(OK);
76184588Sdfr
77184588Sdfr	pline  = (win->_flags & _SUBWIN) ? win->_parent->_line : 0;
78184588Sdfr
79184588Sdfr	/*
80184588Sdfr	 * If the number of lines has changed, adjust the size of the overall
81184588Sdfr	 * vector:
82184588Sdfr	 */
83184588Sdfr	if (ToLines != size_y) {
84184588Sdfr		if (! (win->_flags & _SUBWIN)) {
85184588Sdfr			for (row = ToLines+1; row <= size_y; row++)
86184588Sdfr				free((char *)(win->_line[row].text));
87184588Sdfr		}
88184588Sdfr
89184588Sdfr		win->_line = ld_ALLOC(win->_line, ToLines+1);
90184588Sdfr		if (win->_line == 0)
91184588Sdfr			returnCode(ERR);
92184588Sdfr
93184588Sdfr		for (row = size_y+1; row <= ToLines; row++) {
94184588Sdfr			win->_line[row].text      = 0;
95184588Sdfr			win->_line[row].firstchar = 0;
96184588Sdfr			win->_line[row].lastchar  = ToCols;
97184588Sdfr			if ((win->_flags & _SUBWIN)) {
98184588Sdfr				win->_line[row].text =
99184588Sdfr	    			&pline[win->_begy + row].text[win->_begx];
100184588Sdfr			}
101184588Sdfr		}
102184588Sdfr	}
103184588Sdfr
104184588Sdfr	/*
105184588Sdfr	 * Adjust the width of the columns:
106184588Sdfr	 */
107184588Sdfr	blank = _nc_background(win);
108184588Sdfr	for (row = 0; row <= ToLines; row++) {
109184588Sdfr		chtype	*s	= win->_line[row].text;
110184588Sdfr		int	begin	= (s == 0) ? 0 : size_x + 1;
111184588Sdfr		int	end	= ToCols;
112184588Sdfr
113184588Sdfr		if_USE_SCROLL_HINTS(win->_line[row].oldindex = row);
114184588Sdfr
115184588Sdfr		if (ToCols != size_x || s == 0) {
116184588Sdfr			if (! (win->_flags & _SUBWIN)) {
117184588Sdfr				win->_line[row].text = s = c_ALLOC(s, ToCols+1);
118184588Sdfr				if (win->_line[row].text == 0)
119184588Sdfr					returnCode(ERR);
120184588Sdfr			} else if (s == 0) {
121184588Sdfr				win->_line[row].text = s =
122184588Sdfr	    			&pline[win->_begy + row].text[win->_begx];
123184588Sdfr			}
124184588Sdfr
125184588Sdfr			if (end >= begin) {	/* growing */
126184588Sdfr				if (win->_line[row].firstchar < begin)
127184588Sdfr					win->_line[row].firstchar = begin;
128184588Sdfr				win->_line[row].lastchar = ToCols;
129184588Sdfr				do {
130184588Sdfr					s[end] = blank;
131184588Sdfr				} while (--end >= begin);
132184588Sdfr			} else {		/* shrinking */
133184588Sdfr				win->_line[row].firstchar = 0;
134184588Sdfr				win->_line[row].lastchar  = ToCols;
135184588Sdfr			}
136184588Sdfr		}
137184588Sdfr	}
138184588Sdfr
139184588Sdfr	/*
140184588Sdfr	 * Finally, adjust the parameters showing screen size and cursor
141184588Sdfr	 * position:
142184588Sdfr	 */
143184588Sdfr	win->_maxx = ToCols;
144184588Sdfr	win->_maxy = ToLines;
145184588Sdfr
146184588Sdfr	if (win->_regtop > win->_maxy)
147184588Sdfr		win->_regtop = win->_maxy;
148184588Sdfr	if (win->_regbottom > win->_maxy
149184588Sdfr	 || win->_regbottom == size_y)
150184588Sdfr		win->_regbottom = win->_maxy;
151184588Sdfr
152184588Sdfr	if (win->_curx > win->_maxx)
153184588Sdfr		win->_curx = win->_maxx;
154184588Sdfr	if (win->_cury > win->_maxy)
155		win->_cury = win->_maxy;
156
157#ifdef TRACE
158	TR(TRACE_UPDATE, ("...beg (%d, %d), max(%d,%d), reg(%d,%d)",
159		win->_begy, win->_begx,
160		win->_maxy, win->_maxx,
161		win->_regtop, win->_regbottom));
162	if (_nc_tracing & TRACE_UPDATE)
163		_tracedump("...after:", win);
164#endif
165	returnCode(OK);
166}
167