1 /****************************************************************************
2  * Copyright (c) 1998 Free Software Foundation, Inc.                        *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 
29 /****************************************************************************
30  *  Author: Thomas E. Dickey <dickey@clark.net> 1996,1997                   *
31  ****************************************************************************/
32 
33 /*
34  * This is an extension to the curses library.  It provides callers with a hook
35  * into the NCURSES data to resize windows, primarily for use by programs
36  * running in an X Window terminal (e.g., xterm).  I abstracted this module
37  * from my application library for NCURSES because it must be compiled with
38  * the private data structures -- T.Dickey 1995/7/4.
39  */
40 
41 #include <curses.priv.h>
42 #include <term.h>
43 
44 MODULE_ID("$Id: resizeterm.c,v 1.7 1998/09/19 19:27:43 Alexander.V.Lukyanov Exp $")
45 
46 /*
47  * This function reallocates NCURSES window structures.  It is invoked in
48  * response to a SIGWINCH interrupt.  Other user-defined windows may also need
49  * to be reallocated.
50  *
51  * Because this performs memory allocation, it should not (in general) be
52  * invoked directly from the signal handler.
53  */
54 int
55 resizeterm(int ToLines, int ToCols)
56 {
57 	int stolen = screen_lines - SP->_lines_avail;
58 	int bottom = screen_lines + SP->_topstolen - stolen;
59 
60 	T((T_CALLED("resizeterm(%d,%d) old(%d,%d)"),
61 		ToLines, ToCols,
62 		screen_lines, screen_columns));
63 
64 	SP->_sig_winch = FALSE;
65 
66 	if (ToLines != screen_lines
67 	 || ToCols  != screen_columns) {
68 		WINDOWLIST *wp;
69 
70 #if USE_SIGWINCH
71 		ungetch(KEY_RESIZE);	/* so application can know this */
72 		clearok(curscr, TRUE);	/* screen contents are unknown */
73 #endif
74 
75 		for (wp = _nc_windows; wp != 0; wp = wp->next) {
76 			WINDOW *win = wp->win;
77 			int myLines = win->_maxy + 1;
78 			int myCols  = win->_maxx + 1;
79 
80 			/* pads aren't treated this way */
81 			if (win->_flags & _ISPAD)
82 				continue;
83 
84 			if (win->_begy >= bottom) {
85 				win->_begy += (ToLines - screen_lines);
86 			} else {
87 				if (myLines == screen_lines - stolen
88 				 && ToLines != screen_lines)
89 				 	myLines = ToLines - stolen;
90 				else
91 				if (myLines == screen_lines
92 				 && ToLines != screen_lines)
93 				 	myLines = ToLines;
94 			}
95 
96 			if (myCols  == screen_columns
97 			 && ToCols  != screen_columns)
98 			 	myCols = ToCols;
99 
100 			if (wresize(win, myLines, myCols) != OK)
101 				returnCode(ERR);
102 		}
103 
104 		screen_lines   = lines    = ToLines;
105 		screen_columns = columns  = ToCols;
106 
107 		SP->_lines_avail = lines - stolen;
108 
109 		if (SP->oldhash) { FreeAndNull(SP->oldhash); }
110 		if (SP->newhash) { FreeAndNull(SP->newhash); }
111 	}
112 
113 	/*
114 	 * Always update LINES, to allow for call from lib_doupdate.c which
115 	 * needs to have the count adjusted by the stolen (ripped off) lines.
116 	 */
117 	LINES = ToLines - stolen;
118 	COLS  = ToCols;
119 
120 	returnCode(OK);
121 }
122