// EIKTABDG.CPP
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

#include <eiktabdg.h>
#include <eikenv.h>
#include <eikdlgtb.h>
#include <eikcmbut.h>
#include <eikpriv.hrh>
#include <eikon.rsg>
//
#include <eikchlst.h>
#include <eikfpne.h>
#include <badesca.h>
#include <txtfrmat.h>

const TInt KMinTabValue=14;	//pixels

///////////////////// Multi page tab settings dialog/////////////////////

EXPORT_C CEikTabsDialog::CEikTabsDialog(CParaFormat* aFormat, TParaFormatMask& aAttribChangedMask, TParaFormatMask& aUndeterminedMask,const TInt aPageWidth)
	{
	iFormat=aFormat;
	iAttribChangedMask=&aAttribChangedMask;
	iUndeterminedMask=&aUndeterminedMask;
	iPageWidth=aPageWidth;
	}
 
EXPORT_C CEikTabsDialog::~CEikTabsDialog()
	{
	delete iTabsList;
	delete iCurrentTabsList;
	}

void CEikTabsDialog::PreLayoutDynInitL()
	{
	iTabsList=new(ELeave) CDesCArrayFlat(5);
	iCurrentTabsList=new(ELeave) CDesCArrayFlat(5);

	CEikTwipsEditor* twipsEd=(CEikTwipsEditor*)Control(EEikCidDefaultTab);
	twipsEd->SetMinMax(KMinTabValue,iPageWidth);
	CEikTwipsEditor* newtab=(CEikTwipsEditor*)Control(EEikCidNewTabStop);
	newtab->SetMinMax(KMinTabValue,iPageWidth);

	//default.//
	if (iUndeterminedMask->AttribIsSet(EAttDefaultTabWidth))
		SetTwipsEditorValueL(EEikCidDefaultTab, NULL);
	else	
		{
		TInt tab=iFormat->iDefaultTabWidthInTwips;
		SetTwipsEditorValueL(EEikCidDefaultTab,&tab);
		}
	//custom.//
	TInt item=0;
	SetTwipsEditorValueL(EEikCidNewTabStop, &item);//NULL);
	SetChoiceListCurrentItem(EEikCidTabAlignment,item);
	}

void CEikTabsDialog::PostLayoutDynInitL()
	{
	UpDateTabsListL();
	}

void CEikTabsDialog::UpDateTabsListL()
	{
	CEikDialogToolBar* tBar=(CEikDialogToolBar*)Control(EEikCidTabTBar);
	CEikCommandButton* clearButton=(CEikCommandButton*)tBar->ControlById(EEikCidClearTab);
	CEikCommandButton* clearAllButton=(CEikCommandButton*)tBar->ControlById(EEikCidClearAllTabs);
	
	if (iTabsList)
		iTabsList->Reset();
	if (iCurrentTabsList)
		iCurrentTabsList->Reset();
	TBool dimmed=EFalse;
	if (iFormat->TabCount())
		{
		for (TInt i=0;i<iFormat->TabCount();i++)
			{
			TBuf<4> index;
			index.Num(i+1);
			iTabsList->AppendL(index);

			TBuf<25> buf;
			TBuf<17>string2;
//			TRealFormat format(5,2);
//			buf.Num((TReal)iFormat->TabStop(i).iTwipsPosition/1440.0, format);
			Twips::TwipsToString(iFormat->TabStop(i).iTwipsPosition,2,buf);
			TTabStop::TTabType type=iFormat->TabStop(i).iType;
			switch (type)
				{
			case TTabStop::ELeftTab:
				iCoeEnv->ReadResource(string2,R_EIK_TBUF_LEFT);
				break;
			case TTabStop::ECenteredTab:
				iCoeEnv->ReadResource(string2,R_EIK_TBUF_CENTRED);
				break;
			case TTabStop::ERightTab:
				iCoeEnv->ReadResource(string2,R_EIK_TBUF_RIGHT);
				break;
			default:
				break;
				}
			buf.Append(string2);
			iCurrentTabsList->AppendL(buf);
			}
		}
	else
		{
		dimmed=ETrue;
		iTabsList->AppendL(_L(" "));
		iCurrentTabsList->AppendL(_L(" "));
		}

	CEikChoiceList* sizelist = (CEikChoiceList*) Control(EEikCidTabsList);
	sizelist->SetArrayL(iTabsList);
	sizelist->SetArrayExternalOwnership(ETrue);
	TInt pos=(iFormat->TabCount())? iFormat->TabCount()-1 : 0; //array count starts from 0 
	SetChoiceListCurrentItem(EEikCidTabsList,pos);
	
	CEikChoiceList* tabAtlist = (CEikChoiceList*) Control(EEikCidCurrentTab);
	tabAtlist->SetArrayL(iCurrentTabsList);
	tabAtlist->SetArrayExternalOwnership(ETrue);
	
	clearButton->SetDimmed(dimmed);
	clearButton->DrawNow();
	clearAllButton->SetDimmed(dimmed);
	clearAllButton->DrawNow();
	SetLineDimmedNow(EEikCidTabsList,dimmed);
	SetLineDimmedNow(EEikCidCurrentTab,dimmed);
	if (dimmed && (Control(EEikCidCurrentTab)->IsFocused() ||Control(EEikCidTabsList)->IsFocused()))	//give up focus if dimmed.
		TryChangeFocusToL(EEikCidNewTabStop);
	UpdateControlChanges();
	}

