1 /****************************************************************************
2 * Copyright (c) 1998-2007,2008 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.28 2008/01/06 01:23:36 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 _nc_lock_global(windowlist);
70 _tracef("%s resizing: %2d x %2d (%2d x %2d)", name, LINES, COLS,
71 screen_lines, screen_columns);
72 for (wp = _nc_windows; wp != 0; wp = wp->next) {
73 _tracef(" window %p is %2ld x %2ld at %2ld,%2ld",
75 (long) wp->win._maxy + 1,
76 (long) wp->win._maxx + 1,
78 (long) wp->win._begx);
80 _nc_unlock_global(windowlist);
85 * Return true if the given dimensions do not match the internal terminal
89 is_term_resized(int ToLines, int ToCols)
91 T((T_CALLED("is_term_resized(%d, %d)"), ToLines, ToCols));
92 returnCode(ToLines > 0
94 && (ToLines != screen_lines
95 || ToCols != screen_columns));
101 ripped_window(WINDOW *win)
103 ripoff_t *result = 0;
107 for (rop = ripoff_stack; (rop - ripoff_stack) < N_RIPS; rop++) {
108 if (rop->win == win && rop->line != 0) {
118 * Returns the number of lines from the bottom for the beginning of a ripped
122 ripped_bottom(WINDOW *win)
128 for (rop = ripoff_stack; (rop - ripoff_stack) < N_RIPS; rop++) {
131 if (rop->win == win) {
141 * Return the number of levels of child-windows under the current window.
144 child_depth(WINDOW *cmp)
151 for (wp = _nc_windows; wp != 0; wp = wp->next) {
152 WINDOW *tst = &(wp->win);
153 if (tst->_parent == cmp) {
154 depth = 1 + child_depth(tst);
163 * Return the number of levels of parent-windows above the current window.
166 parent_depth(WINDOW *cmp)
172 while ((tst = cmp->_parent) != 0) {
181 * FIXME: must adjust position so it's within the parent!
184 adjust_window(WINDOW *win, int ToLines, int ToCols, int stolen EXTRA_DCLS)
187 int bottom = CurLines + SP->_topstolen - stolen;
188 int myLines = win->_maxy + 1;
189 int myCols = win->_maxx + 1;
190 ripoff_t *rop = ripped_window(win);
192 T((T_CALLED("adjust_window(%p,%d,%d)%s depth %d/%d currently %ldx%ld at %ld,%ld"),
193 win, ToLines, ToCols,
194 (rop != 0) ? " (rip)" : "",
197 (long) getmaxy(win), (long) getmaxx(win),
198 (long) getbegy(win) + win->_yoffset, (long) getbegx(win)));
200 if (rop != 0 && rop->line < 0) {
202 * If it is a ripped-off window at the bottom of the screen, simply
203 * move it to the same relative position.
205 win->_begy = ToLines - ripped_bottom(win) - 0 - win->_yoffset;
206 } else if (win->_begy >= bottom) {
208 * If it is below the bottom of the new screen, move up by the same
209 * amount that the screen shrank.
211 win->_begy += (ToLines - CurLines);
213 if (myLines == CurLines - stolen
214 && ToLines != CurLines)
215 myLines = ToLines - stolen;
216 else if (myLines == CurLines
217 && ToLines != CurLines)
221 if (myLines > ToLines)
227 if (myLines == CurLines
228 && ToLines != CurLines)
231 if (myCols == CurCols
232 && ToCols != CurCols)
235 result = wresize(win, myLines, myCols);
240 * If we're decreasing size, recursively search for windows that have no
241 * children, decrease those to fit, then decrease the containing window, etc.
244 decrease_size(int ToLines, int ToCols, int stolen EXTRA_DCLS)
250 T((T_CALLED("decrease_size(%d, %d)"), ToLines, ToCols));
254 TR(TRACE_UPDATE, ("decreasing size of windows to %dx%d, depth=%d",
255 ToLines, ToCols, depth));
256 for (wp = _nc_windows; wp != 0; wp = wp->next) {
257 WINDOW *win = &(wp->win);
259 if (!(win->_flags & _ISPAD)) {
260 if (child_depth(win) == depth) {
262 if (adjust_window(win, ToLines, ToCols,
263 stolen EXTRA_ARGS) != OK)
274 * If we're increasing size, recursively search for windows that have no
275 * parent, increase those to fit, then increase the contained window, etc.
278 increase_size(int ToLines, int ToCols, int stolen EXTRA_DCLS)
284 T((T_CALLED("increase_size(%d, %d)"), ToLines, ToCols));
288 TR(TRACE_UPDATE, ("increasing size of windows to %dx%d, depth=%d",
289 ToLines, ToCols, depth));
290 for (wp = _nc_windows; wp != 0; wp = wp->next) {
291 WINDOW *win = &(wp->win);
293 if (!(win->_flags & _ISPAD)) {
294 if (parent_depth(win) == depth) {
296 if (adjust_window(win, ToLines, ToCols,
297 stolen EXTRA_ARGS) != OK)
308 * This function reallocates NCURSES window structures, with no side-effects
312 resize_term(int ToLines, int ToCols)
314 int result = OK EXTRA_ARGS;
317 T((T_CALLED("resize_term(%d,%d) old(%d,%d)"),
319 screen_lines, screen_columns));
325 _nc_lock_global(windowlist);
327 was_stolen = (screen_lines - SP->_lines_avail);
328 if (is_term_resized(ToLines, ToCols)) {
329 int myLines = CurLines = screen_lines;
330 int myCols = CurCols = screen_columns;
333 if (USE_TRACEF(TRACE_UPDATE)) {
334 show_window_sizes("before");
335 _nc_unlock_global(tracef);
338 if (ToLines > screen_lines) {
339 increase_size(myLines = ToLines, myCols, was_stolen EXTRA_ARGS);
344 if (ToCols > screen_columns) {
345 increase_size(myLines, myCols = ToCols, was_stolen EXTRA_ARGS);
350 if (ToLines < myLines ||
352 decrease_size(ToLines, ToCols, was_stolen EXTRA_ARGS);
355 screen_lines = lines = ToLines;
356 screen_columns = columns = ToCols;
358 SP->_lines_avail = lines - was_stolen;
361 FreeAndNull(SP->oldhash);
364 FreeAndNull(SP->newhash);
367 if (USE_TRACEF(TRACE_UPDATE)) {
368 SET_LINES(ToLines - was_stolen);
370 show_window_sizes("after");
371 _nc_unlock_global(tracef);
377 * Always update LINES, to allow for call from lib_doupdate.c which
378 * needs to have the count adjusted by the stolen (ripped off) lines.
380 SET_LINES(ToLines - was_stolen);
383 _nc_unlock_global(windowlist);
389 * This function reallocates NCURSES window structures. It is invoked in
390 * response to a SIGWINCH interrupt. Other user-defined windows may also need
393 * Because this performs memory allocation, it should not (in general) be
394 * invoked directly from the signal handler.
397 resizeterm(int ToLines, int ToCols)
401 T((T_CALLED("resizeterm(%d,%d) old(%d,%d)"),
403 screen_lines, screen_columns));
407 SP->_sig_winch = FALSE;
409 if (is_term_resized(ToLines, ToCols)) {
412 bool slk_visible = (SP != 0
414 && !(SP->_slk->hidden));
420 result = resize_term(ToLines, ToCols);
423 ungetch(KEY_RESIZE); /* so application can know this */
424 clearok(curscr, TRUE); /* screen contents are unknown */
426 /* ripped-off lines are a special case: if we did not lengthen
427 * them, we haven't moved them either. repaint them, too.
429 for (rop = ripoff_stack; (rop - ripoff_stack) < N_RIPS; rop++) {
430 if (rop->win != stdscr
434 if (rop->hook != _nc_slk_initialize) {
436 wnoutrefresh(rop->win);
441 /* soft-keys are a special case: we _know_ how to repaint them */