xref: /openbsd/lib/libcurses/base/lib_window.c (revision 09467b48)
1 /* $OpenBSD: lib_window.c,v 1.3 2010/01/12 23:22:06 nicm Exp $ */
2 
3 /****************************************************************************
4  * Copyright (c) 1998-2007,2008 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("$Id: lib_window.c,v 1.3 2010/01/12 23:22:06 nicm 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     T((T_CALLED("wsyncup(%p)"), win));
104     if (win && win->_parent) {
105 	for (wp = win; wp->_parent; wp = wp->_parent) {
106 	    int y;
107 	    WINDOW *pp = wp->_parent;
108 
109 	    assert((wp->_pary <= pp->_maxy) &&
110 		   ((wp->_pary + wp->_maxy) <= pp->_maxy));
111 
112 	    for (y = 0; y <= wp->_maxy; y++) {
113 		int left = wp->_line[y].firstchar;
114 		if (left >= 0) {	/* line is touched */
115 		    struct ldat *line = &(pp->_line[wp->_pary + y]);
116 		    /* left & right character in parent window coordinates */
117 		    int right = wp->_line[y].lastchar + wp->_parx;
118 		    left += wp->_parx;
119 
120 		    CHANGED_RANGE(line, left, right);
121 		}
122 	    }
123 	}
124     }
125     returnVoid;
126 }
127 
128 NCURSES_EXPORT(void)
129 wsyncdown(WINDOW *win)
130 /* mark changed every cell in win that is changed in any of its ancestors */
131 /* Rewritten by J. Pfeifer, 1-Apr-96 (don't even think that...)           */
132 {
133     T((T_CALLED("wsyncdown(%p)"), win));
134 
135     if (win && win->_parent) {
136 	WINDOW *pp = win->_parent;
137 	int y;
138 
139 	/* This recursion guarantees, that the changes are propagated down-
140 	   wards from the root to our direct parent. */
141 	wsyncdown(pp);
142 
143 	/* and now we only have to propagate the changes from our direct
144 	   parent, if there are any. */
145 	assert((win->_pary <= pp->_maxy) &&
146 	       ((win->_pary + win->_maxy) <= pp->_maxy));
147 
148 	for (y = 0; y <= win->_maxy; y++) {
149 	    if (pp->_line[win->_pary + y].firstchar >= 0) {	/* parent changed */
150 		struct ldat *line = &(win->_line[y]);
151 		/* left and right character in child coordinates */
152 		int left = pp->_line[win->_pary + y].firstchar - win->_parx;
153 		int right = pp->_line[win->_pary + y].lastchar - win->_parx;
154 		/* The change may be outside the child's range */
155 		if (left < 0)
156 		    left = 0;
157 		if (right > win->_maxx)
158 		    right = win->_maxx;
159 		CHANGED_RANGE(line, left, right);
160 	    }
161 	}
162     }
163     returnVoid;
164 }
165 
166 NCURSES_EXPORT(void)
167 wcursyncup(WINDOW *win)
168 /* sync the cursor in all derived windows to its value in the base window */
169 {
170     WINDOW *wp;
171 
172     T((T_CALLED("wcursyncup(%p)"), win));
173     for (wp = win; wp && wp->_parent; wp = wp->_parent) {
174 	wmove(wp->_parent, wp->_pary + wp->_cury, wp->_parx + wp->_curx);
175     }
176     returnVoid;
177 }
178 
179 NCURSES_EXPORT(WINDOW *)
180 dupwin(WINDOW *win)
181 /* make an exact duplicate of the given window */
182 {
183     WINDOW *nwin = 0;
184     size_t linesize;
185     int i;
186 
187     T((T_CALLED("dupwin(%p)"), win));
188 
189     if (win != 0) {
190 
191 	_nc_lock_global(curses);
192 	if (win->_flags & _ISPAD) {
193 	    nwin = newpad(win->_maxy + 1,
194 			  win->_maxx + 1);
195 	} else {
196 	    nwin = newwin(win->_maxy + 1,
197 			  win->_maxx + 1,
198 			  win->_begy,
199 			  win->_begx);
200 	}
201 
202 	if (nwin != 0) {
203 
204 	    nwin->_curx = win->_curx;
205 	    nwin->_cury = win->_cury;
206 	    nwin->_maxy = win->_maxy;
207 	    nwin->_maxx = win->_maxx;
208 	    nwin->_begy = win->_begy;
209 	    nwin->_begx = win->_begx;
210 	    nwin->_yoffset = win->_yoffset;
211 
212 	    nwin->_flags = win->_flags & ~_SUBWIN;
213 	    /* Due to the use of newwin(), the clone is not a subwindow.
214 	     * The text is really copied into the clone.
215 	     */
216 
217 	    WINDOW_ATTRS(nwin) = WINDOW_ATTRS(win);
218 	    nwin->_nc_bkgd = win->_nc_bkgd;
219 
220 	    nwin->_notimeout = win->_notimeout;
221 	    nwin->_clear = win->_clear;
222 	    nwin->_leaveok = win->_leaveok;
223 	    nwin->_scroll = win->_scroll;
224 	    nwin->_idlok = win->_idlok;
225 	    nwin->_idcok = win->_idcok;
226 	    nwin->_immed = win->_immed;
227 	    nwin->_sync = win->_sync;
228 	    nwin->_use_keypad = win->_use_keypad;
229 	    nwin->_delay = win->_delay;
230 
231 	    nwin->_parx = 0;
232 	    nwin->_pary = 0;
233 	    nwin->_parent = (WINDOW *) 0;
234 	    /* See above: the clone isn't a subwindow! */
235 
236 	    nwin->_regtop = win->_regtop;
237 	    nwin->_regbottom = win->_regbottom;
238 
239 	    if (win->_flags & _ISPAD)
240 		nwin->_pad = win->_pad;
241 
242 	    linesize = (win->_maxx + 1) * sizeof(NCURSES_CH_T);
243 	    for (i = 0; i <= nwin->_maxy; i++) {
244 		memcpy(nwin->_line[i].text, win->_line[i].text, linesize);
245 		nwin->_line[i].firstchar = win->_line[i].firstchar;
246 		nwin->_line[i].lastchar = win->_line[i].lastchar;
247 	    }
248 	}
249 	_nc_unlock_global(curses);
250     }
251     returnWin(nwin);
252 }
253