]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/resizeterm.c
ncurses 5.4
[ncurses.git] / ncurses / base / resizeterm.c
1 /****************************************************************************
2  * Copyright (c) 1998-2001,2002 Free Software Foundation, Inc.              *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Thomas E. Dickey                                                *
31  ****************************************************************************/
32
33 /*
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.
39  */
40
41 #include <curses.priv.h>
42 #include <term.h>
43
44 MODULE_ID("$Id: resizeterm.c,v 1.15 2002/12/28 01:21:34 tom Exp $")
45
46 #define stolen_lines (screen_lines - SP->_lines_avail)
47
48 static int current_lines;
49 static int current_cols;
50
51 NCURSES_EXPORT(bool)
52 is_term_resized(int ToLines, int ToCols)
53 {
54     return (ToLines != screen_lines
55             || ToCols != screen_columns);
56 }
57
58 /*
59  * Return the number of levels of child-windows under the current window.
60  */
61 static int
62 child_depth(WINDOW *cmp)
63 {
64     int depth = 0;
65
66     if (cmp != 0) {
67         WINDOWLIST *wp;
68
69         for (wp = _nc_windows; wp != 0; wp = wp->next) {
70             WINDOW *tst = &(wp->win);
71             if (tst->_parent == cmp) {
72                 depth = 1 + child_depth(tst);
73                 break;
74             }
75         }
76     }
77     return depth;
78 }
79
80 /*
81  * Return the number of levels of parent-windows above the current window.
82  */
83 static int
84 parent_depth(WINDOW *cmp)
85 {
86     int depth = 0;
87
88     if (cmp != 0) {
89         WINDOW *tst;
90         while ((tst = cmp->_parent) != 0) {
91             ++depth;
92             cmp = tst;
93         }
94     }
95     return depth;
96 }
97
98 /*
99  * FIXME: must adjust position so it's within the parent!
100  */
101 static int
102 adjust_window(WINDOW *win, int ToLines, int ToCols, int stolen)
103 {
104     int result;
105     int bottom = current_lines + SP->_topstolen - stolen;
106     int myLines = win->_maxy + 1;
107     int myCols = win->_maxx + 1;
108
109     T((T_CALLED("adjust_window(%p,%d,%d) currently %dx%d at %d,%d"),
110        win, ToLines, ToCols,
111        getmaxy(win), getmaxx(win),
112        getbegy(win), getbegx(win)));
113
114     if (win->_begy >= bottom) {
115         win->_begy += (ToLines - current_lines);
116     } else {
117         if (myLines == current_lines - stolen
118             && ToLines != current_lines)
119             myLines = ToLines - stolen;
120         else if (myLines == current_lines
121                  && ToLines != current_lines)
122             myLines = ToLines;
123     }
124
125     if (myLines > ToLines)
126         myLines = ToLines;
127
128     if (myCols > ToCols)
129         myCols = ToCols;
130
131     if (myLines == current_lines
132         && ToLines != current_lines)
133         myLines = ToLines;
134
135     if (myCols == current_cols
136         && ToCols != current_cols)
137         myCols = ToCols;
138
139     result = wresize(win, myLines, myCols);
140     returnCode(result);
141 }
142
143 /*
144  * If we're decreasing size, recursively search for windows that have no
145  * children, decrease those to fit, then decrease the containing window, etc.
146  */
147 static int
148 decrease_size(int ToLines, int ToCols, int stolen)
149 {
150     bool found;
151     int depth = 0;
152     WINDOWLIST *wp;
153
154     T((T_CALLED("decrease_size(%d, %d)"), ToLines, ToCols));
155
156     do {
157         found = FALSE;
158         TR(TRACE_UPDATE, ("decreasing size of windows to %dx%d, depth=%d",
159                           ToLines, ToCols, depth));
160         for (wp = _nc_windows; wp != 0; wp = wp->next) {
161             WINDOW *win = &(wp->win);
162
163             if (!(win->_flags & _ISPAD)) {
164                 if (child_depth(win) == depth) {
165                     if (adjust_window(win, ToLines, ToCols, stolen) != OK)
166                         returnCode(ERR);
167                 }
168             }
169         }
170         ++depth;
171     } while (found);
172     returnCode(OK);
173 }
174
175 /*
176  * If we're increasing size, recursively search for windows that have no
177  * parent, increase those to fit, then increase the contained window, etc.
178  */
179 static int
180 increase_size(int ToLines, int ToCols, int stolen)
181 {
182     bool found;
183     int depth = 0;
184     WINDOWLIST *wp;
185
186     T((T_CALLED("increase_size(%d, %d)"), ToLines, ToCols));
187
188     do {
189         found = FALSE;
190         TR(TRACE_UPDATE, ("increasing size of windows to %dx%d, depth=%d",
191                           ToLines, ToCols, depth));
192         for (wp = _nc_windows; wp != 0; wp = wp->next) {
193             WINDOW *win = &(wp->win);
194
195             if (!(win->_flags & _ISPAD)) {
196                 if (parent_depth(win) == depth) {
197                     if (adjust_window(win, ToLines, ToCols, stolen) != OK)
198                         returnCode(ERR);
199                 }
200             }
201         }
202         ++depth;
203     } while (found);
204     returnCode(OK);
205 }
206
207 /*
208  * This function reallocates NCURSES window structures, with no side-effects
209  * such as ungetch().
210  */
211 NCURSES_EXPORT(int)
212 resize_term(int ToLines, int ToCols)
213 {
214     int result = OK;
215     int was_stolen = (screen_lines - SP->_lines_avail);
216
217     T((T_CALLED("resize_term(%d,%d) old(%d,%d)"),
218        ToLines, ToCols,
219        screen_lines, screen_columns));
220
221     if (is_term_resized(ToLines, ToCols)) {
222         int myLines = current_lines = screen_lines;
223         int myCols = current_cols = screen_columns;
224
225         if (ToLines > screen_lines) {
226             increase_size(myLines = ToLines, myCols, was_stolen);
227             current_lines = myLines;
228             current_cols = myCols;
229         }
230
231         if (ToCols > screen_columns) {
232             increase_size(myLines, myCols = ToCols, was_stolen);
233             current_lines = myLines;
234             current_cols = myCols;
235         }
236
237         if (ToLines < myLines ||
238             ToCols < myCols) {
239             decrease_size(ToLines, ToCols, was_stolen);
240         }
241
242         screen_lines = lines = ToLines;
243         screen_columns = columns = ToCols;
244
245         SP->_lines_avail = lines - was_stolen;
246
247         if (SP->oldhash) {
248             FreeAndNull(SP->oldhash);
249         }
250         if (SP->newhash) {
251             FreeAndNull(SP->newhash);
252         }
253     }
254
255     /*
256      * Always update LINES, to allow for call from lib_doupdate.c which
257      * needs to have the count adjusted by the stolen (ripped off) lines.
258      */
259     LINES = ToLines - was_stolen;
260     COLS = ToCols;
261
262     returnCode(result);
263 }
264
265 /*
266  * This function reallocates NCURSES window structures.  It is invoked in
267  * response to a SIGWINCH interrupt.  Other user-defined windows may also need
268  * to be reallocated.
269  *
270  * Because this performs memory allocation, it should not (in general) be
271  * invoked directly from the signal handler.
272  */
273 NCURSES_EXPORT(int)
274 resizeterm(int ToLines, int ToCols)
275 {
276     int result = OK;
277
278     SP->_sig_winch = FALSE;
279
280     T((T_CALLED("resizeterm(%d,%d) old(%d,%d)"),
281        ToLines, ToCols,
282        screen_lines, screen_columns));
283
284     if (is_term_resized(ToLines, ToCols)) {
285
286 #if USE_SIGWINCH
287         ungetch(KEY_RESIZE);    /* so application can know this */
288         clearok(curscr, TRUE);  /* screen contents are unknown */
289 #endif
290
291         result = resize_term(ToLines, ToCols);
292     }
293
294     returnCode(result);
295 }