Tabs example
From Forum Nokia Wiki
The CTabsContainer illustrates how to use tabs to handle controls inside your container. This example implements two tabs. The arrow keys switch between the two tabs and the rest of the methods are handling the controls according to the tab's active index. You can modify the looks of the tabs by defining one of the following tab styles:
- KTabWidthWithOneTab
- KTabWidthWithThreeTabs
- KTabWidthWithFourTabs
- KTabWidthWithTwoLongTabs
- KTabWidthWithThreeLongTabs
The SetMenuL() method shows how to swap between different menus according to the active tab.
Note that you need to add construction code for the iFirstControl & iSecondControl. You can implement them for example as , or .
Tabs_Container.cpp
CTabsContainer* CTabsContainer::NewL(CEikButtonGroupContainer* aCba)
{
CTabsContainer* self = CTabsContainer::NewLC(aCba);
CleanupStack::Pop();
return self;
}
CTabsContainer* CTabsContainer::NewLC(CEikButtonGroupContainer* aCba)
{
CTabsContainer* self = new (ELeave) CTabsContainer(aCba);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CTabsContainer::CTabsContainer(CEikButtonGroupContainer* aCba) : iCba(aCba)
{
}
CTabsContainer::~CTabsContainer()
{
iTabGroup = NULL;
if (!iNaviPane)
{
CEikStatusPane *sp = ((CAknAppUi*)iEikonEnv->EikAppUi())->StatusPane();
iNaviPane = (CAknNavigationControlContainer*)
sp->ControlL(TUid::Uid(EEikStatusPaneUidNavi));
}
if(iNaviPane)
iNaviPane->Pop(NULL);
iNaviPane = NULL;
delete iNaviDecoratorForTabs;
iNaviDecoratorForTabs = NULL;
delete iNaviDecorator;
iNaviDecorator = NULL;
delete iFirstControl;
delete iSecondControl;
}
void CTabsContainer::ConstructL(void)
{
CreateWindowL();
SetRect(CEikonEnv::Static()->EikAppUi()->ClientRect());
MakeNavipanelL();
// construct iFirstControl & iSecondControl in here..
ActivateL();
SetMenuL();
DrawNow();
}
_LIT(KtxTabText1 ,"Tab 1");
_LIT(KtxTabText2 ,"Tab 2");
void CTabsContainer::MakeNavipanelL(void)
{
CEikStatusPane *sp = ((CAknAppUi*)iEikonEnv->EikAppUi())->StatusPane();
iNaviPane = (CAknNavigationControlContainer*)
sp->ControlL(TUid::Uid(EEikStatusPaneUidNavi));
CAknNavigationDecorator* iNaviDecoratorForTabsTemp;
iNaviDecoratorForTabsTemp = iNaviPane->CreateTabGroupL();
delete iNaviDecoratorForTabs;
iNaviDecoratorForTabs = NULL;
iNaviDecoratorForTabs = iNaviDecoratorForTabsTemp;
iTabGroup = STATIC_CAST(CAknTabGroup*,
iNaviDecoratorForTabs->DecoratedControl());
iTabGroup->SetTabFixedWidthL(KTabWidthWithTwoTabs);
iTabGroup->AddTabL(0,KtxTabText1);
iTabGroup->AddTabL(1,KtxTabText2);
iTabGroup->SetActiveTabByIndex(0);
iNaviPane->PushL(*iNaviDecoratorForTabs);
}
void CTabsContainer::Draw(const TRect& /*aRect*/) const
{
CWindowGc& gc = SystemGc();
gc.Clear(Rect());
}
TKeyResponse CTabsContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,
TEventCode aEventCode)
{
TKeyResponse Ret = EKeyWasNotConsumed;
switch (aKeyEvent.iCode)
{
case EKeyRightArrow:
if(iTabGroup)
{
TInt IndexNum = iTabGroup->ActiveTabIndex();
IndexNum = IndexNum + 1;
if(IndexNum > 1)
IndexNum = 1;
iTabGroup->SetActiveTabByIndex(IndexNum);
SetMenuL();
DrawNow();
}
break;
case EKeyLeftArrow:
if(iTabGroup)
{
TInt IndexNum = iTabGroup->ActiveTabIndex();
IndexNum = IndexNum - 1;
if(IndexNum < 0)
IndexNum = 0;
iTabGroup->SetActiveTabByIndex(IndexNum);
SetMenuL();
DrawNow();
}
break;
default:
if(iTabGroup)
{
switch(iTabGroup->ActiveTabIndex())
{
case 1:
if(iSecondControl)
{
Ret = iSecondControl->OfferKeyEventL(aKeyEvent,aEventCode);
}
break;
default:
if(iFirstControl)
{
Ret = iFirstControl->OfferKeyEventL(aKeyEvent,aEventCode);
}
break;
}
}
break;
}
return Ret;
}
void CTabsContainer::SetMenuL(void)
{
CEikonEnv::Static()->AppUiFactory()->MenuBar()->StopDisplayingMenuBar();
TInt MenuRes(R_MAIN_1_MENUBAR);
TInt ButtomRes(R_MAIN_1_CBA);
if(iTabGroup)
{
switch(iTabGroup->ActiveTabIndex())
{
case 1:
MenuRes = R_MAIN_1_MENUBAR;
ButtomRes(R_MAIN_1_CBA);
break;
case 2:
MenuRes = R_MAIN_2_MENUBAR;
ButtomRes(R_MAIN_2_CBA);
break;
default:
//R_MAIN_MENUBAR
break;
}
}
CEikonEnv::Static()->AppUiFactory()->MenuBar()->
SetMenuTitleResourceId(MenuRes);
if(iCba)
{
iCba->SetCommandSetL(ButtomRes);
iCba->DrawDeferred();
}
}
CCoeControl* CTabsContainer::ComponentControl( TInt /*aIndex*/) const
{
CCoeControl* RetCont(NULL);
if(iTabGroup)
{
switch(iTabGroup->ActiveTabIndex())
{
case 1:
RetCont = iSecondControl;
break;
default:
RetCont = iFirstControl;
break;
}
}
return RetCont;
}
TInt CTabsContainer::CountComponentControls() const
{
TInt RetCount(0);
if(iTabGroup)
{
switch(iTabGroup->ActiveTabIndex())
{
case 1:
if(iSecondControl)
{
RetCount = 1;
}
break;
default:
if(iFirstControl)
{
RetCount = 1;
}
break;
}
}
return RetCount;
}
Tabs_Container.h
class CTabsContainer : public CCoeControl
{
public:
static CTabsContainer* NewL(CEikButtonGroupContainer* aCba);
static CTabsContainer* NewLC(CEikButtonGroupContainer* aCba);
~CTabsContainer();
public:
TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent,
TEventCode aType);
CCoeControl* ComponentControl( TInt aIndex) const;
TInt CountComponentControls() const;
private:
void SetMenuL(void);
void MakeNavipanelL(void);
CTabsContainer(CEikButtonGroupContainer* aCba);
void ConstructL(void);
void Draw(const TRect& aRect) const;
private:
CEikButtonGroupContainer* iCba;
CAknTabGroup* iTabGroup;
CAknNavigationControlContainer* iNaviPane;
CAknNavigationDecorator* iNaviDecorator;
CAknNavigationDecorator* iNaviDecoratorForTabs;
CCoeControl* iFirstControl;
CCoeControl* iSecondControl;
};