/**************************************************************/
/**  C++ code Copyright (C) Nick V. King <nking@eworld.com>  **/
/**************************************************************/

#include <utils_mac.h>

#include <headers_mac.h>
#include <ids_mac.h>

/**************************************************************/
/*  Routine to set state of menu items                        */
/**************************************************************/

void HiliteMenuItem(short menuID, short whichItem, Boolean state)
{
	if (state)
		EnableItem(GetMHandle(menuID), whichItem);
	else
		DisableItem(GetMHandle(menuID), whichItem);
}
		
/**************************************************************/
/*  Routine to pause until mouse button is pressed            */
/**************************************************************/

void TimeDelay(long time, Boolean buttonExits)
{
	long targetTicks;
	if (buttonExits)
	{
		while(Button());
		targetTicks = time+TickCount();
		while((TickCount() <= targetTicks) && !Button());
		FlushEvents(mDownMask+mUpMask, 0);
	}
	else
		Delay(time, &targetTicks);
}

/**************************************************************/
/*  Routine to concatenate two Str255s into the first         */
/**************************************************************/

void ConcatenateStrings(Str255 one, Str255 two)
{
	BlockMove((Ptr)&two[1], (Ptr)&one[1]+one[0], two[0]);
	one[0] += two[0];
}
	
/**************************************************************/
/*  Routine to play sound effect                              */
/**************************************************************/

void PlaySoundEffect(short soundID)
{
	extern Boolean soundEffects;
	
	if (soundEffects)
		SndPlay(0, GetResource('snd ', soundID), false);
}

/**************************************************************/
/*  Routine to get File Type from file's FSSpec               */
/**************************************************************/

short GetFileType(FSSpec* theFSSpec)
{
	FInfo fileInfo;
	FSpGetFInfo(theFSSpec, &fileInfo);
	if ((fileInfo.fdType >= 'VGR0') && (fileInfo.fdType <= 'VGR9'))
		return(fileInfo.fdType-'VGR0');
	else
		return(-1);
}

/**************************************************************/
/*  Routine to get file's OSType from File Type               */
/**************************************************************/

OSType GetFileOSType(short fileType)
{
	return('VGR0'+fileType);
}

/**************************************************************/
/*  Routine to guess File Type from file's FSSpec             */
/**************************************************************/

short GuessFileType(FSSpec* theFSSpec)
{
	Str255 name;
	BlockMove(theFSSpec->name, name, maxStringLength);
	char length = Length(name);
	
	if ((length >= 3) && (name[1] == 'p') && (name[2] == 'r') && (name[3] == 'a'))
		return(2);
	else if ((length == 8) && (name[6] == '.'))
		return(3);
	else if ((length > 8) && (name[6] == '.') && (name[9] == '.'))
		return(4);
	else
		return(1);
}

/**************************************************************/
/*  Routine to display string-indexed error messages          */
/**************************************************************/

void IndexedStringError(short type, short stringIndex)
{
	SetCursor(&qd.arrow);
	Str255 theError;
	GetIndString(theError, errorStringsID, stringIndex);
	ParamText(theError, "\p", "\p", "\p");
	Alert(type, 0L);
}

/**************************************************************/
/*  Routine to display variable-indexed error messages        */
/**************************************************************/

void IndexedVariableError(short variableReference, short errorTypeIndex)
{
	SetCursor(&qd.arrow);
	Str255 theVariableName, theError, theSolution;
	GetIndString(theVariableName, variableNameStringsID, variableReference);
	GetIndString(theError, miscStringsID, errorTypeIndex);
	GetIndString(theSolution, miscStringsID, errorTypeIndex+1);
	ParamText(theVariableName, theError, theSolution, "\p");
	Alert(270, 0L);
}

/**************************************************************/
/*  Routine to display string-indexed file error messages     */
/**************************************************************/

void IndexedFileError(Str255 theFilename, short operationStringIndex, OSErr theError)
{
	Str255 theOperation, theReason;
	GetIndString(theOperation, miscStringsID, operationStringIndex);
	switch(theError)
	{
	case nsvErr:
		GetIndString(theReason, miscStringsID, fileErrorStringIndex);
		break;
	case volOffLinErr:
		GetIndString(theReason, miscStringsID, fileErrorStringIndex+1);
		break;
	case ioErr:
		GetIndString(theReason, miscStringsID, fileErrorStringIndex+2);
		break;
	case fnfErr:
		GetIndString(theReason, miscStringsID, fileErrorStringIndex+3);
		break;
	case opWrErr:
		GetIndString(theReason, miscStringsID, fileErrorStringIndex+4);
		break;
	default:
		GetIndString(theReason, miscStringsID, fileErrorStringIndex+5);
		break;
	}
	ParamText(theFilename, theOperation, theReason, "\p");
	Alert(267, 0L);
}

/**************************************************************/
/*  Routine to set LaserWriter resolution                     */
/**************************************************************/

void SetPrintResolution(THPrint printRecord, short theResolution)
{
	extern short currentResolution;

	TSetRslBlk resInfo;
	resInfo.iOpCode = setRslOp;
	resInfo.hPrint = printRecord;
	resInfo.iXRsl = resInfo.iYRsl = theResolution;
	PrGeneral((Ptr)&resInfo);
	currentResolution = theResolution;
}

/**************************************************************/
/*  Routine to return number of copies to print               */
/**************************************************************/

int PrintCopies(THPrint printRecord)
{
	return((**printRecord).prJob.bJDocLoop == bDraftLoop ? (**printRecord).prJob.iCopies : 1);
}

/**************************************************************/
/*  Routine to set window title appropriately                 */
/**************************************************************/

void SetWindowTitle(WindowPtr theWindow, Str255 name, Str255 type, Boolean includeType)
{
	Str255 windowTitleString;
	BlockMove(name, windowTitleString, 256);
	if (includeType)
	{
		ConcatenateStrings(windowTitleString, "\p, ");
		ConcatenateStrings(windowTitleString, type);
	}
	SetWTitle(theWindow, windowTitleString);
}

/***********/
/**  End  **/
/***********/