// EIKRUTIL.CPP
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

#include <eikenv.h> 
#include <eikrutil.h>
#include <barsread.h>
#include <eikcolor.h>
#include <eikfont.hrh>

enum TEikPanicResourceUtil
	{
	EEikPanicResourceNonEnvironment,
	EEikPanicResourceNullPointer,
	EEikPanicResourceInvalidNumberType,
	};

LOCAL_C void Panic(TEikPanicResourceUtil aPanic)
	{
	User::Panic(_L("EIKON-RUTIL"), aPanic);
	}

//
//	class EikResourceUtils
//

EXPORT_C CFbsFont* EikResourceUtils::CreateScreenFontL(TResourceReader& aResourceReader,CWsScreenDevice& aScreenDevice)
//
//	Create a font from a given position in a resource
//
	{
	TUid fontId;
	fontId.iUid=aResourceReader.ReadInt32();
	TUint flags=aResourceReader.ReadUint16(); // a combination of EEikFontFlagXxxxs
	TAlgStyle algStyle;
	algStyle.SetIsBold(flags&EEikFontFlagBold);
	algStyle.SetIsItalic(flags&EEikFontFlagItalic);
	algStyle.SetIsMono(flags&EEikFontFlagMono);
	algStyle.SetWidthFactor((flags&EEikFontFlagDoubleWidth)? 2: 1); 
	algStyle.SetHeightFactor((flags&EEikFontFlagDoubleHeight)? 2: 1);
	CFbsFont* font;
	User::LeaveIfError(aScreenDevice.GetFontById((CFont*&)font,fontId,algStyle));
	return(font);
	}

EXPORT_C CFbsFont* EikResourceUtils::CreateScreenFontL(TInt aResourceId,CEikonEnv* aEnv)
//
//	Create a font specified by a resource number
//
	{
	if (aEnv==NULL)
		aEnv=CEikonEnv::Static();
	__ASSERT_DEBUG(aEnv!=NULL,Panic(EEikPanicResourceNonEnvironment));
	TResourceReader reader;
	aEnv->CreateResourceReaderLC(reader,aResourceId);
	CWsScreenDevice* screenDevice=aEnv->ScreenDevice();
	__ASSERT_DEBUG(screenDevice!=NULL,Panic(EEikPanicResourceNullPointer));
	CFbsFont* font=CreateScreenFontL(reader,*screenDevice);
	CleanupStack::PopAndDestroy(); // resource reader
	return(font);
	}

EXPORT_C TInt32 EikResourceUtils::ReadResourceIntL(TInt aResourceId,CEikonEnv* aEnv,TResourceTypeInt aSize)
//
//	Read a resource specifying a number
//
	{
	if (aEnv==NULL)
		aEnv=CEikonEnv::Static();
	__ASSERT_DEBUG(aEnv!=NULL,Panic(EEikPanicResourceNonEnvironment));
	TResourceReader reader;
	aEnv->CreateResourceReaderLC(reader,aResourceId);
	TInt32 value=0;
	switch(aSize)
		{
	case EResourceInt8:
		value=reader.ReadInt8();
		break;
	case EResourceInt16:
		value=reader.ReadInt16();
		break;
	case EResourceInt32:
		value=reader.ReadInt32();
		break;
	default:
		Panic(EEikPanicResourceInvalidNumberType);
		}
	CleanupStack::PopAndDestroy(); // resource reader
	return(value);
	}

EXPORT_C CEikColorArray* EikResourceUtils::CreateColorArrayL(TInt aResourceId,CEikonEnv* aEnv)
//
// Create an array of control colors from a resource definition
//
	{ // static
	CEikColorArray* colors=EikResourceUtils::CreateColorArrayLC(aResourceId,aEnv);
	CleanupStack::Pop(); // colors
	return colors;
	}

EXPORT_C CEikColorArray* EikResourceUtils::CreateColorArrayLC(TInt aResourceId,CEikonEnv* aEnv)
//
// Create an array of control colors from a resource definition and return it on the cleanup stack
//
	{ // static
	if (aEnv==NULL)
		aEnv=CEikonEnv::Static();
	__ASSERT_DEBUG(aEnv!=NULL,Panic(EEikPanicResourceNonEnvironment));
	CEikColorArray* colors=CEikColorArray::NewLC();
	TResourceReader reader;
	aEnv->CreateResourceReaderLC(reader,aResourceId);
    TInt listId=reader.ReadInt32();
	CleanupStack::PopAndDestroy(); // reader
	aEnv->CreateResourceReaderLC(reader,listId);
	const TInt count=reader.ReadInt16();
	for (TInt ii=0;ii<count;ii++)
		{
		TInt logicalColor=reader.ReadInt16();
		TInt red=reader.ReadUint8();
		TInt green=reader.ReadUint8();
		TRgb color(red,green,reader.ReadUint8());
		colors->AddL(logicalColor,color);
		}
	CleanupStack::PopAndDestroy(); // reader
	return colors;
	}
