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 1996-2002 *
31 ****************************************************************************/
33 #include <curses.priv.h>
35 MODULE_ID("$Id: wresize.c,v 1.29 2008/06/07 13:59:01 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 _nc_lock_global(curses);
59 for (each_window(wp)) {
60 WINDOW *tst = &(wp->win);
62 if (tst->_parent == cmp) {
64 if (tst->_pary > cmp->_maxy)
65 tst->_pary = cmp->_maxy;
66 if (tst->_parx > cmp->_maxx)
67 tst->_parx = cmp->_maxx;
69 if (tst->_maxy + tst->_pary > cmp->_maxy)
70 tst->_maxy = cmp->_maxy - tst->_pary;
71 if (tst->_maxx + tst->_parx > cmp->_maxx)
72 tst->_maxx = cmp->_maxx - tst->_parx;
74 for (row = 0; row <= tst->_maxy; ++row) {
75 tst->_line[row].text = &pline[tst->_pary + row].text[tst->_parx];
77 repair_subwindows(tst);
80 _nc_unlock_global(curses);
84 * Reallocate a curses WINDOW struct to either shrink or grow to the specified
85 * new lines/columns. If it grows, the new character cells are filled with
86 * blanks. The application is responsible for repainting the blank area.
89 wresize(WINDOW *win, int ToLines, int ToCols)
91 int col, row, size_x, size_y;
93 struct ldat *new_lines = 0;
96 T((T_CALLED("wresize(%p,%d,%d)"), win, ToLines, ToCols));
98 TR(TRACE_UPDATE, ("...beg (%ld, %ld), max(%ld,%ld), reg(%ld,%ld)",
99 (long) win->_begy, (long) win->_begx,
100 (long) win->_maxy, (long) win->_maxx,
101 (long) win->_regtop, (long) win->_regbottom));
102 if (USE_TRACEF(TRACE_UPDATE)) {
103 _tracedump("...before", win);
104 _nc_unlock_global(tracef);
109 if (!win || --ToLines < 0 || --ToCols < 0)
115 if (ToLines == size_y
119 if ((win->_flags & _SUBWIN)) {
121 * Check if the new limits will fit into the parent window's size. If
122 * not, do not resize. We could adjust the location of the subwindow,
123 * but the application may not like that.
125 if (win->_pary + ToLines > win->_parent->_maxy
126 || win->_parx + ToCols > win->_parent->_maxx) {
129 pline = win->_parent->_line;
135 * Allocate new memory as needed. Do the allocations without modifying
136 * the original window, in case an allocation fails. Always allocate
137 * (at least temporarily) the array pointing to the individual lines.
139 new_lines = typeCalloc(struct ldat, (unsigned) (ToLines + 1));
144 * For each line in the target, allocate or adjust pointers for the
145 * corresponding text, depending on whether this is a window or a
148 for (row = 0; row <= ToLines; ++row) {
149 int begin = (row > size_y) ? 0 : (size_x + 1);
153 if (!(win->_flags & _SUBWIN)) {
155 if (ToCols != size_x) {
156 if ((s = typeMalloc(NCURSES_CH_T, ToCols + 1)) == 0)
157 returnCode(cleanup_lines(new_lines, row));
158 for (col = 0; col <= ToCols; ++col) {
159 s[col] = (col <= size_x
160 ? win->_line[row].text[col]
164 s = win->_line[row].text;
167 if ((s = typeMalloc(NCURSES_CH_T, ToCols + 1)) == 0)
168 returnCode(cleanup_lines(new_lines, row));
169 for (col = 0; col <= ToCols; ++col)
170 s[col] = win->_nc_bkgd;
173 s = &pline[win->_pary + row].text[win->_parx];
176 if_USE_SCROLL_HINTS(new_lines[row].oldindex = row);
178 new_lines[row].firstchar = win->_line[row].firstchar;
179 new_lines[row].lastchar = win->_line[row].lastchar;
181 if ((ToCols != size_x) || (row > size_y)) {
182 if (end >= begin) { /* growing */
183 if (new_lines[row].firstchar < begin)
184 new_lines[row].firstchar = begin;
185 } else { /* shrinking */
186 new_lines[row].firstchar = 0;
188 new_lines[row].lastchar = ToCols;
190 new_lines[row].text = s;
194 * Dispose of unwanted memory.
196 if (!(win->_flags & _SUBWIN)) {
197 if (ToCols == size_x) {
198 for (row = ToLines + 1; row <= size_y; row++) {
199 free(win->_line[row].text);
202 for (row = 0; row <= size_y; row++) {
203 free(win->_line[row].text);
209 win->_line = new_lines;
212 * Finally, adjust the parameters showing screen size and cursor
216 win->_maxy = ToLines;
218 if (win->_regtop > win->_maxy)
219 win->_regtop = win->_maxy;
220 if (win->_regbottom > win->_maxy
221 || win->_regbottom == size_y)
222 win->_regbottom = win->_maxy;
224 if (win->_curx > win->_maxx)
225 win->_curx = win->_maxx;
226 if (win->_cury > win->_maxy)
227 win->_cury = win->_maxy;
230 * Check for subwindows of this one, and readjust pointers to our text,
233 repair_subwindows(win);
236 TR(TRACE_UPDATE, ("...beg (%ld, %ld), max(%ld,%ld), reg(%ld,%ld)",
237 (long) win->_begy, (long) win->_begx,
238 (long) win->_maxy, (long) win->_maxx,
239 (long) win->_regtop, (long) win->_regbottom));
240 if (USE_TRACEF(TRACE_UPDATE)) {
241 _tracedump("...after:", win);
242 _nc_unlock_global(tracef);