]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/wresize.c
ncurses 6.0 - patch 20170107
[ncurses.git] / ncurses / base / wresize.c
1 /****************************************************************************
2  * Copyright (c) 1998-2010,2011 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.35 2011/05/21 18:55:07 tom Exp $")
37
38 static int
39 cleanup_lines(struct ldat *data, int length)
40 {
41     while (--length >= 0)
42         free(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             if (tst->_pary > cmp->_maxy)
69                 tst->_pary = cmp->_maxy;
70             if (tst->_parx > cmp->_maxx)
71                 tst->_parx = cmp->_maxx;
72
73             if (tst->_maxy + tst->_pary > cmp->_maxy)
74                 tst->_maxy = (NCURSES_SIZE_T) (cmp->_maxy - tst->_pary);
75             if (tst->_maxx + tst->_parx > cmp->_maxx)
76                 tst->_maxx = (NCURSES_SIZE_T) (cmp->_maxx - tst->_parx);
77
78             for (row = 0; row <= tst->_maxy; ++row) {
79                 tst->_line[row].text = &pline[tst->_pary + row].text[tst->_parx];
80             }
81             repair_subwindows(tst);
82         }
83     }
84     _nc_unlock_global(curses);
85 }
86
87 /*
88  * Reallocate a curses WINDOW struct to either shrink or grow to the specified
89  * new lines/columns.  If it grows, the new character cells are filled with
90  * blanks.  The application is responsible for repainting the blank area.
91  */
92 NCURSES_EXPORT(int)
93 wresize(WINDOW *win, int ToLines, int ToCols)
94 {
95     int col, row, size_x, size_y;
96     struct ldat *pline;
97     struct ldat *new_lines = 0;
98
99 #ifdef TRACE
100     T((T_CALLED("wresize(%p,%d,%d)"), (void *) win, ToLines, ToCols));
101     if (win) {
102         TR(TRACE_UPDATE, ("...beg (%ld, %ld), max(%ld,%ld), reg(%ld,%ld)",
103                           (long) win->_begy, (long) win->_begx,
104                           (long) win->_maxy, (long) win->_maxx,
105                           (long) win->_regtop, (long) win->_regbottom));
106         if (USE_TRACEF(TRACE_UPDATE)) {
107             _tracedump("...before", win);
108             _nc_unlock_global(tracef);
109         }
110     }
111 #endif
112
113     if (!win || --ToLines < 0 || --ToCols < 0)
114         returnCode(ERR);
115
116     size_x = win->_maxx;
117     size_y = win->_maxy;
118
119     if (ToLines == size_y
120         && ToCols == size_x)
121         returnCode(OK);
122
123     if ((win->_flags & _SUBWIN)) {
124         /*
125          * Check if the new limits will fit into the parent window's size.  If
126          * not, do not resize.  We could adjust the location of the subwindow,
127          * but the application may not like that.
128          */
129         if (win->_pary + ToLines > win->_parent->_maxy
130             || win->_parx + ToCols > win->_parent->_maxx) {
131             returnCode(ERR);
132         }
133         pline = win->_parent->_line;
134     } else {
135         pline = 0;
136     }
137
138     /*
139      * Allocate new memory as needed.  Do the allocations without modifying
140      * the original window, in case an allocation fails.  Always allocate
141      * (at least temporarily) the array pointing to the individual lines.
142      */
143     new_lines = typeCalloc(struct ldat, (unsigned) (ToLines + 1));
144     if (new_lines == 0)
145         returnCode(ERR);
146
147     /*
148      * For each line in the target, allocate or adjust pointers for the
149      * corresponding text, depending on whether this is a window or a
150      * subwindow.
151      */
152     for (row = 0; row <= ToLines; ++row) {
153         int begin = (row > size_y) ? 0 : (size_x + 1);
154         int end = ToCols;
155         NCURSES_CH_T *s;
156
157         if (!(win->_flags & _SUBWIN)) {
158             if (row <= size_y) {
159                 if (ToCols != size_x) {
160                     s = typeMalloc(NCURSES_CH_T, (unsigned) ToCols + 1);
161                     if (s == 0)
162                         returnCode(cleanup_lines(new_lines, row));
163                     for (col = 0; col <= ToCols; ++col) {
164                         s[col] = (col <= size_x
165                                   ? win->_line[row].text[col]
166                                   : win->_nc_bkgd);
167                     }
168                 } else {
169                     s = win->_line[row].text;
170                 }
171             } else {
172                 s = typeMalloc(NCURSES_CH_T, (unsigned) ToCols + 1);
173                 if (s == 0)
174                     returnCode(cleanup_lines(new_lines, row));
175                 for (col = 0; col <= ToCols; ++col)
176                     s[col] = win->_nc_bkgd;
177             }
178         } else if (pline != 0 && pline[win->_pary + row].text != 0) {
179             s = &pline[win->_pary + row].text[win->_parx];
180         } else {
181             s = 0;
182         }
183
184         if_USE_SCROLL_HINTS(new_lines[row].oldindex = row);
185         if (row <= size_y) {
186             new_lines[row].firstchar = win->_line[row].firstchar;
187             new_lines[row].lastchar = win->_line[row].lastchar;
188         }
189         if ((ToCols != size_x) || (row > size_y)) {
190             if (end >= begin) { /* growing */
191                 if (new_lines[row].firstchar < begin)
192                     new_lines[row].firstchar = (NCURSES_SIZE_T) begin;
193             } else {            /* shrinking */
194                 new_lines[row].firstchar = 0;
195             }
196             new_lines[row].lastchar = (NCURSES_SIZE_T) ToCols;
197         }
198         new_lines[row].text = s;
199     }
200
201     /*
202      * Dispose of unwanted memory.
203      */
204     if (!(win->_flags & _SUBWIN)) {
205         if (ToCols == size_x) {
206             for (row = ToLines + 1; row <= size_y; row++) {
207                 free(win->_line[row].text);
208             }
209         } else {
210             for (row = 0; row <= size_y; row++) {
211                 free(win->_line[row].text);
212             }
213         }
214     }
215
216     free(win->_line);
217     win->_line = new_lines;
218
219     /*
220      * Finally, adjust the parameters showing screen size and cursor
221      * position:
222      */
223     win->_maxx = (NCURSES_SIZE_T) ToCols;
224     win->_maxy = (NCURSES_SIZE_T) ToLines;
225
226     if (win->_regtop > win->_maxy)
227         win->_regtop = win->_maxy;
228     if (win->_regbottom > win->_maxy
229         || win->_regbottom == size_y)
230         win->_regbottom = win->_maxy;
231
232     if (win->_curx > win->_maxx)
233         win->_curx = win->_maxx;
234     if (win->_cury > win->_maxy)
235         win->_cury = win->_maxy;
236
237     /*
238      * Check for subwindows of this one, and readjust pointers to our text,
239      * if needed.
240      */
241     repair_subwindows(win);
242
243 #ifdef TRACE
244     TR(TRACE_UPDATE, ("...beg (%ld, %ld), max(%ld,%ld), reg(%ld,%ld)",
245                       (long) win->_begy, (long) win->_begx,
246                       (long) win->_maxy, (long) win->_maxx,
247                       (long) win->_regtop, (long) win->_regbottom));
248     if (USE_TRACEF(TRACE_UPDATE)) {
249         _tracedump("...after:", win);
250         _nc_unlock_global(tracef);
251     }
252 #endif
253     returnCode(OK);
254 }