xref: /openbsd-src/lib/libcurses/base/lib_pad.c (revision 1fe33145a0a9b1310a0413297a8deb871918b27f)
1 /*	$OpenBSD: lib_pad.c,v 1.3 2000/06/19 03:53:44 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.29 2000/04/29 21:19:44 tom Exp $")
46 
47 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 	    _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 WINDOW *
76 subpad(WINDOW *orig, int l, int c, int begy, int begx)
77 {
78     WINDOW *win = (WINDOW *) 0;
79 
80     T((T_CALLED("subpad(%d, %d)"), l, c));
81 
82     if (orig) {
83 	if (!(orig->_flags & _ISPAD)
84 	    || ((win = derwin(orig, l, c, begy, begx)) == NULL))
85 	    returnWin(0);
86     }
87     returnWin(win);
88 }
89 
90 int
91 prefresh(WINDOW *win, int pminrow, int pmincol,
92     int sminrow, int smincol, int smaxrow, int smaxcol)
93 {
94     T((T_CALLED("prefresh()")));
95     if (pnoutrefresh(win, pminrow, pmincol, sminrow, smincol, smaxrow,
96 	    smaxcol) != ERR
97 	&& doupdate() != ERR) {
98 	returnCode(OK);
99     }
100     returnCode(ERR);
101 }
102 
103 int
104 pnoutrefresh(WINDOW *win, int pminrow, int pmincol,
105     int sminrow, int smincol, int smaxrow, int smaxcol)
106 {
107     const int my_len = 2;	/* parameterize the threshold for hardscroll */
108     NCURSES_SIZE_T i, j;
109     NCURSES_SIZE_T m, n;
110     NCURSES_SIZE_T pmaxrow;
111     NCURSES_SIZE_T pmaxcol;
112     NCURSES_SIZE_T displaced;
113     bool wide;
114 
115     T((T_CALLED("pnoutrefresh(%p, %d, %d, %d, %d, %d, %d)"),
116 	    win, pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol));
117 
118     if (win == 0)
119 	returnCode(ERR);
120 
121     if (!(win->_flags & _ISPAD))
122 	returnCode(ERR);
123 
124     /* negative values are interpreted as zero */
125     if (pminrow < 0)
126 	pminrow = 0;
127     if (pmincol < 0)
128 	pmincol = 0;
129     if (sminrow < 0)
130 	sminrow = 0;
131     if (smincol < 0)
132 	smincol = 0;
133 
134     pmaxrow = pminrow + smaxrow - sminrow;
135     pmaxcol = pmincol + smaxcol - smincol;
136 
137     T((" pminrow + smaxrow - sminrow %d, win->_maxy %d", pmaxrow, win->_maxy));
138     T((" pmincol + smaxcol - smincol %d, win->_maxx %d", pmaxcol, win->_maxx));
139 
140     /*
141      * Trim the caller's screen size back to the actual limits.
142      */
143     if (pmaxrow > win->_maxy) {
144 	smaxrow -= (pmaxrow - win->_maxy);
145 	pmaxrow = pminrow + smaxrow - sminrow;
146     }
147     if (pmaxcol > win->_maxx) {
148 	smaxcol -= (pmaxcol - win->_maxx);
149 	pmaxcol = pmincol + smaxcol - smincol;
150     }
151 
152     if (smaxrow > screen_lines
153 	|| smaxcol > screen_columns
154 	|| sminrow > smaxrow
155 	|| smincol > smaxcol)
156 	returnCode(ERR);
157 
158     T(("pad being refreshed"));
159 
160     if (win->_pad._pad_y >= 0) {
161 	displaced = pminrow - win->_pad._pad_y
162 	    - (sminrow - win->_pad._pad_top);
163 	T(("pad being shifted by %d line(s)", displaced));
164     } else
165 	displaced = 0;
166 
167     /*
168      * For pure efficiency, we'd want to transfer scrolling information
169      * from the pad to newscr whenever the window is wide enough that
170      * its update will dominate the cost of the update for the horizontal
171      * band of newscr that it occupies.  Unfortunately, this threshold
172      * tends to be complex to estimate, and in any case scrolling the
173      * whole band and rewriting the parts outside win's image would look
174      * really ugly.  So.  What we do is consider the pad "wide" if it
175      * either (a) occupies the whole width of newscr, or (b) occupies
176      * all but at most one column on either vertical edge of the screen
177      * (this caters to fussy people who put boxes around full-screen
178      * windows).  Note that changing this formula will not break any code,
179      * merely change the costs of various update cases.
180      */
181     wide = (smincol < my_len && smaxcol > (newscr->_maxx - my_len));
182 
183     for (i = pminrow, m = sminrow + win->_yoffset;
184 	i <= pmaxrow && m <= newscr->_maxy;
185 	i++, m++) {
186 	register struct ldat *nline = &newscr->_line[m];
187 	register struct ldat *oline = &win->_line[i];
188 
189 	for (j = pmincol, n = smincol; j <= pmaxcol; j++, n++) {
190 	    if (oline->text[j] != nline->text[n]) {
191 		nline->text[n] = oline->text[j];
192 		CHANGED_CELL(nline, n);
193 	    }
194 	}
195 
196 #if USE_SCROLL_HINTS
197 	if (wide) {
198 	    int nind = m + displaced;
199 	    if (oline->oldindex < 0
200 		|| nind < sminrow
201 		|| nind > smaxrow) {
202 		nind = _NEWINDEX;
203 	    } else if (displaced) {
204 		register struct ldat *pline = &curscr->_line[nind];
205 		for (j = 0; j <= my_len; j++) {
206 		    int k = newscr->_maxx - j;
207 		    if (pline->text[j] != nline->text[j]
208 			|| pline->text[k] != nline->text[k]) {
209 			nind = _NEWINDEX;
210 			break;
211 		    }
212 		}
213 	    }
214 
215 	    nline->oldindex = nind;
216 	}
217 #endif /* USE_SCROLL_HINTS */
218 	oline->firstchar = oline->lastchar = _NOCHANGE;
219 	if_USE_SCROLL_HINTS(oline->oldindex = i);
220     }
221 
222     /*
223      * Clean up debris from scrolling or resizing the pad, so we do not
224      * accidentally pick up the index value during the next call to this
225      * procedure.  The only rows that should have an index value are those
226      * that are displayed during this cycle.
227      */
228 #if USE_SCROLL_HINTS
229     for (i = pminrow - 1; (i >= 0) && (win->_line[i].oldindex >= 0); i--)
230 	win->_line[i].oldindex = _NEWINDEX;
231     for (i = pmaxrow + 1; (i <= win->_maxy)
232 	&& (win->_line[i].oldindex >= 0); i++)
233 	win->_line[i].oldindex = _NEWINDEX;
234 #endif
235 
236     win->_begx = smincol;
237     win->_begy = sminrow;
238 
239     if (win->_clear) {
240 	win->_clear = FALSE;
241 	newscr->_clear = TRUE;
242     }
243 
244     /*
245      * Use the pad's current position, if it will be visible.
246      * If not, don't do anything; it's not an error.
247      */
248     if (win->_leaveok == FALSE
249 	&& win->_cury >= pminrow
250 	&& win->_curx >= pmincol
251 	&& win->_cury <= pmaxrow
252 	&& win->_curx <= pmaxcol) {
253 	newscr->_cury = win->_cury - pminrow + win->_begy + win->_yoffset;
254 	newscr->_curx = win->_curx - pmincol + win->_begx;
255     }
256     newscr->_leaveok = win->_leaveok;
257     win->_flags &= ~_HASMOVED;
258 
259     /*
260      * Update our cache of the line-numbers that we displayed from the pad.
261      * We will use this on subsequent calls to this function to derive
262      * values to stuff into 'oldindex[]' -- for scrolling optimization.
263      */
264     win->_pad._pad_y = pminrow;
265     win->_pad._pad_x = pmincol;
266     win->_pad._pad_top = sminrow;
267     win->_pad._pad_left = smincol;
268     win->_pad._pad_bottom = smaxrow;
269     win->_pad._pad_right = smaxcol;
270 
271     returnCode(OK);
272 }
273 
274 int
275 pechochar(WINDOW *pad, const chtype ch)
276 {
277     T((T_CALLED("pechochar(%p, %s)"), pad, _tracechtype(ch)));
278 
279     if (pad == 0)
280 	returnCode(ERR);
281 
282     if (!(pad->_flags & _ISPAD))
283 	returnCode(wechochar(pad, ch));
284 
285     waddch(pad, ch);
286     prefresh(pad, pad->_pad._pad_y,
287 	pad->_pad._pad_x,
288 	pad->_pad._pad_top,
289 	pad->_pad._pad_left,
290 	pad->_pad._pad_bottom,
291 	pad->_pad._pad_right);
292 
293     returnCode(OK);
294 }
295