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