1 /****************************************************************************
2 * Copyright 2019-2023,2024 Thomas E. Dickey *
3 * Copyright 1998-2010,2011 Free Software Foundation, Inc. *
4 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29
30 /****************************************************************************
31 * Author: Thomas E. Dickey 1996-on *
32 * and: Juergen Pfeifer *
33 ****************************************************************************/
34
35 #include <curses.priv.h>
36
37 MODULE_ID("$Id: wresize.c,v 1.45 2024/12/07 18:08:56 tom Exp $")
38
39 static int
cleanup_lines(struct ldat * data,int length)40 cleanup_lines(struct ldat *data, int length)
41 {
42 while (--length >= 0)
43 FreeAndNull(data[length].text);
44 free(data);
45 return ERR;
46 }
47
48 /*
49 * If we have reallocated the ldat structs, we will have to repair pointers
50 * used in subwindows.
51 */
52 static void
repair_subwindows(WINDOW * cmp)53 repair_subwindows(WINDOW *cmp)
54 {
55 WINDOWLIST *wp;
56 struct ldat *pline = cmp->_line;
57 int row;
58 #if NCURSES_SP_FUNCS && defined(USE_SP_WINDOWLIST)
59 SCREEN *sp = _nc_screen_of(cmp);
60 #endif
61
62 _nc_lock_global(curses);
63
64 for (each_window(SP_PARM, wp)) {
65 WINDOW *tst = &(wp->win);
66
67 if (tst->_parent == cmp) {
68
69 #define REPAIR1(field, limit) \
70 if (tst->field > cmp->limit) \
71 tst->field = cmp->limit
72
73 REPAIR1(_pary, _maxy);
74 REPAIR1(_parx, _maxx);
75
76 #define REPAIR2(field, limit) \
77 if (tst->limit + tst->field > cmp->limit) \
78 tst->limit = (NCURSES_SIZE_T) (cmp->limit - tst->field)
79
80 REPAIR2(_pary, _maxy);
81 REPAIR2(_parx, _maxx);
82
83 #define REPAIR3(field, limit) \
84 if (tst->field > tst->limit) \
85 tst->field = tst->limit
86
87 REPAIR3(_cury, _maxy);
88 REPAIR3(_curx, _maxx);
89
90 REPAIR3(_regtop, _maxy);
91 REPAIR3(_regbottom, _maxy);
92
93 for (row = 0; row <= tst->_maxy; ++row) {
94 tst->_line[row].text = &pline[tst->_pary + row].text[tst->_parx];
95 }
96 repair_subwindows(tst);
97 }
98 }
99 _nc_unlock_global(curses);
100 }
101
102 /*
103 * Reallocate a curses WINDOW struct to either shrink or grow to the specified
104 * new lines/columns. If it grows, the new character cells are filled with
105 * blanks. The application is responsible for repainting the blank area.
106 */
107 NCURSES_EXPORT(int)
wresize(WINDOW * win,int ToLines,int ToCols)108 wresize(WINDOW *win, int ToLines, int ToCols)
109 {
110 int col, row, size_x, size_y;
111 struct ldat *pline;
112 struct ldat *new_lines = NULL;
113
114 #ifdef TRACE
115 T((T_CALLED("wresize(%p,%d,%d)"), (void *) win, ToLines, ToCols));
116 if (win) {
117 TR(TRACE_UPDATE, ("...beg (%ld, %ld), max(%ld,%ld), reg(%ld,%ld)",
118 (long) win->_begy, (long) win->_begx,
119 (long) win->_maxy, (long) win->_maxx,
120 (long) win->_regtop, (long) win->_regbottom));
121 if (USE_TRACEF(TRACE_UPDATE)) {
122 _tracedump("...before", win);
123 _nc_unlock_global(tracef);
124 }
125 }
126 #endif
127
128 if (!win || !OK_DIMENSION(ToLines) || !OK_DIMENSION(ToCols))
129 returnCode(ERR);
130
131 ToLines--;
132 ToCols--;
133 size_x = win->_maxx;
134 size_y = win->_maxy;
135
136 if (ToLines == size_y
137 && ToCols == size_x)
138 returnCode(OK);
139
140 if (IS_SUBWIN(win)) {
141 /*
142 * Check if the new limits will fit into the parent window's size. If
143 * not, do not resize. We could adjust the location of the subwindow,
144 * but the application may not like that.
145 */
146 if (win->_pary + ToLines > win->_parent->_maxy
147 || win->_parx + ToCols > win->_parent->_maxx) {
148 returnCode(ERR);
149 }
150 pline = win->_parent->_line;
151 } else {
152 pline = NULL;
153 }
154
155 /*
156 * Allocate new memory as needed. Do the allocations without modifying
157 * the original window, in case an allocation fails. Always allocate
158 * (at least temporarily) the array pointing to the individual lines.
159 */
160 new_lines = typeCalloc(struct ldat, (unsigned) (ToLines + 1));
161 if (new_lines == NULL)
162 returnCode(ERR);
163
164 /*
165 * For each line in the target, allocate or adjust pointers for the
166 * corresponding text, depending on whether this is a window or a
167 * subwindow.
168 */
169 for (row = 0; row <= ToLines; ++row) {
170 int begin = (row > size_y) ? 0 : (size_x + 1);
171 int end = ToCols;
172 NCURSES_CH_T *s;
173
174 if (!IS_SUBWIN(win)) {
175 if (row <= size_y) {
176 if (ToCols != size_x) {
177 s = typeMalloc(NCURSES_CH_T, (unsigned) ToCols + 1);
178 if (s == NULL)
179 returnCode(cleanup_lines(new_lines, row));
180 for (col = 0; col <= ToCols; ++col) {
181 bool valid = (col <= size_x);
182 if_WIDEC({
183 if (col == ToCols
184 && col < size_x
185 && isWidecBase(win->_line[row].text[col])) {
186 valid = FALSE;
187 }
188 });
189 s[col] = (valid
190 ? win->_line[row].text[col]
191 : win->_nc_bkgd);
192 }
193 } else {
194 s = win->_line[row].text;
195 }
196 } else {
197 s = typeMalloc(NCURSES_CH_T, (unsigned) ToCols + 1);
198 if (s == NULL)
199 returnCode(cleanup_lines(new_lines, row));
200 for (col = 0; col <= ToCols; ++col)
201 s[col] = win->_nc_bkgd;
202 }
203 } else if (pline != NULL && pline[win->_pary + row].text != NULL) {
204 s = &pline[win->_pary + row].text[win->_parx];
205 } else {
206 s = NULL;
207 }
208
209 if_USE_SCROLL_HINTS(new_lines[row].oldindex = row);
210 if (row <= size_y) {
211 new_lines[row].firstchar = win->_line[row].firstchar;
212 new_lines[row].lastchar = win->_line[row].lastchar;
213 }
214 if ((ToCols != size_x) || (row > size_y)) {
215 if (end >= begin) { /* growing */
216 if (new_lines[row].firstchar < begin)
217 new_lines[row].firstchar = (NCURSES_SIZE_T) begin;
218 } else { /* shrinking */
219 new_lines[row].firstchar = 0;
220 }
221 new_lines[row].lastchar = (NCURSES_SIZE_T) ToCols;
222 }
223 new_lines[row].text = s;
224 }
225
226 /*
227 * Dispose of unwanted memory.
228 */
229 if (!(win->_flags & _SUBWIN)) {
230 if (ToCols == size_x) {
231 for (row = ToLines + 1; row <= size_y; row++) {
232 FreeAndNull(win->_line[row].text);
233 }
234 } else {
235 for (row = 0; row <= size_y; row++) {
236 FreeAndNull(win->_line[row].text);
237 }
238 }
239 }
240
241 FreeAndNull(win->_line);
242 win->_line = new_lines;
243
244 /*
245 * Finally, adjust the parameters showing screen size and cursor
246 * position:
247 */
248 win->_maxx = (NCURSES_SIZE_T) ToCols;
249 win->_maxy = (NCURSES_SIZE_T) ToLines;
250
251 if (win->_regtop > win->_maxy)
252 win->_regtop = win->_maxy;
253 if (win->_regbottom > win->_maxy
254 || win->_regbottom == size_y)
255 win->_regbottom = win->_maxy;
256
257 if (win->_curx > win->_maxx)
258 win->_curx = win->_maxx;
259 if (win->_cury > win->_maxy)
260 win->_cury = win->_maxy;
261
262 /*
263 * Check for subwindows of this one, and readjust pointers to our text,
264 * if needed.
265 */
266 repair_subwindows(win);
267
268 #ifdef TRACE
269 TR(TRACE_UPDATE, ("...beg (%ld, %ld), max(%ld,%ld), reg(%ld,%ld)",
270 (long) win->_begy, (long) win->_begx,
271 (long) win->_maxy, (long) win->_maxx,
272 (long) win->_regtop, (long) win->_regbottom));
273 if (USE_TRACEF(TRACE_UPDATE)) {
274 _tracedump("...after:", win);
275 _nc_unlock_global(tracef);
276 }
277 #endif
278 returnCode(OK);
279 }
280