ncurses 5.9 - patch 20110917
[ncurses.git] / ncurses / base / lib_window.c
1 /****************************************************************************
2  * Copyright (c) 1998-2009,2010 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.29 2010/12/19 01:47:22 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     int rc = ERR;
61
62     T((T_CALLED("mvderwin(%p,%d,%d)"), (void *) win, y, x));
63
64     if (win != 0
65         && (orig = win->_parent) != 0
66         && (x >= 0 && y >= 0)
67         && (x + getmaxx(win) <= getmaxx(orig))
68         && (y + getmaxy(win) <= getmaxy(orig))) {
69         wsyncup(win);
70         win->_parx = x;
71         win->_pary = y;
72         for (i = 0; i < getmaxy(win); i++)
73             win->_line[i].text = &(orig->_line[y++].text[x]);
74         rc = OK;
75     }
76     returnCode(rc);
77 }
78
79 NCURSES_EXPORT(int)
80 syncok(WINDOW *win, bool bf)
81 /* enable/disable automatic wsyncup() on each change to window */
82 {
83     T((T_CALLED("syncok(%p,%d)"), (void *) win, bf));
84
85     if (win) {
86         win->_sync = bf;
87         returnCode(OK);
88     } else
89         returnCode(ERR);
90 }
91
92 NCURSES_EXPORT(void)
93 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     T((T_CALLED("wsyncup(%p)"), (void *) win));
100     if (win && win->_parent) {
101         for (wp = win; wp->_parent; wp = wp->_parent) {
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                 int left = wp->_line[y].firstchar;
110                 if (left >= 0) {        /* line is touched */
111                     struct ldat *line = &(pp->_line[wp->_pary + y]);
112                     /* left & right character in parent window coordinates */
113                     int right = wp->_line[y].lastchar + wp->_parx;
114                     left += wp->_parx;
115
116                     CHANGED_RANGE(line, left, right);
117                 }
118             }
119         }
120     }
121     returnVoid;
122 }
123
124 NCURSES_EXPORT(void)
125 wsyncdown(WINDOW *win)
126 /* mark changed every cell in win that is changed in any of its ancestors */
127 /* Rewritten by J. Pfeifer, 1-Apr-96 (don't even think that...)           */
128 {
129     T((T_CALLED("wsyncdown(%p)"), (void *) win));
130
131     if (win && win->_parent) {
132         WINDOW *pp = win->_parent;
133         int y;
134
135         /* This recursion guarantees, that the changes are propagated down-
136            wards from the root to our direct parent. */
137         wsyncdown(pp);
138
139         /* and now we only have to propagate the changes from our direct
140            parent, if there are any. */
141         assert((win->_pary <= pp->_maxy) &&
142                ((win->_pary + win->_maxy) <= pp->_maxy));
143
144         for (y = 0; y <= win->_maxy; y++) {
145             if (pp->_line[win->_pary + y].firstchar >= 0) {     /* parent changed */
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 may be outside the child's 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     returnVoid;
160 }
161
162 NCURSES_EXPORT(void)
163 wcursyncup(WINDOW *win)
164 /* sync the cursor in all derived windows to its value in the base window */
165 {
166     WINDOW *wp;
167
168     T((T_CALLED("wcursyncup(%p)"), (void *) win));
169     for (wp = win; wp && wp->_parent; wp = wp->_parent) {
170         wmove(wp->_parent, wp->_pary + wp->_cury, wp->_parx + wp->_curx);
171     }
172     returnVoid;
173 }
174
175 NCURSES_EXPORT(WINDOW *)
176 dupwin(WINDOW *win)
177 /* make an exact duplicate of the given window */
178 {
179     WINDOW *nwin = 0;
180     size_t linesize;
181     int i;
182
183     T((T_CALLED("dupwin(%p)"), (void *) win));
184
185     if (win != 0) {
186 #if NCURSES_SP_FUNCS
187         SCREEN *sp = _nc_screen_of(win);
188 #endif
189         _nc_lock_global(curses);
190         if (win->_flags & _ISPAD) {
191             nwin = NCURSES_SP_NAME(newpad) (NCURSES_SP_ARGx
192                                             win->_maxy + 1,
193                                             win->_maxx + 1);
194         } else {
195             nwin = NCURSES_SP_NAME(newwin) (NCURSES_SP_ARGx
196                                             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 = (unsigned) (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 }