/***********************************************************************
 *
 * TITLE:
 *	beware.c
 *
 * AUTHOR:
 *	Kevin J. Miller, Ana Maria Guerrero
 *
 * DESCRIPTION:
 *	A generic OEL module.  It contains routines which initialize and
 *	control a popup use to warn the user of disastrous consequences.
 *
 *				CHANGE HISTORY
 *
 * $Log: beware.c,v $
 * Revision 1.6  1994/01/06  05:12:06  kevin
 * port for V20
 *
 * Revision 1.5  1993/03/04  21:14:38  kevin
 * renamed functions, added shell management functions,
 * changed to Question box, added comments
 *
 * Revision 1.4  1992/08/04  20:11:06  kevin
 * made generic
 *
 * Revision 1.3  1991/11/12  23:24:38  kevin
 * unmanaged help button
 *
 * Revision 1.2  1991/06/15  00:39:20  kevin
 * put titles on dialog shells
 *
 * Revision 1.1  1991/06/14  19:52:37  kevin
 * Initial revision
 *
 ***********************************************************************
 *
 * WARNINGS:
 *	none
 *
 * EXTERNAL CALLABLE COMPONENTS (PUBLIC):
 *	InitBeware
 *	Beware
 *
 * GLOBALS:
 *	none
 *
 * WAIVERS:
 *	none
 *
 * NOTES:
 *	none
 *
 * MANPAGE:
 *	none
 *
 ***********************************************************************/

#ifndef lint
static char rcsid[] =
    "$Id: beware.c,v 1.6 1994/01/06 05:12:06 kevin OEL $";
#endif

#include <Xm/Xm.h>
#include <Xm/MessageB.h>

#include "beware.h"
#include "shellmgmt.h"

static void bewareCB(Widget, XtPointer, XmAnyCallbackStruct *);

static Widget bewareBox;		/* FileSelectionBox widget */
static int flag = False;		/* stay in this local event loop */
static int answer;			/* answer to the question */
static XtAppContext app;		/* application context */

/***********************************************************************
 *
 * FUNCTION:
 *	InitBeware
 *
 * INPUTS:
 *	parent		- (Widget) the application shell widget
 *	title		- (char *) text title for dialog
 *
 * OUTPUTS:
 *	none
 *
 * RETURNS:
 *	none
 *
 * EXTERNALLY READ:
 *	none
 *
 * EXTERNALLY MODIFIED:
 *	app		- (XtAppContext) current application context
 *	bewareBox	- (Widget) the widget to manage/unmanage
 *
 * DESCRIPTION:
 *	This routine creates a popup dialog that warns the user about
 *	dangerous actions.  It should be called once at the beginning
 *	of the program.
 */

void 
InitBeware(Widget parent, char *title)
{
    XmString tmpString;			/* title of widget */
    Arg args[4];
    Cardinal n;

    app = XtWidgetToApplicationContext(parent);

    tmpString = XmStringCreateSimple(title);

    n = 0;
    XtSetArg(args[n], XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL); n++;
    XtSetArg(args[n], XmNdialogTitle, tmpString); n++;
    bewareBox = XmCreateQuestionDialog(parent, "bewareBox",
	args, n);
    XtAddCallback(bewareBox, XmNokCallback, (XtCallbackProc)bewareCB,
	(XtPointer)True);
    XtAddCallback(bewareBox, XmNcancelCallback, (XtCallbackProc)bewareCB,
	(XtPointer)False);

    XmStringFree(tmpString);

    XtUnmanageChild(XmMessageBoxGetChild(bewareBox, XmDIALOG_HELP_BUTTON));

    /*
     * make MWM Close call the Cancel callback
     */
    ShellAddClose(bewareBox, (XtCallbackProc)bewareCB, False);
}

/***********************************************************************
 *
 * FUNCTION:
 *	Beware
 *
 * INPUTS:
 *	txtptr		- (char *) text to display
 *
 * OUTPUTS:
 *	none
 *
 * RETURNS:
 *	answer		- (int) True for "OK", False for "Cancel"
 *
 * EXTERNALLY READ:
 *	app		- (XtAppContext) required to get events
 *	answer		- (int) True for "OK", False for "Cancel"
 *
 * EXTERNALLY MODIFIED:
 *	flag		- (int)	set to True to remain in local loop
 *
 * DESCRIPTION:
 *	This routine displays a modal dialog with a question and
 *	then enters a local loop.  If the user selects "OK", then
 *	this routine returns True.  If the user selects "Cancel"
 *	or "Close" (from MWM), then this routine returns False.
 */

int 
Beware(char *txtptr)
{
    XEvent event;		/* event structure for loop */
    XmString mcs;		/* Motif compound string */
    Arg args[1];		/* Xt argument list */
    Cardinal n;			/* number of arguments */

    /*
     * set message
     */
    mcs = XmStringCreateLtoR(txtptr, XmSTRING_ISO8859_1);

    n = 0;
    XtSetArg(args[n], XmNmessageString, mcs); n++;
    XtSetValues(bewareBox, args, n);

    XmStringFree(mcs);

    ShellModalManage(bewareBox);

    /*
     * loop until OK or Cancel
     */
    flag = True;
    while (flag) {		/* local event loop for popup */
	XtAppNextEvent(app, &event);
	XtDispatchEvent(&event);
    }

    return answer;
}

/***********************************************************************
 *
 * FUNCTION:
 *	bewareCB
 *
 * INPUTS:
 *	w		- (Widget) source of callback
 *	is_ok		- (int) True for "OK"
 *	callback_data	- (XmAnyCallbackStruct *) not used
 *
 * OUTPUTS:
 *	none
 *
 * RETURNS:
 *	none
 *
 * EXTERNALLY READ:
 *	none
 *
 * EXTERNALLY MODIFIED:
 *	answer		- (int) True for "OK", False for "Cancel"
 *	flag		- (int) set to False
 *
 * DESCRIPTION:
 *	This callback handles all input to the beware box.  If the
 *	MWM Close button is selected, it is validated.  Since the
 *	beware box cannot popup additional modal dialogs, input
 *	will always be valid, but the code is included for
 *	consistency.  The answer is saved, the beware box is unmanaged,
 *	and the loop flag is set to False.
 */

static void 
bewareCB(Widget w, XtPointer is_ok, XmAnyCallbackStruct *callback_data)
{
    if (!ShellValidateClose(w)) return;

    answer = (int)is_ok;
    ShellModalUnmanage(bewareBox);
    flag = False;		/* exit local loop */
}
