1 /****************************************************************************
2 * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
37 ** The routines newwin(), subwin() and their dependent
41 #include <curses.priv.h>
43 MODULE_ID("$Id: lib_newwin.c,v 1.24 2000/04/29 18:49:51 tom Exp $")
46 _nc_freewin(WINDOW *win)
52 for (p = _nc_windows, q = 0; p != 0; q = p, p = p->next) {
55 _nc_windows = p->next;
60 if (!(win->_flags & _SUBWIN)) {
61 for (i = 0; i <= win->_maxy; i++)
62 FreeIfNeeded(win->_line[i].text);
74 T(("...deleted win=%p", win));
82 newwin(int num_lines, int num_columns, int begy, int begx)
88 T((T_CALLED("newwin(%d,%d,%d,%d)"), num_lines, num_columns, begy, begx));
90 if (begy < 0 || begx < 0 || num_lines < 0 || num_columns < 0)
94 num_lines = SP->_lines_avail - begy;
96 num_columns = screen_columns - begx;
98 if (num_columns + begx > SP->_columns || num_lines + begy > SP->_lines_avail)
101 if ((win = _nc_makenew(num_lines, num_columns, begy, begx, 0)) == 0)
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) {
110 for (ptr = win->_line[i].text; ptr < win->_line[i].text +
115 T(("newwin: returned window is %p", win));
121 derwin(WINDOW *orig, int num_lines, int num_columns, int begy, int begx)
127 T((T_CALLED("derwin(%p,%d,%d,%d,%d)"), orig, num_lines, num_columns,
131 ** make sure window fits inside the original one
133 if (begy < 0 || begx < 0 || orig == 0 || num_lines < 0 || num_columns < 0)
135 if (begy + num_lines > orig->_maxy + 1
136 || begx + num_columns > orig->_maxx + 1)
140 num_lines = orig->_maxy + 1 - begy;
142 if (num_columns == 0)
143 num_columns = orig->_maxx + 1 - begx;
145 if (orig->_flags & _ISPAD)
148 if ((win = _nc_makenew(num_lines, num_columns, orig->_begy + begy,
149 orig->_begx + begx, flags)) == 0)
154 win->_attrs = orig->_attrs;
155 win->_bkgd = orig->_bkgd;
157 for (i = 0; i < num_lines; i++)
158 win->_line[i].text = &orig->_line[begy++].text[begx];
162 T(("derwin: returned window is %p", win));
168 subwin(WINDOW *w, int l, int c, int y, int x)
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));
173 returnWin(derwin(w, l, c, y - w->_begy, x - w->_begx));
177 dimension_limit(int value)
179 NCURSES_SIZE_T test = value;
180 return (test == value && value > 0);
184 _nc_makenew(int num_lines, int num_columns, int begy, int begx, int flags)
189 bool is_pad = (flags & _ISPAD);
191 T(("_nc_makenew(%d,%d,%d,%d)", num_lines, num_columns, begy, begx));
193 if (!dimension_limit(num_lines) || !dimension_limit(num_columns))
196 if ((wp = typeCalloc(WINDOWLIST, 1)) == 0)
199 if ((win = typeCalloc(WINDOW, 1)) == 0)
202 if ((win->_line = typeCalloc(struct ldat, ((unsigned) num_lines))) == 0) {
209 win->_maxy = num_lines - 1;
210 win->_maxx = num_columns - 1;
213 win->_yoffset = SP->_topstolen;
216 win->_attrs = A_NORMAL;
219 win->_clear = is_pad ? FALSE : (num_lines == screen_lines && num_columns
223 win->_scroll = FALSE;
224 win->_leaveok = FALSE;
225 win->_use_keypad = FALSE;
234 win->_regbottom = num_lines - 1;
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;
243 for (i = 0; i < num_lines; i++) {
247 * win->_line[i].firstchar = win->_line[i].lastchar = _NOCHANGE;
249 * which marks the whole window unchanged. That's how
250 * SVr1 curses did it, but SVr4 curses marks the whole new
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.
260 * SVr4 doesn't do this. Instead the spaces are actually written.
261 * So that's how we want ncurses to behave.
263 win->_line[i].firstchar = 0;
264 win->_line[i].lastchar = num_columns - 1;
266 if_USE_SCROLL_HINTS(win->_line[i].oldindex = i);
269 if (!is_pad && (begx + num_columns == screen_columns)) {
270 win->_flags |= _ENDLINE;
272 if (begx == 0 && num_lines == screen_lines && begy == 0)
273 win->_flags |= _FULLWIN;
275 if (begy + num_lines == screen_lines)
276 win->_flags |= _SCROLLWIN;
279 wp->next = _nc_windows;
283 T((T_CREATE("window %p"), win));