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