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

/*  YAAF - Yet another application framework
 *  Copyright (C) 1997, 1998 William Edward Woody
 *  
 *  This library is free software; you can redistribute it
 *  and/or modify it under the terms of the GNU Library
 *  General Public License as published by the Free Software
 *  Foundation; either version 2 of the License, or any
 *  later version.
 *  
 *  This library is distributed in the hope that it will be
 *  useful, but WITHOUT ANY WARRANTY; without even the implied
 *  warranty of MERCHANTABIILITY or FITNESS FOR A PARTICULAR
 *  PURPOSE. See the GNU Library General Public License for
 *  more details.
 *  
 *  You should have received a copy of the GNU Library General
 *  Public License along with this library; if not, write to the
 *  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 *  
 *  To contact the author, either e-mail me at woody@pandawave.com, 
 *	or write to
 *  
 *          William Edward Woody
 *          In Phase Consulting
 *          1545 Ard Eevin Avenue
 *          Glendale, CA 91202
 *
 *	Or visit us on the web at http://www.pandawave.com
 */

#include <XConfig.h>			// must be first to optimize compile on MSVC++
#include <XError.h>
#include <XApplication.h>
#include <XWindow.h>

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

#if OPT_WINOS == 1

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

HWND		_GMainWindow;
HWND		_GMDIWindow;

XGWindow	*_GWindow;
XGView		*_GView;

char		_GPopClass[32];
char		_GWindowClass[32];
char		_GMainClass[32];
char		_GFrameClass[32];
char		_GControlClass[32];
char		_GDlogClass[32];
char		_GCopyrightClass[32];

#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;
	
	/*
	 *	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(_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
	 */
	
	catch (XGError &x) {
		x.DisplayError();
	}

	/* Handle gcc bug with exception handling */
	catch (XGPostError &x) {
		x.DisplayError();
	}
	catch (XGAbort &x) {
		x.DisplayError();
	}
	
	return 0;
}

#endif
