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