wresize.c revision 184989
1/**************************************************************************** 2 * Copyright (c) 1998-2007,2008 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 1996-2002 * 31 ****************************************************************************/ 32 33#include <curses.priv.h> 34 35MODULE_ID("$Id: wresize.c,v 1.29 2008/06/07 13:59:01 tom Exp $") 36 37static int 38cleanup_lines(struct ldat *data, int length) 39{ 40 while (--length >= 0) 41 free(data[length].text); 42 free(data); 43 return ERR; 44} 45 46/* 47 * If we have reallocated the ldat structs, we will have to repair pointers 48 * used in subwindows. 49 */ 50static void 51repair_subwindows(WINDOW *cmp) 52{ 53 WINDOWLIST *wp; 54 struct ldat *pline = cmp->_line; 55 int row; 56 57 _nc_lock_global(curses); 58 59 for (each_window(wp)) { 60 WINDOW *tst = &(wp->win); 61 62 if (tst->_parent == cmp) { 63 64 if (tst->_pary > cmp->_maxy) 65 tst->_pary = cmp->_maxy; 66 if (tst->_parx > cmp->_maxx) 67 tst->_parx = cmp->_maxx; 68 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; 73 74 for (row = 0; row <= tst->_maxy; ++row) { 75 tst->_line[row].text = &pline[tst->_pary + row].text[tst->_parx]; 76 } 77 repair_subwindows(tst); 78 } 79 } 80 _nc_unlock_global(curses); 81} 82 83/* 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. 87 */ 88NCURSES_EXPORT(int) 89wresize(WINDOW *win, int ToLines, int ToCols) 90{ 91 int col, row, size_x, size_y; 92 struct ldat *pline; 93 struct ldat *new_lines = 0; 94 95#ifdef TRACE 96 T((T_CALLED("wresize(%p,%d,%d)"), win, ToLines, ToCols)); 97 if (win) { 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); 105 } 106 } 107#endif 108 109 if (!win || --ToLines < 0 || --ToCols < 0) 110 returnCode(ERR); 111 112 size_x = win->_maxx; 113 size_y = win->_maxy; 114 115 if (ToLines == size_y 116 && ToCols == size_x) 117 returnCode(OK); 118 119 if ((win->_flags & _SUBWIN)) { 120 /* 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. 124 */ 125 if (win->_pary + ToLines > win->_parent->_maxy 126 || win->_parx + ToCols > win->_parent->_maxx) { 127 returnCode(ERR); 128 } 129 pline = win->_parent->_line; 130 } else { 131 pline = 0; 132 } 133 134 /* 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. 138 */ 139 new_lines = typeCalloc(struct ldat, (unsigned) (ToLines + 1)); 140 if (new_lines == 0) 141 returnCode(ERR); 142 143 /* 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 146 * subwindow. 147 */ 148 for (row = 0; row <= ToLines; ++row) { 149 int begin = (row > size_y) ? 0 : (size_x + 1); 150 int end = ToCols; 151 NCURSES_CH_T *s; 152 153 if (!(win->_flags & _SUBWIN)) { 154 if (row <= size_y) { 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] 161 : win->_nc_bkgd); 162 } 163 } else { 164 s = win->_line[row].text; 165 } 166 } else { 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; 171 } 172 } else { 173 s = &pline[win->_pary + row].text[win->_parx]; 174 } 175 176 if_USE_SCROLL_HINTS(new_lines[row].oldindex = row); 177 if (row <= size_y) { 178 new_lines[row].firstchar = win->_line[row].firstchar; 179 new_lines[row].lastchar = win->_line[row].lastchar; 180 } 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; 187 } 188 new_lines[row].lastchar = ToCols; 189 } 190 new_lines[row].text = s; 191 } 192 193 /* 194 * Dispose of unwanted memory. 195 */ 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); 200 } 201 } else { 202 for (row = 0; row <= size_y; row++) { 203 free(win->_line[row].text); 204 } 205 } 206 } 207 208 free(win->_line); 209 win->_line = new_lines; 210 211 /* 212 * Finally, adjust the parameters showing screen size and cursor 213 * position: 214 */ 215 win->_maxx = ToCols; 216 win->_maxy = ToLines; 217 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; 223 224 if (win->_curx > win->_maxx) 225 win->_curx = win->_maxx; 226 if (win->_cury > win->_maxy) 227 win->_cury = win->_maxy; 228 229 /* 230 * Check for subwindows of this one, and readjust pointers to our text, 231 * if needed. 232 */ 233 repair_subwindows(win); 234 235#ifdef TRACE 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); 243 } 244#endif 245 returnCode(OK); 246} 247