#include "generic open.h"
#include "kant load-save.h"
#include "kant build files.h"
#include "environment.h"
#include "error.h"
#include "print meat.h"
#include "file utilities.h"
#include "graphics.h"
#include "window layer.h"
#include "program globals.h"

void OpenTheFile(FSSpec *myFSS)
{
	OSErr			oe;
	
	switch (GetFileType(myFSS))
	{
		case SAVE_TYPE:
			if (IndWindowExistsQQ(kMainWindow))
			{
				if (!CloseTheWindow(GetIndWindowPtr(kMainWindow)))
					HandleError(kMustCloseDocument, FALSE, TRUE);
			}
			
			if (!IndWindowExistsQQ(kMainWindow))
			{
				GetTheFile(myFSS);
				gNeedToOpenWindow=FALSE;
			}
			break;
		case BUILD_TYPE:
			if (IndWindowExistsQQ(kBuildWindow))
				CloseTheWindow(GetIndWindowPtr(kBuildWindow));
			oe=OpenTheModule(myFSS, FALSE, TRUE);
			if ((oe!=noErr) && (oe!=-1))	/* -1 = user cancel */
			{
				if (oe==-2)
					HandleError(kModuleTooLarge, FALSE, FALSE);
				else
					HandleError(kCantOpenModule, FALSE, FALSE);
			}
			if (oe==noErr)
				gNeedToOpenWindow=FALSE;
			break;
	}
}

void PrintTheFile(FSSpec *myFSS)
{
	WindowPtr		theWindow;
	OSErr			oe;
	
	switch (GetFileType(myFSS))
	{
		case SAVE_TYPE:
			if (IndWindowExistsQQ(kMainWindow))
			{
				if (!CloseTheWindow(GetIndWindowPtr(kMainWindow)))
					HandleError(kCloseDocBeforePrinting, FALSE, TRUE);
			}
			
			if (!IndWindowExistsQQ(kMainWindow))
			{
				GetTheFile(myFSS);
				theWindow=GetIndWindowPtr(kMainWindow);
				PrintText(GetWindowTE(theWindow));
				CloseTheWindow(theWindow);
			}
			break;
		case BUILD_TYPE:
			if (IndWindowExistsQQ(kBuildWindow))
				CloseTheWindow(GetIndWindowPtr(kBuildWindow));
			oe=OpenTheModule(myFSS, FALSE, TRUE);
			if ((oe!=noErr) && (oe!=-1))	/* -1 = user cancel */
			{
				if (oe==-2)
					HandleError(kModuleTooLarge, FALSE, FALSE);
				else
					HandleError(kCantOpenModule, FALSE, FALSE);
			}
			
			if (oe==noErr)
			{
				theWindow=GetIndWindowPtr(kBuildWindow);
				PrintText(GetWindowTE(theWindow));
				CloseTheWindow(theWindow);
			}
			break;
	}
}
