/*
*	ppdata.c
*
*	Initialisation and event-handler for PPData - an intuition front-end
*	for crunch routines in powerpacker.library.
*
*	Martin W.Scott, 4/92.
*/
#include <exec/types.h>
#include <intuition/intuition.h>
#include <libraries/asl.h>
#include <libraries/ppbase.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/graphics.h>
#include <proto/intuition.h>

#include "display.h"		/* menu numbers, wintexts */
#include "prefs.h"		/* ppdata preferences definition */
#include "messages.h"		/* prototypes for message routines */
#include "crunch.h"		/* prototypes for file operations */

/* cback.o declarations */
#ifndef DEBUG
long _stack = 4000L;
char *_procname = "PPData v1.01";
long _priority = 0;
long _BackGroundIO = 1;
extern BPTR _Backstdout;
#endif

/* function prototypes */
static BOOL OpenLibs(void);
static void CloseLibs(void), ProcessIDCMP(struct MsgPort *);
static ULONG MenuPick(UWORD);
void _main(char *);

/* global pointers */
char version_str[] = "$VER: PPData v1.0\nby Martin W.Scott\n(Compiled : " __DATE__ ")";
#define ABOUT_MESSAGE &version_str[6]

struct IntuitionBase *IntuitionBase;
struct GfxBase *GfxBase;
struct AslBase *AslBase;
struct Library *GadToolsBase;
struct PPBase *PPBase;


static void CloseLibs()		/* close opened libraries */
{
	if (IntuitionBase) CloseLibrary(IntuitionBase);
	if (GfxBase) CloseLibrary(GfxBase);
	if (AslBase) CloseLibrary(AslBase);
	if (GadToolsBase) CloseLibrary(GadToolsBase);
	if (PPBase) CloseLibrary(PPBase);
}


static BOOL OpenLibs()			/* open necessary libraries */
{
	if ((IntuitionBase = (void *)OpenLibrary("intuition.library", 37L)) &&
	    (GfxBase = (void *)OpenLibrary("graphics.library", 0L)) &&
	    (AslBase = (void *)OpenLibrary(AslName, 37L)) &&
	    (GadToolsBase = OpenLibrary("gadtools.library", 37L)) &&
	    (PPBase = (void *)OpenLibrary(PPNAME, PPVERSION)))
		return TRUE;

	CloseLibs();
	return FALSE;
}


static ULONG ProcessMenuPick(UWORD num)
{
	struct MenuItem *item;		/* next item from extended-select */
	UWORD subnum;

	while (num != MENUNULL)
	{
		switch (MENUNUM(num))
		{
		case PROJECT:
			switch (ITEMNUM(num))
			{
			case LOAD:	LoadRequest(); break;
			case SAVE:	SaveRequest(); break;
			case BATCH:	BatchRequest(); break;
			case DELETE:	DeleteRequest(); break;
			case ABOUT:	Message(ABOUT_MESSAGE); break;
			case QUIT:	return CLOSEWINDOW;
			}
			break;
		case PREFS:
			subnum = SUBNUM(num);

			switch (ITEMNUM(num))
			{
			case COLOR:	prefs.color = subnum; break;
			case EFFICIENCY:prefs.efficiency = subnum; break;
			case SPEEDUP:	prefs.speedup = subnum; break;
			case SUFFIX:	prefs.suffix ^= 1;
					break;
			case OVERWRITE: prefs.overwrite ^= 1;
					break;
			case SAVEPREFS:	SavePrefs(); break;
			}
			break;
		case CONTROL:
			prefs.control =  ITEMNUM(num);
			break;

		} /* switch MENUNUM */

		item = ItemAddress(mainmenu, num);
		num = item->NextSelect;

	} /* while */

	return MENUPICK;	/* normal menu choice (not quit) */
}


static void ProcessIDCMP(struct MsgPort *mp)	/* handle window input */
{
	struct IntuiMessage *msg;		/* our message */
	ULONG class;				/* message class */
	UWORD menunum;				/* menu-number */

	for (;;)	/* until closewindow */
	{
		Wait (1 << mp->mp_SigBit);	/* wait for next message */

		while (msg = (struct IntuiMessage *)GetMsg(mp))
		{
			class = msg->Class;
			menunum = msg->Code;
			ReplyMsg((struct Message *)msg);  /* reply asap */

			if (class == MENUPICK)
				class = ProcessMenuPick(menunum);

			if (class == CLOSEWINDOW)
				if (IsBufferSaved() ||
				    Confirm("Work is unsaved.\nReally exit?"))
					return;
		} /* while */

	} /* for */
}


void _main(char *args)	/* ppdata: intuition interface for (de)crunching files */
{
	if (OpenLibs())
	{
		InitMessages();		/* set up message routines */

		if (OpenDisplay())	    /* open window etc. */
		{
			if (InitCrunch())	/* alloc asl requester etc. */
			{
				if (AllocPPDataMenus())
				{
					LoadPrefs();
					SetPPDataMenu(MAINMENU);
#ifndef DEBUG	/* cpr doesn't like cback.o */
					Close(_Backstdout);
					_Backstdout = NULL;
#endif
					ProcessIDCMP(window->UserPort);
					SetMenuStrip(window, NULL);
					FreePPDataMenus();
				}
				else Message("Can't create menus");
					
				FinishCrunch();
			}
			else Message("Can't initialize data");

			CloseDisplay();
		}
		else Message("Can't open display");

		CloseLibs();
	}
	else PutStr("Can't open libraries\n");
#ifndef DEBUG
	if (_Backstdout) Close(_Backstdout);
#endif
} /* _main */
