1 /* $OpenBSD: lib_pad.c,v 1.4 2001/01/22 18:01:42 millert Exp $ */ 2 3 /**************************************************************************** 4 * Copyright (c) 1998,2000 Free Software Foundation, Inc. * 5 * * 6 * Permission is hereby granted, free of charge, to any person obtaining a * 7 * copy of this software and associated documentation files (the * 8 * "Software"), to deal in the Software without restriction, including * 9 * without limitation the rights to use, copy, modify, merge, publish, * 10 * distribute, distribute with modifications, sublicense, and/or sell * 11 * copies of the Software, and to permit persons to whom the Software is * 12 * furnished to do so, subject to the following conditions: * 13 * * 14 * The above copyright notice and this permission notice shall be included * 15 * in all copies or substantial portions of the Software. * 16 * * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 24 * * 25 * Except as contained in this notice, the name(s) of the above copyright * 26 * holders shall not be used in advertising or otherwise to promote the * 27 * sale, use or other dealings in this Software without prior written * 28 * authorization. * 29 ****************************************************************************/ 30 31 /**************************************************************************** 32 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 33 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 34 ****************************************************************************/ 35 36 /* 37 * lib_pad.c 38 * newpad -- create a new pad 39 * pnoutrefresh -- refresh a pad, no update 40 * pechochar -- add a char to a pad and refresh 41 */ 42 43 #include <curses.priv.h> 44 45 MODULE_ID("$From: lib_pad.c,v 1.32 2000/12/10 02:43:27 tom Exp $") 46 47 NCURSES_EXPORT(WINDOW *) 48 newpad(int l, int c) 49 { 50 WINDOW *win; 51 chtype *ptr; 52 int i; 53 54 T((T_CALLED("newpad(%d, %d)"), l, c)); 55 56 if (l <= 0 || c <= 0) 57 returnWin(0); 58 59 if ((win = _nc_makenew(l, c, 0, 0, _ISPAD)) == NULL) 60 returnWin(0); 61 62 for (i = 0; i < l; i++) { 63 if_USE_SCROLL_HINTS(win->_line[i].oldindex = _NEWINDEX); 64 if ((win->_line[i].text = typeCalloc(chtype, ((size_t) c))) == 0) { 65 (void) _nc_freewin(win); 66 returnWin(0); 67 } 68 for (ptr = win->_line[i].text; ptr < win->_line[i].text + c;) 69 *ptr++ = ' '; 70 } 71 72 returnWin(win); 73 } 74 75 NCURSES_EXPORT(WINDOW *) 76 subpad 77 (WINDOW *orig, int l, int c, int begy, int begx) 78 { 79 WINDOW *win = (WINDOW *) 0; 80 81 T((T_CALLED("subpad(%d, %d)"), l, c)); 82 83 if (orig) { 84 if (!(orig->_flags & _ISPAD) 85 || ((win = derwin(orig, l, c, begy, begx)) == NULL)) 86 returnWin(0); 87 } 88 returnWin(win); 89 } 90 91 NCURSES_EXPORT(int) 92 prefresh 93 (WINDOW *win, int pminrow, int pmincol, 94 int sminrow, int smincol, int smaxrow, int smaxcol) 95 { 96 T((T_CALLED("prefresh()"))); 97 if (pnoutrefresh(win, pminrow, pmincol, sminrow, smincol, smaxrow, 98 smaxcol) != ERR 99 && doupdate() != ERR) { 100 returnCode(OK); 101 } 102 returnCode(ERR); 103 } 104 105 NCURSES_EXPORT(int) 106 pnoutrefresh 107 (WINDOW *win, int pminrow, int pmincol, 108 int sminrow, int smincol, int smaxrow, int smaxcol) 109 { 110 NCURSES_SIZE_T i, j; 111 NCURSES_SIZE_T m, n; 112 NCURSES_SIZE_T pmaxrow; 113 NCURSES_SIZE_T pmaxcol; 114 115 #if USE_SCROLL_HINTS 116 const int my_len = 2; /* parameterize the threshold for hardscroll */ 117 NCURSES_SIZE_T displaced; 118 bool wide; 119 #endif 120 121 T((T_CALLED("pnoutrefresh(%p, %d, %d, %d, %d, %d, %d)"), 122 win, pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol)); 123 124 if (win == 0) 125 returnCode(ERR); 126 127 if (!(win->_flags & _ISPAD)) 128 returnCode(ERR); 129 130 /* negative values are interpreted as zero */ 131 if (pminrow < 0) 132 pminrow = 0; 133 if (pmincol < 0) 134 pmincol = 0; 135 if (sminrow < 0) 136 sminrow = 0; 137 if (smincol < 0) 138 smincol = 0; 139 140 pmaxrow = pminrow + smaxrow - sminrow; 141 pmaxcol = pmincol + smaxcol - smincol; 142 143 T((" pminrow + smaxrow - sminrow %d, win->_maxy %d", pmaxrow, win->_maxy)); 144 T((" pmincol + smaxcol - smincol %d, win->_maxx %d", pmaxcol, win->_maxx)); 145 146 /* 147 * Trim the caller's screen size back to the actual limits. 148 */ 149 if (pmaxrow > win->_maxy) { 150 smaxrow -= (pmaxrow - win->_maxy); 151 pmaxrow = pminrow + smaxrow - sminrow; 152 } 153 if (pmaxcol > win->_maxx) { 154 smaxcol -= (pmaxcol - win->_maxx); 155 pmaxcol = pmincol + smaxcol - smincol; 156 } 157 158 if (smaxrow > screen_lines 159 || smaxcol > screen_columns 160 || sminrow > smaxrow 161 || smincol > smaxcol) 162 returnCode(ERR); 163 164 T(("pad being refreshed")); 165 166 #if USE_SCROLL_HINTS 167 if (win->_pad._pad_y >= 0) { 168 displaced = pminrow - win->_pad._pad_y 169 - (sminrow - win->_pad._pad_top); 170 T(("pad being shifted by %d line(s)", displaced)); 171 } else 172 displaced = 0; 173 #endif 174 175 /* 176 * For pure efficiency, we'd want to transfer scrolling information 177 * from the pad to newscr whenever the window is wide enough that 178 * its update will dominate the cost of the update for the horizontal 179 * band of newscr that it occupies. Unfortunately, this threshold 180 * tends to be complex to estimate, and in any case scrolling the 181 * whole band and rewriting the parts outside win's image would look 182 * really ugly. So. What we do is consider the pad "wide" if it 183 * either (a) occupies the whole width of newscr, or (b) occupies 184 * all but at most one column on either vertical edge of the screen 185 * (this caters to fussy people who put boxes around full-screen 186 * windows). Note that changing this formula will not break any code, 187 * merely change the costs of various update cases. 188 */ 189 #if USE_SCROLL_HINTS 190 wide = (smincol < my_len && smaxcol > (newscr->_maxx - my_len)); 191 #endif 192 193 for (i = pminrow, m = sminrow + win->_yoffset; 194 i <= pmaxrow && m <= newscr->_maxy; 195 i++, m++) { 196 register struct ldat *nline = &newscr->_line[m]; 197 register struct ldat *oline = &win->_line[i]; 198 199 for (j = pmincol, n = smincol; j <= pmaxcol; j++, n++) { 200 if (oline->text[j] != nline->text[n]) { 201 nline->text[n] = oline->text[j]; 202 CHANGED_CELL(nline, n); 203 } 204 } 205 206 #if USE_SCROLL_HINTS 207 if (wide) { 208 int nind = m + displaced; 209 if (oline->oldindex < 0 210 || nind < sminrow 211 || nind > smaxrow) { 212 nind = _NEWINDEX; 213 } else if (displaced) { 214 register struct ldat *pline = &curscr->_line[nind]; 215 for (j = 0; j <= my_len; j++) { 216 int k = newscr->_maxx - j; 217 if (pline->text[j] != nline->text[j] 218 || pline->text[k] != nline->text[k]) { 219 nind = _NEWINDEX; 220 break; 221 } 222 } 223 } 224 225 nline->oldindex = nind; 226 } 227 #endif /* USE_SCROLL_HINTS */ 228 oline->firstchar = oline->lastchar = _NOCHANGE; 229 if_USE_SCROLL_HINTS(oline->oldindex = i); 230 } 231 232 /* 233 * Clean up debris from scrolling or resizing the pad, so we do not 234 * accidentally pick up the index value during the next call to this 235 * procedure. The only rows that should have an index value are those 236 * that are displayed during this cycle. 237 */ 238 #if USE_SCROLL_HINTS 239 for (i = pminrow - 1; (i >= 0) && (win->_line[i].oldindex >= 0); i--) 240 win->_line[i].oldindex = _NEWINDEX; 241 for (i = pmaxrow + 1; (i <= win->_maxy) 242 && (win->_line[i].oldindex >= 0); i++) 243 win->_line[i].oldindex = _NEWINDEX; 244 #endif 245 246 win->_begx = smincol; 247 win->_begy = sminrow; 248 249 if (win->_clear) { 250 win->_clear = FALSE; 251 newscr->_clear = TRUE; 252 } 253 254 /* 255 * Use the pad's current position, if it will be visible. 256 * If not, don't do anything; it's not an error. 257 */ 258 if (win->_leaveok == FALSE 259 && win->_cury >= pminrow 260 && win->_curx >= pmincol 261 && win->_cury <= pmaxrow 262 && win->_curx <= pmaxcol) { 263 newscr->_cury = win->_cury - pminrow + win->_begy + win->_yoffset; 264 newscr->_curx = win->_curx - pmincol + win->_begx; 265 } 266 newscr->_leaveok = win->_leaveok; 267 win->_flags &= ~_HASMOVED; 268 269 /* 270 * Update our cache of the line-numbers that we displayed from the pad. 271 * We will use this on subsequent calls to this function to derive 272 * values to stuff into 'oldindex[]' -- for scrolling optimization. 273 */ 274 win->_pad._pad_y = pminrow; 275 win->_pad._pad_x = pmincol; 276 win->_pad._pad_top = sminrow; 277 win->_pad._pad_left = smincol; 278 win->_pad._pad_bottom = smaxrow; 279 win->_pad._pad_right = smaxcol; 280 281 returnCode(OK); 282 } 283 284 NCURSES_EXPORT(int) 285 pechochar(WINDOW *pad, const chtype ch) 286 { 287 T((T_CALLED("pechochar(%p, %s)"), pad, _tracechtype(ch))); 288 289 if (pad == 0) 290 returnCode(ERR); 291 292 if (!(pad->_flags & _ISPAD)) 293 returnCode(wechochar(pad, ch)); 294 295 waddch(pad, ch); 296 prefresh(pad, pad->_pad._pad_y, 297 pad->_pad._pad_x, 298 pad->_pad._pad_top, 299 pad->_pad._pad_left, 300 pad->_pad._pad_bottom, 301 pad->_pad._pad_right); 302 303 returnCode(OK); 304 } 305