1 /****************************************************************************
2 * Copyright (c) 1998-2004,2006 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.41 2006/10/14 20:47:13 tom Exp $")
45 NCURSES_EXPORT(WINDOW *)
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(NCURSES_CH_T, ((size_t) c))) == 0) {
63 (void) _nc_freewin(win);
66 for (ptr = win->_line[i].text; ptr < win->_line[i].text + c; ptr++)
67 SetChar(*ptr, BLANK_TEXT, BLANK_ATTR);
73 NCURSES_EXPORT(WINDOW *)
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))
97 T((T_CALLED("prefresh()")));
98 if (pnoutrefresh(win, pminrow, pmincol, sminrow, smincol, smaxrow,
100 && doupdate() != ERR) {
107 pnoutrefresh(WINDOW *win,
117 NCURSES_SIZE_T pmaxrow;
118 NCURSES_SIZE_T pmaxcol;
121 const int my_len = 2; /* parameterize the threshold for hardscroll */
122 NCURSES_SIZE_T displaced;
126 T((T_CALLED("pnoutrefresh(%p, %d, %d, %d, %d, %d, %d)"),
127 win, pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol));
132 if (!(win->_flags & _ISPAD))
135 /* negative values are interpreted as zero */
145 pmaxrow = pminrow + smaxrow - sminrow;
146 pmaxcol = pmincol + smaxcol - smincol;
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));
154 * Trim the caller's screen size back to the actual limits.
156 if (pmaxrow > win->_maxy) {
157 smaxrow -= (pmaxrow - win->_maxy);
158 pmaxrow = pminrow + smaxrow - sminrow;
160 if (pmaxcol > win->_maxx) {
161 smaxcol -= (pmaxcol - win->_maxx);
162 pmaxcol = pmincol + smaxcol - smincol;
165 if (smaxrow >= screen_lines
166 || smaxcol >= screen_columns
168 || smincol > smaxcol)
171 T(("pad being refreshed"));
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));
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.
197 wide = (smincol < my_len && smaxcol > (newscr->_maxx - my_len));
200 for (i = pminrow, m = sminrow + win->_yoffset;
201 i <= pmaxrow && m <= newscr->_maxy;
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
209 * Special case for leftmost character of the displayed area.
210 * Only half of a double-width character may be visible.
215 SetChar(ch, L(' '), AttrOf(oline->text[j - 1]));
218 if (!CharEq(ch, nline->text[n])) {
220 CHANGED_CELL(nline, n);
226 int nind = m + displaced;
227 if (oline->oldindex < 0
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]) {
243 nline->oldindex = nind;
245 #endif /* USE_SCROLL_HINTS */
246 oline->firstchar = oline->lastchar = _NOCHANGE;
247 if_USE_SCROLL_HINTS(oline->oldindex = i);
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.
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;
264 win->_begx = smincol;
265 win->_begy = sminrow;
269 newscr->_clear = TRUE;
273 * Use the pad's current position, if it will be visible.
274 * If not, don't do anything; it's not an error.
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;
284 newscr->_leaveok = win->_leaveok;
285 win->_flags &= ~_HASMOVED;
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.
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;
303 pechochar(WINDOW *pad, const chtype ch)
305 T((T_CALLED("pechochar(%p, %s)"), pad, _tracechtype(ch)));
310 if (!(pad->_flags & _ISPAD))
311 returnCode(wechochar(pad, ch));
314 prefresh(pad, pad->_pad._pad_y,
318 pad->_pad._pad_bottom,
319 pad->_pad._pad_right);