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.14 2002/07/13 21:32:57 tom Exp $")
46 #define stolen_lines (screen_lines - SP->_lines_avail)
49 is_term_resized(int ToLines, int ToCols)
51 return (ToLines != screen_lines
52 || ToCols != screen_columns);
56 * Return the number of levels of child-windows under the current window.
59 child_depth(WINDOW *cmp)
66 for (wp = _nc_windows; wp != 0; wp = wp->next) {
67 WINDOW *tst = &(wp->win);
68 if (tst->_parent == cmp) {
69 depth = 1 + child_depth(tst);
78 * Return the number of levels of parent-windows above the current window.
81 parent_depth(WINDOW *cmp)
87 while ((tst = cmp->_parent) != 0) {
96 * FIXME: must adjust position so it's within the parent!
99 adjust_window(WINDOW *win, int ToLines, int ToCols, int stolen)
102 int bottom = screen_lines + SP->_topstolen - stolen;
103 int myLines = win->_maxy + 1;
104 int myCols = win->_maxx + 1;
106 T((T_CALLED("adjust_window(%p,%d,%d) currently %dx%d at %d,%d"),
107 win, ToLines, ToCols,
108 getmaxy(win), getmaxx(win),
109 getbegy(win), getbegx(win)));
111 if (win->_begy >= bottom) {
112 win->_begy += (ToLines - screen_lines);
114 if (myLines == screen_lines - stolen
115 && ToLines != screen_lines)
116 myLines = ToLines - stolen;
117 else if (myLines == screen_lines
118 && ToLines != screen_lines)
122 if (myLines > ToLines)
128 if (myLines == screen_lines
129 && ToLines != screen_lines)
132 if (myCols == screen_columns
133 && ToCols != screen_columns)
136 result = wresize(win, myLines, myCols);
141 * If we're decreasing size, recursively search for windows that have no
142 * children, decrease those to fit, then decrease the containing window, etc.
145 decrease_size(int ToLines, int ToCols, int stolen)
151 T((T_CALLED("decrease_size(%d, %d)"), ToLines, ToCols));
155 TR(TRACE_UPDATE, ("decreasing size of windows to %dx%d, depth=%d",
156 ToLines, ToCols, depth));
157 for (wp = _nc_windows; wp != 0; wp = wp->next) {
158 WINDOW *win = &(wp->win);
160 if (!(win->_flags & _ISPAD)) {
161 if (child_depth(win) == depth) {
162 if (adjust_window(win, ToLines, ToCols, stolen) != OK)
173 * If we're increasing size, recursively search for windows that have no
174 * parent, increase those to fit, then increase the contained window, etc.
177 increase_size(int ToLines, int ToCols, int stolen)
183 T((T_CALLED("increase_size(%d, %d)"), ToLines, ToCols));
187 TR(TRACE_UPDATE, ("increasing size of windows to %dx%d, depth=%d",
188 ToLines, ToCols, depth));
189 for (wp = _nc_windows; wp != 0; wp = wp->next) {
190 WINDOW *win = &(wp->win);
192 if (!(win->_flags & _ISPAD)) {
193 if (parent_depth(win) == depth) {
194 if (adjust_window(win, ToLines, ToCols, stolen) != OK)
205 * This function reallocates NCURSES window structures, with no side-effects
209 resize_term(int ToLines, int ToCols)
212 int was_stolen = (screen_lines - SP->_lines_avail);
214 T((T_CALLED("resize_term(%d,%d) old(%d,%d)"),
216 screen_lines, screen_columns));
218 if (is_term_resized(ToLines, ToCols)) {
219 int myLines = screen_lines;
220 int myCols = screen_columns;
222 if (ToLines > screen_lines) {
223 increase_size(myLines = ToLines, myCols, was_stolen);
226 if (ToCols > screen_columns) {
227 increase_size(myLines, myCols = ToCols, was_stolen);
230 if (ToLines < myLines ||
232 decrease_size(ToLines, ToCols, was_stolen);
235 screen_lines = lines = ToLines;
236 screen_columns = columns = ToCols;
238 SP->_lines_avail = lines - was_stolen;
241 FreeAndNull(SP->oldhash);
244 FreeAndNull(SP->newhash);
249 * Always update LINES, to allow for call from lib_doupdate.c which
250 * needs to have the count adjusted by the stolen (ripped off) lines.
252 LINES = ToLines - was_stolen;
259 * This function reallocates NCURSES window structures. It is invoked in
260 * response to a SIGWINCH interrupt. Other user-defined windows may also need
263 * Because this performs memory allocation, it should not (in general) be
264 * invoked directly from the signal handler.
267 resizeterm(int ToLines, int ToCols)
271 SP->_sig_winch = FALSE;
273 T((T_CALLED("resizeterm(%d,%d) old(%d,%d)"),
275 screen_lines, screen_columns));
277 if (is_term_resized(ToLines, ToCols)) {
280 ungetch(KEY_RESIZE); /* so application can know this */
281 clearok(curscr, TRUE); /* screen contents are unknown */
284 result = resize_term(ToLines, ToCols);