Commit b7c3ec5f authored by rswindell's avatar rswindell
Browse files

uifcapi_t:

- msg() now returns an int (e.g. 0 for OK, non-zero otherwise)
- add msgf() a printf-style version of msg()
- add confirm() a printf-stle yes/no dialog (returns TRUE on "Yes"/default)
- add deny() a printf-style no/yes dialog (returns TRUE on "No"/default)
- add yesNoOpts to allow application to over-ride "Yes"/"No" strings.
parent a7a4542e
......@@ -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. */
......
......@@ -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;
}
/***************************************/
......
......@@ -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';
}
/****************************************************************************/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment