1 /****************************************************************************
2 * Copyright (c) 1998 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
37 * newpad -- create a new pad
38 * pnoutrefresh -- refresh a pad, no update
39 * pechochar -- add a char to a pad and refresh
42 #include <curses.priv.h>
44 MODULE_ID("$Id: lib_pad.c,v 1.25 1998/02/11 12:13:55 tom Exp $")
46 WINDOW *newpad(int l, int c)
52 T((T_CALLED("newpad(%d, %d)"), l, c));
57 if ((win = _nc_makenew(l,c,0,0,_ISPAD)) == NULL)
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) {
66 for (ptr = win->_line[i].text; ptr < win->_line[i].text + c; )
73 WINDOW *subpad(WINDOW *orig, int l, int c, int begy, int begx)
75 WINDOW *win = (WINDOW *)0;
77 T((T_CALLED("subpad(%d, %d)"), l, c));
80 if (!(orig->_flags & _ISPAD) || ((win = derwin(orig, l, c, begy, begx)) == NULL))
86 int prefresh(WINDOW *win, int pminrow, int pmincol,
87 int sminrow, int smincol, int smaxrow, int smaxcol)
89 T((T_CALLED("prefresh()")));
90 if (pnoutrefresh(win, pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol) != ERR
91 && doupdate() != ERR) {
97 int pnoutrefresh(WINDOW *win, int pminrow, int pmincol,
98 int sminrow, int smincol, int smaxrow, int smaxcol)
100 const int my_len = 2; /* parameterize the threshold for hardscroll */
108 T((T_CALLED("pnoutrefresh(%p, %d, %d, %d, %d, %d, %d)"),
109 win, pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol));
114 if (!(win->_flags & _ISPAD))
117 /* negative values are interpreted as zero */
118 if (pminrow < 0) pminrow = 0;
119 if (pmincol < 0) pmincol = 0;
120 if (sminrow < 0) sminrow = 0;
121 if (smincol < 0) smincol = 0;
123 pmaxrow = pminrow + smaxrow - sminrow;
124 pmaxcol = pmincol + smaxcol - smincol;
126 T((" pminrow + smaxrow - sminrow %d, win->_maxy %d", pmaxrow, win->_maxy));
127 T((" pmincol + smaxcol - smincol %d, win->_maxx %d", pmaxcol, win->_maxx));
130 * Trim the caller's screen size back to the actual limits.
132 if (pmaxrow > win->_maxy) {
133 smaxrow -= (pmaxrow - win->_maxy);
134 pmaxrow = pminrow + smaxrow - sminrow;
136 if (pmaxcol > win->_maxx) {
137 smaxcol -= (pmaxcol - win->_maxx);
138 pmaxcol = pmincol + smaxcol - smincol;
141 if (smaxrow > screen_lines
142 || smaxcol > screen_columns
144 || smincol > smaxcol)
147 T(("pad being refreshed"));
149 if (win->_pad._pad_y >= 0) {
150 displaced = pminrow - win->_pad._pad_y
151 -(sminrow - win->_pad._pad_top);
152 T(("pad being shifted by %d line(s)", displaced));
157 * For pure efficiency, we'd want to transfer scrolling information
158 * from the pad to newscr whenever the window is wide enough that
159 * its update will dominate the cost of the update for the horizontal
160 * band of newscr that it occupies. Unfortunately, this threshold
161 * tends to be complex to estimate, and in any case scrolling the
162 * whole band and rewriting the parts outside win's image would look
163 * really ugly. So. What we do is consider the pad "wide" if it
164 * either (a) occupies the whole width of newscr, or (b) occupies
165 * all but at most one column on either vertical edge of the screen
166 * (this caters to fussy people who put boxes around full-screen
167 * windows). Note that changing this formula will not break any code,
168 * merely change the costs of various update cases.
170 wide = (smincol < my_len && smaxcol > (newscr->_maxx - my_len));
172 for (i = pminrow, m = sminrow + win->_yoffset;
173 i <= pmaxrow && m <= newscr->_maxy;
175 register struct ldat *nline = &newscr->_line[m];
176 register struct ldat *oline = &win->_line[i];
178 for (j = pmincol, n = smincol; j <= pmaxcol; j++, n++) {
179 if (oline->text[j] != nline->text[n]) {
180 nline->text[n] = oline->text[j];
182 if (nline->firstchar == _NOCHANGE)
183 nline->firstchar = nline->lastchar = n;
184 else if (n < nline->firstchar)
185 nline->firstchar = n;
186 else if (n > nline->lastchar)
193 int nind = m + displaced;
194 if (oline->oldindex < 0
198 } else if (displaced) {
199 register struct ldat *pline = &curscr->_line[nind];
200 for (j = 0; j <= my_len; j++) {
201 int k = newscr->_maxx - j;
202 if (pline->text[j] != nline->text[j]
203 || pline->text[k] != nline->text[k]) {
210 nline->oldindex = nind;
212 #endif /* USE_SCROLL_HINTS */
213 oline->firstchar = oline->lastchar = _NOCHANGE;
214 if_USE_SCROLL_HINTS(oline->oldindex = i);
218 * Clean up debris from scrolling or resizing the pad, so we do not
219 * accidentally pick up the index value during the next call to this
220 * procedure. The only rows that should have an index value are those
221 * that are displayed during this cycle.
224 for (i = pminrow-1; (i >= 0) && (win->_line[i].oldindex >= 0); i--)
225 win->_line[i].oldindex = _NEWINDEX;
226 for (i = pmaxrow+1; (i <= win->_maxy) && (win->_line[i].oldindex >= 0); i++)
227 win->_line[i].oldindex = _NEWINDEX;
230 win->_begx = smincol;
231 win->_begy = sminrow;
235 newscr->_clear = TRUE;
239 * Use the pad's current position, if it will be visible.
240 * If not, don't do anything; it's not an error.
242 if (win->_leaveok == FALSE
243 && win->_cury >= pminrow
244 && win->_curx >= pmincol
245 && win->_cury <= pmaxrow
246 && win->_curx <= pmaxcol) {
247 newscr->_cury = win->_cury - pminrow + win->_begy + win->_yoffset;
248 newscr->_curx = win->_curx - pmincol + win->_begx;
250 win->_flags &= ~_HASMOVED;
253 * Update our cache of the line-numbers that we displayed from the pad.
254 * We will use this on subsequent calls to this function to derive
255 * values to stuff into 'oldindex[]' -- for scrolling optimization.
257 win->_pad._pad_y = pminrow;
258 win->_pad._pad_x = pmincol;
259 win->_pad._pad_top = sminrow;
260 win->_pad._pad_left = smincol;
261 win->_pad._pad_bottom = smaxrow;
262 win->_pad._pad_right = smaxcol;
267 int pechochar(WINDOW *pad, const chtype ch)
269 T((T_CALLED("pechochar(%p, %s)"), pad, _tracechtype(ch)));
274 if (!(pad->_flags & _ISPAD))
275 returnCode(wechochar(pad,ch));
278 prefresh(pad, pad->_pad._pad_y,
282 pad->_pad._pad_bottom,
283 pad->_pad._pad_right);