void CEikTabsDialog::UpdateControlChanges()
	{
	TInt pos=ChoiceListCurrentItem(EEikCidTabsList);
	SetChoiceListCurrentItem(EEikCidCurrentTab,pos);
	}

void CEikTabsDialog::HandleControlStateChangeL(TInt aControlId) 
	{
	switch(aControlId)
		{
	case EEikCidNewTabStop:
		break;
	case EEikCidCurrentTab:
		{
		TInt pos=ChoiceListCurrentItem(EEikCidCurrentTab);
		SetChoiceListCurrentItem(EEikCidTabsList,pos);
		break;
		}
	case EEikCidTabsList:
		UpdateControlChanges();
		break;
	case EEikCidTabTBar:
		{
		const TInt ctrlId=STATIC_CAST(CEikDialogToolBar*,Control(aControlId))->StateChangeControlId();
		switch (ctrlId)
			{
		case EEikCidSetTab:
			SetTabValueL();
			break;
		case EEikCidClearTab:
			ClearTabValueL();
			break;
		case EEikCidClearAllTabs:
			ClearAllTabsL();
			break;
			}
		UpDateTabsListL();
		break;
		}
		}
	}

void CEikTabsDialog::SetTabValueL()
	{
	TTabStop tab;
	Control(EEikCidNewTabStop)->PrepareForFocusLossL();
	tab.iTwipsPosition=TwipsEditorValue(EEikCidNewTabStop);
	TInt type;
	type=ChoiceListCurrentItem(EEikCidTabAlignment);
	switch (type)
		{
	case 0:
		tab.iType=TTabStop::ELeftTab;
		break;
	case 1:
		tab.iType=TTabStop::ECenteredTab;
		break;
	case 2:
		tab.iType=TTabStop::ERightTab;
		break;
		}
	iFormat->StoreTabL(tab);
	iAttribChangedMask->SetAttrib(EAttTabStop);
	}

void CEikTabsDialog::ClearTabValueL()
	{
	TInt focus=ChoiceListCurrentItem(EEikCidCurrentTab);
	TInt tab=iFormat->TabStop(focus).iTwipsPosition;
	iFormat->RemoveTab(tab);  //focus);
	iAttribChangedMask->SetAttrib(EAttTabStop);
	TInt value=0;
	SetTwipsEditorValueL(EEikCidNewTabStop, &value);//NULL);
	}

void CEikTabsDialog::ClearAllTabsL()
	{
	iFormat->RemoveAllTabs();
	iAttribChangedMask->SetAttrib(EAttTabStop);
	TInt tab=0;
	SetTwipsEditorValueL(EEikCidNewTabStop, &tab);//NULL);
	}

TBool CEikTabsDialog::OkToExitL(TInt /*aButtonId*/)
	{
	//default//
	Control(EEikCidDefaultTab)->PrepareForFocusLossL();
	iFormat->iDefaultTabWidthInTwips=TwipsEditorValue(EEikCidDefaultTab);
	iAttribChangedMask->SetAttrib(EAttDefaultTabWidth);
	//custom//
	return ETrue;
	}
