1 /****************************************************************************
2 * Copyright (c) 1998-2002,2003 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.39 2004/10/23 18:48:09 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 *)
75 (WINDOW *orig, int l, int c, int begy, int begx)
77 WINDOW *win = (WINDOW *) 0;
79 T((T_CALLED("subpad(%d, %d)"), l, c));
82 if (!(orig->_flags & _ISPAD)
83 || ((win = derwin(orig, l, c, begy, begx)) == NULL))
91 (WINDOW *win, int pminrow, int pmincol,
92 int sminrow, int smincol, int smaxrow, int smaxcol)
94 T((T_CALLED("prefresh()")));
95 if (pnoutrefresh(win, pminrow, pmincol, sminrow, smincol, smaxrow,
97 && doupdate() != ERR) {
105 (WINDOW *win, int pminrow, int pmincol,
106 int sminrow, int smincol, int smaxrow, int smaxcol)
110 NCURSES_SIZE_T pmaxrow;
111 NCURSES_SIZE_T pmaxcol;
114 const int my_len = 2; /* parameterize the threshold for hardscroll */
115 NCURSES_SIZE_T displaced;
119 T((T_CALLED("pnoutrefresh(%p, %d, %d, %d, %d, %d, %d)"),
120 win, pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol));
125 if (!(win->_flags & _ISPAD))
128 /* negative values are interpreted as zero */
138 pmaxrow = pminrow + smaxrow - sminrow;
139 pmaxcol = pmincol + smaxcol - smincol;
141 T((" pminrow + smaxrow - sminrow %d, win->_maxy %d", pmaxrow, win->_maxy));
142 T((" pmincol + smaxcol - smincol %d, win->_maxx %d", pmaxcol, win->_maxx));
145 * Trim the caller's screen size back to the actual limits.
147 if (pmaxrow > win->_maxy) {
148 smaxrow -= (pmaxrow - win->_maxy);
149 pmaxrow = pminrow + smaxrow - sminrow;
151 if (pmaxcol > win->_maxx) {
152 smaxcol -= (pmaxcol - win->_maxx);
153 pmaxcol = pmincol + smaxcol - smincol;
156 if (smaxrow >= screen_lines
157 || smaxcol >= screen_columns
159 || smincol > smaxcol)
162 T(("pad being refreshed"));
165 if (win->_pad._pad_y >= 0) {
166 displaced = pminrow - win->_pad._pad_y
167 - (sminrow - win->_pad._pad_top);
168 T(("pad being shifted by %d line(s)", displaced));
174 * For pure efficiency, we'd want to transfer scrolling information
175 * from the pad to newscr whenever the window is wide enough that
176 * its update will dominate the cost of the update for the horizontal
177 * band of newscr that it occupies. Unfortunately, this threshold
178 * tends to be complex to estimate, and in any case scrolling the
179 * whole band and rewriting the parts outside win's image would look
180 * really ugly. So. What we do is consider the pad "wide" if it
181 * either (a) occupies the whole width of newscr, or (b) occupies
182 * all but at most one column on either vertical edge of the screen
183 * (this caters to fussy people who put boxes around full-screen
184 * windows). Note that changing this formula will not break any code,
185 * merely change the costs of various update cases.
188 wide = (smincol < my_len && smaxcol > (newscr->_maxx - my_len));
191 for (i = pminrow, m = sminrow + win->_yoffset;
192 i <= pmaxrow && m <= newscr->_maxy;
194 register struct ldat *nline = &newscr->_line[m];
195 register struct ldat *oline = &win->_line[i];
196 for (j = pmincol, n = smincol; j <= pmaxcol; j++, n++) {
197 NCURSES_CH_T ch = oline->text[j];
198 #if USE_WIDEC_SUPPORT
200 * Special case for leftmost character of the displayed area.
201 * Only half of a double-width character may be visible.
206 SetChar(ch, L(' '), AttrOf(oline->text[j - 1]));
209 if (!CharEq(ch, nline->text[n])) {
211 CHANGED_CELL(nline, n);
217 int nind = m + displaced;
218 if (oline->oldindex < 0
222 } else if (displaced) {
223 register struct ldat *pline = &curscr->_line[nind];
224 for (j = 0; j <= my_len; j++) {
225 int k = newscr->_maxx - j;
226 if (pline->text[j] != nline->text[j]
227 || pline->text[k] != nline->text[k]) {
234 nline->oldindex = nind;
236 #endif /* USE_SCROLL_HINTS */
237 oline->firstchar = oline->lastchar = _NOCHANGE;
238 if_USE_SCROLL_HINTS(oline->oldindex = i);
242 * Clean up debris from scrolling or resizing the pad, so we do not
243 * accidentally pick up the index value during the next call to this
244 * procedure. The only rows that should have an index value are those
245 * that are displayed during this cycle.
248 for (i = pminrow - 1; (i >= 0) && (win->_line[i].oldindex >= 0); i--)
249 win->_line[i].oldindex = _NEWINDEX;
250 for (i = pmaxrow + 1; (i <= win->_maxy)
251 && (win->_line[i].oldindex >= 0); i++)
252 win->_line[i].oldindex = _NEWINDEX;
255 win->_begx = smincol;
256 win->_begy = sminrow;
260 newscr->_clear = TRUE;
264 * Use the pad's current position, if it will be visible.
265 * If not, don't do anything; it's not an error.
267 if (win->_leaveok == FALSE
268 && win->_cury >= pminrow
269 && win->_curx >= pmincol
270 && win->_cury <= pmaxrow
271 && win->_curx <= pmaxcol) {
272 newscr->_cury = win->_cury - pminrow + win->_begy + win->_yoffset;
273 newscr->_curx = win->_curx - pmincol + win->_begx;
275 newscr->_leaveok = win->_leaveok;
276 win->_flags &= ~_HASMOVED;
279 * Update our cache of the line-numbers that we displayed from the pad.
280 * We will use this on subsequent calls to this function to derive
281 * values to stuff into 'oldindex[]' -- for scrolling optimization.
283 win->_pad._pad_y = pminrow;
284 win->_pad._pad_x = pmincol;
285 win->_pad._pad_top = sminrow;
286 win->_pad._pad_left = smincol;
287 win->_pad._pad_bottom = smaxrow;
288 win->_pad._pad_right = smaxcol;
294 pechochar(WINDOW *pad, const chtype ch)
296 T((T_CALLED("pechochar(%p, %s)"), pad, _tracechtype(ch)));
301 if (!(pad->_flags & _ISPAD))
302 returnCode(wechochar(pad, ch));
305 prefresh(pad, pad->_pad._pad_y,
309 pad->_pad._pad_bottom,
310 pad->_pad._pad_right);