1 /****************************************************************************
2 * Copyright 2020 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996-on *
34 * and: Juergen Pfeifer 2009 *
35 ****************************************************************************/
40 ** The routines newwin(), subwin() and their dependent
44 #include <curses.priv.h>
47 MODULE_ID("$Id: lib_newwin.c,v 1.75 2020/02/02 23:34:34 tom Exp $")
49 #define window_is(name) ((sp)->_##name == win)
52 #define remove_window(name) \
55 #define remove_window(name) \
62 remove_window_from_screen(WINDOW *win)
66 #ifdef USE_SP_WINDOWLIST
67 if ((sp = _nc_screen_of(win)) != 0) {
68 if (window_is(curscr)) {
69 remove_window(curscr);
70 } else if (window_is(stdscr)) {
71 remove_window(stdscr);
72 } else if (window_is(newscr)) {
73 remove_window(newscr);
77 for (each_screen(sp)) {
78 if (window_is(curscr)) {
79 remove_window(curscr);
81 } else if (window_is(stdscr)) {
82 remove_window(stdscr);
84 } else if (window_is(newscr)) {
85 remove_window(newscr);
93 _nc_freewin(WINDOW *win)
96 #ifdef USE_SP_WINDOWLIST
97 SCREEN *sp = _nc_screen_of(win); /* pretend this is parameter */
100 T((T_CALLED("_nc_freewin(%p)"), (void *) win));
104 if (_nc_nonsp_try_global(curses) == 0) {
108 for (each_window(sp, p)) {
110 if (&(p->win) == win) {
111 remove_window_from_screen(win);
113 WindowList(sp) = p->next;
117 if (!(win->_flags & _SUBWIN)) {
120 for (i = 0; i <= win->_maxy; i++)
121 FreeIfNeeded(win->_line[i].text);
127 T(("...deleted win=%p", (void *) win));
132 _nc_nonsp_unlock_global(curses);
138 NCURSES_EXPORT(WINDOW *)
139 NCURSES_SP_NAME(newwin) (NCURSES_SP_DCLx
140 int num_lines, int num_columns, int begy, int begx)
146 T((T_CALLED("newwin(%p, %d,%d,%d,%d)"), (void *) SP_PARM, num_lines, num_columns,
157 num_lines = SP_PARM->_lines_avail - begy;
158 if (num_columns == 0)
159 num_columns = screen_columns(SP_PARM) - begx;
161 win = NCURSES_SP_NAME(_nc_makenew) (NCURSES_SP_ARGx
162 num_lines, num_columns, begy, begx, 0);
166 for (i = 0; i < num_lines; i++) {
167 win->_line[i].text = typeCalloc(NCURSES_CH_T, (unsigned) num_columns);
168 if (win->_line[i].text == 0) {
169 (void) _nc_freewin(win);
172 for (ptr = win->_line[i].text;
173 ptr < win->_line[i].text + num_columns;
175 SetChar(*ptr, BLANK_TEXT, BLANK_ATTR);
182 NCURSES_EXPORT(WINDOW *)
183 newwin(int num_lines, int num_columns, int begy, int begx)
186 _nc_sp_lock_global(curses);
187 win = NCURSES_SP_NAME(newwin) (CURRENT_SCREEN,
188 num_lines, num_columns, begy, begx);
189 _nc_sp_unlock_global(curses);
194 NCURSES_EXPORT(WINDOW *)
195 derwin(WINDOW *orig, int num_lines, int num_columns, int begy, int begx)
201 SCREEN *sp = _nc_screen_of(orig);
204 T((T_CALLED("derwin(%p,%d,%d,%d,%d)"), (void *) orig, num_lines, num_columns,
208 * make sure window fits inside the original one
210 if (begy < 0 || begx < 0 || orig == 0 || num_lines < 0 || num_columns < 0)
212 if (begy + num_lines > orig->_maxy + 1
213 || begx + num_columns > orig->_maxx + 1)
217 num_lines = orig->_maxy + 1 - begy;
219 if (num_columns == 0)
220 num_columns = orig->_maxx + 1 - begx;
222 if (orig->_flags & _ISPAD)
225 win = NCURSES_SP_NAME(_nc_makenew) (NCURSES_SP_ARGx num_lines, num_columns,
227 orig->_begx + begx, flags);
233 WINDOW_ATTRS(win) = WINDOW_ATTRS(orig);
234 win->_nc_bkgd = orig->_nc_bkgd;
236 for (i = 0; i < num_lines; i++)
237 win->_line[i].text = &orig->_line[begy++].text[begx];
244 NCURSES_EXPORT(WINDOW *)
245 subwin(WINDOW *w, int l, int c, int y, int x)
249 T((T_CALLED("subwin(%p, %d, %d, %d, %d)"), (void *) w, l, c, y, x));
251 T(("parent has begy = %ld, begx = %ld", (long) w->_begy, (long) w->_begx));
253 result = derwin(w, l, c, y - w->_begy, x - w->_begx);
259 dimension_limit(int value)
261 NCURSES_SIZE_T test = (NCURSES_SIZE_T) value;
262 return (test == value && value > 0);
265 NCURSES_EXPORT(WINDOW *)
266 NCURSES_SP_NAME(_nc_makenew) (NCURSES_SP_DCLx
276 bool is_padwin = (flags & _ISPAD);
278 T((T_CALLED("_nc_makenew(%p,%d,%d,%d,%d)"),
279 (void *) SP_PARM, num_lines, num_columns, begy, begx));
284 if (!dimension_limit(num_lines) || !dimension_limit(num_columns))
287 if ((wp = typeCalloc(WINDOWLIST, 1)) == 0)
292 if ((win->_line = typeCalloc(struct ldat, ((unsigned) num_lines))) == 0) {
297 _nc_nonsp_lock_global(curses);
301 win->_maxy = (NCURSES_SIZE_T) (num_lines - 1);
302 win->_maxx = (NCURSES_SIZE_T) (num_columns - 1);
303 win->_begy = (NCURSES_SIZE_T) begy;
304 win->_begx = (NCURSES_SIZE_T) begx;
305 win->_yoffset = SP_PARM->_topstolen;
307 win->_flags = (short) flags;
308 WINDOW_ATTRS(win) = A_NORMAL;
309 SetChar(win->_nc_bkgd, BLANK_TEXT, BLANK_ATTR);
311 win->_clear = (is_padwin
313 : (num_lines == screen_lines(SP_PARM)
314 && num_columns == screen_columns(SP_PARM)));
317 win->_scroll = FALSE;
318 win->_leaveok = FALSE;
319 win->_use_keypad = FALSE;
328 win->_regbottom = (NCURSES_SIZE_T) (num_lines - 1);
330 win->_pad._pad_y = -1;
331 win->_pad._pad_x = -1;
332 win->_pad._pad_top = -1;
333 win->_pad._pad_bottom = -1;
334 win->_pad._pad_left = -1;
335 win->_pad._pad_right = -1;
337 for (i = 0; i < num_lines; i++) {
341 * win->_line[i].firstchar = win->_line[i].lastchar = _NOCHANGE;
343 * which marks the whole window unchanged. That's how
344 * SVr1 curses did it, but SVr4 curses marks the whole new
347 * With the old SVr1-like code, say you have stdscr full of
348 * characters, then create a new window with newwin(),
349 * then do a printw(win, "foo ");, the trailing spaces are
350 * completely ignored by the following refreshes. So, you
351 * get "foojunkjunk" on the screen instead of "foo " as
352 * you actually intended.
354 * SVr4 doesn't do this. Instead the spaces are actually written.
355 * So that's how we want ncurses to behave.
357 win->_line[i].firstchar = 0;
358 win->_line[i].lastchar = (NCURSES_SIZE_T) (num_columns - 1);
360 if_USE_SCROLL_HINTS(win->_line[i].oldindex = i);
363 if (!is_padwin && (begx + num_columns == screen_columns(SP_PARM))) {
364 win->_flags |= _ENDLINE;
366 if (begx == 0 && num_lines == screen_lines(SP_PARM) && begy == 0)
367 win->_flags |= _FULLWIN;
369 if (begy + num_lines == screen_lines(SP_PARM))
370 win->_flags |= _SCROLLWIN;
373 wp->next = WindowList(SP_PARM);
374 wp->screen = SP_PARM;
375 WindowList(SP_PARM) = wp;
377 T((T_CREATE("window %p"), (void *) win));
379 _nc_nonsp_unlock_global(curses);
384 * wgetch() and other functions with a WINDOW* parameter may use a SCREEN*
385 * internally, and it is useful to allow those to be invoked without switching
386 * SCREEN's, e.g., for multi-threaded applications.
389 NCURSES_EXPORT(WINDOW *)
390 _nc_curscr_of(SCREEN *sp)
392 return (sp == 0) ? NULL : CurScreen(sp);
395 NCURSES_EXPORT(WINDOW *)
396 _nc_newscr_of(SCREEN *sp)
398 return (sp == 0) ? NULL : NewScreen(sp);
401 NCURSES_EXPORT(WINDOW *)
402 _nc_stdscr_of(SCREEN *sp)
404 return (sp == 0) ? NULL : StdScreen(sp);