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