xref: /freebsd/contrib/ncurses/ncurses/base/lib_mvwin.c (revision 21817992b3314c908ab50f0bb88d2ee750b9c4ac)
10e3d5408SPeter Wemm /****************************************************************************
2*21817992SBaptiste Daroussin  * Copyright 2020,2021 Thomas E. Dickey                                     *
3e1865124SBaptiste Daroussin  * Copyright 1998-2009,2010 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>                         *
3306bfebdeSXin LI  *     and: Thomas E. Dickey                        1996-on                 *
3406bfebdeSXin LI  *     and: Juergen Pfeifer                                                 *
350e3d5408SPeter Wemm  ****************************************************************************/
360e3d5408SPeter Wemm 
370e3d5408SPeter Wemm /*
380e3d5408SPeter Wemm **	lib_mvwin.c
390e3d5408SPeter Wemm **
400e3d5408SPeter Wemm **	The routine mvwin().
410e3d5408SPeter Wemm **
420e3d5408SPeter Wemm */
430e3d5408SPeter Wemm 
440e3d5408SPeter Wemm #include <curses.priv.h>
450e3d5408SPeter Wemm 
46*21817992SBaptiste Daroussin MODULE_ID("$Id: lib_mvwin.c,v 1.20 2021/10/23 18:57:41 tom Exp $")
470e3d5408SPeter Wemm 
NCURSES_EXPORT(int)487a69bbfbSPeter Wemm NCURSES_EXPORT(int)
497a69bbfbSPeter Wemm mvwin(WINDOW *win, int by, int bx)
500e3d5408SPeter Wemm {
5106bfebdeSXin LI #if NCURSES_SP_FUNCS
5206bfebdeSXin LI     SCREEN *sp = _nc_screen_of(win);
5306bfebdeSXin LI #endif
5406bfebdeSXin LI 
5506bfebdeSXin LI     T((T_CALLED("mvwin(%p,%d,%d)"), (void *) win, by, bx));
560e3d5408SPeter Wemm 
57*21817992SBaptiste Daroussin     if (!win || IS_PAD(win))
580e3d5408SPeter Wemm 	returnCode(ERR);
590e3d5408SPeter Wemm 
604a1a9510SRong-En Fan     /*
614a1a9510SRong-En Fan      * mvwin() should only modify the indices.  See test/demo_menus.c and
624a1a9510SRong-En Fan      * test/movewindow.c for examples.
634a1a9510SRong-En Fan      */
644a1a9510SRong-En Fan #if 0
650e3d5408SPeter Wemm     /* Copying subwindows is allowed, but it is expensive... */
66*21817992SBaptiste Daroussin     if (IS_SUBWIN(win)) {
670e3d5408SPeter Wemm 	int err = ERR;
680e3d5408SPeter Wemm 	WINDOW *parent = win->_parent;
697a69bbfbSPeter Wemm 	if (parent) {		/* Now comes the complicated and costly part, you should really
700e3d5408SPeter Wemm 				 * try to avoid to move subwindows. Because a subwindow shares
710e3d5408SPeter Wemm 				 * the text buffers with its parent, one can't do a simple
720e3d5408SPeter Wemm 				 * memmove of the text buffers. One has to create a copy, then
730e3d5408SPeter Wemm 				 * to relocate the subwindow and then to do a copy.
740e3d5408SPeter Wemm 				 */
750e3d5408SPeter Wemm 	    if ((by - parent->_begy == win->_pary) &&
760e3d5408SPeter Wemm 		(bx - parent->_begx == win->_parx))
770e3d5408SPeter Wemm 		err = OK;	/* we don't actually move */
780e3d5408SPeter Wemm 	    else {
790e3d5408SPeter Wemm 		WINDOW *clone = dupwin(win);
800e3d5408SPeter Wemm 		if (clone) {
810e3d5408SPeter Wemm 		    /* now we have the clone, so relocate win */
820e3d5408SPeter Wemm 
830e3d5408SPeter Wemm 		    werase(win);	/* Erase the original place     */
8439f2269fSPeter Wemm 		    /* fill with parents background */
8539f2269fSPeter Wemm 		    wbkgrnd(win, CHREF(parent->_nc_bkgd));
860e3d5408SPeter Wemm 		    wsyncup(win);	/* Tell the parent(s)           */
870e3d5408SPeter Wemm 
880e3d5408SPeter Wemm 		    err = mvderwin(win,
890e3d5408SPeter Wemm 				   by - parent->_begy,
900e3d5408SPeter Wemm 				   bx - parent->_begx);
910e3d5408SPeter Wemm 		    if (err != ERR) {
920e3d5408SPeter Wemm 			err = copywin(clone, win,
930e3d5408SPeter Wemm 				      0, 0, 0, 0, win->_maxy, win->_maxx, 0);
940e3d5408SPeter Wemm 			if (ERR != err)
950e3d5408SPeter Wemm 			    wsyncup(win);
960e3d5408SPeter Wemm 		    }
970e3d5408SPeter Wemm 		    if (ERR == delwin(clone))
980e3d5408SPeter Wemm 			err = ERR;
990e3d5408SPeter Wemm 		}
1000e3d5408SPeter Wemm 	    }
1010e3d5408SPeter Wemm 	}
1020e3d5408SPeter Wemm 	returnCode(err);
1030e3d5408SPeter Wemm     }
1044a1a9510SRong-En Fan #endif
1050e3d5408SPeter Wemm 
10606bfebdeSXin LI     if (by + win->_maxy > screen_lines(SP_PARM) - 1
10706bfebdeSXin LI 	|| bx + win->_maxx > screen_columns(SP_PARM) - 1
1080e3d5408SPeter Wemm 	|| by < 0
1090e3d5408SPeter Wemm 	|| bx < 0)
1100e3d5408SPeter Wemm 	returnCode(ERR);
1110e3d5408SPeter Wemm 
1120e3d5408SPeter Wemm     /*
1130e3d5408SPeter Wemm      * Whether or not the window is moved, touch the window's contents so
1140e3d5408SPeter Wemm      * that a following call to 'wrefresh()' will paint the window at the
1150e3d5408SPeter Wemm      * new location.  This ensures that if the caller has refreshed another
1160e3d5408SPeter Wemm      * window at the same location, that this one will be displayed.
1170e3d5408SPeter Wemm      */
11806bfebdeSXin LI     win->_begy = (NCURSES_SIZE_T) by;
11906bfebdeSXin LI     win->_begx = (NCURSES_SIZE_T) bx;
1200e3d5408SPeter Wemm     returnCode(touchwin(win));
1210e3d5408SPeter Wemm }
122