/*	Error.cpp
 *
 *		This contains the declaration of the procedures 'Error',
 *	'Warning', and 'Caution'
 */

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

  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>			// must be first to optimize compile on MSVC++
#include <stdio.h>
#include <XError.h>
#include <XDialog.h>

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

static unsigned char GMessageDialog[] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5A, /* 00000000: .......Z */
    0x00, 0x00, 0x00, 0x4A, 0x64, 0x6C, 0x6F, 0x67, /* 00000008: ...Jdlog */
    0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, /* 00000010: ........ */
    0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000018: ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000020: ........ */
    0x00, 0x00, 0x00, 0x00, 0x01, 0x4A, 0x00, 0x00, /* 00000028: .....J.. */
    0x00, 0x76, 0x00, 0x00, 0x01, 0x4A, 0x00, 0x00, /* 00000030: .v...J.. */
    0x00, 0x76, 0x00, 0x00, 0x01, 0x4A, 0x00, 0x00, /* 00000038: .v...J.. */
    0x00, 0x76, 0x00, 0x00, 0x01, 0x4A, 0x00, 0x00, /* 00000040: .v...J.. */
    0x00, 0x76, 0x02, 0x5E, 0x32, 0x00, 0x00, 0x00, /* 00000048: .v.^2... */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, /* 00000050: .....d.. */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x00, 0x00, /* 00000058: .....B.. */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x32, 0x69, 0x63, /* 00000060: .....2ic */
    0x6F, 0x6E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000068: on...... */
    0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, /* 00000070: ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, /* 00000078: ........ */
    0x00, 0x10, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, /* 00000080: .....8.. */
    0x00, 0x30, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, /* 00000088: .0...... */
    0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x20, /* 00000090: ........ */
    0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x3E, /* 00000098: .......> */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2E, /* 000000A0: ........ */
    0x74, 0x65, 0x78, 0x74, 0x00, 0x00, 0x00, 0x00, /* 000000A8: text.... */
    0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, /* 000000B0: ........ */
    0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, /* 000000B8: .......P */
    0x00, 0x00, 0x00, 0x0E, 0xFF, 0xFF, 0xFF, 0xF2, /* 000000C0: ........ */
    0x00, 0x00, 0x00, 0x1E, 0xFF, 0xFF, 0xFF, 0xFF, /* 000000C8: ........ */
    0x00, 0x00, 0x02, 0x5E, 0x30, 0x00, 0x00, 0x00, /* 000000D0: ...^0... */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, /* 000000D8: .....>.. */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x2E, 0x74, 0x65, /* 000000E0: ......te */
    0x78, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 000000E8: xt...... */
    0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, /* 000000F0: ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x5A, 0x00, 0x00, /* 000000F8: .....Z.. */
    0x00, 0x1E, 0xFF, 0xFF, 0xFF, 0xF2, 0x00, 0x00, /* 00000100: ........ */
    0x00, 0x50, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, /* 00000108: .P...... */
    0x02, 0x5E, 0x31, 0x00, 0x00, 0x00, 0x00, 0x01, /* 00000110: .^1..... */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 00000118: ........ */
    0x00, 0x00, 0x00, 0x32, 0x70, 0x62, 0x74, 0x6E, /* 00000120: ...2pbtn */
    0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, /* 00000128: ...d.... */
    0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, /* 00000130: ........ */
    0x00, 0x00, 0x00, 0xFA, 0x00, 0x00, 0x00, 0x56, /* 00000138: .......V */
    0x00, 0x00, 0x01, 0x3E, 0x00, 0x00, 0x00, 0x70, /* 00000140: ...>...p */
    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, /* 00000148: ........ */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x4F, /* 00000150: .......O */
    0x4B, 0x00                                      /* 00000158: K.       */
}; /* _R56526573_0084 */

/************************************************************************/
/*																		*/
/*	Error/Caution/Warning routines										*/
/*																		*/
/************************************************************************/

/**@name Support--Error processing routines
 *
 *		These routines provide a very simple interface for handling errors.
 *	Each of these routines present an error message right away, rather
 *	than the usual mechanism used by YAAF. This is useful if you want to
 *	present an error or warning but don't want to unwind the stack.
 */
 
//@{

/*$	Error
 *
 *		This puts up an error alert and returns immediately, rather
 *	than throwing an exception.
 */

void Error(unsigned long msg,...)
{
	va_list vlist;
	
	va_start(vlist,msg);
	XGPostError e(msg,vlist);	// construct with correct error
	va_end(vlist);
	
	e.DisplayError();
}


/*$	Caution
 *
 *		This puts up a 'caution' alert which has two buttons: 'Ok'
 *	and 'Cancel.' If 'Cancel' is pressed, this will return true
 */

bool Caution(unsigned long msg, ...)
{
	va_list vlist;
	
	va_start(vlist,msg);
	XGPostError e(msg,vlist);
	va_end(vlist);
	e.SetAlertType(KXGAlertCaution);
	
	e.DisplayError();
	if (e.GetAlertResult()) return true;
	else return false;
}

/*$	Warning
 *
 *		This puts up a 'warning' alert, similar to the error alert
 *	box. This returns immediately after the box is closed.
 */

void Warning(unsigned long msg, ...)
{
	va_list vlist;
	
	va_start(vlist,msg);
	XGPostError e(msg,vlist);
	va_end(vlist);
	e.SetAlertType(KXGAlertWarning);
	
	e.DisplayError();
}

/*$	Message
 *
 *		Even weaker than warning, this doesn't even go through the
 *	post error mechanism.
 */

void Message(const char *title, const char *smsg, const char *lmsg, ...)
{
	va_list vlist;
	char bufs[256];
	char bufl[256];
	
	va_start(vlist,lmsg);
	vsprintf(bufs,smsg,vlist);
	va_end(vlist);
	
	va_start(vlist,lmsg);
	vsprintf(bufl,lmsg,vlist);
	va_end(vlist);
	
	XGDialog::ModalAlert(GMessageDialog,bufs,bufl,title,NULL);
}

/*$	Message
 *
 *		Even weaker than warning, this doesn't even go through the
 *	post error mechanism.
 */

void Message(const char *title, unsigned long msg, ...)
{
	va_list vlist;
	char bufs[256];
	char bufl[256];
	
	va_start(vlist,msg);
	XGPostError e(msg,vlist);
	va_end(vlist);
	
	e.GetShortError(bufs);
	e.GetLongError(bufl);
	
	XGDialog::ModalAlert(GMessageDialog,bufs,bufl,title,NULL);
}

//@}
