]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/wresize.c
ncurses 5.7 - patch 20090906
[ncurses.git] / ncurses / base / wresize.c
1 /****************************************************************************
2  * Copyright (c) 1998-2008,2009 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.31 2009/05/23 19:50:16 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 = cmp->_maxy - tst->_pary;
75             if (tst->_maxx + tst->_parx > cmp->_maxx)
76                 tst->_maxx = 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)"), 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                     if ((s = typeMalloc(NCURSES_CH_T, ToCols + 1)) == 0)
161                         returnCode(cleanup_lines(new_lines, row));
162                     for (col = 0; col <= ToCols; ++col) {
163                         s[col] = (col <= size_x
164                                   ? win->_line[row].text[col]
165                                   : win->_nc_bkgd);
166                     }
167                 } else {
168                     s = win->_line[row].text;
169                 }
170             } else {
171                 if ((s = typeMalloc(NCURSES_CH_T, ToCols + 1)) == 0)
172                     returnCode(cleanup_lines(new_lines, row));
173                 for (col = 0; col <= ToCols; ++col)
174                     s[col] = win->_nc_bkgd;
175             }
176         } else {
177             s = &pline[win->_pary + row].text[win->_parx];
178         }
179
180         if_USE_SCROLL_HINTS(new_lines[row].oldindex = row);
181         if (row <= size_y) {
182             new_lines[row].firstchar = win->_line[row].firstchar;
183             new_lines[row].lastchar = win->_line[row].lastchar;
184         }
185         if ((ToCols != size_x) || (row > size_y)) {
186             if (end >= begin) { /* growing */
187                 if (new_lines[row].firstchar < begin)
188                     new_lines[row].firstchar = begin;
189             } else {            /* shrinking */
190                 new_lines[row].firstchar = 0;
191             }
192             new_lines[row].lastchar = ToCols;
193         }
194         new_lines[row].text = s;
195     }
196
197     /*
198      * Dispose of unwanted memory.
199      */
200     if (!(win->_flags & _SUBWIN)) {
201         if (ToCols == size_x) {
202             for (row = ToLines + 1; row <= size_y; row++) {
203                 free(win->_line[row].text);
204             }
205         } else {
206             for (row = 0; row <= size_y; row++) {
207                 free(win->_line[row].text);
208             }
209         }
210     }
211
212     free(win->_line);
213     win->_line = new_lines;
214
215     /*
216      * Finally, adjust the parameters showing screen size and cursor
217      * position:
218      */
219     win->_maxx = ToCols;
220     win->_maxy = ToLines;
221
222     if (win->_regtop > win->_maxy)
223         win->_regtop = win->_maxy;
224     if (win->_regbottom > win->_maxy
225         || win->_regbottom == size_y)
226         win->_regbottom = win->_maxy;
227
228     if (win->_curx > win->_maxx)
229         win->_curx = win->_maxx;
230     if (win->_cury > win->_maxy)
231         win->_cury = win->_maxy;
232
233     /*
234      * Check for subwindows of this one, and readjust pointers to our text,
235      * if needed.
236      */
237     repair_subwindows(win);
238
239 #ifdef TRACE
240     TR(TRACE_UPDATE, ("...beg (%ld, %ld), max(%ld,%ld), reg(%ld,%ld)",
241                       (long) win->_begy, (long) win->_begx,
242                       (long) win->_maxy, (long) win->_maxx,
243                       (long) win->_regtop, (long) win->_regbottom));
244     if (USE_TRACEF(TRACE_UPDATE)) {
245         _tracedump("...after:", win);
246         _nc_unlock_global(tracef);
247     }
248 #endif
249     returnCode(OK);
250 }