1 /****************************************************************************
2 * Copyright (c) 1998-2008,2009 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>
45 #define CUR SP_TERMTYPE
48 MODULE_ID("$Id: resizeterm.c,v 1.39 2009/05/10 00:48:29 tom Exp $")
50 #define stolen_lines (screen_lines - SP->_lines_avail)
53 * If we're trying to be reentrant, do not want any local statics.
56 #define EXTRA_ARGS , CurLines, CurCols
57 #define EXTRA_DCLS , int CurLines, int CurCols
59 static int current_lines;
60 static int current_cols;
61 #define CurLines current_lines
62 #define CurCols current_cols
63 #define EXTRA_ARGS /* nothing */
64 #define EXTRA_DCLS /* nothing */
69 show_window_sizes(const char *name)
74 _nc_lock_global(curses);
75 for (each_screen(sp)) {
76 _tracef("%s resizing: %p: %2d x %2d (%2d x %2d)", name, sp,
79 screen_lines(sp), screen_columns(sp));
80 for (each_window(sp, wp)) {
81 _tracef(" window %p is %2ld x %2ld at %2ld,%2ld",
83 (long) wp->win._maxy + 1,
84 (long) wp->win._maxx + 1,
86 (long) wp->win._begx);
89 _nc_unlock_global(curses);
94 * Return true if the given dimensions do not match the internal terminal
98 NCURSES_SP_NAME(is_term_resized) (NCURSES_SP_DCLx int ToLines, int ToCols)
100 T((T_CALLED("is_term_resized(%p, %d, %d)"), SP_PARM, ToLines, ToCols));
101 returnCode(ToLines > 0
103 && (ToLines != screen_lines(SP_PARM)
104 || ToCols != screen_columns(SP_PARM)));
109 is_term_resized(int ToLines, int ToCols)
111 return NCURSES_SP_NAME(is_term_resized) (CURRENT_SCREEN, ToLines, ToCols);
118 ripped_window(WINDOW *win)
120 ripoff_t *result = 0;
125 SCREEN *sp = _nc_screen_of(win);
127 for (each_ripoff(rop)) {
128 if (rop->win == win && rop->line != 0) {
138 * Returns the number of lines from the bottom for the beginning of a ripped
142 ripped_bottom(WINDOW *win)
149 SCREEN *sp = _nc_screen_of(win);
151 for (each_ripoff(rop)) {
154 if (rop->win == win) {
164 * Return the number of levels of child-windows under the current window.
167 child_depth(WINDOW *cmp)
173 SCREEN *sp = _nc_screen_of(cmp);
177 for (each_window(sp, wp)) {
178 WINDOW *tst = &(wp->win);
179 if (tst->_parent == cmp) {
180 depth = 1 + child_depth(tst);
189 * Return the number of levels of parent-windows above the current window.
192 parent_depth(WINDOW *cmp)
198 while ((tst = cmp->_parent) != 0) {
207 * FIXME: must adjust position so it's within the parent!
210 adjust_window(WINDOW *win, int ToLines, int ToCols, int stolen EXTRA_DCLS)
213 int bottom = CurLines + _nc_screen_of(win)->_topstolen - stolen;
214 int myLines = win->_maxy + 1;
215 int myCols = win->_maxx + 1;
216 ripoff_t *rop = ripped_window(win);
218 T((T_CALLED("adjust_window(%p,%d,%d)%s depth %d/%d currently %ldx%ld at %ld,%ld"),
219 win, ToLines, ToCols,
220 (rop != 0) ? " (rip)" : "",
223 (long) getmaxy(win), (long) getmaxx(win),
224 (long) getbegy(win) + win->_yoffset, (long) getbegx(win)));
226 if (rop != 0 && rop->line < 0) {
228 * If it is a ripped-off window at the bottom of the screen, simply
229 * move it to the same relative position.
231 win->_begy = ToLines - ripped_bottom(win) - 0 - win->_yoffset;
232 } else if (win->_begy >= bottom) {
234 * If it is below the bottom of the new screen, move up by the same
235 * amount that the screen shrank.
237 win->_begy += (ToLines - CurLines);
239 if (myLines == (CurLines - stolen)
240 && ToLines != CurLines) {
241 myLines = ToLines - stolen;
242 } else if (myLines == CurLines
243 && ToLines != CurLines) {
248 if (myLines > ToLines) {
255 if (myCols == CurCols
256 && ToCols != CurCols)
259 result = wresize(win, myLines, myCols);
264 * If we're decreasing size, recursively search for windows that have no
265 * children, decrease those to fit, then decrease the containing window, etc.
268 decrease_size(NCURSES_SP_DCLx int ToLines, int ToCols, int stolen EXTRA_DCLS)
274 T((T_CALLED("decrease_size(%p, %d, %d)"), SP_PARM, ToLines, ToCols));
278 TR(TRACE_UPDATE, ("decreasing size of windows to %dx%d, depth=%d",
279 ToLines, ToCols, depth));
280 for (each_window(SP_PARM, wp)) {
281 WINDOW *win = &(wp->win);
283 if (!(win->_flags & _ISPAD)) {
284 if (child_depth(win) == depth) {
286 if (adjust_window(win, ToLines, ToCols,
287 stolen EXTRA_ARGS) != OK)
298 * If we're increasing size, recursively search for windows that have no
299 * parent, increase those to fit, then increase the contained window, etc.
302 increase_size(NCURSES_SP_DCLx int ToLines, int ToCols, int stolen EXTRA_DCLS)
308 T((T_CALLED("increase_size(%p, %d, %d)"), SP_PARM, ToLines, ToCols));
312 TR(TRACE_UPDATE, ("increasing size of windows to %dx%d, depth=%d",
313 ToLines, ToCols, depth));
314 for (each_window(SP_PARM, wp)) {
315 WINDOW *win = &(wp->win);
317 if (!(win->_flags & _ISPAD)) {
318 if (parent_depth(win) == depth) {
320 if (adjust_window(win, ToLines, ToCols,
321 stolen EXTRA_ARGS) != OK)
332 * This function reallocates NCURSES window structures, with no side-effects
336 NCURSES_SP_NAME(resize_term) (NCURSES_SP_DCLx int ToLines, int ToCols)
338 int result = OK EXTRA_ARGS;
341 T((T_CALLED("resize_term(%p,%d,%d) old(%d,%d)"),
342 SP_PARM, ToLines, ToCols,
343 SP_PARM == 0 ? -1 : screen_lines(SP_PARM),
344 SP_PARM == 0 ? -1 : screen_columns(SP_PARM)));
350 _nc_lock_global(curses);
352 was_stolen = (screen_lines(SP_PARM) - SP_PARM->_lines_avail);
353 if (NCURSES_SP_NAME(is_term_resized) (NCURSES_SP_ARGx ToLines, ToCols)) {
354 int myLines = CurLines = screen_lines(SP_PARM);
355 int myCols = CurCols = screen_columns(SP_PARM);
358 if (USE_TRACEF(TRACE_UPDATE)) {
359 show_window_sizes("before");
360 _nc_unlock_global(tracef);
363 if (ToLines > screen_lines(SP_PARM)) {
364 increase_size(NCURSES_SP_ARGx
365 myLines = ToLines, myCols, was_stolen EXTRA_ARGS);
370 if (ToCols > screen_columns(SP_PARM)) {
371 increase_size(NCURSES_SP_ARGx
372 myLines, myCols = ToCols, was_stolen EXTRA_ARGS);
377 if (ToLines < myLines ||
379 decrease_size(NCURSES_SP_ARGx ToLines, ToCols, was_stolen EXTRA_ARGS);
382 screen_lines(SP_PARM) = lines = ToLines;
383 screen_columns(SP_PARM) = columns = ToCols;
385 SP_PARM->_lines_avail = lines - was_stolen;
387 if (SP_PARM->oldhash) {
388 FreeAndNull(SP_PARM->oldhash);
390 if (SP_PARM->newhash) {
391 FreeAndNull(SP_PARM->newhash);
394 if (USE_TRACEF(TRACE_UPDATE)) {
395 SET_LINES(ToLines - was_stolen);
397 show_window_sizes("after");
398 _nc_unlock_global(tracef);
404 * Always update LINES, to allow for call from lib_doupdate.c which
405 * needs to have the count adjusted by the stolen (ripped off) lines.
407 SET_LINES(ToLines - was_stolen);
410 _nc_unlock_global(curses);
417 resize_term(int ToLines, int ToCols)
420 _nc_lock_global(curses);
421 res = NCURSES_SP_NAME(resize_term) (CURRENT_SCREEN, ToLines, ToCols);
422 _nc_unlock_global(curses);
428 * This function reallocates NCURSES window structures. It is invoked in
429 * response to a SIGWINCH interrupt. Other user-defined windows may also need
432 * Because this performs memory allocation, it should not (in general) be
433 * invoked directly from the signal handler.
436 NCURSES_SP_NAME(resizeterm) (NCURSES_SP_DCLx int ToLines, int ToCols)
440 T((T_CALLED("resizeterm(%p, %d,%d) old(%d,%d)"),
441 SP_PARM, ToLines, ToCols,
442 SP_PARM == 0 ? -1 : screen_lines(SP_PARM), screen_columns(SP_PARM)));
446 SP_PARM->_sig_winch = FALSE;
448 if (NCURSES_SP_NAME(is_term_resized) (NCURSES_SP_ARGx ToLines, ToCols)) {
451 bool slk_visible = (SP_PARM != 0
452 && SP_PARM->_slk != 0
453 && !(SP_PARM->_slk->hidden));
459 result = NCURSES_SP_NAME(resize_term) (NCURSES_SP_ARGx ToLines, ToCols);
462 safe_ungetch(SP_PARM, KEY_RESIZE); /* so application can know this */
463 clearok(CurScreen(SP_PARM), TRUE); /* screen contents are unknown */
465 /* ripped-off lines are a special case: if we did not lengthen
466 * them, we haven't moved them either. repaint them, too.
468 * for the rest - stdscr and other windows - the client has to
469 * decide which to repaint, since without panels, ncurses does
470 * not know which are really on top.
472 for (each_ripoff(rop)) {
473 if (rop->win != StdScreen(SP_PARM)
477 if (rop->hook != _nc_slk_initialize) {
479 wnoutrefresh(rop->win);
484 /* soft-keys are a special case: we _know_ how to repaint them */
486 NCURSES_SP_NAME(slk_restore) (NCURSES_SP_ARG);
487 NCURSES_SP_NAME(slk_touch) (NCURSES_SP_ARG);
488 NCURSES_SP_NAME(slk_refresh) (NCURSES_SP_ARG);
499 resizeterm(int ToLines, int ToCols)
501 return NCURSES_SP_NAME(resizeterm) (CURRENT_SCREEN, ToLines, ToCols);