xref: /freebsd/contrib/ncurses/ncurses/base/lib_window.c (revision 21817992b3314c908ab50f0bb88d2ee750b9c4ac)
10e3d5408SPeter Wemm /****************************************************************************
2*21817992SBaptiste Daroussin  * Copyright 2020,2021 Thomas E. Dickey                                     *
3e1865124SBaptiste Daroussin  * Copyright 1998-2010,2016 Free Software Foundation, Inc.                  *
40e3d5408SPeter Wemm  *                                                                          *
50e3d5408SPeter Wemm  * Permission is hereby granted, free of charge, to any person obtaining a  *
60e3d5408SPeter Wemm  * copy of this software and associated documentation files (the            *
70e3d5408SPeter Wemm  * "Software"), to deal in the Software without restriction, including      *
80e3d5408SPeter Wemm  * without limitation the rights to use, copy, modify, merge, publish,      *
90e3d5408SPeter Wemm  * distribute, distribute with modifications, sublicense, and/or sell       *
100e3d5408SPeter Wemm  * copies of the Software, and to permit persons to whom the Software is    *
110e3d5408SPeter Wemm  * furnished to do so, subject to the following conditions:                 *
120e3d5408SPeter Wemm  *                                                                          *
130e3d5408SPeter Wemm  * The above copyright notice and this permission notice shall be included  *
140e3d5408SPeter Wemm  * in all copies or substantial portions of the Software.                   *
150e3d5408SPeter Wemm  *                                                                          *
160e3d5408SPeter Wemm  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
170e3d5408SPeter Wemm  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
180e3d5408SPeter Wemm  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
190e3d5408SPeter Wemm  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
200e3d5408SPeter Wemm  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
210e3d5408SPeter Wemm  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
220e3d5408SPeter Wemm  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
230e3d5408SPeter Wemm  *                                                                          *
240e3d5408SPeter Wemm  * Except as contained in this notice, the name(s) of the above copyright   *
250e3d5408SPeter Wemm  * holders shall not be used in advertising or otherwise to promote the     *
260e3d5408SPeter Wemm  * sale, use or other dealings in this Software without prior written       *
270e3d5408SPeter Wemm  * authorization.                                                           *
280e3d5408SPeter Wemm  ****************************************************************************/
290e3d5408SPeter Wemm 
300e3d5408SPeter Wemm /****************************************************************************
310e3d5408SPeter Wemm  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
320e3d5408SPeter Wemm  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
330e3d5408SPeter Wemm  ****************************************************************************/
340e3d5408SPeter Wemm 
350e3d5408SPeter Wemm /*
360e3d5408SPeter Wemm **	lib_window.c
370e3d5408SPeter Wemm **
380e3d5408SPeter Wemm **
390e3d5408SPeter Wemm */
400e3d5408SPeter Wemm 
410e3d5408SPeter Wemm #include <curses.priv.h>
420e3d5408SPeter Wemm 
43*21817992SBaptiste Daroussin MODULE_ID("$Id: lib_window.c,v 1.32 2021/10/23 23:06:24 tom Exp $")
440e3d5408SPeter Wemm 
NCURSES_EXPORT(void)457a69bbfbSPeter Wemm NCURSES_EXPORT(void)
467a69bbfbSPeter Wemm _nc_synchook(WINDOW *win)
470e3d5408SPeter Wemm /* hook to be called after each window change */
480e3d5408SPeter Wemm {
497a69bbfbSPeter Wemm     if (win->_immed)
507a69bbfbSPeter Wemm 	wrefresh(win);
517a69bbfbSPeter Wemm     if (win->_sync)
527a69bbfbSPeter Wemm 	wsyncup(win);
530e3d5408SPeter Wemm }
540e3d5408SPeter Wemm 
557a69bbfbSPeter Wemm NCURSES_EXPORT(int)
mvderwin(WINDOW * win,int y,int x)567a69bbfbSPeter Wemm mvderwin(WINDOW *win, int y, int x)
570e3d5408SPeter Wemm /* move a derived window */
580e3d5408SPeter Wemm {
590e3d5408SPeter Wemm     WINDOW *orig;
6006bfebdeSXin LI     int rc = ERR;
610e3d5408SPeter Wemm 
6206bfebdeSXin LI     T((T_CALLED("mvderwin(%p,%d,%d)"), (void *) win, y, x));
630e3d5408SPeter Wemm 
6406bfebdeSXin LI     if (win != 0
6506bfebdeSXin LI 	&& (orig = win->_parent) != 0
6606bfebdeSXin LI 	&& (x >= 0 && y >= 0)
6706bfebdeSXin LI 	&& (x + getmaxx(win) <= getmaxx(orig))
6806bfebdeSXin LI 	&& (y + getmaxy(win) <= getmaxy(orig))) {
69aae38d10SBaptiste Daroussin 	int i;
70aae38d10SBaptiste Daroussin 
710e3d5408SPeter Wemm 	wsyncup(win);
720e3d5408SPeter Wemm 	win->_parx = x;
730e3d5408SPeter Wemm 	win->_pary = y;
740e3d5408SPeter Wemm 	for (i = 0; i < getmaxy(win); i++)
750e3d5408SPeter Wemm 	    win->_line[i].text = &(orig->_line[y++].text[x]);
7606bfebdeSXin LI 	rc = OK;
7706bfebdeSXin LI     }
7806bfebdeSXin LI     returnCode(rc);
790e3d5408SPeter Wemm }
800e3d5408SPeter Wemm 
817a69bbfbSPeter Wemm NCURSES_EXPORT(int)
syncok(WINDOW * win,bool bf)827a69bbfbSPeter Wemm syncok(WINDOW *win, bool bf)
830e3d5408SPeter Wemm /* enable/disable automatic wsyncup() on each change to window */
840e3d5408SPeter Wemm {
8506bfebdeSXin LI     T((T_CALLED("syncok(%p,%d)"), (void *) win, bf));
860e3d5408SPeter Wemm 
870e3d5408SPeter Wemm     if (win) {
880e3d5408SPeter Wemm 	win->_sync = bf;
890e3d5408SPeter Wemm 	returnCode(OK);
900e3d5408SPeter Wemm     } else
910e3d5408SPeter Wemm 	returnCode(ERR);
920e3d5408SPeter Wemm }
930e3d5408SPeter Wemm 
947a69bbfbSPeter Wemm NCURSES_EXPORT(void)
wsyncup(WINDOW * win)957a69bbfbSPeter Wemm wsyncup(WINDOW *win)
960e3d5408SPeter Wemm /* mark changed every cell in win's ancestors that is changed in win */
970e3d5408SPeter Wemm /* Rewritten by J. Pfeifer, 1-Apr-96 (don't even think that...)      */
980e3d5408SPeter Wemm {
990e3d5408SPeter Wemm     WINDOW *wp;
1000e3d5408SPeter Wemm 
10106bfebdeSXin LI     T((T_CALLED("wsyncup(%p)"), (void *) win));
10239f2269fSPeter Wemm     if (win && win->_parent) {
1037a69bbfbSPeter Wemm 	for (wp = win; wp->_parent; wp = wp->_parent) {
1040e3d5408SPeter Wemm 	    int y;
1050e3d5408SPeter Wemm 	    WINDOW *pp = wp->_parent;
1060e3d5408SPeter Wemm 
1070e3d5408SPeter Wemm 	    assert((wp->_pary <= pp->_maxy) &&
1080e3d5408SPeter Wemm 		   ((wp->_pary + wp->_maxy) <= pp->_maxy));
1090e3d5408SPeter Wemm 
1107a69bbfbSPeter Wemm 	    for (y = 0; y <= wp->_maxy; y++) {
1110e3d5408SPeter Wemm 		int left = wp->_line[y].firstchar;
1127a69bbfbSPeter Wemm 		if (left >= 0) {	/* line is touched */
1130e3d5408SPeter Wemm 		    struct ldat *line = &(pp->_line[wp->_pary + y]);
1140e3d5408SPeter Wemm 		    /* left & right character in parent window coordinates */
1150e3d5408SPeter Wemm 		    int right = wp->_line[y].lastchar + wp->_parx;
1160e3d5408SPeter Wemm 		    left += wp->_parx;
1170e3d5408SPeter Wemm 
1180e3d5408SPeter Wemm 		    CHANGED_RANGE(line, left, right);
1190e3d5408SPeter Wemm 		}
1200e3d5408SPeter Wemm 	    }
1210e3d5408SPeter Wemm 	}
1220e3d5408SPeter Wemm     }
12339f2269fSPeter Wemm     returnVoid;
12439f2269fSPeter Wemm }
1250e3d5408SPeter Wemm 
1267a69bbfbSPeter Wemm NCURSES_EXPORT(void)
wsyncdown(WINDOW * win)1277a69bbfbSPeter Wemm wsyncdown(WINDOW *win)
1280e3d5408SPeter Wemm /* mark changed every cell in win that is changed in any of its ancestors */
1290e3d5408SPeter Wemm /* Rewritten by J. Pfeifer, 1-Apr-96 (don't even think that...)           */
1300e3d5408SPeter Wemm {
13106bfebdeSXin LI     T((T_CALLED("wsyncdown(%p)"), (void *) win));
13239f2269fSPeter Wemm 
133*21817992SBaptiste Daroussin     if (win != NULL && win->_parent != NULL) {
1340e3d5408SPeter Wemm 	WINDOW *pp = win->_parent;
1350e3d5408SPeter Wemm 	int y;
1360e3d5408SPeter Wemm 
1370e3d5408SPeter Wemm 	/* This recursion guarantees, that the changes are propagated down-
1380e3d5408SPeter Wemm 	   wards from the root to our direct parent. */
1390e3d5408SPeter Wemm 	wsyncdown(pp);
1400e3d5408SPeter Wemm 
1410e3d5408SPeter Wemm 	/* and now we only have to propagate the changes from our direct
1420e3d5408SPeter Wemm 	   parent, if there are any. */
1430e3d5408SPeter Wemm 	assert((win->_pary <= pp->_maxy) &&
1440e3d5408SPeter Wemm 	       ((win->_pary + win->_maxy) <= pp->_maxy));
1450e3d5408SPeter Wemm 
1467a69bbfbSPeter Wemm 	for (y = 0; y <= win->_maxy; y++) {
1477a69bbfbSPeter Wemm 	    if (pp->_line[win->_pary + y].firstchar >= 0) {	/* parent changed */
1480e3d5408SPeter Wemm 		struct ldat *line = &(win->_line[y]);
1490e3d5408SPeter Wemm 		/* left and right character in child coordinates */
1500e3d5408SPeter Wemm 		int left = pp->_line[win->_pary + y].firstchar - win->_parx;
1510e3d5408SPeter Wemm 		int right = pp->_line[win->_pary + y].lastchar - win->_parx;
1525ca44d1cSRong-En Fan 		/* The change may be outside the child's range */
1530e3d5408SPeter Wemm 		if (left < 0)
1540e3d5408SPeter Wemm 		    left = 0;
1550e3d5408SPeter Wemm 		if (right > win->_maxx)
1560e3d5408SPeter Wemm 		    right = win->_maxx;
1570e3d5408SPeter Wemm 		CHANGED_RANGE(line, left, right);
1580e3d5408SPeter Wemm 	    }
1590e3d5408SPeter Wemm 	}
1600e3d5408SPeter Wemm     }
16139f2269fSPeter Wemm     returnVoid;
1620e3d5408SPeter Wemm }
1630e3d5408SPeter Wemm 
1647a69bbfbSPeter Wemm NCURSES_EXPORT(void)
wcursyncup(WINDOW * win)1657a69bbfbSPeter Wemm wcursyncup(WINDOW *win)
1660e3d5408SPeter Wemm /* sync the cursor in all derived windows to its value in the base window */
1670e3d5408SPeter Wemm {
1680e3d5408SPeter Wemm     WINDOW *wp;
16939f2269fSPeter Wemm 
17006bfebdeSXin LI     T((T_CALLED("wcursyncup(%p)"), (void *) win));
1710e3d5408SPeter Wemm     for (wp = win; wp && wp->_parent; wp = wp->_parent) {
1720e3d5408SPeter Wemm 	wmove(wp->_parent, wp->_pary + wp->_cury, wp->_parx + wp->_curx);
1730e3d5408SPeter Wemm     }
17439f2269fSPeter Wemm     returnVoid;
1750e3d5408SPeter Wemm }
1760e3d5408SPeter Wemm 
1777a69bbfbSPeter Wemm NCURSES_EXPORT(WINDOW *)
dupwin(WINDOW * win)1787a69bbfbSPeter Wemm dupwin(WINDOW *win)
1790e3d5408SPeter Wemm /* make an exact duplicate of the given window */
1800e3d5408SPeter Wemm {
1814a1a9510SRong-En Fan     WINDOW *nwin = 0;
1820e3d5408SPeter Wemm 
18306bfebdeSXin LI     T((T_CALLED("dupwin(%p)"), (void *) win));
1840e3d5408SPeter Wemm 
1854a1a9510SRong-En Fan     if (win != 0) {
18606bfebdeSXin LI #if NCURSES_SP_FUNCS
18706bfebdeSXin LI 	SCREEN *sp = _nc_screen_of(win);
18806bfebdeSXin LI #endif
1895d08fb1fSRong-En Fan 	_nc_lock_global(curses);
190*21817992SBaptiste Daroussin 	if (IS_PAD(win)) {
19106bfebdeSXin LI 	    nwin = NCURSES_SP_NAME(newpad) (NCURSES_SP_ARGx
19206bfebdeSXin LI 					    win->_maxy + 1,
1934a1a9510SRong-En Fan 					    win->_maxx + 1);
1944a1a9510SRong-En Fan 	} else {
19506bfebdeSXin LI 	    nwin = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx
19606bfebdeSXin LI 					    win->_maxy + 1,
1974a1a9510SRong-En Fan 					    win->_maxx + 1,
1984a1a9510SRong-En Fan 					    win->_begy,
1994a1a9510SRong-En Fan 					    win->_begx);
2004a1a9510SRong-En Fan 	}
2014a1a9510SRong-En Fan 
2024a1a9510SRong-En Fan 	if (nwin != 0) {
203aae38d10SBaptiste Daroussin 	    int i;
204aae38d10SBaptiste Daroussin 	    size_t linesize;
2050e3d5408SPeter Wemm 
2060e3d5408SPeter Wemm 	    nwin->_curx = win->_curx;
2070e3d5408SPeter Wemm 	    nwin->_cury = win->_cury;
2080e3d5408SPeter Wemm 	    nwin->_maxy = win->_maxy;
2090e3d5408SPeter Wemm 	    nwin->_maxx = win->_maxx;
2100e3d5408SPeter Wemm 	    nwin->_begy = win->_begy;
2110e3d5408SPeter Wemm 	    nwin->_begx = win->_begx;
2120e3d5408SPeter Wemm 	    nwin->_yoffset = win->_yoffset;
2130e3d5408SPeter Wemm 
2140e3d5408SPeter Wemm 	    nwin->_flags = win->_flags & ~_SUBWIN;
2150e3d5408SPeter Wemm 	    /* Due to the use of newwin(), the clone is not a subwindow.
2160e3d5408SPeter Wemm 	     * The text is really copied into the clone.
2170e3d5408SPeter Wemm 	     */
2180e3d5408SPeter Wemm 
2194a1a9510SRong-En Fan 	    WINDOW_ATTRS(nwin) = WINDOW_ATTRS(win);
22039f2269fSPeter Wemm 	    nwin->_nc_bkgd = win->_nc_bkgd;
2210e3d5408SPeter Wemm 
2224a1a9510SRong-En Fan 	    nwin->_notimeout = win->_notimeout;
2230e3d5408SPeter Wemm 	    nwin->_clear = win->_clear;
2240e3d5408SPeter Wemm 	    nwin->_leaveok = win->_leaveok;
2254a1a9510SRong-En Fan 	    nwin->_scroll = win->_scroll;
2264a1a9510SRong-En Fan 	    nwin->_idlok = win->_idlok;
2274a1a9510SRong-En Fan 	    nwin->_idcok = win->_idcok;
2280e3d5408SPeter Wemm 	    nwin->_immed = win->_immed;
2290e3d5408SPeter Wemm 	    nwin->_sync = win->_sync;
2304a1a9510SRong-En Fan 	    nwin->_use_keypad = win->_use_keypad;
2314a1a9510SRong-En Fan 	    nwin->_delay = win->_delay;
2320e3d5408SPeter Wemm 
2330e3d5408SPeter Wemm 	    nwin->_parx = 0;
2340e3d5408SPeter Wemm 	    nwin->_pary = 0;
2350e3d5408SPeter Wemm 	    nwin->_parent = (WINDOW *) 0;
2360e3d5408SPeter Wemm 	    /* See above: the clone isn't a subwindow! */
2370e3d5408SPeter Wemm 
2380e3d5408SPeter Wemm 	    nwin->_regtop = win->_regtop;
2390e3d5408SPeter Wemm 	    nwin->_regbottom = win->_regbottom;
2400e3d5408SPeter Wemm 
241*21817992SBaptiste Daroussin 	    if (IS_PAD(win))
2424a1a9510SRong-En Fan 		nwin->_pad = win->_pad;
2434a1a9510SRong-En Fan 
24406bfebdeSXin LI 	    linesize = (unsigned) (win->_maxx + 1) * sizeof(NCURSES_CH_T);
2450e3d5408SPeter Wemm 	    for (i = 0; i <= nwin->_maxy; i++) {
2460e3d5408SPeter Wemm 		memcpy(nwin->_line[i].text, win->_line[i].text, linesize);
2470e3d5408SPeter Wemm 		nwin->_line[i].firstchar = win->_line[i].firstchar;
2480e3d5408SPeter Wemm 		nwin->_line[i].lastchar = win->_line[i].lastchar;
2490e3d5408SPeter Wemm 	    }
2504a1a9510SRong-En Fan 	}
2515d08fb1fSRong-En Fan 	_nc_unlock_global(curses);
2524a1a9510SRong-En Fan     }
2530e3d5408SPeter Wemm     returnWin(nwin);
2540e3d5408SPeter Wemm }
255