ncurses 4.1
[ncurses.git] / ncurses / wresize.c
1 /******************************************************************************
2  * Copyright 1996,1997 by Thomas E. Dickey <dickey@clark.net>                 *
3  * All Rights Reserved.                                                       *
4  *                                                                            *
5  * Permission to use, copy, modify, and distribute this software and its      *
6  * documentation for any purpose and without fee is hereby granted, provided  *
7  * that the above copyright notice appear in all copies and that both that    *
8  * copyright notice and this permission notice appear in supporting           *
9  * documentation, and that the name of the above listed copyright holder(s)   *
10  * not be used in advertising or publicity pertaining to distribution of the  *
11  * software without specific, written prior permission. THE ABOVE LISTED      *
12  * COPYRIGHT HOLDER(S) DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,  *
13  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO     *
14  * EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY         *
15  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER       *
16  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF       *
17  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN        *
18  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.                   *
19  ******************************************************************************/
20
21 #include <curses.priv.h>
22 #include <term.h>
23
24 MODULE_ID("$Id: wresize.c,v 1.5 1997/02/01 23:22:54 tom Exp $")
25
26 /*
27  * Reallocate a curses WINDOW struct to either shrink or grow to the specified
28  * new lines/columns.  If it grows, the new character cells are filled with
29  * blanks.  The application is responsible for repainting the blank area.
30  */
31
32 static void *doalloc(void *p, size_t n)
33 {
34         if (p == 0)
35                 p = malloc(n);
36         else
37                 p = realloc(p, n);
38         return p;
39 }
40
41 #define DOALLOC(p,t,n)  (t *)doalloc(p, sizeof(t)*(n))
42 #define ld_ALLOC(p,n)   DOALLOC(p,struct ldat,n)
43 #define c_ALLOC(p,n)    DOALLOC(p,chtype,n)
44
45 int
46 wresize(WINDOW *win, int ToLines, int ToCols)
47 {
48         register int    row;
49         int     size_x, size_y;
50         struct ldat *pline = (win->_flags & _SUBWIN) ? win->_parent->_line : 0;
51
52 #ifdef TRACE
53         T((T_CALLED("wresize(%p,%d,%d)"), win, ToLines, ToCols));
54         TR(TRACE_UPDATE, ("...beg (%d, %d), max(%d,%d), reg(%d,%d)",
55                 win->_begy, win->_begx,
56                 win->_maxy, win->_maxx,
57                 win->_regtop, win->_regbottom));
58         if (_nc_tracing & TRACE_UPDATE)
59                 _tracedump("...before", win);
60 #endif
61
62         if (--ToLines < 0 || --ToCols < 0)
63                 returnCode(ERR);
64
65         size_x = win->_maxx;
66         size_y = win->_maxy;
67
68         if (ToLines == size_y
69          && ToCols  == size_x)
70                 returnCode(OK);
71
72         /*
73          * If the number of lines has changed, adjust the size of the overall
74          * vector:
75          */
76         if (ToLines != size_y) {
77                 if (! (win->_flags & _SUBWIN)) {
78                         for (row = ToLines+1; row <= size_y; row++)
79                                 free((char *)(win->_line[row].text));
80                 }
81
82                 win->_line = ld_ALLOC(win->_line, ToLines+1);
83                 if (win->_line == 0)
84                         returnCode(ERR);
85
86                 for (row = size_y+1; row <= ToLines; row++) {
87                         win->_line[row].text      = 0;
88                         win->_line[row].firstchar = 0;
89                         win->_line[row].lastchar  = ToCols;
90                         if ((win->_flags & _SUBWIN)) {
91                                 win->_line[row].text =
92                                 &pline[win->_begy + row].text[win->_begx];
93                         }
94                 }
95         }
96
97         /*
98          * Adjust the width of the columns:
99          */
100         for (row = 0; row <= ToLines; row++) {
101                 chtype  *s      = win->_line[row].text;
102                 int     begin   = (s == 0) ? 0 : size_x + 1;
103                 int     end     = ToCols;
104                 chtype  blank   = _nc_background(win);
105
106                 win->_line[row].oldindex = row;
107
108                 if (ToCols != size_x || s == 0) {
109                         if (! (win->_flags & _SUBWIN)) {
110                                 win->_line[row].text = s = c_ALLOC(s, ToCols+1);
111                                 if (win->_line[row].text == 0)
112                                         returnCode(ERR);
113                         } else if (s == 0) {
114                                 win->_line[row].text = s =
115                                 &pline[win->_begy + row].text[win->_begx];
116                         }
117
118                         if (end >= begin) {     /* growing */
119                                 if (win->_line[row].firstchar < begin)
120                                         win->_line[row].firstchar = begin;
121                                 win->_line[row].lastchar = ToCols;
122                                 do {
123                                         s[end] = blank;
124                                 } while (--end >= begin);
125                         } else {                /* shrinking */
126                                 win->_line[row].firstchar = 0;
127                                 win->_line[row].lastchar  = ToCols;
128                         }
129                 }
130         }
131
132         /*
133          * Finally, adjust the parameters showing screen size and cursor
134          * position:
135          */
136         win->_maxx = ToCols;
137         win->_maxy = ToLines;
138
139         if (win->_regtop > win->_maxy)
140                 win->_regtop = win->_maxy;
141         if (win->_regbottom > win->_maxy
142          || win->_regbottom == size_y)
143                 win->_regbottom = win->_maxy;
144
145         if (win->_curx > win->_maxx)
146                 win->_curx = win->_maxx;
147         if (win->_cury > win->_maxy)
148                 win->_cury = win->_maxy;
149
150 #ifdef TRACE
151         TR(TRACE_UPDATE, ("...beg (%d, %d), max(%d,%d), reg(%d,%d)",
152                 win->_begy, win->_begx,
153                 win->_maxy, win->_maxx,
154                 win->_regtop, win->_regbottom));
155         if (_nc_tracing & TRACE_UPDATE)
156                 _tracedump("...after:", win);
157 #endif
158         returnCode(OK);
159 }