blob: a6723197c0936871f6a1dbccf92fb78e0357ad24 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2020,2021 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304 * *
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: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
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. *
23 * *
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 *
27 * authorization. *
28 ****************************************************************************/
29
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 *
Steve Kondikae271bc2015-11-15 02:50:53 +010034 * and: Juergen Pfeifer 2009 *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053035 ****************************************************************************/
36
37/*
38** lib_newwin.c
39**
40** The routines newwin(), subwin() and their dependent
41**
42*/
43
44#include <curses.priv.h>
45#include <stddef.h>
46
micky3879b9f5e72025-07-08 18:04:53 -040047MODULE_ID("$Id: lib_newwin.c,v 1.76 2021/10/23 18:53:38 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053048
49#define window_is(name) ((sp)->_##name == win)
50
51#if USE_REENTRANT
52#define remove_window(name) \
53 sp->_##name = 0
54#else
55#define remove_window(name) \
56 sp->_##name = 0; \
57 if (win == name) \
58 name = 0
59#endif
60
61static void
62remove_window_from_screen(WINDOW *win)
63{
64 SCREEN *sp;
65
Steve Kondikae271bc2015-11-15 02:50:53 +010066#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);
74 }
75 }
76#else
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053077 for (each_screen(sp)) {
78 if (window_is(curscr)) {
79 remove_window(curscr);
80 break;
81 } else if (window_is(stdscr)) {
82 remove_window(stdscr);
83 break;
84 } else if (window_is(newscr)) {
85 remove_window(newscr);
86 break;
87 }
88 }
Steve Kondikae271bc2015-11-15 02:50:53 +010089#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053090}
91
92NCURSES_EXPORT(int)
93_nc_freewin(WINDOW *win)
94{
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053095 int result = ERR;
Steve Kondikae271bc2015-11-15 02:50:53 +010096#ifdef USE_SP_WINDOWLIST
97 SCREEN *sp = _nc_screen_of(win); /* pretend this is parameter */
98#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053099
Steve Kondikae271bc2015-11-15 02:50:53 +0100100 T((T_CALLED("_nc_freewin(%p)"), (void *) win));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530101
102 if (win != 0) {
micky3879b9f5e72025-07-08 18:04:53 -0400103
Steve Kondikae271bc2015-11-15 02:50:53 +0100104 if (_nc_nonsp_try_global(curses) == 0) {
micky3879b9f5e72025-07-08 18:04:53 -0400105 WINDOWLIST *p, *q;
106
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530107 q = 0;
micky3879b9f5e72025-07-08 18:04:53 -0400108 for (each_window(sp, p)) {
109
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530110 if (&(p->win) == win) {
111 remove_window_from_screen(win);
112 if (q == 0)
micky3879b9f5e72025-07-08 18:04:53 -0400113 WindowList(sp) = p->next;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530114 else
115 q->next = p->next;
116
micky3879b9f5e72025-07-08 18:04:53 -0400117 if (!IS_SUBWIN(win)) {
118 int i;
119
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530120 for (i = 0; i <= win->_maxy; i++)
121 FreeIfNeeded(win->_line[i].text);
122 }
123 free(win->_line);
124 free(p);
125
126 result = OK;
Steve Kondikae271bc2015-11-15 02:50:53 +0100127 T(("...deleted win=%p", (void *) win));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530128 break;
129 }
130 q = p;
131 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100132 _nc_nonsp_unlock_global(curses);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530133 }
134 }
135 returnCode(result);
136}
137
138NCURSES_EXPORT(WINDOW *)
Steve Kondikae271bc2015-11-15 02:50:53 +0100139NCURSES_SP_NAME(newwin) (NCURSES_SP_DCLx
140 int num_lines, int num_columns, int begy, int begx)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530141{
142 WINDOW *win;
143 NCURSES_CH_T *ptr;
144 int i;
145
Steve Kondikae271bc2015-11-15 02:50:53 +0100146 T((T_CALLED("newwin(%p, %d,%d,%d,%d)"), (void *) SP_PARM, num_lines, num_columns,
147 begy, begx));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530148
Steve Kondikae271bc2015-11-15 02:50:53 +0100149 if (begy < 0
150 || begx < 0
151 || num_lines < 0
152 || num_columns < 0
153 || SP_PARM == 0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530154 returnWin(0);
155
156 if (num_lines == 0)
Steve Kondikae271bc2015-11-15 02:50:53 +0100157 num_lines = SP_PARM->_lines_avail - begy;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530158 if (num_columns == 0)
Steve Kondikae271bc2015-11-15 02:50:53 +0100159 num_columns = screen_columns(SP_PARM) - begx;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530160
Steve Kondikae271bc2015-11-15 02:50:53 +0100161 win = NCURSES_SP_NAME(_nc_makenew) (NCURSES_SP_ARGx
162 num_lines, num_columns, begy, begx, 0);
163 if (win == 0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530164 returnWin(0);
165
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);
170 returnWin(0);
171 }
172 for (ptr = win->_line[i].text;
173 ptr < win->_line[i].text + num_columns;
174 ptr++)
175 SetChar(*ptr, BLANK_TEXT, BLANK_ATTR);
176 }
177
178 returnWin(win);
179}
180
Steve Kondikae271bc2015-11-15 02:50:53 +0100181#if NCURSES_SP_FUNCS
182NCURSES_EXPORT(WINDOW *)
183newwin(int num_lines, int num_columns, int begy, int begx)
184{
185 WINDOW *win;
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);
190 return (win);
191}
192#endif
193
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530194NCURSES_EXPORT(WINDOW *)
195derwin(WINDOW *orig, int num_lines, int num_columns, int begy, int begx)
196{
197 WINDOW *win;
198 int i;
199 int flags = _SUBWIN;
Steve Kondikae271bc2015-11-15 02:50:53 +0100200#if NCURSES_SP_FUNCS
201 SCREEN *sp = _nc_screen_of(orig);
202#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530203
Steve Kondikae271bc2015-11-15 02:50:53 +0100204 T((T_CALLED("derwin(%p,%d,%d,%d,%d)"), (void *) orig, num_lines, num_columns,
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530205 begy, begx));
206
207 /*
208 * make sure window fits inside the original one
209 */
210 if (begy < 0 || begx < 0 || orig == 0 || num_lines < 0 || num_columns < 0)
211 returnWin(0);
212 if (begy + num_lines > orig->_maxy + 1
213 || begx + num_columns > orig->_maxx + 1)
214 returnWin(0);
215
216 if (num_lines == 0)
217 num_lines = orig->_maxy + 1 - begy;
218
219 if (num_columns == 0)
220 num_columns = orig->_maxx + 1 - begx;
221
micky3879b9f5e72025-07-08 18:04:53 -0400222 if (IS_PAD(orig))
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530223 flags |= _ISPAD;
224
Steve Kondikae271bc2015-11-15 02:50:53 +0100225 win = NCURSES_SP_NAME(_nc_makenew) (NCURSES_SP_ARGx num_lines, num_columns,
226 orig->_begy + begy,
227 orig->_begx + begx, flags);
228 if (win == 0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530229 returnWin(0);
230
231 win->_pary = begy;
232 win->_parx = begx;
233 WINDOW_ATTRS(win) = WINDOW_ATTRS(orig);
234 win->_nc_bkgd = orig->_nc_bkgd;
235
236 for (i = 0; i < num_lines; i++)
237 win->_line[i].text = &orig->_line[begy++].text[begx];
238
239 win->_parent = orig;
240
241 returnWin(win);
242}
243
244NCURSES_EXPORT(WINDOW *)
245subwin(WINDOW *w, int l, int c, int y, int x)
246{
Steve Kondikae271bc2015-11-15 02:50:53 +0100247 WINDOW *result = 0;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530248
Steve Kondikae271bc2015-11-15 02:50:53 +0100249 T((T_CALLED("subwin(%p, %d, %d, %d, %d)"), (void *) w, l, c, y, x));
250 if (w != 0) {
251 T(("parent has begy = %ld, begx = %ld", (long) w->_begy, (long) w->_begx));
252
253 result = derwin(w, l, c, y - w->_begy, x - w->_begx);
254 }
255 returnWin(result);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530256}
257
258static bool
259dimension_limit(int value)
260{
Steve Kondikae271bc2015-11-15 02:50:53 +0100261 NCURSES_SIZE_T test = (NCURSES_SIZE_T) value;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530262 return (test == value && value > 0);
263}
264
265NCURSES_EXPORT(WINDOW *)
Steve Kondikae271bc2015-11-15 02:50:53 +0100266NCURSES_SP_NAME(_nc_makenew) (NCURSES_SP_DCLx
267 int num_lines,
268 int num_columns,
269 int begy,
270 int begx,
271 int flags)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530272{
273 int i;
274 WINDOWLIST *wp;
275 WINDOW *win;
Steve Kondikae271bc2015-11-15 02:50:53 +0100276 bool is_padwin = (flags & _ISPAD);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530277
Steve Kondikae271bc2015-11-15 02:50:53 +0100278 T((T_CALLED("_nc_makenew(%p,%d,%d,%d,%d)"),
279 (void *) SP_PARM, num_lines, num_columns, begy, begx));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530280
Steve Kondikae271bc2015-11-15 02:50:53 +0100281 if (SP_PARM == 0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530282 returnWin(0);
283
284 if (!dimension_limit(num_lines) || !dimension_limit(num_columns))
285 returnWin(0);
286
287 if ((wp = typeCalloc(WINDOWLIST, 1)) == 0)
288 returnWin(0);
289
290 win = &(wp->win);
291
292 if ((win->_line = typeCalloc(struct ldat, ((unsigned) num_lines))) == 0) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100293 free(wp);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530294 returnWin(0);
295 }
296
Steve Kondikae271bc2015-11-15 02:50:53 +0100297 _nc_nonsp_lock_global(curses);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530298
299 win->_curx = 0;
300 win->_cury = 0;
Steve Kondikae271bc2015-11-15 02:50:53 +0100301 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;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530306
Steve Kondikae271bc2015-11-15 02:50:53 +0100307 win->_flags = (short) flags;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530308 WINDOW_ATTRS(win) = A_NORMAL;
309 SetChar(win->_nc_bkgd, BLANK_TEXT, BLANK_ATTR);
310
Steve Kondikae271bc2015-11-15 02:50:53 +0100311 win->_clear = (is_padwin
312 ? FALSE
313 : (num_lines == screen_lines(SP_PARM)
314 && num_columns == screen_columns(SP_PARM)));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530315 win->_idlok = FALSE;
316 win->_idcok = TRUE;
317 win->_scroll = FALSE;
318 win->_leaveok = FALSE;
319 win->_use_keypad = FALSE;
320 win->_delay = -1;
321 win->_immed = FALSE;
322 win->_sync = 0;
323 win->_parx = -1;
324 win->_pary = -1;
325 win->_parent = 0;
326
327 win->_regtop = 0;
Steve Kondikae271bc2015-11-15 02:50:53 +0100328 win->_regbottom = (NCURSES_SIZE_T) (num_lines - 1);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530329
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;
336
337 for (i = 0; i < num_lines; i++) {
338 /*
339 * This used to do
340 *
341 * win->_line[i].firstchar = win->_line[i].lastchar = _NOCHANGE;
342 *
343 * which marks the whole window unchanged. That's how
344 * SVr1 curses did it, but SVr4 curses marks the whole new
345 * window changed.
346 *
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.
353 *
354 * SVr4 doesn't do this. Instead the spaces are actually written.
355 * So that's how we want ncurses to behave.
356 */
357 win->_line[i].firstchar = 0;
Steve Kondikae271bc2015-11-15 02:50:53 +0100358 win->_line[i].lastchar = (NCURSES_SIZE_T) (num_columns - 1);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530359
360 if_USE_SCROLL_HINTS(win->_line[i].oldindex = i);
361 }
362
Steve Kondikae271bc2015-11-15 02:50:53 +0100363 if (!is_padwin && (begx + num_columns == screen_columns(SP_PARM))) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530364 win->_flags |= _ENDLINE;
365
Steve Kondikae271bc2015-11-15 02:50:53 +0100366 if (begx == 0 && num_lines == screen_lines(SP_PARM) && begy == 0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530367 win->_flags |= _FULLWIN;
368
Steve Kondikae271bc2015-11-15 02:50:53 +0100369 if (begy + num_lines == screen_lines(SP_PARM))
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530370 win->_flags |= _SCROLLWIN;
371 }
372
Steve Kondikae271bc2015-11-15 02:50:53 +0100373 wp->next = WindowList(SP_PARM);
374 wp->screen = SP_PARM;
375 WindowList(SP_PARM) = wp;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530376
Steve Kondikae271bc2015-11-15 02:50:53 +0100377 T((T_CREATE("window %p"), (void *) win));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530378
Steve Kondikae271bc2015-11-15 02:50:53 +0100379 _nc_nonsp_unlock_global(curses);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530380 returnWin(win);
381}
382
383/*
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.
387 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100388#if NCURSES_SP_FUNCS
389NCURSES_EXPORT(WINDOW *)
390_nc_curscr_of(SCREEN *sp)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530391{
micky3879b9f5e72025-07-08 18:04:53 -0400392 return (sp == 0) ? NULL : CurScreen(sp);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530393}
Steve Kondikae271bc2015-11-15 02:50:53 +0100394
395NCURSES_EXPORT(WINDOW *)
396_nc_newscr_of(SCREEN *sp)
397{
micky3879b9f5e72025-07-08 18:04:53 -0400398 return (sp == 0) ? NULL : NewScreen(sp);
Steve Kondikae271bc2015-11-15 02:50:53 +0100399}
400
401NCURSES_EXPORT(WINDOW *)
402_nc_stdscr_of(SCREEN *sp)
403{
micky3879b9f5e72025-07-08 18:04:53 -0400404 return (sp == 0) ? NULL : StdScreen(sp);
Steve Kondikae271bc2015-11-15 02:50:53 +0100405}
406#endif