1 /****************************************************************************
2 * Copyright (c) 1998-2006,2009 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 * and: Thomas E. Dickey 1996-on *
33 * and: Juergen Pfeifer 2009 *
34 ****************************************************************************/
38 * newpad -- create a new pad
39 * pnoutrefresh -- refresh a pad, no update
40 * pechochar -- add a char to a pad and refresh
43 #include <curses.priv.h>
45 MODULE_ID("$Id: lib_pad.c,v 1.43 2009/04/18 18:25:37 tom Exp $")
47 NCURSES_EXPORT(WINDOW *)
48 NCURSES_SP_NAME(newpad) (NCURSES_SP_DCLx int l, int c)
54 T((T_CALLED("newpad(%d, %d)"), l, c));
59 if ((win = _nc_makenew(l, c, 0, 0, _ISPAD)) == NULL)
62 for (i = 0; i < l; i++) {
63 if_USE_SCROLL_HINTS(win->_line[i].oldindex = _NEWINDEX);
64 if ((win->_line[i].text = typeCalloc(NCURSES_CH_T, ((size_t) c))) == 0) {
65 (void) _nc_freewin(win);
68 for (ptr = win->_line[i].text; ptr < win->_line[i].text + c; ptr++)
69 SetChar(*ptr, BLANK_TEXT, BLANK_ATTR);
76 NCURSES_EXPORT(WINDOW *)
79 return NCURSES_SP_NAME(newpad) (CURRENT_SCREEN, l, c);
83 NCURSES_EXPORT(WINDOW *)
84 subpad(WINDOW *orig, int l, int c, int begy, int begx)
86 WINDOW *win = (WINDOW *) 0;
88 T((T_CALLED("subpad(%d, %d)"), l, c));
91 if (!(orig->_flags & _ISPAD)
92 || ((win = derwin(orig, l, c, begy, begx)) == NULL))
107 T((T_CALLED("prefresh()")));
108 if (pnoutrefresh(win, pminrow, pmincol, sminrow, smincol, smaxrow,
110 && doupdate() != ERR) {
117 pnoutrefresh(WINDOW *win,
127 NCURSES_SIZE_T pmaxrow;
128 NCURSES_SIZE_T pmaxcol;
131 const int my_len = 2; /* parameterize the threshold for hardscroll */
132 NCURSES_SIZE_T displaced;
136 T((T_CALLED("pnoutrefresh(%p, %d, %d, %d, %d, %d, %d)"),
137 win, pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol));
142 if (!(win->_flags & _ISPAD))
145 /* negative values are interpreted as zero */
155 pmaxrow = pminrow + smaxrow - sminrow;
156 pmaxcol = pmincol + smaxcol - smincol;
158 T((" pminrow + smaxrow - sminrow %ld, win->_maxy %ld",
159 (long) pmaxrow, (long) win->_maxy));
160 T((" pmincol + smaxcol - smincol %ld, win->_maxx %ld",
161 (long) pmaxcol, (long) win->_maxx));
164 * Trim the caller's screen size back to the actual limits.
166 if (pmaxrow > win->_maxy) {
167 smaxrow -= (pmaxrow - win->_maxy);
168 pmaxrow = pminrow + smaxrow - sminrow;
170 if (pmaxcol > win->_maxx) {
171 smaxcol -= (pmaxcol - win->_maxx);
172 pmaxcol = pmincol + smaxcol - smincol;
175 if (smaxrow >= screen_lines(CURRENT_SCREEN)
176 || smaxcol >= screen_columns(CURRENT_SCREEN)
178 || smincol > smaxcol)
181 T(("pad being refreshed"));
184 if (win->_pad._pad_y >= 0) {
185 displaced = pminrow - win->_pad._pad_y
186 - (sminrow - win->_pad._pad_top);
187 T(("pad being shifted by %d line(s)", displaced));
193 * For pure efficiency, we'd want to transfer scrolling information
194 * from the pad to newscr whenever the window is wide enough that
195 * its update will dominate the cost of the update for the horizontal
196 * band of newscr that it occupies. Unfortunately, this threshold
197 * tends to be complex to estimate, and in any case scrolling the
198 * whole band and rewriting the parts outside win's image would look
199 * really ugly. So. What we do is consider the pad "wide" if it
200 * either (a) occupies the whole width of newscr, or (b) occupies
201 * all but at most one column on either vertical edge of the screen
202 * (this caters to fussy people who put boxes around full-screen
203 * windows). Note that changing this formula will not break any code,
204 * merely change the costs of various update cases.
207 wide = (smincol < my_len && smaxcol > (newscr->_maxx - my_len));
210 for (i = pminrow, m = sminrow + win->_yoffset;
211 i <= pmaxrow && m <= newscr->_maxy;
213 register struct ldat *nline = &newscr->_line[m];
214 register struct ldat *oline = &win->_line[i];
215 for (j = pmincol, n = smincol; j <= pmaxcol; j++, n++) {
216 NCURSES_CH_T ch = oline->text[j];
217 #if USE_WIDEC_SUPPORT
219 * Special case for leftmost character of the displayed area.
220 * Only half of a double-width character may be visible.
225 SetChar(ch, L(' '), AttrOf(oline->text[j - 1]));
228 if (!CharEq(ch, nline->text[n])) {
230 CHANGED_CELL(nline, n);
236 int nind = m + displaced;
237 if (oline->oldindex < 0
241 } else if (displaced) {
242 register struct ldat *pline = &curscr->_line[nind];
243 for (j = 0; j <= my_len; j++) {
244 int k = newscr->_maxx - j;
245 if (pline->text[j] != nline->text[j]
246 || pline->text[k] != nline->text[k]) {
253 nline->oldindex = nind;
255 #endif /* USE_SCROLL_HINTS */
256 oline->firstchar = oline->lastchar = _NOCHANGE;
257 if_USE_SCROLL_HINTS(oline->oldindex = i);
261 * Clean up debris from scrolling or resizing the pad, so we do not
262 * accidentally pick up the index value during the next call to this
263 * procedure. The only rows that should have an index value are those
264 * that are displayed during this cycle.
267 for (i = pminrow - 1; (i >= 0) && (win->_line[i].oldindex >= 0); i--)
268 win->_line[i].oldindex = _NEWINDEX;
269 for (i = pmaxrow + 1; (i <= win->_maxy)
270 && (win->_line[i].oldindex >= 0); i++)
271 win->_line[i].oldindex = _NEWINDEX;
274 win->_begx = smincol;
275 win->_begy = sminrow;
279 newscr->_clear = TRUE;
283 * Use the pad's current position, if it will be visible.
284 * If not, don't do anything; it's not an error.
286 if (win->_leaveok == FALSE
287 && win->_cury >= pminrow
288 && win->_curx >= pmincol
289 && win->_cury <= pmaxrow
290 && win->_curx <= pmaxcol) {
291 newscr->_cury = win->_cury - pminrow + win->_begy + win->_yoffset;
292 newscr->_curx = win->_curx - pmincol + win->_begx;
294 newscr->_leaveok = win->_leaveok;
295 win->_flags &= ~_HASMOVED;
298 * Update our cache of the line-numbers that we displayed from the pad.
299 * We will use this on subsequent calls to this function to derive
300 * values to stuff into 'oldindex[]' -- for scrolling optimization.
302 win->_pad._pad_y = pminrow;
303 win->_pad._pad_x = pmincol;
304 win->_pad._pad_top = sminrow;
305 win->_pad._pad_left = smincol;
306 win->_pad._pad_bottom = smaxrow;
307 win->_pad._pad_right = smaxcol;
313 pechochar(WINDOW *pad, const chtype ch)
315 T((T_CALLED("pechochar(%p, %s)"), pad, _tracechtype(ch)));
320 if (!(pad->_flags & _ISPAD))
321 returnCode(wechochar(pad, ch));
324 prefresh(pad, pad->_pad._pad_y,
328 pad->_pad._pad_bottom,
329 pad->_pad._pad_right);