2
0
mirror of https://github.com/xcat2/xNBA.git synced 2025-01-05 11:05:07 +00:00

- implemented cursor retreat function (_wcursback) as a core function

- reimplemented cleaner wgetnstr
- fixed wdelch for use with _wcursback
This commit is contained in:
Dan Lynch 2006-06-28 11:48:40 +00:00
parent 6e2c97b0c0
commit c29c868475
4 changed files with 74 additions and 41 deletions

View File

@ -51,11 +51,8 @@ int wclrtoeol ( WINDOW *win ) {
* @ret rc return status code
*/
int wdelch ( WINDOW *win ) {
struct cursor_pos pos;
_store_curs_pos( win, &pos );
_wputch( win, (unsigned)' ', NOWRAP );
_restore_curs_pos( win, &pos );
_wcursback( win );
return OK;
}

View File

@ -48,6 +48,25 @@ void _wputch ( WINDOW *win, chtype ch, int wrap ) {
}
}
/**
* Retreat the cursor back one position (useful for a whole host of
* ops)
*
* @v *win window in which to retreat
*/
void _wcursback ( WINDOW *win ) {
if ( win->curs_x == 0 ) {
if ( win->curs_y == 0 )
win->curs_y = win->height - 1;
win->curs_x = win->width = 1;
} else {
win->curs_x--;
}
win->scr->movetoyx( win->scr, win->ori_y + win->curs_y,
win->ori_x + win->curs_x );
}
/**
* Write a chtype string to a window
*

View File

@ -13,6 +13,7 @@
void _wputch ( WINDOW *win, chtype ch, int wrap );
void _wputchstr ( WINDOW *win, const chtype *chstr, int wrap, int n );
void _wputstr ( WINDOW *win, const char *str, int wrap, int n );
void _wcursback ( WINDOW *win );
int wmove ( WINDOW *win, int y, int x );
#endif /* CURSES_H */

View File

@ -30,14 +30,9 @@ int has_key ( int kc __unused ) {
return TRUE;
}
/**
* Pop a character from the FIFO into a window
*
* @v *win window in which to echo input
* @ret c char from input stream
*/
int wgetch ( WINDOW *win ) {
int c, timer;
int _wgetc ( WINDOW *win ) {
int timer, c;
if ( win == NULL )
return ERR;
@ -45,28 +40,38 @@ int wgetch ( WINDOW *win ) {
while ( ! win->scr->peek( win->scr ) ) {
if ( m_delay == 0 ) // non-blocking read
return ERR;
if ( timer > 0 ) {
if ( timer > 0 ) { // time-limited blocking read
if ( m_delay > 0 )
timer -= INPUT_DELAY;
mdelay( INPUT_DELAY );
} else { return ERR; }
} else { return ERR; } // non-blocking read
}
c = win->scr->getc( win->scr );
if ( m_echo && ( c >= 32 && c <= 126 ) ) // printable ASCII characters
_wputch( win, (chtype) ( c | win->attrs ), WRAP );
return c;
}
/**
* Pop a character from the FIFO into a window
*
* @v *win window in which to echo input
* @ret c char from input stream
*/
int wgetch ( WINDOW *win ) {
int c;
c = _wgetc( win );
if ( m_echo ) {
if ( c >= 0401 && c <= 0633 ) {
switch(c) {
case KEY_LEFT :
case KEY_BACKSPACE :
if ( win->curs_x == 0 )
wmove( win,
--(win->curs_y),
win->width - 1 );
else
wmove( win,
win->curs_y,
--(win->curs_x) );
_wcursback( win );
wdelch( win );
break;
default :
@ -86,38 +91,49 @@ int wgetch ( WINDOW *win ) {
*
* @v *win window in which to echo input
* @v *str pointer to string in which to store result
* @v n maximum number of characters to read into string (inc. NUL)
* @ret rc return status code
*/
int wgetnstr ( WINDOW *win, char *str, int n ) {
char *_str;
int c;
if ( n == 0 ) {
str = '\0';
return OK;
}
_str = str;
while (!( n == 0 ) ) {
c = wgetch( win );
if ( c >= 0401 && c <= 0633 ) {
switch(c) {
case KEY_LEFT :
case KEY_BACKSPACE :
if ( _str > str ) {
_str--; n++;
while ( ( c = _wgetc( win ) ) != ERR ) {
/* termination enforcement - don't let us go past the
end of the allocated buffer... */
if ( n == 0 && ( c >= 32 && c <= 126 ) ) {
_wcursback( win );
wdelch( win );
} else {
if ( c >= 0401 && c <= 0633 ) {
switch(c) {
case KEY_LEFT :
case KEY_BACKSPACE :
_wcursback( win );
wdelch( win );
break;
case KEY_ENTER :
*_str = '\0';
return OK;
default :
beep();
break;
}
break;
case KEY_ENTER :
*_str = '\0';
break;
}
} else if ( c == '\n' ) {
*_str = '\0';
break;
}else { // *should* only be ASCII chars now
*(_str++) = (char)c;
n--;
if ( c >= 32 && c <= 126 ) {
*(_str++) = c; n--;
}
}
}
return OK;
return ERR;
}