]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/lib_pad.c
ncurses 4.2
[ncurses.git] / ncurses / 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.25 1998/02/11 12:13:55 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
182                                 if (nline->firstchar == _NOCHANGE)
183                                         nline->firstchar = nline->lastchar = n;
184                                 else if (n < nline->firstchar)
185                                         nline->firstchar = n;
186                                 else if (n > nline->lastchar)
187                                         nline->lastchar = n;
188                         }
189                 }
190
191 #if USE_SCROLL_HINTS
192                 if (wide) {
193                     int nind = m + displaced;
194                     if (oline->oldindex < 0
195                      || nind < sminrow
196                      || nind > smaxrow) {
197                         nind = _NEWINDEX;
198                     } else if (displaced) {
199                         register struct ldat *pline = &curscr->_line[nind];
200                         for (j = 0; j <= my_len; j++) {
201                             int k = newscr->_maxx - j;
202                             if (pline->text[j] != nline->text[j]
203                              || pline->text[k] != nline->text[k]) {
204                                 nind = _NEWINDEX;
205                                 break;
206                             }
207                         }
208                     }
209
210                     nline->oldindex = nind;
211                 }
212 #endif /* USE_SCROLL_HINTS */
213                 oline->firstchar = oline->lastchar = _NOCHANGE;
214                 if_USE_SCROLL_HINTS(oline->oldindex = i);
215         }
216
217         /*
218          * Clean up debris from scrolling or resizing the pad, so we do not
219          * accidentally pick up the index value during the next call to this
220          * procedure.  The only rows that should have an index value are those
221          * that are displayed during this cycle.
222          */
223 #if USE_SCROLL_HINTS
224         for (i = pminrow-1; (i >= 0) && (win->_line[i].oldindex >= 0); i--)
225                 win->_line[i].oldindex = _NEWINDEX;
226         for (i = pmaxrow+1; (i <= win->_maxy) && (win->_line[i].oldindex >= 0); i++)
227                 win->_line[i].oldindex = _NEWINDEX;
228 #endif
229
230         win->_begx = smincol;
231         win->_begy = sminrow;
232
233         if (win->_clear) {
234             win->_clear = FALSE;
235             newscr->_clear = TRUE;
236         }
237
238         /*
239          * Use the pad's current position, if it will be visible.
240          * If not, don't do anything; it's not an error.
241          */
242         if (win->_leaveok == FALSE
243          && win->_cury  >= pminrow
244          && win->_curx  >= pmincol
245          && win->_cury  <= pmaxrow
246          && win->_curx  <= pmaxcol) {
247                 newscr->_cury = win->_cury - pminrow + win->_begy + win->_yoffset;
248                 newscr->_curx = win->_curx - pmincol + win->_begx;
249         }
250         win->_flags &= ~_HASMOVED;
251
252         /*
253          * Update our cache of the line-numbers that we displayed from the pad.
254          * We will use this on subsequent calls to this function to derive
255          * values to stuff into 'oldindex[]' -- for scrolling optimization.
256          */
257         win->_pad._pad_y      = pminrow;
258         win->_pad._pad_x      = pmincol;
259         win->_pad._pad_top    = sminrow;
260         win->_pad._pad_left   = smincol;
261         win->_pad._pad_bottom = smaxrow;
262         win->_pad._pad_right  = smaxcol;
263
264         returnCode(OK);
265 }
266
267 int pechochar(WINDOW *pad, const chtype ch)
268 {
269         T((T_CALLED("pechochar(%p, %s)"), pad, _tracechtype(ch)));
270
271         if (pad == 0)
272           returnCode(ERR);
273
274         if (!(pad->_flags & _ISPAD))
275                 returnCode(wechochar(pad,ch));
276
277         waddch(pad, ch);
278         prefresh(pad, pad->_pad._pad_y,
279                       pad->_pad._pad_x,
280                       pad->_pad._pad_top,
281                       pad->_pad._pad_left,
282                       pad->_pad._pad_bottom,
283                       pad->_pad._pad_right);
284         
285         returnCode(OK);
286 }