xref: /freebsd/contrib/ncurses/ncurses/base/lib_window.c (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
1 /****************************************************************************
2  * Copyright (c) 1998,2000 Free Software Foundation, Inc.                   *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  ****************************************************************************/
33 
34 /*
35 **	lib_window.c
36 **
37 **
38 */
39 
40 #include <curses.priv.h>
41 
42 MODULE_ID("$Id: lib_window.c,v 1.15 2000/12/10 02:43:28 tom Exp $")
43 
44 NCURSES_EXPORT(void)
45 _nc_synchook(WINDOW *win)
46 /* hook to be called after each window change */
47 {
48     if (win->_immed)
49 	wrefresh(win);
50     if (win->_sync)
51 	wsyncup(win);
52 }
53 
54 NCURSES_EXPORT(int)
55 mvderwin(WINDOW *win, int y, int x)
56 /* move a derived window */
57 {
58     WINDOW *orig;
59     int i;
60 
61     T((T_CALLED("mvderwin(%p,%d,%d)"), win, y, x));
62 
63     if (win && (orig = win->_parent)) {
64 	if (win->_parx == x && win->_pary == y)
65 	    returnCode(OK);
66 	if (x < 0 || y < 0)
67 	    returnCode(ERR);
68 	if ((x + getmaxx(win) > getmaxx(orig)) ||
69 	    (y + getmaxy(win) > getmaxy(orig)))
70 	    returnCode(ERR);
71     } else
72 	returnCode(ERR);
73     wsyncup(win);
74     win->_parx = x;
75     win->_pary = y;
76     for (i = 0; i < getmaxy(win); i++)
77 	win->_line[i].text = &(orig->_line[y++].text[x]);
78     returnCode(OK);
79 }
80 
81 NCURSES_EXPORT(int)
82 syncok(WINDOW *win, bool bf)
83 /* enable/disable automatic wsyncup() on each change to window */
84 {
85     T((T_CALLED("syncok(%p,%d)"), win, bf));
86 
87     if (win) {
88 	win->_sync = bf;
89 	returnCode(OK);
90     } else
91 	returnCode(ERR);
92 }
93 
94 NCURSES_EXPORT(void)
95 wsyncup(WINDOW *win)
96 /* mark changed every cell in win's ancestors that is changed in win */
97 /* Rewritten by J. Pfeifer, 1-Apr-96 (don't even think that...)      */
98 {
99     WINDOW *wp;
100 
101     if (win && win->_parent)
102 	for (wp = win; wp->_parent; wp = wp->_parent) {
103 	    int y;
104 	    WINDOW *pp = wp->_parent;
105 
106 	    assert((wp->_pary <= pp->_maxy) &&
107 		   ((wp->_pary + wp->_maxy) <= pp->_maxy));
108 
109 	    for (y = 0; y <= wp->_maxy; y++) {
110 		int left = wp->_line[y].firstchar;
111 		if (left >= 0) {	/* line is touched */
112 		    struct ldat *line = &(pp->_line[wp->_pary + y]);
113 		    /* left & right character in parent window coordinates */
114 		    int right = wp->_line[y].lastchar + wp->_parx;
115 		    left += wp->_parx;
116 
117 		    CHANGED_RANGE(line, left, right);
118 		}
119 	    }
120 	}
121 }
122 
123 NCURSES_EXPORT(void)
124 wsyncdown(WINDOW *win)
125 /* mark changed every cell in win that is changed in any of its ancestors */
126 /* Rewritten by J. Pfeifer, 1-Apr-96 (don't even think that...)           */
127 {
128     if (win && win->_parent) {
129 	WINDOW *pp = win->_parent;
130 	int y;
131 
132 	/* This recursion guarantees, that the changes are propagated down-
133 	   wards from the root to our direct parent. */
134 	wsyncdown(pp);
135 
136 	/* and now we only have to propagate the changes from our direct
137 	   parent, if there are any. */
138 	assert((win->_pary <= pp->_maxy) &&
139 	       ((win->_pary + win->_maxy) <= pp->_maxy));
140 
141 	for (y = 0; y <= win->_maxy; y++) {
142 	    if (pp->_line[win->_pary + y].firstchar >= 0) {	/* parent changed */
143 		struct ldat *line = &(win->_line[y]);
144 		/* left and right character in child coordinates */
145 		int left = pp->_line[win->_pary + y].firstchar - win->_parx;
146 		int right = pp->_line[win->_pary + y].lastchar - win->_parx;
147 		/* The change maybe outside the childs range */
148 		if (left < 0)
149 		    left = 0;
150 		if (right > win->_maxx)
151 		    right = win->_maxx;
152 		CHANGED_RANGE(line, left, right);
153 	    }
154 	}
155     }
156 }
157 
158 NCURSES_EXPORT(void)
159 wcursyncup(WINDOW *win)
160 /* sync the cursor in all derived windows to its value in the base window */
161 {
162     WINDOW *wp;
163     for (wp = win; wp && wp->_parent; wp = wp->_parent) {
164 	wmove(wp->_parent, wp->_pary + wp->_cury, wp->_parx + wp->_curx);
165     }
166 }
167 
168 NCURSES_EXPORT(WINDOW *)
169 dupwin(WINDOW *win)
170 /* make an exact duplicate of the given window */
171 {
172     WINDOW *nwin;
173     size_t linesize;
174     int i;
175 
176     T((T_CALLED("dupwin(%p)"), win));
177 
178     if ((win == NULL) ||
179 	((nwin = newwin(win->_maxy + 1, win->_maxx + 1, win->_begy,
180 	 win->_begx)) == NULL))
181 	returnWin(0);
182 
183     nwin->_curx = win->_curx;
184     nwin->_cury = win->_cury;
185     nwin->_maxy = win->_maxy;
186     nwin->_maxx = win->_maxx;
187     nwin->_begy = win->_begy;
188     nwin->_begx = win->_begx;
189     nwin->_yoffset = win->_yoffset;
190 
191     nwin->_flags = win->_flags & ~_SUBWIN;
192     /* Due to the use of newwin(), the clone is not a subwindow.
193      * The text is really copied into the clone.
194      */
195 
196     nwin->_attrs = win->_attrs;
197     nwin->_bkgd = win->_bkgd;
198 
199     nwin->_clear = win->_clear;
200     nwin->_scroll = win->_scroll;
201     nwin->_leaveok = win->_leaveok;
202     nwin->_use_keypad = win->_use_keypad;
203     nwin->_delay = win->_delay;
204     nwin->_immed = win->_immed;
205     nwin->_sync = win->_sync;
206 
207     nwin->_parx = 0;
208     nwin->_pary = 0;
209     nwin->_parent = (WINDOW *) 0;
210     /* See above: the clone isn't a subwindow! */
211 
212     nwin->_regtop = win->_regtop;
213     nwin->_regbottom = win->_regbottom;
214 
215     linesize = (win->_maxx + 1) * sizeof(chtype);
216     for (i = 0; i <= nwin->_maxy; i++) {
217 	memcpy(nwin->_line[i].text, win->_line[i].text, linesize);
218 	nwin->_line[i].firstchar = win->_line[i].firstchar;
219 	nwin->_line[i].lastchar = win->_line[i].lastchar;
220     }
221 
222     returnWin(nwin);
223 }
224