1 /****************************************************************************
2 * Copyright (c) 1998-2006,2007 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.23 2007/10/13 20:12:13 tom Exp $")
46 #define stolen_lines (screen_lines - SP->_lines_avail)
49 * If we're trying to be reentrant, do not want any local statics.
52 #define EXTRA_ARGS , CurLines, CurCols
53 #define EXTRA_DCLS , int CurLines, int CurCols
55 static int current_lines;
56 static int current_cols;
57 #define CurLines current_lines
58 #define CurCols current_cols
59 #define EXTRA_ARGS /* nothing */
60 #define EXTRA_DCLS /* nothing */
65 show_window_sizes(const char *name)
69 _tracef("%s resizing: %2d x %2d (%2d x %2d)", name, LINES, COLS,
70 screen_lines, screen_columns);
71 for (wp = _nc_windows; wp != 0; wp = wp->next) {
72 _tracef(" window %p is %2ld x %2ld at %2ld,%2ld",
74 (long) wp->win._maxy + 1,
75 (long) wp->win._maxx + 1,
77 (long) wp->win._begx);
83 is_term_resized(int ToLines, int ToCols)
85 T((T_CALLED("is_term_resized(%d, %d)"), ToLines, ToCols));
86 returnCode(ToLines > 0
88 && (ToLines != screen_lines
89 || ToCols != screen_columns));
93 * Return the number of levels of child-windows under the current window.
96 child_depth(WINDOW *cmp)
103 for (wp = _nc_windows; wp != 0; wp = wp->next) {
104 WINDOW *tst = &(wp->win);
105 if (tst->_parent == cmp) {
106 depth = 1 + child_depth(tst);
115 * Return the number of levels of parent-windows above the current window.
118 parent_depth(WINDOW *cmp)
124 while ((tst = cmp->_parent) != 0) {
133 * FIXME: must adjust position so it's within the parent!
136 adjust_window(WINDOW *win, int ToLines, int ToCols, int stolen EXTRA_DCLS)
139 int bottom = CurLines + SP->_topstolen - stolen;
140 int myLines = win->_maxy + 1;
141 int myCols = win->_maxx + 1;
143 T((T_CALLED("adjust_window(%p,%d,%d) currently %ldx%ld at %ld,%ld"),
144 win, ToLines, ToCols,
145 (long) getmaxy(win), (long) getmaxx(win),
146 (long) getbegy(win), (long) getbegx(win)));
148 if (win->_begy >= bottom) {
149 win->_begy += (ToLines - CurLines);
151 if (myLines == CurLines - stolen
152 && ToLines != CurLines)
153 myLines = ToLines - stolen;
154 else if (myLines == CurLines
155 && ToLines != CurLines)
159 if (myLines > ToLines)
165 if (myLines == CurLines
166 && ToLines != CurLines)
169 if (myCols == CurCols
170 && ToCols != CurCols)
173 result = wresize(win, myLines, myCols);
178 * If we're decreasing size, recursively search for windows that have no
179 * children, decrease those to fit, then decrease the containing window, etc.
182 decrease_size(int ToLines, int ToCols, int stolen EXTRA_DCLS)
188 T((T_CALLED("decrease_size(%d, %d)"), ToLines, ToCols));
192 TR(TRACE_UPDATE, ("decreasing size of windows to %dx%d, depth=%d",
193 ToLines, ToCols, depth));
194 for (wp = _nc_windows; wp != 0; wp = wp->next) {
195 WINDOW *win = &(wp->win);
197 if (!(win->_flags & _ISPAD)) {
198 if (child_depth(win) == depth) {
200 if (adjust_window(win, ToLines, ToCols,
201 stolen EXTRA_ARGS) != OK)
212 * If we're increasing size, recursively search for windows that have no
213 * parent, increase those to fit, then increase the contained window, etc.
216 increase_size(int ToLines, int ToCols, int stolen EXTRA_DCLS)
222 T((T_CALLED("increase_size(%d, %d)"), ToLines, ToCols));
226 TR(TRACE_UPDATE, ("increasing size of windows to %dx%d, depth=%d",
227 ToLines, ToCols, depth));
228 for (wp = _nc_windows; wp != 0; wp = wp->next) {
229 WINDOW *win = &(wp->win);
231 if (!(win->_flags & _ISPAD)) {
232 if (parent_depth(win) == depth) {
234 if (adjust_window(win, ToLines, ToCols,
235 stolen EXTRA_ARGS) != OK)
246 * This function reallocates NCURSES window structures, with no side-effects
250 resize_term(int ToLines, int ToCols)
252 int result = OK EXTRA_ARGS;
255 T((T_CALLED("resize_term(%d,%d) old(%d,%d)"),
257 screen_lines, screen_columns));
262 was_stolen = (screen_lines - SP->_lines_avail);
263 if (is_term_resized(ToLines, ToCols)) {
264 int myLines = CurLines = screen_lines;
265 int myCols = CurCols = screen_columns;
268 if (USE_TRACEF(TRACE_UPDATE)) {
269 show_window_sizes("before");
270 _nc_unlock_global(tracef);
273 if (ToLines > screen_lines) {
274 increase_size(myLines = ToLines, myCols, was_stolen EXTRA_ARGS);
279 if (ToCols > screen_columns) {
280 increase_size(myLines, myCols = ToCols, was_stolen EXTRA_ARGS);
285 if (ToLines < myLines ||
287 decrease_size(ToLines, ToCols, was_stolen EXTRA_ARGS);
290 screen_lines = lines = ToLines;
291 screen_columns = columns = ToCols;
293 SP->_lines_avail = lines - was_stolen;
296 FreeAndNull(SP->oldhash);
299 FreeAndNull(SP->newhash);
302 if (USE_TRACEF(TRACE_UPDATE)) {
303 SET_LINES(ToLines - was_stolen);
305 show_window_sizes("after");
306 _nc_unlock_global(tracef);
312 * Always update LINES, to allow for call from lib_doupdate.c which
313 * needs to have the count adjusted by the stolen (ripped off) lines.
315 SET_LINES(ToLines - was_stolen);
322 * This function reallocates NCURSES window structures. It is invoked in
323 * response to a SIGWINCH interrupt. Other user-defined windows may also need
326 * Because this performs memory allocation, it should not (in general) be
327 * invoked directly from the signal handler.
330 resizeterm(int ToLines, int ToCols)
334 T((T_CALLED("resizeterm(%d,%d) old(%d,%d)"),
336 screen_lines, screen_columns));
340 SP->_sig_winch = FALSE;
342 if (is_term_resized(ToLines, ToCols)) {
345 ungetch(KEY_RESIZE); /* so application can know this */
346 clearok(curscr, TRUE); /* screen contents are unknown */
349 result = resize_term(ToLines, ToCols);