/*	XGModalDialog.cpp
 *
 *		Modal dailog encapsulation
 */

/**********************************************************************

  YAAF is Copyright 1997-1999 by William Woody.

  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions 
  are met:

  Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.

  Neither the name of the copyright holder or other contributors may
  be used to promote products derived from this software without
  specific written prior permission.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
  SOFTWARE.
  
  Contact:   woody@alumni.caltech.edu
  Web page:  http://www.pandawave.com/yaaf/

**********************************************************************/

#include <XConfig.h>
#include <XApplication.h>
#include <XDialog.h>

/************************************************************************/
/*																		*/
/*	Globals																*/
/*																		*/
/************************************************************************/

#if OPT_XWIN == 1
extern bool		_GDialogFlag;
#endif

/************************************************************************/
/*																		*/
/*	Construction/Destruction											*/
/*																		*/
/************************************************************************/

/*$	XGModalDialog::XGModalDialog
 *
 *		Construction. This builds the modal dialog box based on it's
 *	resource identifier, a dialog callback procedure, and a pointer to
 *	a block of data.
 */

XGModalDialog::XGModalDialog(short resID, 
							 XGPDialogProc proc, 
							 void *data)
{
#if OPT_XWIN
	_GDialogFlag = true;
#endif

	fDialog = XGDialog::Create(resID,proc,data);

#if OPT_XWIN
	_GDialogFlag = false;
#endif

	fDialog->fModal = true;			// mark as modal dialog
	fCloseRetVal = 0;
}

/*$	XGModalDialog::XGModalDialog
 *
 *		Construction
 */

XGModalDialog::XGModalDialog(const unsigned char *d, 
							 XGPDialogProc proc, 
							 void *data)
{
#if OPT_XWIN
	_GDialogFlag = true;
#endif

	fDialog = XGDialog::Create(d,proc,data);

#if OPT_XWIN
	_GDialogFlag = false;
#endif

	fDialog->fModal = true;			// mark as modal dialog
	fCloseRetVal = 0;
}

/*$	XGModalDialog::XGModalDialog
 *
 *		Construction
 */

XGModalDialog::XGModalDialog(short resID, 
							 const char *a, 
							 const char *b, 
							 const char *c, 
							 const char *d)
{
	const char *list[4];
	
	list[0] = a;
	list[1] = b;
	list[2] = c;
	list[3] = d;

#if OPT_XWIN
	_GDialogFlag = true;
#endif

	fDialog = XGDialog::Create(resID,XGDialog::AlertProc,(void *)list);
	

#if OPT_XWIN
	_GDialogFlag = false;
#endif

	fDialog->fModal = true;			// mark as modal dialog
	fCloseRetVal = 0;
}

/*$	XGModalDialog::XGModalDialog
 *
 *		Construction
 */

XGModalDialog::XGModalDialog(const unsigned char *data, 
							 const char *a, 
							 const char *b, 
							 const char *c, 
							 const char *d)
{
	const char *list[4];
	
	list[0] = a;
	list[1] = b;
	list[2] = c;
	list[3] = d;

#if OPT_XWIN
	_GDialogFlag = true;
#endif

	fDialog = XGDialog::Create(data,XGDialog::AlertProc,(void *)list);
	

#if OPT_XWIN
	_GDialogFlag = false;
#endif

	fDialog->fModal = true;			// mark as modal dialog
	fCloseRetVal = 0;
}

/*$	XGModalDialog::~XGModalDialog
 *
 *		Destruct this thing.
 */

XGModalDialog::~XGModalDialog()
{
	if (fDialog) delete fDialog;
}

/************************************************************************/
/*																		*/
/*	Run modal dialog one step											*/
/*																		*/
/************************************************************************/

/*$	XGModalDialog::ModalRun
 *
 *		This executes the event loop for this modal dialog once. If
 *	return value is true, this means the dialog was terminated
 */

bool XGModalDialog::ModalRun(long &retval)
{
	if (fDialog) XGAppCore::GetApplication()->RunOnce(fDialog);
	
	if (fDialog == NULL) {
		retval = fCloseRetVal;
		return true;
	}
	if (fDialog->fTerminate) {
		retval = fDialog->fTermValue;
		return true;
	} else {
		return false;
	}
}

/*$	XGModalDialog::CloseDialog
 *
 *		Close the dialog box in this modal dialog box prematurely.
 *	This will force the window to be closed, and return the value
 *	specified
 */

void XGModalDialog::CloseDialog(long retval)
{
	fCloseRetVal = retval;
	if (fDialog) {
		delete fDialog;
		fDialog = NULL;
	}
}
