1 /****************************************************************************
2 * Copyright (c) 1998,2000 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 ****************************************************************************/
36 * newpad -- create a new pad
37 * pnoutrefresh -- refresh a pad, no update
38 * pechochar -- add a char to a pad and refresh
41 #include <curses.priv.h>
43 MODULE_ID("$Id: lib_pad.c,v 1.29 2000/04/29 21:19:44 tom Exp $")
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;)
74 subpad(WINDOW *orig, int l, int c, int begy, int begx)
76 WINDOW *win = (WINDOW *) 0;
78 T((T_CALLED("subpad(%d, %d)"), l, c));
81 if (!(orig->_flags & _ISPAD)
82 || ((win = derwin(orig, l, c, begy, begx)) == NULL))
89 prefresh(WINDOW *win, int pminrow, int pmincol,
90 int sminrow, int smincol, int smaxrow, int smaxcol)
92 T((T_CALLED("prefresh()")));
93 if (pnoutrefresh(win, pminrow, pmincol, sminrow, smincol, smaxrow,
95 && doupdate() != ERR) {
102 pnoutrefresh(WINDOW *win, int pminrow, int pmincol,
103 int sminrow, int smincol, int smaxrow, int smaxcol)
105 const int my_len = 2; /* parameterize the threshold for hardscroll */
108 NCURSES_SIZE_T pmaxrow;
109 NCURSES_SIZE_T pmaxcol;
110 NCURSES_SIZE_T displaced;
113 T((T_CALLED("pnoutrefresh(%p, %d, %d, %d, %d, %d, %d)"),
114 win, pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol));
119 if (!(win->_flags & _ISPAD))
122 /* negative values are interpreted as zero */
132 pmaxrow = pminrow + smaxrow - sminrow;
133 pmaxcol = pmincol + smaxcol - smincol;
135 T((" pminrow + smaxrow - sminrow %d, win->_maxy %d", pmaxrow, win->_maxy));
136 T((" pmincol + smaxcol - smincol %d, win->_maxx %d", pmaxcol, win->_maxx));
139 * Trim the caller's screen size back to the actual limits.
141 if (pmaxrow > win->_maxy) {
142 smaxrow -= (pmaxrow - win->_maxy);
143 pmaxrow = pminrow + smaxrow - sminrow;
145 if (pmaxcol > win->_maxx) {
146 smaxcol -= (pmaxcol - win->_maxx);
147 pmaxcol = pmincol + smaxcol - smincol;
150 if (smaxrow > screen_lines
151 || smaxcol > screen_columns
153 || smincol > smaxcol)
156 T(("pad being refreshed"));
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));
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.
179 wide = (smincol < my_len && smaxcol > (newscr->_maxx - my_len));
181 for (i = pminrow, m = sminrow + win->_yoffset;
182 i <= pmaxrow && m <= newscr->_maxy;
184 register struct ldat *nline = &newscr->_line[m];
185 register struct ldat *oline = &win->_line[i];
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);
196 int nind = m + displaced;
197 if (oline->oldindex < 0
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]) {
213 nline->oldindex = nind;
215 #endif /* USE_SCROLL_HINTS */
216 oline->firstchar = oline->lastchar = _NOCHANGE;
217 if_USE_SCROLL_HINTS(oline->oldindex = i);
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.
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;
234 win->_begx = smincol;
235 win->_begy = sminrow;
239 newscr->_clear = TRUE;
243 * Use the pad's current position, if it will be visible.
244 * If not, don't do anything; it's not an error.
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;
254 newscr->_leaveok = win->_leaveok;
255 win->_flags &= ~_HASMOVED;
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.
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;
273 pechochar(WINDOW *pad, const chtype ch)
275 T((T_CALLED("pechochar(%p, %s)"), pad, _tracechtype(ch)));
280 if (!(pad->_flags & _ISPAD))
281 returnCode(wechochar(pad, ch));
284 prefresh(pad, pad->_pad._pad_y,
288 pad->_pad._pad_bottom,
289 pad->_pad._pad_right);