]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/wresize.c
ncurses 6.1 - patch 20191207
[ncurses.git] / ncurses / base / wresize.c
1 /****************************************************************************
2  * Copyright (c) 1998-2011,2019 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-on                                        *
31  *     and: Juergen Pfeifer                                                 *
32  ****************************************************************************/
33
34 #include <curses.priv.h>
35
36 MODULE_ID("$Id: wresize.c,v 1.38 2019/05/11 20:15:15 tom Exp $")
37
38 static int
39 cleanup_lines(struct ldat *data, int length)
40 {
41     while (--length >= 0)
42         FreeAndNull(data[length].text);
43     free(data);
44     return ERR;
45 }
46
47 /*
48  * If we have reallocated the ldat structs, we will have to repair pointers
49  * used in subwindows.
50  */
51 static void
52 repair_subwindows(WINDOW *cmp)
53 {
54     WINDOWLIST *wp;
55     struct ldat *pline = cmp->_line;
56     int row;
57 #ifdef USE_SP_WINDOWLIST
58     SCREEN *sp = _nc_screen_of(cmp);
59 #endif
60
61     _nc_lock_global(curses);
62
63     for (each_window(SP_PARM, wp)) {
64         WINDOW *tst = &(wp->win);
65
66         if (tst->_parent == cmp) {
67
68 #define REPAIR1(field, limit) \
69             if (tst->field > cmp->limit) \
70                 tst->field = cmp->limit
71
72             REPAIR1(_pary, _maxy);
73             REPAIR1(_parx, _maxx);
74
75 #define REPAIR2(field, limit) \
76             if (tst->limit + tst->field > cmp->limit) \
77                 tst->limit = (NCURSES_SIZE_T) (cmp->limit - tst->field)
78
79             REPAIR2(_pary, _maxy);
80             REPAIR2(_parx, _maxx);
81
82 #define REPAIR3(field, limit) \
83             if (tst->field > tst->limit) \
84                 tst->field = tst->limit
85
86             REPAIR3(_cury, _maxy);
87             REPAIR3(_curx, _maxx);
88
89             REPAIR3(_regtop, _maxy);
90             REPAIR3(_regbottom, _maxy);
91
92             for (row = 0; row <= tst->_maxy; ++row) {
93                 tst->_line[row].text = &pline[tst->_pary + row].text[tst->_parx];
94             }
95             repair_subwindows(tst);
96         }
97     }
98     _nc_unlock_global(curses);
99 }
100
101 /*
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.
105  */
106 NCURSES_EXPORT(int)
107 wresize(WINDOW *win, int ToLines, int ToCols)
108 {
109     int col, row, size_x, size_y;
110     struct ldat *pline;
111     struct ldat *new_lines = 0;
112
113 #ifdef TRACE
114     T((T_CALLED("wresize(%p,%d,%d)"), (void *) win, ToLines, ToCols));
115     if (win) {
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);
123         }
124     }
125 #endif
126
127     if (!win || --ToLines < 0 || --ToCols < 0)
128         returnCode(ERR);
129
130     size_x = win->_maxx;
131     size_y = win->_maxy;
132
133     if (ToLines == size_y
134         && ToCols == size_x)
135         returnCode(OK);
136
137     if ((win->_flags & _SUBWIN)) {
138         /*
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.
142          */
143         if (win->_pary + ToLines > win->_parent->_maxy
144             || win->_parx + ToCols > win->_parent->_maxx) {
145             returnCode(ERR);
146         }
147         pline = win->_parent->_line;
148     } else {
149         pline = 0;
150     }
151
152     /*
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.
156      */
157     new_lines = typeCalloc(struct ldat, (unsigned) (ToLines + 1));
158     if (new_lines == 0)
159         returnCode(ERR);
160
161     /*
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
164      * subwindow.
165      */
166     for (row = 0; row <= ToLines; ++row) {
167         int begin = (row > size_y) ? 0 : (size_x + 1);
168         int end = ToCols;
169         NCURSES_CH_T *s;
170
171         if (!(win->_flags & _SUBWIN)) {
172             if (row <= size_y) {
173                 if (ToCols != size_x) {
174                     s = typeMalloc(NCURSES_CH_T, (unsigned) ToCols + 1);
175                     if (s == 0)
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]
180                                   : win->_nc_bkgd);
181                     }
182                 } else {
183                     s = win->_line[row].text;
184                 }
185             } else {
186                 s = typeMalloc(NCURSES_CH_T, (unsigned) ToCols + 1);
187                 if (s == 0)
188                     returnCode(cleanup_lines(new_lines, row));
189                 for (col = 0; col <= ToCols; ++col)
190                     s[col] = win->_nc_bkgd;
191             }
192         } else if (pline != 0 && pline[win->_pary + row].text != 0) {
193             s = &pline[win->_pary + row].text[win->_parx];
194         } else {
195             s = 0;
196         }
197
198         if_USE_SCROLL_HINTS(new_lines[row].oldindex = row);
199         if (row <= size_y) {
200             new_lines[row].firstchar = win->_line[row].firstchar;
201             new_lines[row].lastchar = win->_line[row].lastchar;
202         }
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;
209             }
210             new_lines[row].lastchar = (NCURSES_SIZE_T) ToCols;
211         }
212         new_lines[row].text = s;
213     }
214
215     /*
216      * Dispose of unwanted memory.
217      */
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);
222             }
223         } else {
224             for (row = 0; row <= size_y; row++) {
225                 FreeAndNull(win->_line[row].text);
226             }
227         }
228     }
229
230     FreeAndNull(win->_line);
231     win->_line = new_lines;
232
233     /*
234      * Finally, adjust the parameters showing screen size and cursor
235      * position:
236      */
237     win->_maxx = (NCURSES_SIZE_T) ToCols;
238     win->_maxy = (NCURSES_SIZE_T) ToLines;
239
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;
245
246     if (win->_curx > win->_maxx)
247         win->_curx = win->_maxx;
248     if (win->_cury > win->_maxy)
249         win->_cury = win->_maxy;
250
251     /*
252      * Check for subwindows of this one, and readjust pointers to our text,
253      * if needed.
254      */
255     repair_subwindows(win);
256
257 #ifdef TRACE
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);
265     }
266 #endif
267     returnCode(OK);
268 }