1b91476cbfd3dbced32d97e6943597da5f50752c
[ncurses.git] / ncurses / base / wresize.c
1 /****************************************************************************
2  * Copyright (c) 1998 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 <dickey@clark.net> 1996,1997                   *
31  ****************************************************************************/
32
33 #include <curses.priv.h>
34
35 MODULE_ID("$Id: wresize.c,v 1.12 1999/02/27 18:57:31 tom Exp $")
36
37 /*
38  * Reallocate a curses WINDOW struct to either shrink or grow to the specified
39  * new lines/columns.  If it grows, the new character cells are filled with
40  * blanks.  The application is responsible for repainting the blank area.
41  */
42
43 #define DOALLOC(p,t,n)  typeRealloc(t, n, p)
44 #define ld_ALLOC(p,n)   DOALLOC(p,struct ldat,n)
45 #define c_ALLOC(p,n)    DOALLOC(p,chtype,n)
46
47 int
48 wresize(WINDOW *win, int ToLines, int ToCols)
49 {
50         register int row;
51         int size_x, size_y;
52         struct ldat *pline;
53         chtype blank;
54
55 #ifdef TRACE
56         T((T_CALLED("wresize(%p,%d,%d)"), win, ToLines, ToCols));
57         if (win) {
58           TR(TRACE_UPDATE, ("...beg (%d, %d), max(%d,%d), reg(%d,%d)",
59                             win->_begy, win->_begx,
60                             win->_maxy, win->_maxx,
61                             win->_regtop, win->_regbottom));
62           if (_nc_tracing & TRACE_UPDATE)
63             _tracedump("...before", win);
64         }
65 #endif
66
67         if (!win || --ToLines < 0 || --ToCols < 0)
68                 returnCode(ERR);
69
70         size_x = win->_maxx;
71         size_y = win->_maxy;
72
73         if (ToLines == size_y
74          && ToCols  == size_x)
75                 returnCode(OK);
76
77         pline  = (win->_flags & _SUBWIN) ? win->_parent->_line : 0;
78
79         /*
80          * If the number of lines has changed, adjust the size of the overall
81          * vector:
82          */
83         if (ToLines != size_y) {
84                 if (! (win->_flags & _SUBWIN)) {
85                         for (row = ToLines+1; row <= size_y; row++)
86                                 free((char *)(win->_line[row].text));
87                 }
88
89                 win->_line = ld_ALLOC(win->_line, ToLines+1);
90                 if (win->_line == 0)
91                         returnCode(ERR);
92
93                 for (row = size_y+1; row <= ToLines; row++) {
94                         win->_line[row].text      = 0;
95                         win->_line[row].firstchar = 0;
96                         win->_line[row].lastchar  = ToCols;
97                         if ((win->_flags & _SUBWIN)) {
98                                 win->_line[row].text =
99                                 &pline[win->_begy + row].text[win->_begx];
100                         }
101                 }
102         }
103
104         /*
105          * Adjust the width of the columns:
106          */
107         blank = _nc_background(win);
108         for (row = 0; row <= ToLines; row++) {
109                 chtype  *s      = win->_line[row].text;
110                 int     begin   = (s == 0) ? 0 : size_x + 1;
111                 int     end     = ToCols;
112
113                 if_USE_SCROLL_HINTS(win->_line[row].oldindex = row);
114
115                 if (ToCols != size_x || s == 0) {
116                         if (! (win->_flags & _SUBWIN)) {
117                                 win->_line[row].text = s = c_ALLOC(s, ToCols+1);
118                                 if (win->_line[row].text == 0)
119                                         returnCode(ERR);
120                         } else if (s == 0) {
121                                 win->_line[row].text = s =
122                                 &pline[win->_begy + row].text[win->_begx];
123                         }
124
125                         if (end >= begin) {     /* growing */
126                                 if (win->_line[row].firstchar < begin)
127                                         win->_line[row].firstchar = begin;
128                                 win->_line[row].lastchar = ToCols;
129                                 do {
130                                         s[end] = blank;
131                                 } while (--end >= begin);
132                         } else {                /* shrinking */
133                                 win->_line[row].firstchar = 0;
134                                 win->_line[row].lastchar  = ToCols;
135                         }
136                 }
137         }
138
139         /*
140          * Finally, adjust the parameters showing screen size and cursor
141          * position:
142          */
143         win->_maxx = ToCols;
144         win->_maxy = ToLines;
145
146         if (win->_regtop > win->_maxy)
147                 win->_regtop = win->_maxy;
148         if (win->_regbottom > win->_maxy
149          || win->_regbottom == size_y)
150                 win->_regbottom = win->_maxy;
151
152         if (win->_curx > win->_maxx)
153                 win->_curx = win->_maxx;
154         if (win->_cury > win->_maxy)
155                 win->_cury = win->_maxy;
156
157 #ifdef TRACE
158         TR(TRACE_UPDATE, ("...beg (%d, %d), max(%d,%d), reg(%d,%d)",
159                 win->_begy, win->_begx,
160                 win->_maxy, win->_maxx,
161                 win->_regtop, win->_regbottom));
162         if (_nc_tracing & TRACE_UPDATE)
163                 _tracedump("...after:", win);
164 #endif
165         returnCode(OK);
166 }