1 /****************************************************************************
2  * Copyright (c) 1998,1999,2000 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: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  ****************************************************************************/
33 
34 /*
35 **	lib_newwin.c
36 **
37 **	The routines newwin(), subwin() and their dependent
38 **
39 */
40 
41 #include <curses.priv.h>
42 
43 MODULE_ID("$Id: lib_newwin.c,v 1.24 2000/04/29 18:49:51 tom Exp $")
44 
45 void
46 _nc_freewin(WINDOW *win)
47 {
48     WINDOWLIST *p, *q;
49     int i;
50 
51     if (win != 0) {
52 	for (p = _nc_windows, q = 0; p != 0; q = p, p = p->next) {
53 	    if (p->win == win) {
54 		if (q == 0)
55 		    _nc_windows = p->next;
56 		else
57 		    q->next = p->next;
58 		free(p);
59 
60 		if (!(win->_flags & _SUBWIN)) {
61 		    for (i = 0; i <= win->_maxy; i++)
62 			FreeIfNeeded(win->_line[i].text);
63 		}
64 		free(win->_line);
65 		free(win);
66 
67 		if (win == curscr)
68 		    curscr = 0;
69 		if (win == stdscr)
70 		    stdscr = 0;
71 		if (win == newscr)
72 		    newscr = 0;
73 
74 		T(("...deleted win=%p", win));
75 		break;
76 	    }
77 	}
78     }
79 }
80 
81 WINDOW *
82 newwin(int num_lines, int num_columns, int begy, int begx)
83 {
84     WINDOW *win;
85     chtype *ptr;
86     int i;
87 
88     T((T_CALLED("newwin(%d,%d,%d,%d)"), num_lines, num_columns, begy, begx));
89 
90     if (begy < 0 || begx < 0 || num_lines < 0 || num_columns < 0)
91 	returnWin(0);
92 
93     if (num_lines == 0)
94 	num_lines = SP->_lines_avail - begy;
95     if (num_columns == 0)
96 	num_columns = screen_columns - begx;
97 
98     if (num_columns + begx > SP->_columns || num_lines + begy > SP->_lines_avail)
99 	returnWin(0);
100 
101     if ((win = _nc_makenew(num_lines, num_columns, begy, begx, 0)) == 0)
102 	returnWin(0);
103 
104     for (i = 0; i < num_lines; i++) {
105 	win->_line[i].text = typeCalloc(chtype, (unsigned) num_columns);
106 	if (win->_line[i].text == 0) {
107 	    _nc_freewin(win);
108 	    returnWin(0);
109 	}
110 	for (ptr = win->_line[i].text; ptr < win->_line[i].text +
111 	    num_columns;)
112 	    *ptr++ = ' ';
113     }
114 
115     T(("newwin: returned window is %p", win));
116 
117     returnWin(win);
118 }
119 
120 WINDOW *
121 derwin(WINDOW *orig, int num_lines, int num_columns, int begy, int begx)
122 {
123     WINDOW *win;
124     int i;
125     int flags = _SUBWIN;
126 
127     T((T_CALLED("derwin(%p,%d,%d,%d,%d)"), orig, num_lines, num_columns,
128 	    begy, begx));
129 
130     /*
131        ** make sure window fits inside the original one
132      */
133     if (begy < 0 || begx < 0 || orig == 0 || num_lines < 0 || num_columns < 0)
134 	returnWin(0);
135     if (begy + num_lines > orig->_maxy + 1
136 	|| begx + num_columns > orig->_maxx + 1)
137 	returnWin(0);
138 
139     if (num_lines == 0)
140 	num_lines = orig->_maxy + 1 - begy;
141 
142     if (num_columns == 0)
143 	num_columns = orig->_maxx + 1 - begx;
144 
145     if (orig->_flags & _ISPAD)
146 	flags |= _ISPAD;
147 
148     if ((win = _nc_makenew(num_lines, num_columns, orig->_begy + begy,
149 		orig->_begx + begx, flags)) == 0)
150 	returnWin(0);
151 
152     win->_pary = begy;
153     win->_parx = begx;
154     win->_attrs = orig->_attrs;
155     win->_bkgd = orig->_bkgd;
156 
157     for (i = 0; i < num_lines; i++)
158 	win->_line[i].text = &orig->_line[begy++].text[begx];
159 
160     win->_parent = orig;
161 
162     T(("derwin: returned window is %p", win));
163 
164     returnWin(win);
165 }
166 
167 WINDOW *
168 subwin(WINDOW *w, int l, int c, int y, int x)
169 {
170     T((T_CALLED("subwin(%p, %d, %d, %d, %d)"), w, l, c, y, x));
171     T(("parent has begy = %d, begx = %d", w->_begy, w->_begx));
172 
173     returnWin(derwin(w, l, c, y - w->_begy, x - w->_begx));
174 }
175 
176 static bool
177 dimension_limit(int value)
178 {
179     NCURSES_SIZE_T test = value;
180     return (test == value && value > 0);
181 }
182 
183 WINDOW *
184 _nc_makenew(int num_lines, int num_columns, int begy, int begx, int flags)
185 {
186     int i;
187     WINDOWLIST *wp;
188     WINDOW *win;
189     bool is_pad = (flags & _ISPAD);
190 
191     T(("_nc_makenew(%d,%d,%d,%d)", num_lines, num_columns, begy, begx));
192 
193     if (!dimension_limit(num_lines) || !dimension_limit(num_columns))
194 	return 0;
195 
196     if ((wp = typeCalloc(WINDOWLIST, 1)) == 0)
197 	return 0;
198 
199     if ((win = typeCalloc(WINDOW, 1)) == 0)
200 	  return 0;
201 
202     if ((win->_line = typeCalloc(struct ldat, ((unsigned) num_lines))) == 0) {
203 	free(win);
204 	return 0;
205     }
206 
207     win->_curx = 0;
208     win->_cury = 0;
209     win->_maxy = num_lines - 1;
210     win->_maxx = num_columns - 1;
211     win->_begy = begy;
212     win->_begx = begx;
213     win->_yoffset = SP->_topstolen;
214 
215     win->_flags = flags;
216     win->_attrs = A_NORMAL;
217     win->_bkgd = BLANK;
218 
219     win->_clear = is_pad ? FALSE : (num_lines == screen_lines && num_columns
220 	== screen_columns);
221     win->_idlok = FALSE;
222     win->_idcok = TRUE;
223     win->_scroll = FALSE;
224     win->_leaveok = FALSE;
225     win->_use_keypad = FALSE;
226     win->_delay = -1;
227     win->_immed = FALSE;
228     win->_sync = 0;
229     win->_parx = -1;
230     win->_pary = -1;
231     win->_parent = 0;
232 
233     win->_regtop = 0;
234     win->_regbottom = num_lines - 1;
235 
236     win->_pad._pad_y = -1;
237     win->_pad._pad_x = -1;
238     win->_pad._pad_top = -1;
239     win->_pad._pad_bottom = -1;
240     win->_pad._pad_left = -1;
241     win->_pad._pad_right = -1;
242 
243     for (i = 0; i < num_lines; i++) {
244 	/*
245 	 * This used to do
246 	 *
247 	 * win->_line[i].firstchar = win->_line[i].lastchar = _NOCHANGE;
248 	 *
249 	 * which marks the whole window unchanged.  That's how
250 	 * SVr1 curses did it, but SVr4 curses marks the whole new
251 	 * window changed.
252 	 *
253 	 * With the old SVr1-like code, say you have stdscr full of
254 	 * characters, then create a new window with newwin(),
255 	 * then do a printw(win, "foo        ");, the trailing spaces are
256 	 * completely ignored by the following refreshes.  So, you
257 	 * get "foojunkjunk" on the screen instead of "foo        " as
258 	 * you actually intended.
259 	 *
260 	 * SVr4 doesn't do this.  Instead the spaces are actually written.
261 	 * So that's how we want ncurses to behave.
262 	 */
263 	win->_line[i].firstchar = 0;
264 	win->_line[i].lastchar = num_columns - 1;
265 
266 	if_USE_SCROLL_HINTS(win->_line[i].oldindex = i);
267     }
268 
269     if (!is_pad && (begx + num_columns == screen_columns)) {
270 	win->_flags |= _ENDLINE;
271 
272 	if (begx == 0 && num_lines == screen_lines && begy == 0)
273 	    win->_flags |= _FULLWIN;
274 
275 	if (begy + num_lines == screen_lines)
276 	    win->_flags |= _SCROLLWIN;
277     }
278 
279     wp->next = _nc_windows;
280     wp->win = win;
281     _nc_windows = wp;
282 
283     T((T_CREATE("window %p"), win));
284 
285     return (win);
286 }
287