/*
 *  prefs.c - entry point for the preferences program
 *  Copyright (C) 1997 Aki Laukkanen
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software Foundation,
 *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <string.h>
#include "prefs.h"
#include "macros.h"
#include "turboevdprefs_rev.h"

/*
 * prototypes
 */

int main(int ac, char** av);
static BOOL initGui(void);
static BOOL initClasses(void);
static void exitGui(void);
static void exitClasses(void);

/*
 * globals
 */

struct Library* MUIMasterBase = NULL;
struct Library* UtilityBase = NULL;
struct IntuitionBase* IntuitionBase = NULL;

static Object* appMain;
static Object* winPrefs;

struct MUI_CustomClass* CL_prefsWindow;
struct MUI_CustomClass* CL_evdPanel;
struct MUI_CustomClass* CL_paletteWindow;

static char* version = VERSTAG;

#if defined(__SASC) || defined(__GNUC__)
LONG __stack = 10000;
#endif

/*
 * functions
 */

static BOOL
initGui(void)
{
	appMain = ApplicationObject,

		MUIA_Application_Title,         "TurboEVDPrefs",
		MUIA_Application_Version,       VERS" ("DATE")",
		MUIA_Application_Copyright,     "Copyright (C) 1997 Aki Laukkanen (amlaukka@cc.helsinki.fi)\n\n"
										"This program is free software; you can redistribute it and/or modify\n"
										"it under the terms of the GNU General Public License as published by\n"
										"the Free Software Foundation; either version 2 of the License, or\n"
										"(at your option) any later version.\n\n"

										"This program is distributed in the hope that it will be useful,\n"
										"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
										"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
										"GNU General Public License for more details.\n\n"

										"You should have received a copy of the GNU General Public License\n"
										"along with this program; if not, write to the Free Software Foundation,\n"
										"Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.",

		MUIA_Application_Description,   "Preferences editor for TurboEVD",
		MUIA_Application_Base,          "TEVDPREFS",
		MUIA_Application_Window, winPrefs = NewObject(CL_prefsWindow->mcc_Class, NULL, TAG_DONE),
	End;

	if (!appMain)
		return FALSE;

	DoMethod(winPrefs, MUIM_Notify,
			MUIA_Window_CloseRequest, TRUE, appMain, 2,
			MUIM_Application_ReturnID, MUIV_Application_ReturnID_Quit);

	set(winPrefs, MUIA_Window_Open, (ULONG)TRUE);

	return TRUE;
}

static BOOL
initClasses(void)
{
	CL_prefsWindow = MUI_CreateCustomClass(NULL,
										MUIC_Window, NULL,
										sizeof (struct prefsWindowData),
										prefsWindow_Dispatcher);

	CL_evdPanel = MUI_CreateCustomClass(NULL,
										MUIC_Group, NULL,
										sizeof (struct evdPanelData),
										evdPanel_Dispatcher);

	CL_paletteWindow = MUI_CreateCustomClass(NULL,
										MUIC_Window, NULL,
										sizeof (struct paletteWindowData),
										paletteWindow_Dispatcher);

	if (!CL_prefsWindow || !CL_evdPanel || !CL_paletteWindow )
		return FALSE;

	return TRUE;
}

static void
exitGui(void)
{
	if (appMain)
		MUI_DisposeObject(appMain);
}

static void
exitClasses(void)
{
	if (CL_prefsWindow)
		MUI_DeleteCustomClass(CL_prefsWindow);

	if (CL_evdPanel)
		MUI_DeleteCustomClass(CL_evdPanel);

	if (CL_paletteWindow)
		MUI_DeleteCustomClass(CL_paletteWindow);

}

int
main(int ac, char** av)
{
	ULONG sigs = 0;

	UtilityBase = OpenLibrary("utility.library", 37ul);
	MUIMasterBase = OpenLibrary(MUIMASTER_NAME, MUIMASTER_VMIN);
	IntuitionBase = (struct IntuitionBase* )OpenLibrary("intuition.library", 37ul);

	if (UtilityBase && MUIMasterBase && IntuitionBase)
	{
		if (initClasses())
		{
			if (initGui())
			{
				while (DoMethod(appMain, MUIM_Application_NewInput, &sigs) != MUIV_Application_ReturnID_Quit)
				{
					if (sigs)
					{
						sigs = Wait(sigs | SIGBREAKF_CTRL_C);
						if (sigs & SIGBREAKF_CTRL_C)
							break;
					}
				}
			}
		}
	}

	exitGui();
	exitClasses();

	if (MUIMasterBase)
		CloseLibrary(MUIMasterBase);
	if (UtilityBase)
		CloseLibrary(UtilityBase);

	return 0;
}
