]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_pad.c
ncurses 5.7 - patch 20090321
[ncurses.git] / ncurses / base / lib_pad.c
1 /****************************************************************************
2  * Copyright (c) 1998-2006,2009 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  *     and: Thomas E. Dickey                        1996-on                 *
33  *     and: Juergen Pfeifer                         2009                    *
34  ****************************************************************************/
35
36 /*
37  * lib_pad.c
38  * newpad       -- create a new pad
39  * pnoutrefresh -- refresh a pad, no update
40  * pechochar    -- add a char to a pad and refresh
41  */
42
43 #include <curses.priv.h>
44
45 MODULE_ID("$Id: lib_pad.c,v 1.42 2009/02/15 00:38:48 tom Exp $")
46
47 NCURSES_EXPORT(WINDOW *)
48 NCURSES_SP_NAME(newpad) (NCURSES_SP_DCLx int l, int c)
49 {
50     WINDOW *win;
51     NCURSES_CH_T *ptr;
52     int i;
53
54     T((T_CALLED("newpad(%d, %d)"), l, c));
55
56     if (l <= 0 || c <= 0)
57         returnWin(0);
58
59     if ((win = _nc_makenew(l, c, 0, 0, _ISPAD)) == NULL)
60         returnWin(0);
61
62     for (i = 0; i < l; i++) {
63         if_USE_SCROLL_HINTS(win->_line[i].oldindex = _NEWINDEX);
64         if ((win->_line[i].text = typeCalloc(NCURSES_CH_T, ((size_t) c))) == 0) {
65             (void) _nc_freewin(win);
66             returnWin(0);
67         }
68         for (ptr = win->_line[i].text; ptr < win->_line[i].text + c; ptr++)
69             SetChar(*ptr, BLANK_TEXT, BLANK_ATTR);
70     }
71
72     returnWin(win);
73 }
74
75 #if NCURSES_SP_FUNCS
76 NCURSES_EXPORT(WINDOW *)
77 newpad(int l, int c)
78 {
79     return NCURSES_SP_NAME(newpad) (CURRENT_SCREEN, l, c);
80 }
81 #endif
82
83 NCURSES_EXPORT(WINDOW *)
84 subpad(WINDOW *orig, int l, int c, int begy, int begx)
85 {
86     WINDOW *win = (WINDOW *) 0;
87
88     T((T_CALLED("subpad(%d, %d)"), l, c));
89
90     if (orig) {
91         if (!(orig->_flags & _ISPAD)
92             || ((win = derwin(orig, l, c, begy, begx)) == NULL))
93             returnWin(0);
94     }
95     returnWin(win);
96 }
97
98 NCURSES_EXPORT(int)
99 prefresh(WINDOW *win,
100          int pminrow,
101          int pmincol,
102          int sminrow,
103          int smincol,
104          int smaxrow,
105          int smaxcol)
106 {
107     T((T_CALLED("prefresh()")));
108     if (pnoutrefresh(win, pminrow, pmincol, sminrow, smincol, smaxrow,
109                      smaxcol) != ERR
110         && doupdate() != ERR) {
111         returnCode(OK);
112     }
113     returnCode(ERR);
114 }
115
116 NCURSES_EXPORT(int)
117 pnoutrefresh(WINDOW *win,
118              int pminrow,
119              int pmincol,
120              int sminrow,
121              int smincol,
122              int smaxrow,
123              int smaxcol)
124 {
125     NCURSES_SIZE_T i, j;
126     NCURSES_SIZE_T m, n;
127     NCURSES_SIZE_T pmaxrow;
128     NCURSES_SIZE_T pmaxcol;
129
130 #if USE_SCROLL_HINTS
131     const int my_len = 2;       /* parameterize the threshold for hardscroll */
132     NCURSES_SIZE_T displaced;
133     bool wide;
134 #endif
135
136     T((T_CALLED("pnoutrefresh(%p, %d, %d, %d, %d, %d, %d)"),
137        win, pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol));
138
139     if (win == 0)
140         returnCode(ERR);
141
142     if (!(win->_flags & _ISPAD))
143         returnCode(ERR);
144
145     /* negative values are interpreted as zero */
146     if (pminrow < 0)
147         pminrow = 0;
148     if (pmincol < 0)
149         pmincol = 0;
150     if (sminrow < 0)
151         sminrow = 0;
152     if (smincol < 0)
153         smincol = 0;
154
155     pmaxrow = pminrow + smaxrow - sminrow;
156     pmaxcol = pmincol + smaxcol - smincol;
157
158     T((" pminrow + smaxrow - sminrow %ld, win->_maxy %ld",
159        (long) pmaxrow, (long) win->_maxy));
160     T((" pmincol + smaxcol - smincol %ld, win->_maxx %ld",
161        (long) pmaxcol, (long) win->_maxx));
162
163     /*
164      * Trim the caller's screen size back to the actual limits.
165      */
166     if (pmaxrow > win->_maxy) {
167         smaxrow -= (pmaxrow - win->_maxy);
168         pmaxrow = pminrow + smaxrow - sminrow;
169     }
170     if (pmaxcol > win->_maxx) {
171         smaxcol -= (pmaxcol - win->_maxx);
172         pmaxcol = pmincol + smaxcol - smincol;
173     }
174
175     if (smaxrow >= screen_lines
176         || smaxcol >= screen_columns
177         || sminrow > smaxrow
178         || smincol > smaxcol)
179         returnCode(ERR);
180
181     T(("pad being refreshed"));
182
183 #if USE_SCROLL_HINTS
184     if (win->_pad._pad_y >= 0) {
185         displaced = pminrow - win->_pad._pad_y
186             - (sminrow - win->_pad._pad_top);
187         T(("pad being shifted by %d line(s)", displaced));
188     } else
189         displaced = 0;
190 #endif
191
192     /*
193      * For pure efficiency, we'd want to transfer scrolling information
194      * from the pad to newscr whenever the window is wide enough that
195      * its update will dominate the cost of the update for the horizontal
196      * band of newscr that it occupies.  Unfortunately, this threshold
197      * tends to be complex to estimate, and in any case scrolling the
198      * whole band and rewriting the parts outside win's image would look
199      * really ugly.  So.  What we do is consider the pad "wide" if it
200      * either (a) occupies the whole width of newscr, or (b) occupies
201      * all but at most one column on either vertical edge of the screen
202      * (this caters to fussy people who put boxes around full-screen
203      * windows).  Note that changing this formula will not break any code,
204      * merely change the costs of various update cases.
205      */
206 #if USE_SCROLL_HINTS
207     wide = (smincol < my_len && smaxcol > (newscr->_maxx - my_len));
208 #endif
209
210     for (i = pminrow, m = sminrow + win->_yoffset;
211          i <= pmaxrow && m <= newscr->_maxy;
212          i++, m++) {
213         register struct ldat *nline = &newscr->_line[m];
214         register struct ldat *oline = &win->_line[i];
215         for (j = pmincol, n = smincol; j <= pmaxcol; j++, n++) {
216             NCURSES_CH_T ch = oline->text[j];
217 #if USE_WIDEC_SUPPORT
218             /*
219              * Special case for leftmost character of the displayed area.
220              * Only half of a double-width character may be visible.
221              */
222             if (j == pmincol
223                 && j > 0
224                 && isWidecExt(ch)) {
225                 SetChar(ch, L(' '), AttrOf(oline->text[j - 1]));
226             }
227 #endif
228             if (!CharEq(ch, nline->text[n])) {
229                 nline->text[n] = ch;
230                 CHANGED_CELL(nline, n);
231             }
232         }
233
234 #if USE_SCROLL_HINTS
235         if (wide) {
236             int nind = m + displaced;
237             if (oline->oldindex < 0
238                 || nind < sminrow
239                 || nind > smaxrow) {
240                 nind = _NEWINDEX;
241             } else if (displaced) {
242                 register struct ldat *pline = &curscr->_line[nind];
243                 for (j = 0; j <= my_len; j++) {
244                     int k = newscr->_maxx - j;
245                     if (pline->text[j] != nline->text[j]
246                         || pline->text[k] != nline->text[k]) {
247                         nind = _NEWINDEX;
248                         break;
249                     }
250                 }
251             }
252
253             nline->oldindex = nind;
254         }
255 #endif /* USE_SCROLL_HINTS */
256         oline->firstchar = oline->lastchar = _NOCHANGE;
257         if_USE_SCROLL_HINTS(oline->oldindex = i);
258     }
259
260     /*
261      * Clean up debris from scrolling or resizing the pad, so we do not
262      * accidentally pick up the index value during the next call to this
263      * procedure.  The only rows that should have an index value are those
264      * that are displayed during this cycle.
265      */
266 #if USE_SCROLL_HINTS
267     for (i = pminrow - 1; (i >= 0) && (win->_line[i].oldindex >= 0); i--)
268         win->_line[i].oldindex = _NEWINDEX;
269     for (i = pmaxrow + 1; (i <= win->_maxy)
270          && (win->_line[i].oldindex >= 0); i++)
271         win->_line[i].oldindex = _NEWINDEX;
272 #endif
273
274     win->_begx = smincol;
275     win->_begy = sminrow;
276
277     if (win->_clear) {
278         win->_clear = FALSE;
279         newscr->_clear = TRUE;
280     }
281
282     /*
283      * Use the pad's current position, if it will be visible.
284      * If not, don't do anything; it's not an error.
285      */
286     if (win->_leaveok == FALSE
287         && win->_cury >= pminrow
288         && win->_curx >= pmincol
289         && win->_cury <= pmaxrow
290         && win->_curx <= pmaxcol) {
291         newscr->_cury = win->_cury - pminrow + win->_begy + win->_yoffset;
292         newscr->_curx = win->_curx - pmincol + win->_begx;
293     }
294     newscr->_leaveok = win->_leaveok;
295     win->_flags &= ~_HASMOVED;
296
297     /*
298      * Update our cache of the line-numbers that we displayed from the pad.
299      * We will use this on subsequent calls to this function to derive
300      * values to stuff into 'oldindex[]' -- for scrolling optimization.
301      */
302     win->_pad._pad_y = pminrow;
303     win->_pad._pad_x = pmincol;
304     win->_pad._pad_top = sminrow;
305     win->_pad._pad_left = smincol;
306     win->_pad._pad_bottom = smaxrow;
307     win->_pad._pad_right = smaxcol;
308
309     returnCode(OK);
310 }
311
312 NCURSES_EXPORT(int)
313 pechochar(WINDOW *pad, const chtype ch)
314 {
315     T((T_CALLED("pechochar(%p, %s)"), pad, _tracechtype(ch)));
316
317     if (pad == 0)
318         returnCode(ERR);
319
320     if (!(pad->_flags & _ISPAD))
321         returnCode(wechochar(pad, ch));
322
323     waddch(pad, ch);
324     prefresh(pad, pad->_pad._pad_y,
325              pad->_pad._pad_x,
326              pad->_pad._pad_top,
327              pad->_pad._pad_left,
328              pad->_pad._pad_bottom,
329              pad->_pad._pad_right);
330
331     returnCode(OK);
332 }