/*	XDialogs.h
 *
 *		This contains the declarations for both the modal and the
 *	modeless dialog boxes
 */

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

  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/

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

#ifndef PandaWave_XDIALOGS
#define PandaWave_XDIALOGS

#include <XConfig.h>
#include <XWindow.h>

/**@name Dialog Processing
 *
 *		This is the dialog processing classes. These handle creating,
 *	managing and running a modal or modeless dialog box.
 */
 
//@{

/************************************************************************/
/*																		*/
/*	Forwards															*/
/*																		*/
/************************************************************************/

class XGDialog;
class XGForm;

/************************************************************************/
/*																		*/
/*	Dialog Events														*/
/*																		*/
/************************************************************************/

/*$	KXGDialogMessage
 *
 *		Dialog messages. This is basically events sent by the dialog
 *	to the callback, rather than messages sent by different controls.
 *	The values of the arg parameter of this message indicates if the
 *	application was being brought up or shut down.
 *
 *		If the arg parameter is #KXGDMEndDialog#, the dialog box is
 *	being shut down. If #KXGDMInitDilaog#, this dialog box is being
 *	initialized. And if it's #KXGDMIdleDialog#, this is an idle event.
 */

#define KXGDialogMessage	'dmsg'

#define KXGDMEndDialog		1
#define KXGDMInitDialog		2
#define KXGDMIdleDialog		3

/************************************************************************/
/*																		*/
/*	Modal Dialog Declarations											*/
/*																		*/
/************************************************************************/

/**
 *	Procedure callback. This is the callback procedure definition that is
 *	used by the dialog management routines.
 *
 *	The parameters are:
 *
 *	#d# is a pointer to the XGDialog being run.
 *
 *	#data# is a pointer to the data pointer that was passed to the
 *	XGDialog class when this was started up.
 *
 *	#msg# is the event message pointer
 *
 *	#arg# is the event message argument
 *
 *	#parg# is the event message argument pointer.
 *
 *	If the event is not handled here, this should return 0.
 */

typedef long (*XGPDialogProc)(XGDialog *d,void *data,long msg,long arg,void *parg);

/*$	XGDialog
 *
 *		This is the dialog box declaration. This declaration is set
 *	up so I can be created either modal or modeless; modal dialogs
 *	should use the modal dialog function setup given below
 */

class XGDialog : public XGWindow {
	public:
								XGDialog(XGSWindowInitRecord &, XGForm *);
								XGDialog(XGArgStream &, XGForm *);
		virtual					~XGDialog();
		bool					CloseWindow();
		
		enum {
			kClassID = 'dlog'
		};
		
		/*
		 *	Modeless creation/destruction
		 */
		
		static XGDialog			*Create(short,XGPDialogProc, void *);
		static XGDialog			*Create(const unsigned char *,
										XGPDialogProc, 
										void *);
		
		/*
		 *	Events
		 */
		
		void					DoDrawView(XRect);

		/*
		 *	Modal creation/destruction
		 */
		
		static long				ModalDialog(short,XGPDialogProc, void *);
		static long				ModalDialog(const unsigned char *,
											XGPDialogProc, 
											void *);
		static long				ModalAlert(short, 
										   const char *,
										   const char *, 
										   const char *, 
										   const char *);
		static long				ModalAlert(const unsigned char *, 
										   const char *, 
										   const char *, 
										   const char *, 
										   const char *);
										   
		/*
		 *	Staged modal construction/destruction
		 */
		
		
		
		/*
		 *	End the dialog
		 */
		
		void					EndDialog(long v);
		
		/*
		 *	Message management
		 */
		
		DECLAREDISPATCH
		long					Dispatch(long,long,void *);
		long					DoKey(long,void *);
		
#if OPT_MACOS
		long					DoMenuKey(long,void *);
#endif

		/*
		 *	Contents management
		 */
		
		void					GetText(short,short,char *);
		void					SetText(short,const char *);
		long					GetValue(short);
		void					SetValue(short,long);
		
		void					SetDefaultOK(short);
		void					SetDefaultCancel(short);

	private:
		static XGDialog			*CreateInit(XGDialog *, XGPDialogProc, void *);
		static long				ModalRun(XGDialog *);
		
		static void				ModifyText(char *text, const char **list);
		static long				AlertProc(XGDialog *, void *, long, long, void *);
		long					SendMessage(long,long,void *);

		long					fTermValue;
		bool					fTerminate;
		bool					fModal;
		void					*fDataPtr;
		XGPDialogProc			fDialogProc;
		
		short					fOK;
		short					fCancel;
		
	friend class XGModalDialog;
};

/*$	XGModalDialog
 *
 *		This allows me to construct a modal dialog but defer the
 *	event loop processing to a callback routine, for handling things
 *	like progress dialogs. This is a stack-based object which allows
 *	us to quickly create a dialog or alert.
 */

class XGModalDialog {
	public:
								XGModalDialog(short,XGPDialogProc, void *);
								XGModalDialog(const unsigned char *,
											  XGPDialogProc, 
											  void *);
								XGModalDialog(short, 
										      const char *,
										      const char *, 
										      const char *, 
										      const char *);
								XGModalDialog(const unsigned char *, 
										      const char *, 
										      const char *, 
										      const char *, 
										      const char *);

								~XGModalDialog();

		bool					ModalRun(long &retval);
		void					CloseDialog(long retval = 0);
		
		/*$	XGModalDialog::GetDialog
		 *
		 *		This returns a pointer to the XGDialog which this
		 *	class created to process the modal dialog.
		 */
		
		XGDialog				*GetDialog()
									{
										return fDialog;
									}
	private:
		long					fCloseRetVal;
		XGDialog				*fDialog;
};		


//@}

#endif // PandaWave_XDIALOGS
