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