2 /***************************************************************************
4 ****************************************************************************
5 * ncurses is copyright (C) 1992-1995 *
7 * zmbenhal@netcom.com *
9 * esr@snark.thyrsus.com *
11 * Permission is hereby granted to reproduce and distribute ncurses *
12 * by any means and for any fee, whether alone or as part of a *
13 * larger distribution, in source or in binary form, PROVIDED *
14 * this notice is included with any such distribution, and is not *
15 * removed from any of its header files. Mention of ncurses in any *
16 * applications linked with it is highly appreciated. *
18 * ncurses comes AS IS with no warranty, implied or expressed. *
20 ***************************************************************************/
27 ** The routines newwin(), subwin() and their dependent
31 #include <curses.priv.h>
33 MODULE_ID("$Id: lib_newwin.c,v 1.17 1997/02/15 21:46:05 tom Exp $")
35 void _nc_freewin(WINDOW *win)
41 for (p = _nc_windows, q = 0; p != 0; q = p, p = p->next) {
44 _nc_windows = p->next;
49 if (! (win->_flags & _SUBWIN)) {
50 for (i = 0; i <= win->_maxy && win->_line[i].text; i++)
51 free(win->_line[i].text);
56 if (win == curscr) curscr = 0;
57 if (win == stdscr) stdscr = 0;
58 if (win == newscr) newscr = 0;
60 T(("...deleted win=%p", win));
67 WINDOW * newwin(int num_lines, int num_columns, int begy, int begx)
73 T((T_CALLED("newwin(%d,%d,%d,%d)"), num_lines, num_columns, begy, begx));
75 if (begy < 0 || begx < 0 || num_lines < 0 || num_columns < 0)
79 num_lines = SP->_lines_avail - begy;
81 num_columns = screen_columns - begx;
83 if (num_columns + begx > SP->_columns || num_lines + begy > SP->_lines_avail)
86 if ((win = _nc_makenew(num_lines, num_columns, begy, begx, 0)) == 0)
89 for (i = 0; i < num_lines; i++) {
90 if ((win->_line[i].text = typeCalloc(chtype, (unsigned)num_columns)) == 0) {
94 for (ptr = win->_line[i].text; ptr < win->_line[i].text + num_columns; )
98 T(("newwin: returned window is %p", win));
103 WINDOW * derwin(WINDOW *orig, int num_lines, int num_columns, int begy, int begx)
109 T((T_CALLED("derwin(%p,%d,%d,%d,%d)"), orig, num_lines, num_columns, begy, begx));
112 ** make sure window fits inside the original one
114 if ( begy < 0 || begx < 0 || orig == 0 || num_lines < 0 || num_columns < 0)
116 if ( begy + num_lines > orig->_maxy + 1
117 || begx + num_columns > orig->_maxx + 1)
121 num_lines = orig->_maxy - begy;
123 if (num_columns == 0)
124 num_columns = orig->_maxx - begx;
126 if (orig->_flags & _ISPAD)
129 if ((win = _nc_makenew(num_lines, num_columns, orig->_begy + begy, orig->_begx + begx, flags)) == 0)
134 win->_attrs = orig->_attrs;
135 win->_bkgd = orig->_bkgd;
137 for (i = 0; i < num_lines; i++)
138 win->_line[i].text = &orig->_line[begy++].text[begx];
142 T(("derwin: returned window is %p", win));
148 WINDOW *subwin(WINDOW *w, int l, int c, int y, int x)
150 T((T_CALLED("subwin(%p, %d, %d, %d, %d)"), w, l, c, y, x));
151 T(("parent has begy = %d, begx = %d", w->_begy, w->_begx));
153 returnWin(derwin(w, l, c, y - w->_begy, x - w->_begx));
157 _nc_makenew(int num_lines, int num_columns, int begy, int begx, int flags)
162 bool is_pad = (flags & _ISPAD);
164 T(("_nc_makenew(%d,%d,%d,%d)", num_lines, num_columns, begy, begx));
166 if (num_lines <= 0 || num_columns <= 0)
169 if ((wp = typeCalloc(WINDOWLIST, 1)) == 0)
172 if ((win = typeCalloc(WINDOW, 1)) == 0)
175 if ((win->_line = typeCalloc(struct ldat, ((unsigned)num_lines))) == 0) {
182 win->_maxy = num_lines - 1;
183 win->_maxx = num_columns - 1;
186 win->_yoffset = SP->_topstolen;
189 win->_attrs = A_NORMAL;
192 win->_clear = is_pad ? FALSE : (num_lines == screen_lines && num_columns == screen_columns);
195 win->_scroll = FALSE;
196 win->_leaveok = FALSE;
197 win->_use_keypad = FALSE;
206 win->_regbottom = num_lines - 1;
208 win->_pad._pad_y = -1;
209 win->_pad._pad_x = -1;
210 win->_pad._pad_top = -1;
211 win->_pad._pad_bottom = -1;
212 win->_pad._pad_left = -1;
213 win->_pad._pad_right = -1;
215 for (i = 0; i < num_lines; i++)
220 * win->_line[i].firstchar = win->_line[i].lastchar = _NOCHANGE;
222 * which marks the whole window unchanged. That's how
223 * SVr1 curses did it, but SVr4 curses marks the whole new
226 * With the old SVr1-like code, say you have stdscr full of
227 * characters, then create a new window with newwin(),
228 * then do a printw(win, "foo ");, the trailing spaces are
229 * completely ignored by the following refreshes. So, you
230 * get "foojunkjunk" on the screen instead of "foo " as
231 * you actually intended.
233 * SVr4 doesn't do this. Instead the spaces are actually written.
234 * So that's how we want ncurses to behave.
236 win->_line[i].firstchar = 0;
237 win->_line[i].lastchar = num_columns-1;
239 win->_line[i].oldindex = i;
242 if (!is_pad && (begx + num_columns == screen_columns)) {
243 win->_flags |= _ENDLINE;
245 if (begx == 0 && num_lines == screen_lines && begy == 0)
246 win->_flags |= _FULLWIN;
248 if (begy + num_lines == screen_lines)
249 win->_flags |= _SCROLLWIN;
252 wp->next = _nc_windows;
256 T((T_CREATE("window %p"), win));