ncurses 4.2
[ncurses.git] / ncurses / lib_window.c
1 /****************************************************************************
2  * Copyright (c) 1998 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.11 1998/02/11 12:13:53 tom Exp $")
43
44 void _nc_synchook(WINDOW *win)
45 /* hook to be called after each window change */
46 {
47         if (win->_immed) wrefresh(win);
48         if (win->_sync) wsyncup(win);
49 }
50
51 int mvderwin(WINDOW *win, int y, int x)
52 /* move a derived window */
53 {
54    WINDOW *orig;
55    int i;
56
57    T((T_CALLED("mvderwin(%p,%d,%d)"), win, y, x));
58
59    if (win && (orig = win->_parent))
60    {
61       if (win->_parx==x && win->_pary==y)
62         returnCode(OK);
63       if (x<0 || y<0)
64         returnCode(ERR);
65       if ( (x+getmaxx(win) > getmaxx(orig)) ||
66            (y+getmaxy(win) > getmaxy(orig)) )
67         returnCode(ERR);
68    }
69    else
70       returnCode(ERR);
71    wsyncup(win);
72    win->_parx = x;
73    win->_pary = y;
74    for(i=0;i<getmaxy(win);i++)
75      win->_line[i].text = &(orig->_line[y++].text[x]);
76    returnCode(OK);
77 }
78
79 int syncok(WINDOW *win, bool bf)
80 /* enable/disable automatic wsyncup() on each change to window */
81 {
82         T((T_CALLED("syncok(%p,%d)"), win, bf));
83
84         if (win) {
85                 win->_sync = bf;
86                 returnCode(OK);
87         } else
88                 returnCode(ERR);
89 }
90
91 void wsyncup(WINDOW *win)
92 /* mark changed every cell in win's ancestors that is changed in win */
93 /* Rewritten by J. Pfeifer, 1-Apr-96 (don't even think that...)      */
94 {
95   WINDOW        *wp;
96
97   if (win && win->_parent)
98     for (wp = win; wp->_parent; wp = wp->_parent)
99       {
100         int y;
101         WINDOW *pp = wp->_parent;
102
103         assert((wp->_pary <= pp->_maxy) &&
104                ((wp->_pary+wp->_maxy) <= pp->_maxy));
105
106         for (y = 0; y <= wp->_maxy; y++)
107           {
108             int left = wp->_line[y].firstchar;
109             if (left >= 0) /* line is touched */
110               {
111                 /* left & right character in parent window coordinates */
112                 int right = wp->_line[y].lastchar + wp->_parx;
113                 left += wp->_parx;
114
115                 if (pp->_line[wp->_pary + y].firstchar == _NOCHANGE)
116                   {
117                     pp->_line[wp->_pary + y].firstchar = left;
118                     pp->_line[wp->_pary + y].lastchar  = right;
119                   }
120                 else
121                   {
122                     if (left < pp->_line[wp->_pary + y].firstchar)
123                       pp->_line[wp->_pary + y].firstchar = left;
124                     if (pp->_line[wp->_pary + y].lastchar < right)
125                       pp->_line[wp->_pary + y].lastchar = right;
126                   }
127               }
128           }
129       }
130 }
131
132 void wsyncdown(WINDOW *win)
133 /* mark changed every cell in win that is changed in any of its ancestors */
134 /* Rewritten by J. Pfeifer, 1-Apr-96 (don't even think that...)           */
135 {
136   if (win && win->_parent)
137     {
138       WINDOW *pp = win->_parent;
139       int y;
140
141       /* This recursion guarantees, that the changes are propagated down-
142          wards from the root to our direct parent. */
143       wsyncdown(pp);
144
145       /* and now we only have to propagate the changes from our direct
146          parent, if there are any. */
147       assert((win->_pary <= pp->_maxy) &&
148              ((win->_pary + win->_maxy) <= pp->_maxy));
149
150       for (y = 0; y <= win->_maxy; y++)
151         {
152           if (pp->_line[win->_pary + y].firstchar >= 0) /* parent changed */
153             {
154               /* left and right character in child coordinates */
155               int left  = pp->_line[win->_pary + y].firstchar - win->_parx;
156               int right = pp->_line[win->_pary + y].lastchar  - win->_parx;
157               /* The change maybe outside the childs range */
158               if (left<0)
159                 left = 0;
160               if (right > win->_maxx)
161                 right = win->_maxx;
162               if (win->_line[y].firstchar == _NOCHANGE)
163                 {
164                   win->_line[y].firstchar = left;
165                   win->_line[y].lastchar  = right;
166                 }
167               else
168                 {
169                   if (left < win->_line[y].firstchar)
170                     win->_line[y].firstchar = left;
171                   if (win->_line[y].lastchar < right)
172                     win->_line[y].lastchar = right;
173                 }
174             }
175         }
176     }
177 }
178
179 void wcursyncup(WINDOW *win)
180 /* sync the cursor in all derived windows to its value in the base window */
181 {
182    WINDOW *wp;
183    for( wp = win; wp && wp->_parent; wp = wp->_parent ) {
184       wmove( wp->_parent, wp->_pary + wp->_cury, wp->_parx + wp->_curx );
185    }
186 }
187
188 WINDOW *dupwin(WINDOW *win)
189 /* make an exact duplicate of the given window */
190 {
191 WINDOW *nwin;
192 size_t linesize;
193 int i;
194
195         T((T_CALLED("dupwin(%p)"), win));
196
197         if ((win==NULL) ||
198             ((nwin = newwin(win->_maxy + 1, win->_maxx + 1, win->_begy, win->_begx)) == NULL))
199           returnWin(0);
200         
201         nwin->_curx        = win->_curx;
202         nwin->_cury        = win->_cury;
203         nwin->_maxy        = win->_maxy;
204         nwin->_maxx        = win->_maxx;
205         nwin->_begy        = win->_begy;
206         nwin->_begx        = win->_begx;
207         nwin->_yoffset     = win->_yoffset;
208
209         nwin->_flags       = win->_flags & ~_SUBWIN;
210         /* Due to the use of newwin(), the clone is not a subwindow.
211          * The text is really copied into the clone.
212          */
213
214         nwin->_attrs       = win->_attrs;
215         nwin->_bkgd        = win->_bkgd;
216
217         nwin->_clear       = win->_clear;
218         nwin->_scroll      = win->_scroll;
219         nwin->_leaveok     = win->_leaveok;
220         nwin->_use_keypad  = win->_use_keypad;
221         nwin->_delay       = win->_delay;
222         nwin->_immed       = win->_immed;
223         nwin->_sync        = win->_sync;
224
225         nwin->_parx        = 0;
226         nwin->_pary        = 0;
227         nwin->_parent      = (WINDOW*)0; 
228         /* See above: the clone isn't a subwindow! */
229
230         nwin->_regtop      = win->_regtop;
231         nwin->_regbottom   = win->_regbottom;
232
233         linesize = (win->_maxx + 1) * sizeof(chtype);
234         for (i = 0; i <= nwin->_maxy; i++) {
235                 memcpy(nwin->_line[i].text, win->_line[i].text, linesize);
236                 nwin->_line[i].firstchar  = win->_line[i].firstchar;
237                 nwin->_line[i].lastchar = win->_line[i].lastchar;
238         }
239
240         returnWin(nwin);
241 }