#include "SingleLineCFString.h"
#include "BlugsAppearanceExtras.h"


static void			local_SLSSet( OSType flavor, Ptr data, UInt32 dataSize, CFStringRef* ioStr );
static void			local_SLSGet( BLHandlerParamPtr param, CFStringRef str );
static void			local_SLSDraw( CFStringRef inString, ThemeFontID inFont,
									const Rect* inRect, UInt16 inIndent );


pascal UInt32 SingleLineCFStringHandler(	BLMessage			message,
											BLHandlerParamPtr	param		)
{
	UInt32			answer = 0;
	CFStringRef		string;
	
	string = (CFStringRef)param->ioStorage;
	switch (message)
	{
		case blCellDrawMsg:
		if (string) local_SLSDraw( string, kThemeCurrentPortFont, 
									&param->inRect, param->inIndent );
		break;
		
		case blCellSetDataMsg:
		local_SLSSet( param->ioDataFlavor, param->ioDataBuffer,
						param->ioDataSize, &(CFStringRef)param->ioStorage );
		break;
		
		case blCellGetDataMsg:
		local_SLSGet( param, string );
		break;
		
		case blCellDeinitMsg:
		if (string) CFRelease( string );
		break;
	}
	return answer;
}


//===============================
static void local_SLSSet( OSType flavor, Ptr data, UInt32 dataSize, CFStringRef* ioStr )
{
	switch (flavor)
	{
		case slcfsStringListResourceFlavor:
		{
			SInt16		strIndex;
			
			strIndex = ((HMStringResType*)data)->hmmIndex;
			if (strIndex)
			{
				Str255		string;
				
				GetIndString( string, ((HMStringResType*)data)->hmmResID, strIndex );
				*ioStr = CFStringCreateWithPascalString( nil, string, kCFStringEncodingMacRoman );
			}
		}
		break;
		
		case slcfsCFStringFlavor:
		if (dataSize == sizeof(CFStringRef))
		{
			CFStringRef	str;
			
			str = *ioStr;
			if (str)
			{
				CFRelease( str );
				*ioStr = nil;
			}
			str = *(CFStringRef*)data;
			if (str) CFRetain( str );
			*ioStr = str;
		}
		break;
	}
}

//===============================
static void local_SLSGet( BLHandlerParamPtr param, CFStringRef str )
{
	SInt32	actualSize = 0;
	
	if (str)
	{
		switch (param->ioDataFlavor)
		{
			case slcfsCFStringFlavor:
			actualSize = sizeof(CFStringRef);
			if (param->ioDataBuffer)
			{
				CFStringRef*	dest;
				
				CFRetain( str );
				dest = (CFStringRef*)param->ioDataBuffer;
				*dest = str;
			}
			break;
		}
	}
	param->ioDataSize = actualSize;
}

//===============================
static void local_SLSDraw( CFStringRef inString, ThemeFontID inFont,
							const Rect* inRect, UInt16 inIndent )
{
	OSErr				err;
	CFMutableStringRef	stringCopy;
	Point				bounds;
	SInt16				baseline;
	Rect				textRect;
	
	textRect = *inRect;
	textRect.left += inIndent;
	stringCopy = CFStringCreateMutableCopy( nil, 0xFFFF, inString );
	err = TruncateThemeText( stringCopy, inFont, kThemeStateActive,
							inRect->right - inRect->left - inIndent, truncMiddle, nil );
	err = GetThemeTextDimensions( stringCopy, inFont, kThemeStateActive,
									false, &bounds, &baseline );
	textRect.bottom = textRect.top + bounds.v;
	textRect.right = textRect.left + bounds.h;
	BLAlignRectInRect( blAlignVCenter, false, &textRect, inRect );
	err = DrawThemeTextBox( stringCopy, inFont, kThemeStateActive,
							false, &textRect, teFlushDefault, nil );
	CFRelease( stringCopy );
}