ncursesw-morphos/ncurses/base/lib_pad.c

341 lines
10 KiB
C
Raw Normal View History

1998-03-01 12:21:12 +08:00
/****************************************************************************
* Copyright (c) 1998-2006,2009 Free Software Foundation, Inc. *
1998-03-01 12:21:12 +08:00
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the *
* "Software"), to deal in the Software without restriction, including *
* without limitation the rights to use, copy, modify, merge, publish, *
* distribute, distribute with modifications, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included *
* in all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
* IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
* THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
* *
* Except as contained in this notice, the name(s) of the above copyright *
* holders shall not be used in advertising or otherwise to promote the *
* sale, use or other dealings in this Software without prior written *
* authorization. *
****************************************************************************/
/****************************************************************************
* Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
* and: Eric S. Raymond <esr@snark.thyrsus.com> *
* and: Thomas E. Dickey 1996-on *
* and: Juergen Pfeifer 2009 *
1998-03-01 12:21:12 +08:00
****************************************************************************/
1997-05-15 12:00:00 +08:00
/*
* lib_pad.c
* newpad -- create a new pad
* pnoutrefresh -- refresh a pad, no update
* pechochar -- add a char to a pad and refresh
*/
#include <curses.priv.h>
MODULE_ID("$Id: lib_pad.c,v 1.45 2009/10/24 22:07:03 tom Exp $")
1997-05-15 12:00:00 +08:00
2002-10-13 11:35:53 +08:00
NCURSES_EXPORT(WINDOW *)
NCURSES_SP_NAME(newpad) (NCURSES_SP_DCLx int l, int c)
1997-05-15 12:00:00 +08:00
{
2000-07-09 10:46:08 +08:00
WINDOW *win;
2002-10-13 11:35:53 +08:00
NCURSES_CH_T *ptr;
2000-07-09 10:46:08 +08:00
int i;
1997-05-15 12:00:00 +08:00
T((T_CALLED("newpad(%p,%d, %d)"), (void *) SP_PARM, l, c));
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
if (l <= 0 || c <= 0)
returnWin(0);
1997-05-15 12:00:00 +08:00
win = NCURSES_SP_NAME(_nc_makenew) (NCURSES_SP_ARGx l, c, 0, 0, _ISPAD);
if (win == NULL)
2000-07-09 10:46:08 +08:00
returnWin(0);
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
for (i = 0; i < l; i++) {
if_USE_SCROLL_HINTS(win->_line[i].oldindex = _NEWINDEX);
2002-10-13 11:35:53 +08:00
if ((win->_line[i].text = typeCalloc(NCURSES_CH_T, ((size_t) c))) == 0) {
(void) _nc_freewin(win);
2000-07-09 10:46:08 +08:00
returnWin(0);
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
for (ptr = win->_line[i].text; ptr < win->_line[i].text + c; ptr++)
SetChar(*ptr, BLANK_TEXT, BLANK_ATTR);
2000-07-09 10:46:08 +08:00
}
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
returnWin(win);
1997-05-15 12:00:00 +08:00
}
#if NCURSES_SP_FUNCS
NCURSES_EXPORT(WINDOW *)
newpad(int l, int c)
{
return NCURSES_SP_NAME(newpad) (CURRENT_SCREEN, l, c);
}
#endif
2002-10-13 11:35:53 +08:00
NCURSES_EXPORT(WINDOW *)
2006-12-18 12:32:42 +08:00
subpad(WINDOW *orig, int l, int c, int begy, int begx)
1997-05-15 12:00:00 +08:00
{
2000-07-09 10:46:08 +08:00
WINDOW *win = (WINDOW *) 0;
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
T((T_CALLED("subpad(%d, %d)"), l, c));
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
if (orig) {
if (!(orig->_flags & _ISPAD)
|| ((win = derwin(orig, l, c, begy, begx)) == NULL))
1997-05-15 12:00:00 +08:00
returnWin(0);
2000-07-09 10:46:08 +08:00
}
returnWin(win);
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
NCURSES_EXPORT(int)
2006-12-18 12:32:42 +08:00
prefresh(WINDOW *win,
int pminrow,
int pmincol,
int sminrow,
int smincol,
int smaxrow,
int smaxcol)
1997-05-15 12:00:00 +08:00
{
#if NCURSES_SP_FUNCS
SCREEN *sp = _nc_screen_of(win);
#endif
2000-07-09 10:46:08 +08:00
T((T_CALLED("prefresh()")));
if (pnoutrefresh(win, pminrow, pmincol, sminrow, smincol, smaxrow,
2002-10-13 11:35:53 +08:00
smaxcol) != ERR
&& NCURSES_SP_NAME(doupdate) (NCURSES_SP_ARG) != ERR) {
2000-07-09 10:46:08 +08:00
returnCode(OK);
}
returnCode(ERR);
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
NCURSES_EXPORT(int)
2006-12-18 12:32:42 +08:00
pnoutrefresh(WINDOW *win,
int pminrow,
int pmincol,
int sminrow,
int smincol,
int smaxrow,
int smaxcol)
1997-05-15 12:00:00 +08:00
{
2000-07-09 10:46:08 +08:00
NCURSES_SIZE_T i, j;
NCURSES_SIZE_T m, n;
NCURSES_SIZE_T pmaxrow;
NCURSES_SIZE_T pmaxcol;
SCREEN *sp;
2002-10-13 11:35:53 +08:00
#if USE_SCROLL_HINTS
const int my_len = 2; /* parameterize the threshold for hardscroll */
2000-07-09 10:46:08 +08:00
NCURSES_SIZE_T displaced;
bool wide;
2002-10-13 11:35:53 +08:00
#endif
2000-07-09 10:46:08 +08:00
T((T_CALLED("pnoutrefresh(%p, %d, %d, %d, %d, %d, %d)"),
(void *) win, pminrow, pmincol, sminrow, smincol, smaxrow, smaxcol));
2000-07-09 10:46:08 +08:00
if (win == 0)
returnCode(ERR);
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
if (!(win->_flags & _ISPAD))
returnCode(ERR);
1997-05-15 12:00:00 +08:00
sp = _nc_screen_of(win);
2000-07-09 10:46:08 +08:00
/* negative values are interpreted as zero */
if (pminrow < 0)
pminrow = 0;
if (pmincol < 0)
pmincol = 0;
if (sminrow < 0)
sminrow = 0;
if (smincol < 0)
smincol = 0;
pmaxrow = pminrow + smaxrow - sminrow;
pmaxcol = pmincol + smaxcol - smincol;
2006-12-18 12:32:42 +08:00
T((" pminrow + smaxrow - sminrow %ld, win->_maxy %ld",
(long) pmaxrow, (long) win->_maxy));
T((" pmincol + smaxcol - smincol %ld, win->_maxx %ld",
(long) pmaxcol, (long) win->_maxx));
2000-07-09 10:46:08 +08:00
/*
* Trim the caller's screen size back to the actual limits.
*/
if (pmaxrow > win->_maxy) {
smaxrow -= (pmaxrow - win->_maxy);
1997-05-15 12:00:00 +08:00
pmaxrow = pminrow + smaxrow - sminrow;
2000-07-09 10:46:08 +08:00
}
if (pmaxcol > win->_maxx) {
smaxcol -= (pmaxcol - win->_maxx);
1997-05-15 12:00:00 +08:00
pmaxcol = pmincol + smaxcol - smincol;
2000-07-09 10:46:08 +08:00
}
1997-05-15 12:00:00 +08:00
if (smaxrow >= screen_lines(sp)
|| smaxcol >= screen_columns(sp)
2000-07-09 10:46:08 +08:00
|| sminrow > smaxrow
|| smincol > smaxcol)
returnCode(ERR);
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
T(("pad being refreshed"));
2002-10-13 11:35:53 +08:00
#if USE_SCROLL_HINTS
2000-07-09 10:46:08 +08:00
if (win->_pad._pad_y >= 0) {
displaced = pminrow - win->_pad._pad_y
- (sminrow - win->_pad._pad_top);
T(("pad being shifted by %d line(s)", displaced));
} else
displaced = 0;
2002-10-13 11:35:53 +08:00
#endif
2000-07-09 10:46:08 +08:00
/*
* For pure efficiency, we'd want to transfer scrolling information
* from the pad to newscr whenever the window is wide enough that
* its update will dominate the cost of the update for the horizontal
* band of newscr that it occupies. Unfortunately, this threshold
* tends to be complex to estimate, and in any case scrolling the
* whole band and rewriting the parts outside win's image would look
* really ugly. So. What we do is consider the pad "wide" if it
* either (a) occupies the whole width of newscr, or (b) occupies
* all but at most one column on either vertical edge of the screen
* (this caters to fussy people who put boxes around full-screen
* windows). Note that changing this formula will not break any code,
* merely change the costs of various update cases.
*/
2002-10-13 11:35:53 +08:00
#if USE_SCROLL_HINTS
wide = (smincol < my_len && smaxcol > (NewScreen(sp)->_maxx - my_len));
2002-10-13 11:35:53 +08:00
#endif
2000-07-09 10:46:08 +08:00
for (i = pminrow, m = sminrow + win->_yoffset;
i <= pmaxrow && m <= NewScreen(sp)->_maxy;
2002-10-13 11:35:53 +08:00
i++, m++) {
register struct ldat *nline = &NewScreen(sp)->_line[m];
2000-07-09 10:46:08 +08:00
register struct ldat *oline = &win->_line[i];
for (j = pmincol, n = smincol; j <= pmaxcol; j++, n++) {
2002-10-13 11:35:53 +08:00
NCURSES_CH_T ch = oline->text[j];
#if USE_WIDEC_SUPPORT
/*
* Special case for leftmost character of the displayed area.
* Only half of a double-width character may be visible.
*/
if (j == pmincol
&& j > 0
2005-10-10 02:41:57 +08:00
&& isWidecExt(ch)) {
2002-10-13 11:35:53 +08:00
SetChar(ch, L(' '), AttrOf(oline->text[j - 1]));
}
#endif
if (!CharEq(ch, nline->text[n])) {
nline->text[n] = ch;
2000-07-09 10:46:08 +08:00
CHANGED_CELL(nline, n);
}
1997-05-15 12:00:00 +08:00
}
1998-03-01 12:21:12 +08:00
#if USE_SCROLL_HINTS
2000-07-09 10:46:08 +08:00
if (wide) {
int nind = m + displaced;
if (oline->oldindex < 0
|| nind < sminrow
|| nind > smaxrow) {
nind = _NEWINDEX;
} else if (displaced) {
register struct ldat *pline = &CurScreen(sp)->_line[nind];
2000-07-09 10:46:08 +08:00
for (j = 0; j <= my_len; j++) {
int k = NewScreen(sp)->_maxx - j;
2000-07-09 10:46:08 +08:00
if (pline->text[j] != nline->text[j]
|| pline->text[k] != nline->text[k]) {
1997-05-15 12:00:00 +08:00
nind = _NEWINDEX;
2000-07-09 10:46:08 +08:00
break;
1998-03-01 12:21:12 +08:00
}
1997-05-15 12:00:00 +08:00
}
2000-07-09 10:46:08 +08:00
}
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
nline->oldindex = nind;
}
#endif /* USE_SCROLL_HINTS */
oline->firstchar = oline->lastchar = _NOCHANGE;
if_USE_SCROLL_HINTS(oline->oldindex = i);
}
/*
* Clean up debris from scrolling or resizing the pad, so we do not
* accidentally pick up the index value during the next call to this
* procedure. The only rows that should have an index value are those
* that are displayed during this cycle.
*/
1998-03-01 12:21:12 +08:00
#if USE_SCROLL_HINTS
2000-07-09 10:46:08 +08:00
for (i = pminrow - 1; (i >= 0) && (win->_line[i].oldindex >= 0); i--)
win->_line[i].oldindex = _NEWINDEX;
for (i = pmaxrow + 1; (i <= win->_maxy)
2002-10-13 11:35:53 +08:00
&& (win->_line[i].oldindex >= 0); i++)
2000-07-09 10:46:08 +08:00
win->_line[i].oldindex = _NEWINDEX;
1998-03-01 12:21:12 +08:00
#endif
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
win->_begx = smincol;
win->_begy = sminrow;
if (win->_clear) {
win->_clear = FALSE;
NewScreen(sp)->_clear = TRUE;
2000-07-09 10:46:08 +08:00
}
/*
* Use the pad's current position, if it will be visible.
* If not, don't do anything; it's not an error.
*/
if (win->_leaveok == FALSE
&& win->_cury >= pminrow
&& win->_curx >= pmincol
&& win->_cury <= pmaxrow
&& win->_curx <= pmaxcol) {
NewScreen(sp)->_cury = win->_cury - pminrow + win->_begy + win->_yoffset;
NewScreen(sp)->_curx = win->_curx - pmincol + win->_begx;
2000-07-09 10:46:08 +08:00
}
NewScreen(sp)->_leaveok = win->_leaveok;
2000-07-09 10:46:08 +08:00
win->_flags &= ~_HASMOVED;
/*
* Update our cache of the line-numbers that we displayed from the pad.
* We will use this on subsequent calls to this function to derive
* values to stuff into 'oldindex[]' -- for scrolling optimization.
*/
win->_pad._pad_y = pminrow;
win->_pad._pad_x = pmincol;
win->_pad._pad_top = sminrow;
win->_pad._pad_left = smincol;
win->_pad._pad_bottom = smaxrow;
win->_pad._pad_right = smaxcol;
returnCode(OK);
}
1997-05-15 12:00:00 +08:00
2002-10-13 11:35:53 +08:00
NCURSES_EXPORT(int)
2000-07-09 10:46:08 +08:00
pechochar(WINDOW *pad, const chtype ch)
{
T((T_CALLED("pechochar(%p, %s)"), (void *) pad, _tracechtype(ch)));
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
if (pad == 0)
returnCode(ERR);
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
if (!(pad->_flags & _ISPAD))
returnCode(wechochar(pad, ch));
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
waddch(pad, ch);
prefresh(pad, pad->_pad._pad_y,
2002-10-13 11:35:53 +08:00
pad->_pad._pad_x,
pad->_pad._pad_top,
pad->_pad._pad_left,
pad->_pad._pad_bottom,
pad->_pad._pad_right);
2000-07-09 10:46:08 +08:00
returnCode(OK);
1997-05-15 12:00:00 +08:00
}