xref: /openbsd-src/lib/libcurses/base/lib_window.c (revision 92dd1ec0a89df25171bc5d61a3d95ea1a68cef0b)
1 /*	$OpenBSD: lib_window.c,v 1.1 1999/01/18 19:10:06 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998 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.13 1998/06/28 00:10:59 tom Exp $")
45 
46 void _nc_synchook(WINDOW *win)
47 /* hook to be called after each window change */
48 {
49 	if (win->_immed) wrefresh(win);
50 	if (win->_sync) wsyncup(win);
51 }
52 
53 int mvderwin(WINDOW *win, int y, int x)
54 /* move a derived window */
55 {
56    WINDOW *orig;
57    int i;
58 
59    T((T_CALLED("mvderwin(%p,%d,%d)"), win, y, x));
60 
61    if (win && (orig = win->_parent))
62    {
63       if (win->_parx==x && win->_pary==y)
64 	returnCode(OK);
65       if (x<0 || y<0)
66 	returnCode(ERR);
67       if ( (x+getmaxx(win) > getmaxx(orig)) ||
68            (y+getmaxy(win) > getmaxy(orig)) )
69         returnCode(ERR);
70    }
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 int syncok(WINDOW *win, bool bf)
82 /* enable/disable automatic wsyncup() on each change to window */
83 {
84 	T((T_CALLED("syncok(%p,%d)"), win, bf));
85 
86 	if (win) {
87 		win->_sync = bf;
88 		returnCode(OK);
89 	} else
90 		returnCode(ERR);
91 }
92 
93 void wsyncup(WINDOW *win)
94 /* mark changed every cell in win's ancestors that is changed in win */
95 /* Rewritten by J. Pfeifer, 1-Apr-96 (don't even think that...)      */
96 {
97   WINDOW	*wp;
98 
99   if (win && win->_parent)
100     for (wp = win; wp->_parent; wp = wp->_parent)
101       {
102 	int y;
103 	WINDOW *pp = wp->_parent;
104 
105 	assert((wp->_pary <= pp->_maxy) &&
106 	       ((wp->_pary+wp->_maxy) <= pp->_maxy));
107 
108 	for (y = 0; y <= wp->_maxy; y++)
109 	  {
110 	    int left = wp->_line[y].firstchar;
111 	    if (left >= 0) /* line is touched */
112 	      {
113 		struct ldat *line = &(pp->_line[wp->_pary + y]);
114 		/* left & right character in parent window coordinates */
115 		int right = wp->_line[y].lastchar + wp->_parx;
116 		left += wp->_parx;
117 
118 		CHANGED_RANGE(line, left, right);
119 	      }
120 	  }
121       }
122 }
123 
124 void 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     {
130       WINDOW *pp = win->_parent;
131       int y;
132 
133       /* This recursion guarantees, that the changes are propagated down-
134 	 wards from the root to our direct parent. */
135       wsyncdown(pp);
136 
137       /* and now we only have to propagate the changes from our direct
138 	 parent, if there are any. */
139       assert((win->_pary <= pp->_maxy) &&
140 	     ((win->_pary + win->_maxy) <= pp->_maxy));
141 
142       for (y = 0; y <= win->_maxy; y++)
143 	{
144 	  if (pp->_line[win->_pary + y].firstchar >= 0) /* parent changed */
145 	    {
146 	      struct ldat *line = &(win->_line[y]);
147 	      /* left and right character in child coordinates */
148 	      int left  = pp->_line[win->_pary + y].firstchar - win->_parx;
149 	      int right = pp->_line[win->_pary + y].lastchar  - win->_parx;
150 	      /* The change maybe outside the childs range */
151 	      if (left<0)
152 		left = 0;
153 	      if (right > win->_maxx)
154 		right = win->_maxx;
155 	      CHANGED_RANGE(line, left, right);
156 	    }
157 	}
158     }
159 }
160 
161 void 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 WINDOW *dupwin(WINDOW *win)
171 /* make an exact duplicate of the given window */
172 {
173 WINDOW *nwin;
174 size_t linesize;
175 int i;
176 
177 	T((T_CALLED("dupwin(%p)"), win));
178 
179 	if ((win==NULL) ||
180 	    ((nwin = newwin(win->_maxy + 1, win->_maxx + 1, win->_begy, 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