1 /****************************************************************************
2 * Copyright (c) 1998-2011,2019 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 1996-on *
31 * and: Juergen Pfeifer *
32 ****************************************************************************/
34 #include <curses.priv.h>
36 MODULE_ID("$Id: wresize.c,v 1.38 2019/05/11 20:15:15 tom Exp $")
39 cleanup_lines(struct ldat *data, int length)
42 FreeAndNull(data[length].text);
48 * If we have reallocated the ldat structs, we will have to repair pointers
52 repair_subwindows(WINDOW *cmp)
55 struct ldat *pline = cmp->_line;
57 #ifdef USE_SP_WINDOWLIST
58 SCREEN *sp = _nc_screen_of(cmp);
61 _nc_lock_global(curses);
63 for (each_window(SP_PARM, wp)) {
64 WINDOW *tst = &(wp->win);
66 if (tst->_parent == cmp) {
68 #define REPAIR1(field, limit) \
69 if (tst->field > cmp->limit) \
70 tst->field = cmp->limit
72 REPAIR1(_pary, _maxy);
73 REPAIR1(_parx, _maxx);
75 #define REPAIR2(field, limit) \
76 if (tst->limit + tst->field > cmp->limit) \
77 tst->limit = (NCURSES_SIZE_T) (cmp->limit - tst->field)
79 REPAIR2(_pary, _maxy);
80 REPAIR2(_parx, _maxx);
82 #define REPAIR3(field, limit) \
83 if (tst->field > tst->limit) \
84 tst->field = tst->limit
86 REPAIR3(_cury, _maxy);
87 REPAIR3(_curx, _maxx);
89 REPAIR3(_regtop, _maxy);
90 REPAIR3(_regbottom, _maxy);
92 for (row = 0; row <= tst->_maxy; ++row) {
93 tst->_line[row].text = &pline[tst->_pary + row].text[tst->_parx];
95 repair_subwindows(tst);
98 _nc_unlock_global(curses);
102 * Reallocate a curses WINDOW struct to either shrink or grow to the specified
103 * new lines/columns. If it grows, the new character cells are filled with
104 * blanks. The application is responsible for repainting the blank area.
107 wresize(WINDOW *win, int ToLines, int ToCols)
109 int col, row, size_x, size_y;
111 struct ldat *new_lines = 0;
114 T((T_CALLED("wresize(%p,%d,%d)"), (void *) win, ToLines, ToCols));
116 TR(TRACE_UPDATE, ("...beg (%ld, %ld), max(%ld,%ld), reg(%ld,%ld)",
117 (long) win->_begy, (long) win->_begx,
118 (long) win->_maxy, (long) win->_maxx,
119 (long) win->_regtop, (long) win->_regbottom));
120 if (USE_TRACEF(TRACE_UPDATE)) {
121 _tracedump("...before", win);
122 _nc_unlock_global(tracef);
127 if (!win || --ToLines < 0 || --ToCols < 0)
133 if (ToLines == size_y
137 if ((win->_flags & _SUBWIN)) {
139 * Check if the new limits will fit into the parent window's size. If
140 * not, do not resize. We could adjust the location of the subwindow,
141 * but the application may not like that.
143 if (win->_pary + ToLines > win->_parent->_maxy
144 || win->_parx + ToCols > win->_parent->_maxx) {
147 pline = win->_parent->_line;
153 * Allocate new memory as needed. Do the allocations without modifying
154 * the original window, in case an allocation fails. Always allocate
155 * (at least temporarily) the array pointing to the individual lines.
157 new_lines = typeCalloc(struct ldat, (unsigned) (ToLines + 1));
162 * For each line in the target, allocate or adjust pointers for the
163 * corresponding text, depending on whether this is a window or a
166 for (row = 0; row <= ToLines; ++row) {
167 int begin = (row > size_y) ? 0 : (size_x + 1);
171 if (!(win->_flags & _SUBWIN)) {
173 if (ToCols != size_x) {
174 s = typeMalloc(NCURSES_CH_T, (unsigned) ToCols + 1);
176 returnCode(cleanup_lines(new_lines, row));
177 for (col = 0; col <= ToCols; ++col) {
178 s[col] = (col <= size_x
179 ? win->_line[row].text[col]
183 s = win->_line[row].text;
186 s = typeMalloc(NCURSES_CH_T, (unsigned) ToCols + 1);
188 returnCode(cleanup_lines(new_lines, row));
189 for (col = 0; col <= ToCols; ++col)
190 s[col] = win->_nc_bkgd;
192 } else if (pline != 0 && pline[win->_pary + row].text != 0) {
193 s = &pline[win->_pary + row].text[win->_parx];
198 if_USE_SCROLL_HINTS(new_lines[row].oldindex = row);
200 new_lines[row].firstchar = win->_line[row].firstchar;
201 new_lines[row].lastchar = win->_line[row].lastchar;
203 if ((ToCols != size_x) || (row > size_y)) {
204 if (end >= begin) { /* growing */
205 if (new_lines[row].firstchar < begin)
206 new_lines[row].firstchar = (NCURSES_SIZE_T) begin;
207 } else { /* shrinking */
208 new_lines[row].firstchar = 0;
210 new_lines[row].lastchar = (NCURSES_SIZE_T) ToCols;
212 new_lines[row].text = s;
216 * Dispose of unwanted memory.
218 if (!(win->_flags & _SUBWIN)) {
219 if (ToCols == size_x) {
220 for (row = ToLines + 1; row <= size_y; row++) {
221 FreeAndNull(win->_line[row].text);
224 for (row = 0; row <= size_y; row++) {
225 FreeAndNull(win->_line[row].text);
230 FreeAndNull(win->_line);
231 win->_line = new_lines;
234 * Finally, adjust the parameters showing screen size and cursor
237 win->_maxx = (NCURSES_SIZE_T) ToCols;
238 win->_maxy = (NCURSES_SIZE_T) ToLines;
240 if (win->_regtop > win->_maxy)
241 win->_regtop = win->_maxy;
242 if (win->_regbottom > win->_maxy
243 || win->_regbottom == size_y)
244 win->_regbottom = win->_maxy;
246 if (win->_curx > win->_maxx)
247 win->_curx = win->_maxx;
248 if (win->_cury > win->_maxy)
249 win->_cury = win->_maxy;
252 * Check for subwindows of this one, and readjust pointers to our text,
255 repair_subwindows(win);
258 TR(TRACE_UPDATE, ("...beg (%ld, %ld), max(%ld,%ld), reg(%ld,%ld)",
259 (long) win->_begy, (long) win->_begx,
260 (long) win->_maxy, (long) win->_maxx,
261 (long) win->_regtop, (long) win->_regbottom));
262 if (USE_TRACEF(TRACE_UPDATE)) {
263 _tracedump("...after:", win);
264 _nc_unlock_global(tracef);