/*===============================
|	SingleLineString.h
|
|	Blugs 1.1 simple content handler for one-line Pascal strings
|	www.blugs.com
|
|	Copyright © 1998-2001 Brian Hall
|	You may use these routines in your software without restriction.
|
|===============================*/
#include "SingleLineString.h"
#include "BlugsAppearanceExtras.h"

#ifndef __BALLOONS__
#include <Balloons.h>
#endif

#ifndef __TEXTUTILS__
#include <TextUtils.h>
#endif

#ifndef __TOOLUTILS__
#include <ToolUtils.h>
#endif

#ifndef __FIXMATH__
#include <FixMath.h>
#endif

#ifndef __FONTS__
#include <Fonts.h>
#endif


static void			local_SLSSet( OSType flavor, Ptr data, UInt32 dataSize, SLSHdl str );
static void			local_SLSGet( BLHandlerParamPtr param, SLSHdl str );
static SInt32		local_SLSSort( BLHandlerParamPtr pp, SLSHdl stringH1 );
static SInt32		local_SLSSearch( BLHandlerParamPtr pp, SLSHdl str );




pascal UInt32 SingleLineStringHandler(		BLMessage			message,
											BLHandlerParamPtr	param		)
{
	UInt32		answer = 0;
	SLSHdl		dataH;
	
	if (param) dataH = (SLSHdl)param->ioStorage;
	switch (message)
	{
		case blHandlerInitMsg:
		answer = blWantsSort;
		break;
		
		case blCellDrawMsg:
		TextFace( (*dataH)->style );
		BLDrawStringVerticallyCentered( (*dataH)->string, &param->inRect, param->inIndent );
		TextFace( 0 );
		break;
		
		case blCellInitMsg:
		param->ioStorage = NewHandleClear( sizeof(SLSRec) );
		break;
		
		case blCellSetDataMsg:
		local_SLSSet( param->ioDataFlavor, param->ioDataBuffer, param->ioDataSize, dataH );
		break;
		
		case blCellGetDataMsg:
		local_SLSGet( param, dataH );
		break;
		
		case blCountImportFlavorsMsg:
		answer = kSingleLineStringNImportFlavors;
		break;
		
		case blCountExportFlavorsMsg:
		answer = kSingleLineStringNExportFlavors;
		break;
		
		case blGetNthImportFlavorInfoMsg:
		switch (param->inIndex)
		{
			case 1:
			param->ioDataSize = 255;
			param->ioDataFlavor = slsTextFlavor;
			break;
			
			case 2:
			param->ioDataSize = sizeof(HMStringResType);
			param->ioDataFlavor = slsStringListResourceFlavor;
			break;
			
			case 3:
			param->ioDataSize = sizeof(Style);
			param->ioDataFlavor = slsStyleFlavor;
			break;
			
			default:
			param->ioDataSize = 0;
			break;
		}
		break;
		
		case blGetNthExportFlavorInfoMsg:
		switch (param->inIndex)
		{
			case 1: // TEXT
			param->ioDataSize = 255;
			param->ioDataFlavor = slsTextFlavor;
			break;
			
			default:
			param->ioDataSize = 0;
			break;
		}
		break;
		
		case blCellDeinitMsg:
		if (dataH) DisposeHandle( (Handle)dataH );
		break;
		
		case blCellSortMsg:
		answer = local_SLSSort( param, dataH );
		break;
		
		case blCellSearchMsg:
		answer = local_SLSSearch( param, dataH );
		break;
		
		default:
		break;
	}
	return answer;
}

//===============================
static void local_SLSSet( OSType flavor, Ptr data, UInt32 dataSize, SLSHdl str )
{
	SInt16		resID;
	SInt16		strIndex;

	if (str)
	{
		switch (flavor)
		{
			Size	neededSize;
			
			case slsStringListResourceFlavor:
			{
				Str255				string;
				
				//resID = HiWord(*(SInt32*)data);
				//strIndex = LoWord(*(SInt32*)data);
				resID = ((HMStringResType*)data)->hmmResID;
				strIndex = ((HMStringResType*)data)->hmmIndex;
				// Make sure index is not zero.
				if (strIndex)
				{
					GetIndString( string, resID, strIndex );
					neededSize = sizeof(SLSRec) + string[0];
					SetHandleSize( (Handle)str, neededSize );
					if (!MemError())
						BlockMoveData( &string[0], &((*str)->string[0]), string[0] + 1 );
				}
			}
			break;
			
			case slsTextFlavor:
			neededSize = sizeof(SLSRec) + dataSize;
			SetHandleSize( (Handle)str, neededSize );
			if (!MemError())
			{
				BlockMoveData( data, &((*str)->string[1]), dataSize );
				(*str)->string[0] = dataSize;
			}
			break;
			
			case slsStyleFlavor:
			(*str)->style = *(Style*)data;
			break;
		}
	}
}

//===============================
static void local_SLSGet( BLHandlerParamPtr param, SLSHdl str )
{
	if (str)
	{
		switch (param->ioDataFlavor)
		{
			case slsTextFlavor:
			if (param->ioDataBuffer)
				BlockMoveData( &((*str)->string[1]), param->ioDataBuffer, (*str)->string[0] );
			param->ioDataSize = (*str)->string[0];
			break;
		}
	}
}

//===============================
static SInt32 local_SLSSort( BLHandlerParamPtr pp, SLSHdl stringH1 )
{
	SInt16		result;
	SInt32		longResult = 0;
	SLSHdl		stringH2;
	
	stringH2 = (SLSHdl)(pp->inStorage2);
	if (stringH1 && stringH2)
	{
		result = RelString( (*stringH1)->string, (*stringH2)->string, false, false );
		longResult = result;
	}
	return longResult;
}

//===============================
static SInt32 local_SLSSearch( BLHandlerParamPtr pp, SLSHdl storage )
{
	SInt16		result;
	SInt32		longResult = 0;
	
	if (storage && pp->ioDataBuffer)
	{
		result = CompareText( &((*storage)->string[1]), pp->ioDataBuffer,
								(*storage)->string[0], pp->ioDataSize, nil );
		longResult = result;
	}
	return longResult;
}

