1 /****************************************************************************
2 * Copyright (c) 1998-2004,2006 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.18 2006/10/14 20:43:31 tom Exp $")
46 #define stolen_lines (screen_lines - SP->_lines_avail)
48 static int current_lines;
49 static int current_cols;
53 show_window_sizes(const char *name)
57 _tracef("%s resizing: %2d x %2d (%2d x %2d)", name, LINES, COLS,
58 screen_lines, screen_columns);
59 for (wp = _nc_windows; wp != 0; wp = wp->next) {
60 _tracef(" window %p is %2ld x %2ld at %2ld,%2ld",
62 (long) wp->win._maxy + 1,
63 (long) wp->win._maxx + 1,
65 (long) wp->win._begx);
71 is_term_resized(int ToLines, int ToCols)
73 T((T_CALLED("is_term_resized(%d, %d)"), ToLines, ToCols));
74 returnCode(ToLines > 0
76 && (ToLines != screen_lines
77 || ToCols != screen_columns));
81 * Return the number of levels of child-windows under the current window.
84 child_depth(WINDOW *cmp)
91 for (wp = _nc_windows; wp != 0; wp = wp->next) {
92 WINDOW *tst = &(wp->win);
93 if (tst->_parent == cmp) {
94 depth = 1 + child_depth(tst);
103 * Return the number of levels of parent-windows above the current window.
106 parent_depth(WINDOW *cmp)
112 while ((tst = cmp->_parent) != 0) {
121 * FIXME: must adjust position so it's within the parent!
124 adjust_window(WINDOW *win, int ToLines, int ToCols, int stolen)
127 int bottom = current_lines + SP->_topstolen - stolen;
128 int myLines = win->_maxy + 1;
129 int myCols = win->_maxx + 1;
131 T((T_CALLED("adjust_window(%p,%d,%d) currently %ldx%ld at %ld,%ld"),
132 win, ToLines, ToCols,
133 (long) getmaxy(win), (long) getmaxx(win),
134 (long) getbegy(win), (long) getbegx(win)));
136 if (win->_begy >= bottom) {
137 win->_begy += (ToLines - current_lines);
139 if (myLines == current_lines - stolen
140 && ToLines != current_lines)
141 myLines = ToLines - stolen;
142 else if (myLines == current_lines
143 && ToLines != current_lines)
147 if (myLines > ToLines)
153 if (myLines == current_lines
154 && ToLines != current_lines)
157 if (myCols == current_cols
158 && ToCols != current_cols)
161 result = wresize(win, myLines, myCols);
166 * If we're decreasing size, recursively search for windows that have no
167 * children, decrease those to fit, then decrease the containing window, etc.
170 decrease_size(int ToLines, int ToCols, int stolen)
176 T((T_CALLED("decrease_size(%d, %d)"), ToLines, ToCols));
180 TR(TRACE_UPDATE, ("decreasing size of windows to %dx%d, depth=%d",
181 ToLines, ToCols, depth));
182 for (wp = _nc_windows; wp != 0; wp = wp->next) {
183 WINDOW *win = &(wp->win);
185 if (!(win->_flags & _ISPAD)) {
186 if (child_depth(win) == depth) {
188 if (adjust_window(win, ToLines, ToCols, stolen) != OK)
199 * If we're increasing size, recursively search for windows that have no
200 * parent, increase those to fit, then increase the contained window, etc.
203 increase_size(int ToLines, int ToCols, int stolen)
209 T((T_CALLED("increase_size(%d, %d)"), ToLines, ToCols));
213 TR(TRACE_UPDATE, ("increasing size of windows to %dx%d, depth=%d",
214 ToLines, ToCols, depth));
215 for (wp = _nc_windows; wp != 0; wp = wp->next) {
216 WINDOW *win = &(wp->win);
218 if (!(win->_flags & _ISPAD)) {
219 if (parent_depth(win) == depth) {
221 if (adjust_window(win, ToLines, ToCols, stolen) != OK)
232 * This function reallocates NCURSES window structures, with no side-effects
236 resize_term(int ToLines, int ToCols)
239 int was_stolen = (screen_lines - SP->_lines_avail);
241 T((T_CALLED("resize_term(%d,%d) old(%d,%d)"),
243 screen_lines, screen_columns));
245 if (is_term_resized(ToLines, ToCols)) {
246 int myLines = current_lines = screen_lines;
247 int myCols = current_cols = screen_columns;
250 if (_nc_tracing & TRACE_UPDATE)
251 show_window_sizes("before");
253 if (ToLines > screen_lines) {
254 increase_size(myLines = ToLines, myCols, was_stolen);
255 current_lines = myLines;
256 current_cols = myCols;
259 if (ToCols > screen_columns) {
260 increase_size(myLines, myCols = ToCols, was_stolen);
261 current_lines = myLines;
262 current_cols = myCols;
265 if (ToLines < myLines ||
267 decrease_size(ToLines, ToCols, was_stolen);
270 screen_lines = lines = ToLines;
271 screen_columns = columns = ToCols;
273 SP->_lines_avail = lines - was_stolen;
276 FreeAndNull(SP->oldhash);
279 FreeAndNull(SP->newhash);
282 if (_nc_tracing & TRACE_UPDATE) {
283 LINES = ToLines - was_stolen;
285 show_window_sizes("after");
291 * Always update LINES, to allow for call from lib_doupdate.c which
292 * needs to have the count adjusted by the stolen (ripped off) lines.
294 LINES = ToLines - was_stolen;
301 * This function reallocates NCURSES window structures. It is invoked in
302 * response to a SIGWINCH interrupt. Other user-defined windows may also need
305 * Because this performs memory allocation, it should not (in general) be
306 * invoked directly from the signal handler.
309 resizeterm(int ToLines, int ToCols)
313 SP->_sig_winch = FALSE;
315 T((T_CALLED("resizeterm(%d,%d) old(%d,%d)"),
317 screen_lines, screen_columns));
319 if (is_term_resized(ToLines, ToCols)) {
322 ungetch(KEY_RESIZE); /* so application can know this */
323 clearok(curscr, TRUE); /* screen contents are unknown */
326 result = resize_term(ToLines, ToCols);