1
0
mirror of https://git.zap.org.au/git/trader.git synced 2024-11-03 17:27:29 -05:00

Add the wait_for_key() function

This commit is contained in:
John Zaitseff 2011-07-11 14:43:16 +10:00
parent 5055ea57ac
commit 20f6f6e73a
2 changed files with 88 additions and 50 deletions

View File

@ -104,6 +104,7 @@ void init_screen (void)
init_pair(YELLOW_ON_BLACK, COLOR_YELLOW, COLOR_BLACK);
init_pair(YELLOW_ON_BLUE, COLOR_YELLOW, COLOR_BLUE);
init_pair(YELLOW_ON_CYAN, COLOR_YELLOW, COLOR_CYAN);
init_pair(CYAN_ON_BLUE, COLOR_CYAN, COLOR_BLUE);
init_pair(BLACK_ON_WHITE, COLOR_BLACK, COLOR_WHITE);
bkgd(ATTR_ROOT_WINDOW);
@ -389,54 +390,6 @@ int attrpr (WINDOW *win, int attr, const char *format, ...)
* Input routines *
************************************************************************/
/*-----------------------------------------------------------------------
Function: answer_yesno - Read a Yes/No answer and return true/false
Arguments: win - Window to use
Returns: bool - true if Yes ("Y") was selected, else false
This function waits for either "Y" or "N" to be pressed on the
keyboard. If "Y" was pressed, "Yes." is printed and true is returned.
If "N" was pressed, "No." is printed and false is returned. Note that
the cursor becomes invisible after this function.
*/
bool answer_yesno (WINDOW *win)
{
int key, oldattr;
bool ok;
keypad(win, true);
meta(win, true);
wtimeout(win, -1);
oldattr = getbkgd(win) & ~A_CHARTEXT;
wattron(win, A_BOLD);
curs_set(CURS_ON);
do {
key = toupper(wgetch(win));
ok = ((key == 'Y') || (key == 'N'));
if (! ok) {
beep();
}
} while (! ok);
curs_set(CURS_OFF);
if (key == 'Y') {
waddstr(win, "Yes");
} else {
waddstr(win, "No");
}
wattrset(win, oldattr);
wrefresh(win);
return (key == 'Y');
}
/*-----------------------------------------------------------------------
Function: gettxchar - Read a keyboard character
Arguments: win - Window to use
@ -1185,3 +1138,82 @@ int gettxstring (WINDOW *win, char **bufptr, bool multifield, int y, int x,
return gettxline(win, *bufptr, BUFSIZE, multifield, BUFSIZE - 1, "", "",
NULL, true, y, x, fieldsize, attr, modified);
}
/*-----------------------------------------------------------------------
Function: answer_yesno - Read a Yes/No answer and return true/false
Arguments: win - Window to use
Returns: bool - true if Yes ("Y") was selected, else false
This function waits for either "Y" or "N" to be pressed on the
keyboard. If "Y" was pressed, "Yes." is printed and true is returned.
If "N" was pressed, "No." is printed and false is returned. Note that
the cursor becomes invisible after this function.
*/
bool answer_yesno (WINDOW *win)
{
int key, oldattr;
bool ok;
keypad(win, true);
meta(win, true);
wtimeout(win, -1);
oldattr = getbkgd(win) & ~A_CHARTEXT;
wattron(win, A_BOLD);
curs_set(CURS_ON);
do {
key = toupper(wgetch(win));
ok = ((key == 'Y') || (key == 'N'));
if (! ok) {
beep();
}
} while (! ok);
curs_set(CURS_OFF);
if (key == 'Y') {
waddstr(win, "Yes");
} else {
waddstr(win, "No");
}
wattrset(win, oldattr);
wrefresh(win);
return (key == 'Y');
}
/*-----------------------------------------------------------------------
Function: wait_for_key - Print a message and wait for any key
Arguments: win - Window to use
y - Line on which to print message
Returns: (nothing)
This function prints a message, then waits for any key to be pressed.
*/
void wait_for_key (WINDOW *win, int y)
{
int key, oldattr;
keypad(win, true);
meta(win, true);
wtimeout(win, -1);
oldattr = getbkgd(win) & ~A_CHARTEXT;
curs_set(CURS_OFF);
wattrset(win, ATTR_WAITFORKEY_STR);
center(win, y, false, "[ Press <SPACE> to continue ] ");
wrefresh(win);
key = wgetch(win);
wattrset(win, oldattr);
}

View File

@ -110,6 +110,7 @@ enum color_pairs {
YELLOW_ON_BLACK,
YELLOW_ON_BLUE,
YELLOW_ON_CYAN,
CYAN_ON_BLUE,
BLACK_ON_WHITE,
};
@ -120,8 +121,10 @@ enum color_pairs {
#define ATTR_NORMAL_WINDOW ATTR(COLOR_PAIR(WHITE_ON_BLUE), A_NORMAL)
#define ATTR_STATUS_WINDOW ATTR(COLOR_PAIR(BLACK_ON_WHITE), A_REVERSE)
#define ATTR_WINDOW_TITLE ATTR(COLOR_PAIR(YELLOW_ON_BLACK) | A_BOLD, A_REVERSE)
#define ATTR_KEYCODE_STR ATTR(COLOR_PAIR(YELLOW_ON_BLACK) | A_BOLD, A_REVERSE)
#define ATTR_INPUT_FIELD ATTR(COLOR_PAIR(WHITE_ON_BLACK), A_BOLD | '_')
#define ATTR_KEYCODE_STR ATTR(COLOR_PAIR(YELLOW_ON_BLACK) | A_BOLD, A_REVERSE)
#define ATTR_HIGHLIGHT_STR ATTR(COLOR_PAIR(YELLOW_ON_BLUE) | A_BOLD, A_BOLD)
#define ATTR_WAITFORKEY_STR ATTR(COLOR_PAIR(CYAN_ON_BLUE), A_NORMAL)
/************************************************************************
@ -157,7 +160,6 @@ extern int attrpr (WINDOW *win, int attr, const char *format, ...)
// Input routines
extern bool answer_yesno (WINDOW *win);
extern int gettxchar (WINDOW *win);
extern int gettxline (WINDOW *win, char *buf, int bufsize, bool multifield,
int maxlen, const char *emptyval, const char *defaultval,
@ -166,4 +168,8 @@ extern int gettxline (WINDOW *win, char *buf, int bufsize, bool multifield,
extern int gettxstring (WINDOW *win, char **bufptr, bool multifield,
int y, int x, int fieldsize, int attr, bool *modified);
extern bool answer_yesno (WINDOW *win);
extern void wait_for_key (WINDOW *win, int y);
#endif /* included_INTF_H */