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 1996-2002 *
31 ****************************************************************************/
33 #include <curses.priv.h>
35 MODULE_ID("$Id: wresize.c,v 1.25 2007/04/07 17:13:42 tom Exp $")
38 cleanup_lines(struct ldat *data, int length)
41 free(data[length].text);
47 * If we have reallocated the ldat structs, we will have to repair pointers
51 repair_subwindows(WINDOW *cmp)
54 struct ldat *pline = cmp->_line;
57 for (wp = _nc_windows; wp != 0; wp = wp->next) {
58 WINDOW *tst = &(wp->win);
60 if (tst->_parent == cmp) {
62 if (tst->_pary > cmp->_maxy)
63 tst->_pary = cmp->_maxy;
64 if (tst->_parx > cmp->_maxx)
65 tst->_parx = cmp->_maxx;
67 if (tst->_maxy + tst->_pary > cmp->_maxy)
68 tst->_maxy = cmp->_maxy - tst->_pary;
69 if (tst->_maxx + tst->_parx > cmp->_maxx)
70 tst->_maxx = cmp->_maxx - tst->_parx;
72 for (row = 0; row <= tst->_maxy; ++row) {
73 tst->_line[row].text = &pline[tst->_pary + row].text[tst->_parx];
75 repair_subwindows(tst);
81 * Reallocate a curses WINDOW struct to either shrink or grow to the specified
82 * new lines/columns. If it grows, the new character cells are filled with
83 * blanks. The application is responsible for repainting the blank area.
86 wresize(WINDOW *win, int ToLines, int ToCols)
88 int col, row, size_x, size_y;
90 struct ldat *new_lines = 0;
93 T((T_CALLED("wresize(%p,%d,%d)"), win, ToLines, ToCols));
95 TR(TRACE_UPDATE, ("...beg (%ld, %ld), max(%ld,%ld), reg(%ld,%ld)",
96 (long) win->_begy, (long) win->_begx,
97 (long) win->_maxy, (long) win->_maxx,
98 (long) win->_regtop, (long) win->_regbottom));
99 if (_nc_tracing & TRACE_UPDATE)
100 _tracedump("...before", win);
104 if (!win || --ToLines < 0 || --ToCols < 0)
110 if (ToLines == size_y
114 if ((win->_flags & _SUBWIN)) {
116 * Check if the new limits will fit into the parent window's size. If
117 * not, do not resize. We could adjust the location of the subwindow,
118 * but the application may not like that.
120 if (win->_pary + ToLines > win->_parent->_maxy
121 || win->_parx + ToCols > win->_parent->_maxx) {
124 pline = win->_parent->_line;
130 * Allocate new memory as needed. Do the allocations without modifying
131 * the original window, in case an allocation fails. Always allocate
132 * (at least temporarily) the array pointing to the individual lines.
134 new_lines = typeCalloc(struct ldat, (unsigned) (ToLines + 1));
139 * For each line in the target, allocate or adjust pointers for the
140 * corresponding text, depending on whether this is a window or a
143 for (row = 0; row <= ToLines; ++row) {
144 int begin = (row > size_y) ? 0 : (size_x + 1);
148 if (!(win->_flags & _SUBWIN)) {
150 if (ToCols != size_x) {
151 if ((s = typeMalloc(NCURSES_CH_T, ToCols + 1)) == 0)
152 returnCode(cleanup_lines(new_lines, row));
153 for (col = 0; col <= ToCols; ++col) {
154 s[col] = (col <= size_x
155 ? win->_line[row].text[col]
159 s = win->_line[row].text;
162 if ((s = typeMalloc(NCURSES_CH_T, ToCols + 1)) == 0)
163 returnCode(cleanup_lines(new_lines, row));
164 for (col = 0; col <= ToCols; ++col)
165 s[col] = win->_nc_bkgd;
168 s = &pline[win->_pary + row].text[win->_parx];
171 if_USE_SCROLL_HINTS(new_lines[row].oldindex = row);
173 new_lines[row].firstchar = win->_line[row].firstchar;
174 new_lines[row].lastchar = win->_line[row].lastchar;
176 if ((ToCols != size_x) || (row > size_y)) {
177 if (end >= begin) { /* growing */
178 if (new_lines[row].firstchar < begin)
179 new_lines[row].firstchar = begin;
180 } else { /* shrinking */
181 new_lines[row].firstchar = 0;
183 new_lines[row].lastchar = ToCols;
185 new_lines[row].text = s;
189 * Dispose of unwanted memory.
191 if (!(win->_flags & _SUBWIN)) {
192 if (ToCols == size_x) {
193 for (row = ToLines + 1; row <= size_y; row++) {
194 free(win->_line[row].text);
197 for (row = 0; row <= size_y; row++) {
198 free(win->_line[row].text);
204 win->_line = new_lines;
207 * Finally, adjust the parameters showing screen size and cursor
211 win->_maxy = ToLines;
213 if (win->_regtop > win->_maxy)
214 win->_regtop = win->_maxy;
215 if (win->_regbottom > win->_maxy
216 || win->_regbottom == size_y)
217 win->_regbottom = win->_maxy;
219 if (win->_curx > win->_maxx)
220 win->_curx = win->_maxx;
221 if (win->_cury > win->_maxy)
222 win->_cury = win->_maxy;
225 * Check for subwindows of this one, and readjust pointers to our text,
228 repair_subwindows(win);
231 TR(TRACE_UPDATE, ("...beg (%ld, %ld), max(%ld,%ld), reg(%ld,%ld)",
232 (long) win->_begy, (long) win->_begx,
233 (long) win->_maxy, (long) win->_maxx,
234 (long) win->_regtop, (long) win->_regbottom));
235 if (_nc_tracing & TRACE_UPDATE)
236 _tracedump("...after:", win);