//==============================================================================
// KEYQUENCER EXTENSIONS SAMPLE - VERSION 1.2.1 - NOVEMBER 1994
// ©1994 Alessandro Levi Montalcini <LMontalcini@pmn.it>
// DonŐt forget to send me any cool extensions you create!
// This text looks best in monaco 9 font, 4 spaces per tab, no wrapping
//
// DOCUMENTATION IS AVAILABLE IN THE EXTENSION.H AND ACTION.H HEADERS

#include "Extension.h"
#include "Action.h"

Handle	gTextResource;

//==============================================================================
// THIS ROUTINE IS CALLED WHEN THE EXTENSION IS INVOKED BY A MACRO

short run(ParamsPtr params, MachineHandle mac, GluePtr glue)
{
	// This is where the extension does its work
	// cur heap zone = active app heap, cur res file = unknown
	// the extension file is closed, resources are not available
	
	StringPtr	string;
	long		length;
	short		index;
	Str255		message;
	
	message[0] = 0;														// clear message string
	for(index=0; index<params->paramsCount; ++index)					// scan all params
	{
		string = params->parameter[index];								// get nth parameter
		if(string[1]=='"' && string[0]>2)								// check for quotes
		{
			if(message[0]>0) return kTellTooManyParams;					// we only accept one parameter
			BlockMoveData(&string[2], &message[1], (long)string[0]-2);	// copy text, strip quotes
			message[0] = string[0]-2;
		}
		else if(EqualString("\pLOADED", string, false, true))			// check for "loaded" keyword
		{
			if(message[0]>0) return kTellTooManyParams;					// again, no more than 1 param
			if(gTextResource==0L)										// 'TEXT' 128 not found
			{
				(*glue->DisplayMessage)("\ptext resource not found at startup.", true);
				return kExtGenericError;
			}
			length = GetHandleSize(gTextResource);						// get text length
			if(length>255) length = 255;								// max length of our string
			BlockMoveData(*gTextResource, &message[1], length);			// copy to message string
			message[0] = (unsigned char)length;
		}
		else return kTellUnknownKeyword;								// unknown keyword found
	}
	if(message[0]==0) return kTellNotEnoughParams;						// didn't find required parameter
	
	(*glue->DisplayMessage)(message, false);							// display our message
	return kExtNoError;													// we're done
}

//==============================================================================
// THIS ROUTINE IS CALLED ONCE AT STARTUP TIME

short init(MachineHandle mac, GluePtr glue)
{
	// This is only called once at startup time
	// cur heap zone = sys heap, cur res file = extension res file
	// you may load (and detach) resources and initialize your data
	// glue is only available in recent versions, check for nil glue
	
	gTextResource = Get1Resource('TEXT', 128);							// load resource
	if(gTextResource)
	{
		HNoPurge(gTextResource);										// make it unpurgeable
		DetachResource(gTextResource);									// res file will be closed soon
	}
	if(glue) (*glue->ShowStartupIcon)(128);								// show our own icon (no glue was available in KQ 1.0)
	return kExtNoError;													// everything OK
}

//==============================================================================
