]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_pad.c
d4e341c05c753bda93d6e235aaa3b20df01d05a2
[ncurses.git] / ncurses / base / lib_pad.c
1 /****************************************************************************
2  * Copyright (c) 1998 Free Software Foundation, Inc.                        *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  ****************************************************************************/
33
34
35 /*
36  * lib_pad.c
37  * newpad       -- create a new pad
38  * pnoutrefresh -- refresh a pad, no update
39  * pechochar    -- add a char to a pad and refresh
40  */
41
42 #include <curses.priv.h>
43
44 MODULE_ID("$Id: lib_pad.c,v 1.27 1998/06/28 00:10:16 tom Exp $")
45
46 WINDOW *newpad(int l, int c)
47 {
48 WINDOW *win;
49 chtype *ptr;
50 int i;
51
52         T((T_CALLED("newpad(%d, %d)"), l, c));
53
54         if (l <= 0 || c <= 0)
55                 returnWin(0);
56
57         if ((win = _nc_makenew(l,c,0,0,_ISPAD)) == NULL)
58                 returnWin(0);
59
60         for (i = 0; i < l; i++) {
61             if_USE_SCROLL_HINTS(win->_line[i].oldindex = _NEWINDEX);
62             if ((win->_line[i].text = typeCalloc(chtype, ((size_t)c))) == 0) {
63                 _nc_freewin(win);
64                 returnWin(0);
65             }
66             for (ptr = win->_line[i].text; ptr < win->_line[i].text + c; )
67                 *ptr++ = ' ';
68         }
69
70         returnWin(win);
71 }
72
73 WINDOW *subpad(WINDOW *orig, int l, int c, int begy, int begx)
74 {
75 WINDOW  *win = (WINDOW *)0;
76
77         T((T_CALLED("subpad(%d, %d)"), l, c));
78
79         if (orig) {
80           if (!(orig->_flags & _ISPAD) || ((win = derwin(orig, l, c, begy, begx)) == NULL))
81             returnWin(0);
82         }
83         returnWin(win);
84 }
85
86 int prefresh(WINDOW *win, int pminrow, int pmincol,
87         int sminrow, int smincol, int smaxrow, int smaxcol)
88 {
89         T((T_CALLED("prefresh()")));
90         if (pnoutrefresh(win, pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol) != ERR
91          && doupdate() != ERR) {
92                 returnCode(OK);
93         }
94         returnCode(ERR);
95 }
96
97 int pnoutrefresh(WINDOW *win, int pminrow, int pmincol,
98         int sminrow, int smincol, int smaxrow, int smaxcol)
99 {
100 const   int my_len = 2; /* parameterize the threshold for hardscroll */
101 short   i, j;
102 short   m, n;
103 short   pmaxrow;
104 short   pmaxcol;
105 short   displaced;
106 bool    wide;
107
108         T((T_CALLED("pnoutrefresh(%p, %d, %d, %d, %d, %d, %d)"),
109                 win, pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol));
110
111         if (win == 0)
112                 returnCode(ERR);
113
114         if (!(win->_flags & _ISPAD))
115                 returnCode(ERR);
116
117         /* negative values are interpreted as zero */
118         if (pminrow < 0) pminrow = 0;
119         if (pmincol < 0) pmincol = 0;
120         if (sminrow < 0) sminrow = 0;
121         if (smincol < 0) smincol = 0;
122
123         pmaxrow = pminrow + smaxrow - sminrow;
124         pmaxcol = pmincol + smaxcol - smincol;
125
126         T((" pminrow + smaxrow - sminrow %d, win->_maxy %d", pmaxrow, win->_maxy));
127         T((" pmincol + smaxcol - smincol %d, win->_maxx %d", pmaxcol, win->_maxx));
128
129         /*
130          * Trim the caller's screen size back to the actual limits.
131          */
132         if (pmaxrow > win->_maxy) {
133                 smaxrow -= (pmaxrow - win->_maxy);
134                 pmaxrow = pminrow + smaxrow - sminrow;
135         }
136         if (pmaxcol > win->_maxx) {
137                 smaxcol -= (pmaxcol - win->_maxx);
138                 pmaxcol = pmincol + smaxcol - smincol;
139         }
140
141         if (smaxrow > screen_lines
142          || smaxcol > screen_columns
143          || sminrow > smaxrow
144          || smincol > smaxcol)
145                 returnCode(ERR);
146
147         T(("pad being refreshed"));
148
149         if (win->_pad._pad_y >= 0) {
150                 displaced = pminrow - win->_pad._pad_y
151                           -(sminrow - win->_pad._pad_top);
152                 T(("pad being shifted by %d line(s)", displaced));
153         } else
154                 displaced = 0;
155
156         /*
157          * For pure efficiency, we'd want to transfer scrolling information
158          * from the pad to newscr whenever the window is wide enough that
159          * its update will dominate the cost of the update for the horizontal
160          * band of newscr that it occupies.  Unfortunately, this threshold
161          * tends to be complex to estimate, and in any case scrolling the
162          * whole band and rewriting the parts outside win's image would look
163          * really ugly.  So.  What we do is consider the pad "wide" if it
164          * either (a) occupies the whole width of newscr, or (b) occupies
165          * all but at most one column on either vertical edge of the screen
166          * (this caters to fussy people who put boxes around full-screen
167          * windows).  Note that changing this formula will not break any code,
168          * merely change the costs of various update cases.
169          */
170         wide = (smincol < my_len && smaxcol > (newscr->_maxx - my_len));
171
172         for (i = pminrow, m = sminrow + win->_yoffset;
173                 i <= pmaxrow && m <= newscr->_maxy;
174                         i++, m++) {
175                 register struct ldat    *nline = &newscr->_line[m];
176                 register struct ldat    *oline = &win->_line[i];
177
178                 for (j = pmincol, n = smincol; j <= pmaxcol; j++, n++) {
179                         if (oline->text[j] != nline->text[n]) {
180                                 nline->text[n] = oline->text[j];
181                                 CHANGED_CELL(nline,n);
182                         }
183                 }
184
185 #if USE_SCROLL_HINTS
186                 if (wide) {
187                     int nind = m + displaced;
188                     if (oline->oldindex < 0
189                      || nind < sminrow
190                      || nind > smaxrow) {
191                         nind = _NEWINDEX;
192                     } else if (displaced) {
193                         register struct ldat *pline = &curscr->_line[nind];
194                         for (j = 0; j <= my_len; j++) {
195                             int k = newscr->_maxx - j;
196                             if (pline->text[j] != nline->text[j]
197                              || pline->text[k] != nline->text[k]) {
198                                 nind = _NEWINDEX;
199                                 break;
200                             }
201                         }
202                     }
203
204                     nline->oldindex = nind;
205                 }
206 #endif /* USE_SCROLL_HINTS */
207                 oline->firstchar = oline->lastchar = _NOCHANGE;
208                 if_USE_SCROLL_HINTS(oline->oldindex = i);
209         }
210
211         /*
212          * Clean up debris from scrolling or resizing the pad, so we do not
213          * accidentally pick up the index value during the next call to this
214          * procedure.  The only rows that should have an index value are those
215          * that are displayed during this cycle.
216          */
217 #if USE_SCROLL_HINTS
218         for (i = pminrow-1; (i >= 0) && (win->_line[i].oldindex >= 0); i--)
219                 win->_line[i].oldindex = _NEWINDEX;
220         for (i = pmaxrow+1; (i <= win->_maxy) && (win->_line[i].oldindex >= 0); i++)
221                 win->_line[i].oldindex = _NEWINDEX;
222 #endif
223
224         win->_begx = smincol;
225         win->_begy = sminrow;
226
227         if (win->_clear) {
228             win->_clear = FALSE;
229             newscr->_clear = TRUE;
230         }
231
232         /*
233          * Use the pad's current position, if it will be visible.
234          * If not, don't do anything; it's not an error.
235          */
236         if (win->_leaveok == FALSE
237          && win->_cury  >= pminrow
238          && win->_curx  >= pmincol
239          && win->_cury  <= pmaxrow
240          && win->_curx  <= pmaxcol) {
241                 newscr->_cury = win->_cury - pminrow + win->_begy + win->_yoffset;
242                 newscr->_curx = win->_curx - pmincol + win->_begx;
243         }
244         win->_flags &= ~_HASMOVED;
245
246         /*
247          * Update our cache of the line-numbers that we displayed from the pad.
248          * We will use this on subsequent calls to this function to derive
249          * values to stuff into 'oldindex[]' -- for scrolling optimization.
250          */
251         win->_pad._pad_y      = pminrow;
252         win->_pad._pad_x      = pmincol;
253         win->_pad._pad_top    = sminrow;
254         win->_pad._pad_left   = smincol;
255         win->_pad._pad_bottom = smaxrow;
256         win->_pad._pad_right  = smaxcol;
257
258         returnCode(OK);
259 }
260
261 int pechochar(WINDOW *pad, const chtype ch)
262 {
263         T((T_CALLED("pechochar(%p, %s)"), pad, _tracechtype(ch)));
264
265         if (pad == 0)
266           returnCode(ERR);
267
268         if (!(pad->_flags & _ISPAD))
269                 returnCode(wechochar(pad,ch));
270
271         waddch(pad, ch);
272         prefresh(pad, pad->_pad._pad_y,
273                       pad->_pad._pad_x,
274                       pad->_pad._pad_top,
275                       pad->_pad._pad_left,
276                       pad->_pad._pad_bottom,
277                       pad->_pad._pad_right);
278         
279         returnCode(OK);
280 }