/*	AppMain.cpp
 *
 *		This is where the main loop is hidden.
 */

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

  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 <XError.h>
#include <XApplication.h>
#include <XWindow.h>

#if OPT_XWIN
#include <stdio.h>
#endif

/************************************************************************/
/*																		*/
/*	Internal globals													*/
/*																		*/
/************************************************************************/

#if OPT_WINOS == 1

HINSTANCE	_GInstance;
HINSTANCE	_GPrevInstance;
LPSTR		_GArgString;
int			_GArgShow;

HWND		_GMainWindow;
HWND		_GMDIWindow;
HWND		_GToolWindow;

XGWindow	*_GWindow;
XGView		*_GView;

char		_GPopClass[32];
char		_GWindowClass[32];
char		_GToolClass[32];
char		_GMainClass[32];
char		_GFrameClass[32];
char		_GControlClass[32];
char		_GDlogClass[32];
char		_GCopyrightClass[32];
XGView		*_GToolView;

#endif

#if OPT_XWIN == 1

int			_GArgC;
char		**_GArgV;

Display		*_GDisplay;
bool		_GDialogFlag = false;

#endif

/************************************************************************/
/*																		*/
/*	Main entry point													*/
/*																		*/
/************************************************************************/

#if (OPT_MACOS == 1) || (OPT_WINOS == 1)

/*	main
 *
 *		Where the Macintosh version starts
 */

int main()
{
	XGAppCore *a;
	
	try {
		/*
		 *	Call the startup routine
		 */
		
		a = StartApplication();
		
		/*
		 *	Run the event loop
		 */
		
		if (a) {
			a->Run();
			delete a;
		} else {
			throw XGPostError(KXGAppCoreError);
		}
	}
	
	/*
	 *	Catch random errors.
	 *
	 *		<<Kludge Alert>>
	 *
	 *		If I get here, I don't have an XGAppCore object in which
	 *	to run my application event loop. This is a bad idea, by the way,
	 *	as I need that in order to manage the state of my event loop.
	 *
	 *		So if I get here, I create a <<temporary>> application
	 *	context. I assume I get here only because there was a problem
	 *	initializing the XGAppCore object, meaning that this will be
	 *	the first successfully created XGAppCore object.
	 *
	 *		I don't need to run this, by the way, as the initialization
	 *	of my app object is self-contained in the constructor.
	 */
	
	catch (XGError &x) {
		XGAppSingleWindow app;
		
		x.DisplayError();
	}
	return 0;
}

#endif

#if OPT_WINOS == 1

int WINAPI WinMain(HINSTANCE h, HINSTANCE prev, LPSTR cmd, int show)
{
	/*
	 *	Store the startup variables globally
	 */
	
	_GInstance = h;
	_GPrevInstance = prev;
	_GArgString = cmd;
	_GArgShow = show;
	
	_GMainWindow = NULL;
	_GMDIWindow = NULL;
	_GToolWindow = NULL;
	_GToolView = NULL;
	
	/*
	 *	Generate the class names. I use the module handle to give
	 *	me a unique identifier for me
	 */
	
	wsprintf(_GWindowClass,"_XGWindow_%08lX",(long)GetModuleHandle(NULL));
	wsprintf(_GPopClass,"_XGPop_%08lX",(long)GetModuleHandle(NULL));
	wsprintf(_GMainClass,"_XGMain_%08lX",(long)GetModuleHandle(NULL));
	wsprintf(_GToolClass,"_XGTool_%08lX",(long)GetModuleHandle(NULL));
	wsprintf(_GFrameClass,"_XGFrame_%08lX",(long)GetModuleHandle(NULL));
	wsprintf(_GControlClass,"_XGControl_%08lX",(long)GetModuleHandle(NULL));
	wsprintf(_GDlogClass,"_XGDialog_%08lX",(long)GetModuleHandle(NULL));
	wsprintf(_GCopyrightClass,"_XGC_%08lX",(long)GetModuleHandle(NULL));
	
	/*
	 *	Call the main function above
	 */
	
	return main();
}

#endif

#if OPT_XWIN == 1

/*	main
 *
 *		Where the X application starts
 */

int main(int argc, char **argv)
{
	_GArgC = argc;
	_GArgV = argv;
	XGAppCore *a;
	
	try {
		/*
		 *	Call the startup routine
		 */
		
		a = StartApplication();
		
		/*
		 *	Run the event loop
		 */
		
		if (a) {
			a->Run();
			delete a;
		} else {
			throw XGPostError(KXGAppCoreError);
		}
	}
	
	/*
	 *	Catch random errors. Print to console rather than
	 *	displaying them as there is a chance there is no
	 *	windowing engine under me...
	 */
	
	/* Handle gcc bug with exception handling */
	catch (XGPostError &x) {
		char serror[256];
		char lerror[256];
		
		x.GetShortError(serror);
		x.GetLongError(lerror);
		printf("Fatal error caught:\n");
		printf("  %s\n\n%s\n",serror,lerror);
	}
	catch (XGAbort &x) {
		printf("YAAF abort error caught: application terminated\n");
	}
	catch (...) {
		printf("Unknown YAAF exception caught: application terminated\n");
	}
	
	return 0;
}

#endif
