1 /****************************************************************************
2 * Copyright (c) 1998-2001,2002 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Thomas E. Dickey *
31 ****************************************************************************/
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.
41 #include <curses.priv.h>
44 MODULE_ID("$Id: resizeterm.c,v 1.15 2002/12/28 01:21:34 tom Exp $")
46 #define stolen_lines (screen_lines - SP->_lines_avail)
48 static int current_lines;
49 static int current_cols;
52 is_term_resized(int ToLines, int ToCols)
54 return (ToLines != screen_lines
55 || ToCols != screen_columns);
59 * Return the number of levels of child-windows under the current window.
62 child_depth(WINDOW *cmp)
69 for (wp = _nc_windows; wp != 0; wp = wp->next) {
70 WINDOW *tst = &(wp->win);
71 if (tst->_parent == cmp) {
72 depth = 1 + child_depth(tst);
81 * Return the number of levels of parent-windows above the current window.
84 parent_depth(WINDOW *cmp)
90 while ((tst = cmp->_parent) != 0) {
99 * FIXME: must adjust position so it's within the parent!
102 adjust_window(WINDOW *win, int ToLines, int ToCols, int stolen)
105 int bottom = current_lines + SP->_topstolen - stolen;
106 int myLines = win->_maxy + 1;
107 int myCols = win->_maxx + 1;
109 T((T_CALLED("adjust_window(%p,%d,%d) currently %dx%d at %d,%d"),
110 win, ToLines, ToCols,
111 getmaxy(win), getmaxx(win),
112 getbegy(win), getbegx(win)));
114 if (win->_begy >= bottom) {
115 win->_begy += (ToLines - current_lines);
117 if (myLines == current_lines - stolen
118 && ToLines != current_lines)
119 myLines = ToLines - stolen;
120 else if (myLines == current_lines
121 && ToLines != current_lines)
125 if (myLines > ToLines)
131 if (myLines == current_lines
132 && ToLines != current_lines)
135 if (myCols == current_cols
136 && ToCols != current_cols)
139 result = wresize(win, myLines, myCols);
144 * If we're decreasing size, recursively search for windows that have no
145 * children, decrease those to fit, then decrease the containing window, etc.
148 decrease_size(int ToLines, int ToCols, int stolen)
154 T((T_CALLED("decrease_size(%d, %d)"), ToLines, ToCols));
158 TR(TRACE_UPDATE, ("decreasing size of windows to %dx%d, depth=%d",
159 ToLines, ToCols, depth));
160 for (wp = _nc_windows; wp != 0; wp = wp->next) {
161 WINDOW *win = &(wp->win);
163 if (!(win->_flags & _ISPAD)) {
164 if (child_depth(win) == depth) {
165 if (adjust_window(win, ToLines, ToCols, stolen) != OK)
176 * If we're increasing size, recursively search for windows that have no
177 * parent, increase those to fit, then increase the contained window, etc.
180 increase_size(int ToLines, int ToCols, int stolen)
186 T((T_CALLED("increase_size(%d, %d)"), ToLines, ToCols));
190 TR(TRACE_UPDATE, ("increasing size of windows to %dx%d, depth=%d",
191 ToLines, ToCols, depth));
192 for (wp = _nc_windows; wp != 0; wp = wp->next) {
193 WINDOW *win = &(wp->win);
195 if (!(win->_flags & _ISPAD)) {
196 if (parent_depth(win) == depth) {
197 if (adjust_window(win, ToLines, ToCols, stolen) != OK)
208 * This function reallocates NCURSES window structures, with no side-effects
212 resize_term(int ToLines, int ToCols)
215 int was_stolen = (screen_lines - SP->_lines_avail);
217 T((T_CALLED("resize_term(%d,%d) old(%d,%d)"),
219 screen_lines, screen_columns));
221 if (is_term_resized(ToLines, ToCols)) {
222 int myLines = current_lines = screen_lines;
223 int myCols = current_cols = screen_columns;
225 if (ToLines > screen_lines) {
226 increase_size(myLines = ToLines, myCols, was_stolen);
227 current_lines = myLines;
228 current_cols = myCols;
231 if (ToCols > screen_columns) {
232 increase_size(myLines, myCols = ToCols, was_stolen);
233 current_lines = myLines;
234 current_cols = myCols;
237 if (ToLines < myLines ||
239 decrease_size(ToLines, ToCols, was_stolen);
242 screen_lines = lines = ToLines;
243 screen_columns = columns = ToCols;
245 SP->_lines_avail = lines - was_stolen;
248 FreeAndNull(SP->oldhash);
251 FreeAndNull(SP->newhash);
256 * Always update LINES, to allow for call from lib_doupdate.c which
257 * needs to have the count adjusted by the stolen (ripped off) lines.
259 LINES = ToLines - was_stolen;
266 * This function reallocates NCURSES window structures. It is invoked in
267 * response to a SIGWINCH interrupt. Other user-defined windows may also need
270 * Because this performs memory allocation, it should not (in general) be
271 * invoked directly from the signal handler.
274 resizeterm(int ToLines, int ToCols)
278 SP->_sig_winch = FALSE;
280 T((T_CALLED("resizeterm(%d,%d) old(%d,%d)"),
282 screen_lines, screen_columns));
284 if (is_term_resized(ToLines, ToCols)) {
287 ungetch(KEY_RESIZE); /* so application can know this */
288 clearok(curscr, TRUE); /* screen contents are unknown */
291 result = resize_term(ToLines, ToCols);