1 /****************************************************************************
2 * Copyright (c) 1998-2008,2009 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 * and: Thomas E. Dickey 1996-on *
33 * and: Juergen Pfeifer 2009 *
34 ****************************************************************************/
39 ** The routines newwin(), subwin() and their dependent
43 #include <curses.priv.h>
46 MODULE_ID("$Id: lib_newwin.c,v 1.58 2009/06/06 17:53:11 tom Exp $")
48 #define window_is(name) ((sp)->_##name == win)
51 #define remove_window(name) \
54 #define remove_window(name) \
61 remove_window_from_screen(WINDOW *win)
65 for (each_screen(sp)) {
66 if (window_is(curscr)) {
67 remove_window(curscr);
69 } else if (window_is(stdscr)) {
70 remove_window(stdscr);
72 } else if (window_is(newscr)) {
73 remove_window(newscr);
80 _nc_freewin(WINDOW *win)
86 T((T_CALLED("_nc_freewin(%p)"), win));
89 if (_nc_try_global(curses) == 0) {
91 for (each_window(SP, p)) {
92 if (&(p->win) == win) {
93 remove_window_from_screen(win);
95 WindowList(SP) = p->next;
99 if (!(win->_flags & _SUBWIN)) {
100 for (i = 0; i <= win->_maxy; i++)
101 FreeIfNeeded(win->_line[i].text);
107 T(("...deleted win=%p", win));
112 _nc_unlock_global(curses);
118 NCURSES_EXPORT(WINDOW *)
119 NCURSES_SP_NAME(newwin) (NCURSES_SP_DCLx
120 int num_lines, int num_columns, int begy, int begx)
126 T((T_CALLED("newwin(%d,%d,%d,%d)"), num_lines, num_columns, begy, begx));
128 if (begy < 0 || begx < 0 || num_lines < 0 || num_columns < 0)
132 num_lines = SP_PARM->_lines_avail - begy;
133 if (num_columns == 0)
134 num_columns = screen_columns(SP_PARM) - begx;
136 win = NCURSES_SP_NAME(_nc_makenew) (NCURSES_SP_ARGx
137 num_lines, num_columns, begy, begx, 0);
141 for (i = 0; i < num_lines; i++) {
142 win->_line[i].text = typeCalloc(NCURSES_CH_T, (unsigned) num_columns);
143 if (win->_line[i].text == 0) {
144 (void) _nc_freewin(win);
147 for (ptr = win->_line[i].text;
148 ptr < win->_line[i].text + num_columns;
150 SetChar(*ptr, BLANK_TEXT, BLANK_ATTR);
157 NCURSES_EXPORT(WINDOW *)
158 newwin(int num_lines, int num_columns, int begy, int begx)
160 return NCURSES_SP_NAME(newwin) (CURRENT_SCREEN,
161 num_lines, num_columns,
166 NCURSES_EXPORT(WINDOW *)
167 derwin(WINDOW *orig, int num_lines, int num_columns, int begy, int begx)
170 SCREEN *sp = CURRENT_SCREEN;
176 T((T_CALLED("derwin(%p,%d,%d,%d,%d)"), orig, num_lines, num_columns,
180 * make sure window fits inside the original one
182 if (begy < 0 || begx < 0 || orig == 0 || num_lines < 0 || num_columns < 0)
184 if (begy + num_lines > orig->_maxy + 1
185 || begx + num_columns > orig->_maxx + 1)
189 num_lines = orig->_maxy + 1 - begy;
191 if (num_columns == 0)
192 num_columns = orig->_maxx + 1 - begx;
194 if (orig->_flags & _ISPAD)
197 win = NCURSES_SP_NAME(_nc_makenew) (NCURSES_SP_ARGx num_lines, num_columns,
199 orig->_begx + begx, flags);
205 WINDOW_ATTRS(win) = WINDOW_ATTRS(orig);
206 win->_nc_bkgd = orig->_nc_bkgd;
208 for (i = 0; i < num_lines; i++)
209 win->_line[i].text = &orig->_line[begy++].text[begx];
216 NCURSES_EXPORT(WINDOW *)
217 subwin(WINDOW *w, int l, int c, int y, int x)
219 T((T_CALLED("subwin(%p, %d, %d, %d, %d)"), w, l, c, y, x));
220 T(("parent has begy = %ld, begx = %ld", (long) w->_begy, (long) w->_begx));
222 returnWin(derwin(w, l, c, y - w->_begy, x - w->_begx));
226 dimension_limit(int value)
228 NCURSES_SIZE_T test = value;
229 return (test == value && value > 0);
232 NCURSES_EXPORT(WINDOW *)
233 NCURSES_SP_NAME(_nc_makenew) (NCURSES_SP_DCLx
243 bool is_pad = (flags & _ISPAD);
245 T((T_CALLED("_nc_makenew(%p,%d,%d,%d,%d)"),
246 SP_PARM, num_lines, num_columns, begy, begx));
251 if (!dimension_limit(num_lines) || !dimension_limit(num_columns))
254 if ((wp = typeCalloc(WINDOWLIST, 1)) == 0)
259 if ((win->_line = typeCalloc(struct ldat, ((unsigned) num_lines))) == 0) {
264 _nc_lock_global(curses);
268 win->_maxy = num_lines - 1;
269 win->_maxx = num_columns - 1;
272 win->_yoffset = SP_PARM->_topstolen;
275 WINDOW_ATTRS(win) = A_NORMAL;
276 SetChar(win->_nc_bkgd, BLANK_TEXT, BLANK_ATTR);
278 win->_clear = (is_pad
280 : (num_lines == screen_lines(SP_PARM)
281 && num_columns == screen_columns(SP_PARM)));
284 win->_scroll = FALSE;
285 win->_leaveok = FALSE;
286 win->_use_keypad = FALSE;
295 win->_regbottom = num_lines - 1;
297 win->_pad._pad_y = -1;
298 win->_pad._pad_x = -1;
299 win->_pad._pad_top = -1;
300 win->_pad._pad_bottom = -1;
301 win->_pad._pad_left = -1;
302 win->_pad._pad_right = -1;
304 for (i = 0; i < num_lines; i++) {
308 * win->_line[i].firstchar = win->_line[i].lastchar = _NOCHANGE;
310 * which marks the whole window unchanged. That's how
311 * SVr1 curses did it, but SVr4 curses marks the whole new
314 * With the old SVr1-like code, say you have stdscr full of
315 * characters, then create a new window with newwin(),
316 * then do a printw(win, "foo ");, the trailing spaces are
317 * completely ignored by the following refreshes. So, you
318 * get "foojunkjunk" on the screen instead of "foo " as
319 * you actually intended.
321 * SVr4 doesn't do this. Instead the spaces are actually written.
322 * So that's how we want ncurses to behave.
324 win->_line[i].firstchar = 0;
325 win->_line[i].lastchar = num_columns - 1;
327 if_USE_SCROLL_HINTS(win->_line[i].oldindex = i);
330 if (!is_pad && (begx + num_columns == screen_columns(SP_PARM))) {
331 win->_flags |= _ENDLINE;
333 if (begx == 0 && num_lines == screen_lines(SP_PARM) && begy == 0)
334 win->_flags |= _FULLWIN;
336 if (begy + num_lines == screen_lines(SP_PARM))
337 win->_flags |= _SCROLLWIN;
340 wp->next = WindowList(SP_PARM);
341 wp->screen = SP_PARM;
342 WindowList(SP_PARM) = wp;
344 T((T_CREATE("window %p"), win));
346 _nc_unlock_global(curses);
351 NCURSES_EXPORT(WINDOW *)
352 _nc_makenew(int num_lines, int num_columns, int begy, int begx, int flags)
354 return NCURSES_SP_NAME(_nc_makenew) (CURRENT_SCREEN,
355 num_lines, num_columns,
361 * wgetch() and other functions with a WINDOW* parameter may use a SCREEN*
362 * internally, and it is useful to allow those to be invoked without switching
363 * SCREEN's, e.g., for multi-threaded applications.
366 NCURSES_EXPORT(WINDOW *)
367 _nc_curscr_of(SCREEN *sp)
369 return sp == 0 ? 0 : sp->_curscr;
372 NCURSES_EXPORT(WINDOW *)
373 _nc_newscr_of(SCREEN *sp)
375 return sp == 0 ? 0 : sp->_newscr;
378 NCURSES_EXPORT(WINDOW *)
379 _nc_stdscr_of(SCREEN *sp)
381 return sp == 0 ? 0 : sp->_stdscr;