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