#include "maker.h"
#include "proto.h"
#include "global.h"

#include <exec/memory.h>

#include <proto/intuition.h>
#include <proto/graphics.h>
#include <proto/asl.h>
#include <proto/exec.h>
#include <proto/gadtools.h>
#include <proto/layers.h>
#include <proto/diskfont.h>

#include <stdio.h>
#include <stdlib.h>

/* ===================================================================================== */

void init()
{
	struct Node	*nodePtr;
	USHORT		i;
	

	//	Check for OS 2.0
	
	if (	(*((struct Library **) 4L))->lib_Version < 36)
		quit("Sorry, but I can only work under AmigaOS Version 2.0\n");

	//	Open all the libraries
	
	IntuitionBase = OpenLibrary("intuition.library", 36L);
	if (!IntuitionBase)
		quit("can't open intuition library\n");
	
	GfxBase = OpenLibrary("graphics.library", 36L);
	if (!GfxBase)
		quit("can't open graphics library\n");
	
	GadToolsBase = OpenLibrary("gadtools.library", 36L);
	if (!GadToolsBase)
		quit("can't open gadtools library\n");
	
	AslBase = OpenLibrary("asl.library", 36L);
	if (!AslBase)
		quit("can't open asl library\n");
	
	LayersBase = OpenLibrary("layers.library", 36L);
	if (!LayersBase)
		quit("can't open layers library\n");
	
	DiskfontBase = OpenLibrary("diskfont.library", 36L);
	if (!DiskfontBase)
		quit("can't open diskfont library\n");
	
	//	Initialize some variables
	
	NewList(&gDefaultList);
	i = 0;
	while (gLabelText[i])
	{
		nodePtr = (struct Node *) AllocRemember(&gRemKey, sizeof(struct Node), MEMF_CLEAR);
		nodePtr->ln_Name = gLabelText[i++];
		AddTail(&gDefaultList, nodePtr);
	}
	
	gEditMode = TRUE;
	gWindSizeable = TRUE;
	
	gFont = OpenDiskFont(&topaz8);

	//	Get a lock on the public screen.  We will keep this lock for the duration of program
	// execution in case we need to close & reopen the window (to enable/disable sizing),
	// or to open other windows
	
	if (!(gPubScreen = LockPubScreen(0L)))
		quit("LockPubScreen failed?\n");

	if (!(gVI = GetVisualInfo(gPubScreen, TAG_DONE)))
		quit("GetVisualInfo failed\n");

	gWindPtr = 
			OpenWindowTags(0L,
								WA_Left,		20,
								WA_Top,		20,
								WA_Width,	200,
								WA_Height,	150,
								WA_IDCMP,	WIND_IDCMP,
								WA_Flags,	WIND_FLAG | (gWindSizeable ? WFLG_SIZEGADGET : 0),
								WA_Title,	gWindTitle,
								WA_PubScreen, gPubScreen,
								WA_MinWidth,	70,
								WA_MinHeight,	30,
								WA_MaxWidth,	-1L,
								WA_MaxHeight,	-1L,
								TAG_DONE);
								
	if (!gWindPtr)
		quit("failed to open a window\n");
								
	SetClip();
	SetFont(gWindPtr->RPort, gFont);
	
	//	Open the info window
	
	gInfoWindPtr = 
			OpenWindowTags(0L,
								WA_Left,		300,
								WA_Top,		100,
								WA_Width,	120,
								WA_Height,	65,
								WA_IDCMP,	0L,
								WA_Flags,	WFLG_DRAGBAR | WFLG_DEPTHGADGET |
												WFLG_SMART_REFRESH |	WFLG_NOCAREREFRESH,
								WA_Title,	"Info",
								WA_PubScreen, gPubScreen,
								TAG_DONE);
								
	if (!gInfoWindPtr)
		quit("failed to open the info window\n");
	
	SetAPen(gInfoWindPtr->RPort, 1);
	SetFont(gInfoWindPtr->RPort, gFont);
		
	//	And the menus...
	
	if (!(gMenuPtr = CreateMenus(gNewMenu, GTMN_FrontPen, 2, TAG_DONE)))
		quit("CreateMenus() failed!\n");

	if (!LayoutMenus(gMenuPtr, gVI, TAG_DONE))
		quit("LayoutMenus() failed!\n");

	SetMenuStrip(gWindPtr, gMenuPtr);

	//	CreateContext() is the start of an extended relationship with the Gadtools library
	
	if (!CreateContext(&gGadgetContext))
		quit("CreateContext() failed!\n");
	
	gFileRequest = AllocFileRequest();
		
	ReDraw();
}

/* ===================================================================================== */

void quit(char *note)
{
	LinkNode		*nodePtr = gObjList,
					*nextNode;
	
	if (gRemKey)
		FreeRemember(&gRemKey, TRUE);
		
	while (gObjList)
		DisposeObj( gObjList );
/*
	while (nodePtr)
	{
		nextNode = nodePtr->next;

		DisposeObj( nodePtr );
		nodePtr = nextNode;
	}
*/
	
	if (gWindPtr)
	{
		InstallClipRegion(gWindPtr->WLayer, 0L);
		ClearMenuStrip(gWindPtr);
		CloseWindow(gWindPtr);
	}
	
	if (gInfoWindPtr)
		CloseWindow(gInfoWindPtr);
		
	if (gFont)
		CloseFont(gFont);

	UnlockPubScreen(0L, gPubScreen);
	
	if (gFileRequest)
		FreeFileRequest(gFileRequest);
				
	if (gGadgetContext)
		FreeGadgets(gGadgetContext);
	
	if (gMenuPtr)
		FreeMenus(gMenuPtr);
	
	if (gVI)
		FreeVisualInfo(gVI);
		
	if (IntuitionBase)
		CloseLibrary(IntuitionBase);

	if (GfxBase)
		CloseLibrary(GfxBase);

	if (GadToolsBase)
		CloseLibrary(GadToolsBase);

	if (AslBase)
		CloseLibrary(AslBase);

	if (LayersBase)
		CloseLibrary(LayersBase);

	if (DiskfontBase)
		CloseLibrary(DiskfontBase);

	if (note)	
		printf(note);
		
	exit(0);
}

/* ===================================================================================== */

