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