// EIKDLGTB.CPP
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

#include <barsread.h>
#include <coemain.h>
#include <eikdlgtb.h>
#include <eikcmobs.h>
#include <eikfctry.h>
#include <eikpanic.h>
#include <eikcolor.h>
#include <eikenv.h>

EXPORT_C CEikDialogToolBar::~CEikDialogToolBar()
	{
	}	

EXPORT_C CEikDialogToolBar::CEikDialogToolBar()
	{
	__DECLARE_NAME(_S("CEikDialogToolBar"));
	iBrushContext.iBrushStyle=CGraphicsContext::ESolidBrush;
	iBrushContext.iBrushColor=CEikonEnv::Static()->Color(EEikColorWindowBackground);
	iContext=&iBrushContext;
	}

EXPORT_C void CEikDialogToolBar::BaseConstructL()
	{
	__ASSERT_DEBUG(Observer(),Panic(EEikPanicDialogToolBarNoObserver));
	CEikControlGroup::ConstructL(CEikControlGroup::EFromTopLeft,CEikControlGroup::ELayHorizontally);
	RWindow& window=Window();
	window.SetBackgroundColor(iEikonEnv->ControlColor(EEikColorDialogBackground,*this));
	window.SetPointerGrab(ETrue);
	window.SetShadowDisabled(ETrue);
	EnableDragEvents();
	}

EXPORT_C void CEikDialogToolBar::ConstructFromResourceL(TResourceReader& aReader)
	{
	BaseConstructL();
	const TInt width=aReader.ReadInt16();
	if (width)
		SetLengthInPixels(width);
	const TInt count=aReader.ReadInt16();
	for (TInt ii=0;ii<count;++ii)
		{
		const TInt ctrlType=aReader.ReadInt16();
		CCoeControl* ctrl=EikControlFactory::CreateByTypeL(ctrlType).iControl;
//		if (!ctrl)
//			ctrl=iCommandObserver->CreateCustomCommandControlL(ctrlType);
		__ASSERT_DEBUG(ctrl, Panic(EEikPanicToolBarNullControl));
		ctrl->SetContainerWindowL(*this);
		ctrl->SetNonFocusing();
		CleanupStack::PushL(ctrl);
		const TInt id=aReader.ReadInt16();
		const TInt length=aReader.ReadInt16();
		TEikGroupControl groupCtrl(ctrl,id,aReader.ReadInt16(),length);
		AddControlL(groupCtrl); //ownership now taken by array
		CleanupStack::Pop();
		ctrl->ConstructFromResourceL(aReader);
		}
	}

EXPORT_C TKeyResponse CEikDialogToolBar::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
	{
	const TInt count=iControlArray->Count();
	for (TInt ii=0;ii<count;ii++)
		{
		if ((*iControlArray)[ii].iControl->OfferKeyEventL(aKeyEvent,aType)==EKeyWasConsumed)
			return EKeyWasConsumed;
		}
	return EKeyWasNotConsumed;
	}

EXPORT_C void CEikDialogToolBar::AddControlL(TEikGroupControl& aGroupControl)
	{
	CEikControlGroup::AddControlL(aGroupControl);
	aGroupControl.iControl->SetObserver(this);
	}

void CEikDialogToolBar::HandleControlEventL(CCoeControl* aControl,TCoeEvent aEventType)
	{
	if (aEventType==MCoeControlObserver::EEventStateChanged)
		{
		iStateChangeControl=aControl;
		ReportEventL(aEventType);
		}
	}

EXPORT_C CCoeControl* CEikDialogToolBar::StateChangeControl() const
	{
	return iStateChangeControl;
	}

EXPORT_C TInt CEikDialogToolBar::StateChangeControlId() const
	{
	if (!iStateChangeControl)
		return KErrNotFound;
	return ControlId(iStateChangeControl);
	}
