1 /****************************************************************************
2  * Copyright 2020 Thomas E. Dickey                                          *
3  * Copyright 1998-2009,2010 Free Software Foundation, Inc.                  *
4  *                                                                          *
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                 *
34  *     and: Juergen Pfeifer                                                 *
35  ****************************************************************************/
36 
37 /*
38 **	lib_mvwin.c
39 **
40 **	The routine mvwin().
41 **
42 */
43 
44 #include <curses.priv.h>
45 
46 MODULE_ID("$Id: lib_mvwin.c,v 1.19 2020/02/02 23:34:34 tom Exp $")
47 
48 NCURSES_EXPORT(int)
49 mvwin(WINDOW *win, int by, int bx)
50 {
51 #if NCURSES_SP_FUNCS
52     SCREEN *sp = _nc_screen_of(win);
53 #endif
54 
55     T((T_CALLED("mvwin(%p,%d,%d)"), (void *) win, by, bx));
56 
57     if (!win || (win->_flags & _ISPAD))
58 	returnCode(ERR);
59 
60     /*
61      * mvwin() should only modify the indices.  See test/demo_menus.c and
62      * test/movewindow.c for examples.
63      */
64 #if 0
65     /* Copying subwindows is allowed, but it is expensive... */
66     if (win->_flags & _SUBWIN) {
67 	int err = ERR;
68 	WINDOW *parent = win->_parent;
69 	if (parent) {		/* Now comes the complicated and costly part, you should really
70 				 * try to avoid to move subwindows. Because a subwindow shares
71 				 * the text buffers with its parent, one can't do a simple
72 				 * memmove of the text buffers. One has to create a copy, then
73 				 * to relocate the subwindow and then to do a copy.
74 				 */
75 	    if ((by - parent->_begy == win->_pary) &&
76 		(bx - parent->_begx == win->_parx))
77 		err = OK;	/* we don't actually move */
78 	    else {
79 		WINDOW *clone = dupwin(win);
80 		if (clone) {
81 		    /* now we have the clone, so relocate win */
82 
83 		    werase(win);	/* Erase the original place     */
84 		    /* fill with parents background */
85 		    wbkgrnd(win, CHREF(parent->_nc_bkgd));
86 		    wsyncup(win);	/* Tell the parent(s)           */
87 
88 		    err = mvderwin(win,
89 				   by - parent->_begy,
90 				   bx - parent->_begx);
91 		    if (err != ERR) {
92 			err = copywin(clone, win,
93 				      0, 0, 0, 0, win->_maxy, win->_maxx, 0);
94 			if (ERR != err)
95 			    wsyncup(win);
96 		    }
97 		    if (ERR == delwin(clone))
98 			err = ERR;
99 		}
100 	    }
101 	}
102 	returnCode(err);
103     }
104 #endif
105 
106     if (by + win->_maxy > screen_lines(SP_PARM) - 1
107 	|| bx + win->_maxx > screen_columns(SP_PARM) - 1
108 	|| by < 0
109 	|| bx < 0)
110 	returnCode(ERR);
111 
112     /*
113      * Whether or not the window is moved, touch the window's contents so
114      * that a following call to 'wrefresh()' will paint the window at the
115      * new location.  This ensures that if the caller has refreshed another
116      * window at the same location, that this one will be displayed.
117      */
118     win->_begy = (NCURSES_SIZE_T) by;
119     win->_begx = (NCURSES_SIZE_T) bx;
120     returnCode(touchwin(win));
121 }
122