xref: /freebsd/contrib/ncurses/ncurses/base/lib_window.c (revision 5d08fb1f777aa988958a4f0c45887b0cbfaef9a0)
10e3d5408SPeter Wemm /****************************************************************************
2aa59d4d4SRong-En Fan  * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
30e3d5408SPeter Wemm  *                                                                          *
40e3d5408SPeter Wemm  * Permission is hereby granted, free of charge, to any person obtaining a  *
50e3d5408SPeter Wemm  * copy of this software and associated documentation files (the            *
60e3d5408SPeter Wemm  * "Software"), to deal in the Software without restriction, including      *
70e3d5408SPeter Wemm  * without limitation the rights to use, copy, modify, merge, publish,      *
80e3d5408SPeter Wemm  * distribute, distribute with modifications, sublicense, and/or sell       *
90e3d5408SPeter Wemm  * copies of the Software, and to permit persons to whom the Software is    *
100e3d5408SPeter Wemm  * furnished to do so, subject to the following conditions:                 *
110e3d5408SPeter Wemm  *                                                                          *
120e3d5408SPeter Wemm  * The above copyright notice and this permission notice shall be included  *
130e3d5408SPeter Wemm  * in all copies or substantial portions of the Software.                   *
140e3d5408SPeter Wemm  *                                                                          *
150e3d5408SPeter Wemm  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
160e3d5408SPeter Wemm  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
170e3d5408SPeter Wemm  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
180e3d5408SPeter Wemm  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
190e3d5408SPeter Wemm  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
200e3d5408SPeter Wemm  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
210e3d5408SPeter Wemm  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
220e3d5408SPeter Wemm  *                                                                          *
230e3d5408SPeter Wemm  * Except as contained in this notice, the name(s) of the above copyright   *
240e3d5408SPeter Wemm  * holders shall not be used in advertising or otherwise to promote the     *
250e3d5408SPeter Wemm  * sale, use or other dealings in this Software without prior written       *
260e3d5408SPeter Wemm  * authorization.                                                           *
270e3d5408SPeter Wemm  ****************************************************************************/
280e3d5408SPeter Wemm 
290e3d5408SPeter Wemm /****************************************************************************
300e3d5408SPeter Wemm  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
310e3d5408SPeter Wemm  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
320e3d5408SPeter Wemm  ****************************************************************************/
330e3d5408SPeter Wemm 
340e3d5408SPeter Wemm /*
350e3d5408SPeter Wemm **	lib_window.c
360e3d5408SPeter Wemm **
370e3d5408SPeter Wemm **
380e3d5408SPeter Wemm */
390e3d5408SPeter Wemm 
400e3d5408SPeter Wemm #include <curses.priv.h>
410e3d5408SPeter Wemm 
425d08fb1fSRong-En Fan MODULE_ID("$Id: lib_window.c,v 1.25 2008/06/07 14:12:56 tom Exp $")
430e3d5408SPeter Wemm 
447a69bbfbSPeter Wemm NCURSES_EXPORT(void)
457a69bbfbSPeter Wemm _nc_synchook(WINDOW *win)
460e3d5408SPeter Wemm /* hook to be called after each window change */
470e3d5408SPeter Wemm {
487a69bbfbSPeter Wemm     if (win->_immed)
497a69bbfbSPeter Wemm 	wrefresh(win);
507a69bbfbSPeter Wemm     if (win->_sync)
517a69bbfbSPeter Wemm 	wsyncup(win);
520e3d5408SPeter Wemm }
530e3d5408SPeter Wemm 
547a69bbfbSPeter Wemm NCURSES_EXPORT(int)
557a69bbfbSPeter Wemm mvderwin(WINDOW *win, int y, int x)
560e3d5408SPeter Wemm /* move a derived window */
570e3d5408SPeter Wemm {
580e3d5408SPeter Wemm     WINDOW *orig;
590e3d5408SPeter Wemm     int i;
600e3d5408SPeter Wemm 
610e3d5408SPeter Wemm     T((T_CALLED("mvderwin(%p,%d,%d)"), win, y, x));
620e3d5408SPeter Wemm 
637a69bbfbSPeter Wemm     if (win && (orig = win->_parent)) {
640e3d5408SPeter Wemm 	if (win->_parx == x && win->_pary == y)
650e3d5408SPeter Wemm 	    returnCode(OK);
660e3d5408SPeter Wemm 	if (x < 0 || y < 0)
670e3d5408SPeter Wemm 	    returnCode(ERR);
680e3d5408SPeter Wemm 	if ((x + getmaxx(win) > getmaxx(orig)) ||
690e3d5408SPeter Wemm 	    (y + getmaxy(win) > getmaxy(orig)))
700e3d5408SPeter Wemm 	    returnCode(ERR);
717a69bbfbSPeter Wemm     } else
720e3d5408SPeter Wemm 	returnCode(ERR);
730e3d5408SPeter Wemm     wsyncup(win);
740e3d5408SPeter Wemm     win->_parx = x;
750e3d5408SPeter Wemm     win->_pary = y;
760e3d5408SPeter Wemm     for (i = 0; i < getmaxy(win); i++)
770e3d5408SPeter Wemm 	win->_line[i].text = &(orig->_line[y++].text[x]);
780e3d5408SPeter Wemm     returnCode(OK);
790e3d5408SPeter Wemm }
800e3d5408SPeter Wemm 
817a69bbfbSPeter Wemm NCURSES_EXPORT(int)
827a69bbfbSPeter Wemm syncok(WINDOW *win, bool bf)
830e3d5408SPeter Wemm /* enable/disable automatic wsyncup() on each change to window */
840e3d5408SPeter Wemm {
850e3d5408SPeter Wemm     T((T_CALLED("syncok(%p,%d)"), 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)
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 
10139f2269fSPeter Wemm     T((T_CALLED("wsyncup(%p)"), 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)
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 {
13139f2269fSPeter Wemm     T((T_CALLED("wsyncdown(%p)"), win));
13239f2269fSPeter Wemm 
1337a69bbfbSPeter Wemm     if (win && win->_parent) {
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)
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 
17039f2269fSPeter Wemm     T((T_CALLED("wcursyncup(%p)"), 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 *)
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     size_t linesize;
1830e3d5408SPeter Wemm     int i;
1840e3d5408SPeter Wemm 
1850e3d5408SPeter Wemm     T((T_CALLED("dupwin(%p)"), win));
1860e3d5408SPeter Wemm 
1874a1a9510SRong-En Fan     if (win != 0) {
1884a1a9510SRong-En Fan 
1895d08fb1fSRong-En Fan 	_nc_lock_global(curses);
1904a1a9510SRong-En Fan 	if (win->_flags & _ISPAD) {
1914a1a9510SRong-En Fan 	    nwin = newpad(win->_maxy + 1,
1924a1a9510SRong-En Fan 			  win->_maxx + 1);
1934a1a9510SRong-En Fan 	} else {
1944a1a9510SRong-En Fan 	    nwin = newwin(win->_maxy + 1,
1954a1a9510SRong-En Fan 			  win->_maxx + 1,
1964a1a9510SRong-En Fan 			  win->_begy,
1974a1a9510SRong-En Fan 			  win->_begx);
1984a1a9510SRong-En Fan 	}
1994a1a9510SRong-En Fan 
2004a1a9510SRong-En Fan 	if (nwin != 0) {
2010e3d5408SPeter Wemm 
2020e3d5408SPeter Wemm 	    nwin->_curx = win->_curx;
2030e3d5408SPeter Wemm 	    nwin->_cury = win->_cury;
2040e3d5408SPeter Wemm 	    nwin->_maxy = win->_maxy;
2050e3d5408SPeter Wemm 	    nwin->_maxx = win->_maxx;
2060e3d5408SPeter Wemm 	    nwin->_begy = win->_begy;
2070e3d5408SPeter Wemm 	    nwin->_begx = win->_begx;
2080e3d5408SPeter Wemm 	    nwin->_yoffset = win->_yoffset;
2090e3d5408SPeter Wemm 
2100e3d5408SPeter Wemm 	    nwin->_flags = win->_flags & ~_SUBWIN;
2110e3d5408SPeter Wemm 	    /* Due to the use of newwin(), the clone is not a subwindow.
2120e3d5408SPeter Wemm 	     * The text is really copied into the clone.
2130e3d5408SPeter Wemm 	     */
2140e3d5408SPeter Wemm 
2154a1a9510SRong-En Fan 	    WINDOW_ATTRS(nwin) = WINDOW_ATTRS(win);
21639f2269fSPeter Wemm 	    nwin->_nc_bkgd = win->_nc_bkgd;
2170e3d5408SPeter Wemm 
2184a1a9510SRong-En Fan 	    nwin->_notimeout = win->_notimeout;
2190e3d5408SPeter Wemm 	    nwin->_clear = win->_clear;
2200e3d5408SPeter Wemm 	    nwin->_leaveok = win->_leaveok;
2214a1a9510SRong-En Fan 	    nwin->_scroll = win->_scroll;
2224a1a9510SRong-En Fan 	    nwin->_idlok = win->_idlok;
2234a1a9510SRong-En Fan 	    nwin->_idcok = win->_idcok;
2240e3d5408SPeter Wemm 	    nwin->_immed = win->_immed;
2250e3d5408SPeter Wemm 	    nwin->_sync = win->_sync;
2264a1a9510SRong-En Fan 	    nwin->_use_keypad = win->_use_keypad;
2274a1a9510SRong-En Fan 	    nwin->_delay = win->_delay;
2280e3d5408SPeter Wemm 
2290e3d5408SPeter Wemm 	    nwin->_parx = 0;
2300e3d5408SPeter Wemm 	    nwin->_pary = 0;
2310e3d5408SPeter Wemm 	    nwin->_parent = (WINDOW *) 0;
2320e3d5408SPeter Wemm 	    /* See above: the clone isn't a subwindow! */
2330e3d5408SPeter Wemm 
2340e3d5408SPeter Wemm 	    nwin->_regtop = win->_regtop;
2350e3d5408SPeter Wemm 	    nwin->_regbottom = win->_regbottom;
2360e3d5408SPeter Wemm 
2374a1a9510SRong-En Fan 	    if (win->_flags & _ISPAD)
2384a1a9510SRong-En Fan 		nwin->_pad = win->_pad;
2394a1a9510SRong-En Fan 
2404a1a9510SRong-En Fan 	    linesize = (win->_maxx + 1) * sizeof(NCURSES_CH_T);
2410e3d5408SPeter Wemm 	    for (i = 0; i <= nwin->_maxy; i++) {
2420e3d5408SPeter Wemm 		memcpy(nwin->_line[i].text, win->_line[i].text, linesize);
2430e3d5408SPeter Wemm 		nwin->_line[i].firstchar = win->_line[i].firstchar;
2440e3d5408SPeter Wemm 		nwin->_line[i].lastchar = win->_line[i].lastchar;
2450e3d5408SPeter Wemm 	    }
2464a1a9510SRong-En Fan 	}
2475d08fb1fSRong-En Fan 	_nc_unlock_global(curses);
2484a1a9510SRong-En Fan     }
2490e3d5408SPeter Wemm     returnWin(nwin);
2500e3d5408SPeter Wemm }
251