diff --git a/src/uifc/uifc.h b/src/uifc/uifc.h index 053847e9d5562b4e591aaf064db3589c60a0d81c..e2c7d39d7592c49e14d2531051bb31c3590e8002 100644 --- a/src/uifc/uifc.h +++ b/src/uifc/uifc.h @@ -415,6 +415,11 @@ typedef struct { /****************************************************************************/ uifc_graphics_t *chars; +/****************************************************************************/ +/* Allow application override */ +/****************************************************************************/ + char** yesNoOpts; + /****************************************************************************/ /* Exit/uninitialize function. */ /****************************************************************************/ @@ -428,7 +433,10 @@ typedef struct { /****************************************************************************/ /* Popup a message, maybe wait for the user to hit a key or click button. */ /****************************************************************************/ - void (*msg) (char* str); + int (*msg) (char* str); + int (*msgf) (char* fmt, ...); + BOOL (*deny) (char* fmt, ...); + BOOL (*confirm) (char* fmt, ...); /****************************************************************************/ /* Popup/down a status message. */ /* str is the message to display on popup. */ diff --git a/src/uifc/uifc32.c b/src/uifc/uifc32.c index 32f1f54039474a9853e129a8ab7b4c3e69bfd5c7..fe7d1fce9c3828c759faacb716afad86c3bfd619 100644 --- a/src/uifc/uifc32.c +++ b/src/uifc/uifc32.c @@ -48,6 +48,7 @@ #endif #include <genwrap.h> // for alloca() #include <datewrap.h> // localtime_r() +#include "xpprintf.h" #include "ciolib.h" #include "uifc.h" @@ -80,7 +81,10 @@ static int ulist(int mode, int left, int top, int width, int *dflt, int *bar ,char *title, char **option); static int uinput(int imode, int left, int top, char *prompt, char *str ,int len ,int kmode); -static void umsg(char *str); +static int umsg(char *str); +static int umsgf(char *fmt, ...); +static BOOL confirm(char *fmt, ...); +static BOOL deny(char *fmt, ...); static void upop(char *str); static void sethelp(int line, char* file); static void showbuf(int mode, int left, int top, int width, int height, char *title @@ -210,12 +214,18 @@ int UIFCCALL uifcini32(uifcapi_t* uifcapi) api=uifcapi; if (api->chars == NULL) - api->chars = &cp437_chars; + api->chars = &cp437_chars; + + if (api->yesNoOpts == NULL) + api->yesNoOpts = uifcYesNoOpts; /* install function handlers */ api->bail=uifcbail; api->scrn=uscrn; api->msg=umsg; + api->msgf=umsgf; + api->confirm=confirm; + api->deny=deny; api->pop=upop; api->list=ulist; api->input=uinput; @@ -1875,16 +1885,69 @@ int uinput(int mode, int left, int top, char *inprompt, char *str, /****************************************************************************/ /* Displays the message 'str' and waits for the user to select "OK" */ /****************************************************************************/ -void umsg(char *str) +int umsg(char *str) { int i=0; char *ok[2]={"OK",""}; if(api->mode&UIFC_INMSG) /* non-cursive */ - return; + return -1; api->mode|=UIFC_INMSG; - ulist(WIN_SAV|WIN_MID,0,0,0,&i,0,str,ok); + i = ulist(WIN_SAV|WIN_MID,0,0,0,&i,0,str,ok); api->mode&=~UIFC_INMSG; + return i; +} + +/* Same as above, using printf-style varargs */ +int umsgf(char* fmt, ...) +{ + int retval = -1; + va_list va; + char* buf = NULL; + + va_start(va, fmt); + vasprintf(&buf, fmt, va); + va_end(va); + if(buf != NULL) { + retval = umsg(buf); + free(buf); + } + return retval; +} + +static int yesno(int dflt, char* fmt, va_list va) +{ + int retval; + char* buf = NULL; + + vasprintf(&buf, fmt, va); + if(buf == NULL) + return dflt; + retval = ulist(WIN_SAV|WIN_MID,0,0,0,&dflt,0,buf,api->yesNoOpts); + free(buf); + return retval; +} + +static BOOL confirm(char* fmt, ...) +{ + int retval; + + va_list va; + va_start(va, fmt); + retval = yesno(0, fmt, va); + va_end(va); + return retval == 0; +} + +static BOOL deny(char* fmt, ...) +{ + int retval; + + va_list va; + va_start(va, fmt); + retval = yesno(1, fmt, va); + va_end(va); + return retval != 0; } /***************************************/ diff --git a/src/uifc/uifcx.c b/src/uifc/uifcx.c index e6c7e139309ef4883a9407fb962935608657d918..f4c537e97e69a2d5eb969a76364295cfd674cf15 100644 --- a/src/uifc/uifcx.c +++ b/src/uifc/uifcx.c @@ -35,6 +35,7 @@ #include "genwrap.h" #include "gen_defs.h" +#include "xpprintf.h" #include "uifc.h" #include <sys/types.h> @@ -58,7 +59,10 @@ static int ulist(int mode, int left, int top, int width, int *dflt, int *bar ,char *title, char **option); static int uinput(int imode, int left, int top, char *prompt, char *str ,int len ,int kmode); -static void umsg(char *str); +static int umsg(char *str); +static int umsgf(char *str, ...); +static BOOL confirm(char *str, ...); +static BOOL deny(char *str, ...); static void upop(char *str); static void sethelp(int line, char* file); @@ -84,16 +88,23 @@ static int uprintf(int x, int y, unsigned attr, char *fmat, ...) /****************************************************************************/ int UIFCCALL uifcinix(uifcapi_t* uifcapi) { + static char* yesNoOpts[] = {"Yes", "No", NULL}; if(uifcapi==NULL || uifcapi->size!=sizeof(uifcapi_t)) return(-1); api=uifcapi; + if (api->yesNoOpts == NULL) + api->yesNoOpts = yesNoOpts; // Not currently used in this interface instance + /* install function handlers */ api->bail=uifcbail; api->scrn=uscrn; api->msg=umsg; + api->msgf=umsgf; + api->confirm=confirm; + api->deny=deny; api->pop=upop; api->list=ulist; api->input=uinput; @@ -160,7 +171,6 @@ static int getstr(char* str, int maxlen) return(len); } - /****************************************************************************/ /* Local utility function. */ /****************************************************************************/ @@ -349,11 +359,57 @@ int uinput(int mode, int left, int top, char *prompt, char *outstr, } /****************************************************************************/ -/* Displays the message 'str' and waits for the user to select "OK" */ +/* Displays the message 'str' and waits for the user to hit ENTER */ /****************************************************************************/ -void umsg(char *str) +int umsg(char *str) +{ + int ch; + printf("%s\nHit enter to continue:",str); + ch = getchar(); + return ch == '\r' || ch == '\n'; +} + +/* Same as above, using printf-style varargs */ +int umsgf(char* fmt, ...) +{ + int retval = -1; + va_list va; + char* buf = NULL; + + va_start(va, fmt); + vasprintf(&buf, fmt, va); + va_end(va); + if(buf != NULL) { + retval = umsg(buf); + free(buf); + } + return retval; +} + +BOOL confirm(char* fmt, ...) +{ + int ch; + va_list va; + + va_start(va, fmt); + vprintf(fmt, va); + va_end(va); + printf(" (Y/n)? "); + ch = getchar(); + return tolower(ch) != 'n' && ch != EOF; +} + +BOOL deny(char* fmt, ...) { - printf("%s\n",str); + int ch; + va_list va; + + va_start(va, fmt); + vprintf(fmt, va); + va_end(va); + printf(" (N/y)? "); + ch = getchar(); + return tolower(ch) != 'y'; } /****************************************************************************/