×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Regular Contributor
    Join Date
    Mar 2008
    Location
    Bangalore, India
    Posts
    68

    How to create a custom tab control

    I want to create a custom tab control how to start with it...

    I have an example of custom tab control Please can tell me how to integrate this in application....
    Code:
    ////////////////////////////////////////////////////////////////////////////////////////////////
    
    // custom tab control
    /////////////////////////////////////////////////////////////////////////////////////////////////
    #include <gulicon.h>
    #include <eikenv.h>
    #include <AknAppUi.h>
    
    #include "CustomTabControl.h"
    
    CCustomTabControl* CCustomTabControl::NewL(const TRect& aRect,const CCoeControl* aParent)
    {
        CCustomTabControl* self = new(ELeave) CCustomTabControl(aRect);
        CleanupStack::PushL(self);
        self->ConstructL(aRect,aParent);
        CleanupStack::Pop(); // self
        return self;
    }
    
    CCustomTabControl::~CCustomTabControl()
    {
        if(iBackGround)
        {
            delete iBackGround;
            iBackGround = NULL;
        }
        if(iFocusedBackgroundImage)
        {
        	delete iFocusedBackgroundImage;
        	iFocusedBackgroundImage=NULL;
        }
        if(iDeFocusedBackgroundImage)
        {
        	delete iDeFocusedBackgroundImage;
        	iDeFocusedBackgroundImage=NULL;
        }
        if(iIconArray)
        {
        	iIconArray->ResetAndDestroy();
        }
        if(iTabTitleArray)
        {
        	iTabTitleArray->Reset();
        }
    }
    
    void CCustomTabControl::ConstructL(const TRect& aRect,const CCoeControl* aParent)
    {
    	iFocusedBackgroundImage=NULL;
    	iDeFocusedBackgroundImage=NULL;
    	iIconArray=NULL;
    	iTabTitleArray=NULL;
    	iNumberOfTabsToBeShown=5;
    	iActiveTabPosition=2;
    	iActiveTabIndex=2;
    	if (aParent == NULL)
    	{
    		CreateWindowL();
    		iAvkonAppUi->AddToStackL( this);
    	}
        else
        {
    	    // Part of a compound control, so just share
    	    // the parent's window
    	    SetContainerWindowL(*aParent);
        }
    	SetRect(aRect);
        iBackGround = CAknsBasicBackgroundControlContext::NewL( KAknsIIDQsnBgAreaMain, iRect, EFalse );
        SetRect(aRect);
        ActivateL();
    
    }
    
    TTypeUid::Ptr CCustomTabControl::MopSupplyObject(TTypeUid aId)
    {
        if(aId.iUid == MAknsControlContext::ETypeId && iBackGround)
           {
            return MAknsControlContext::SupplyMopObject( aId, iBackGround);
           }
        return CCoeControl::MopSupplyObject( aId );
    }
    
    void CCustomTabControl::HandleControlEventL(CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
    {
    }
    
    void CCustomTabControl::ShiftTabsBack()
    {
    	if(iActiveTabIndex>0)
    	{
    		iActiveTabIndex--;
    	}
    
    	else
    	{
    		iActiveTabIndex=iIconArray->Count()-1;
    	}
    
    	ShiftTabs(SHIFT_LEFT);
    	DrawNow();
    }
    
    TKeyResponse CCustomTabControl::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
    {
    //	KN_UI_LOG((KN_UI_LOG_TYPE_DEBUG,"CCustomTabControl::OfferKeyEventL\n"));
    
    	TKeyResponse ret=EKeyWasNotConsumed;
    	if(IsFocused() && EEventKeyUp == aType)
    	{
    		switch (aKeyEvent.iScanCode)
    		{
    			case EStdKeyLeftArrow:
    			{
    				if(iActiveTabIndex>0)
    					iActiveTabIndex--;
    				else
    					iActiveTabIndex=iIconArray->Count()-1;
    				ShiftTabs(SHIFT_LEFT);
    				DrawNow();
    				ret=EKeyWasConsumed;
    			}
    			break;
    			case EStdKeyRightArrow:
    			{
    				if(iActiveTabIndex<iIconArray->Count()-1)
    					iActiveTabIndex++;
    				else
    					iActiveTabIndex=0;
    				ShiftTabs(SHIFT_RIGHT);
    				DrawNow();
    				ret=EKeyWasConsumed;
    			}
    			break;
    			default:
    			break;
    		}
    	}
        return ret;
    }
    
    void CCustomTabControl::ShiftTabs(SHIFT_DIR aDir)
    {
    	if(iIconArray && iIconArray->Count()>0)
    	{
    		CGulIcon* icon=NULL;
    		if(aDir==SHIFT_RIGHT)
    		{
    			icon=iIconArray->At(0);
    			iIconArray->Delete(0);
    			iIconArray->Compress();
    			if(icon)
    				iIconArray->AppendL(icon);
    		}
    		else
    		{
    			icon=iIconArray->At(iIconArray->Count()-1);
    			iIconArray->Delete(iIconArray->Count()-1);
    			iIconArray->Compress();
    			if(icon)
    				iIconArray->InsertL(0,icon);
    		}
    	}
    }
    
    void CCustomTabControl::SetDefaultTabByIndex(TInt aIndex)
    {
    	if(aIndex>=0 && aIndex<iActiveTabPosition)
    	{
    		for(TInt i=0;i<iActiveTabPosition-aIndex;i++)
    		{
    			ShiftTabs(SHIFT_LEFT);
    		}
    	}
    	if(aIndex>iActiveTabPosition && aIndex<iIconArray->Count())
    	{
    		for(TInt i=0;i<aIndex-iActiveTabPosition;i++)
    		{
    			ShiftTabs(SHIFT_RIGHT);
    		}
    	}
    	if(iIconArray && aIndex>=0 && aIndex<iIconArray->Count())
    	{
    		iActiveTabIndex=aIndex;
    		DrawNow();
    	}
    }
    void CCustomTabControl::Draw(const TRect& aRect) const
    {
    	CWindowGc& gc = SystemGc();
    	//TRect aRect(aaRect);
    	//aRect.iTl.iX=0;
    	//aRect.iTl.iY=0;
    	gc.Clear(aRect);
    	gc.SetPenStyle(CGraphicsContext::ESolidPen);
    	TSize pensize(2,2);
    	gc.SetPenSize(pensize);
    	gc.SetPenColor(KRgbBlack);
    	gc.SetBrushStyle(CGraphicsContext::ENullBrush);
    	gc.DrawRect(aRect);
    	MAknsSkinInstance* skin = AknsUtils::SkinInstance();
    	MAknsControlContext* cc = AknsDrawUtils::ControlContext( this );
    	AknsDrawUtils::Background( skin, cc, this, gc, aRect );
    	if(iFocused)
    	{
    		if(iFocusedBackgroundImage)
    		{		
    			gc.DrawBitmap(aRect, iFocusedBackgroundImage);
    		}
    	}
    	else
    	{
    		if(iDeFocusedBackgroundImage)
    		{		
    			gc.DrawBitmap(aRect, iDeFocusedBackgroundImage);
    		}
    	}
    
    	if(iIconArray && iIconArray->Count()>0 && iNumberOfTabsToBeShown>0
    		&& iNumberOfTabsToBeShown<=iIconArray->Count())
    	{
    		TInt tabWidth=aRect.Width()/iNumberOfTabsToBeShown;
    		TSize tabSize(tabWidth,aRect.Height());
    		TRect tabRect(tabSize);
    		tabRect.Shrink(4,4);
    		tabRect.iTl.iY-=3;
    		tabRect.iBr.iY-=3;
    		TInt dX=tabRect.Width()+3;
    #ifdef __UI_FRAMEWORKS_V2
    		dX=tabRect.Width()+5;
    #endif
    		for(TInt i=0;i<iNumberOfTabsToBeShown;i++)
    		{
    			CFbsBitmap* tab=NULL;
    			CGulIcon* icon=NULL;
    			if(i<iIconArray->Count())
    				icon = iIconArray->At(i);
    			if(icon)
    				tab = icon->Bitmap();
    			// to adjust the middle tab (middle tab is bigger than other tabs)
    			if(i==2)
    			{
    #ifdef __UI_FRAMEWORKS_V2
    				tabRect.iTl.iX+=5;
    				tabRect.iBr.iX+=5;
    #else
    				tabRect.iTl.iX+=9;
    				tabRect.iBr.iX+=9;
    #endif
    			}
    			if(tab)
    			{
    				TRect rect;
    				rect.iTl.iX=0;
    				rect.iTl.iY=0;
    				rect.iBr.iX=tabRect.Width();
    				rect.iBr.iY=tabRect.Height();
    				gc.BitBltMasked(tabRect.iTl,tab,rect,icon->Mask(),EFalse);
    			}
    			tabRect.iTl.iX+=dX;
    			tabRect.iBr.iX+=dX;
    			// to adjust the middle tab (middle tab is bigger than other tabs)
    			if(i==2)
    			{
    				tabRect.iTl.iX+=10;
    				tabRect.iBr.iX+=10;
    			}
    
    		}
    	}
    
    	if(iTabTitleArray && iTabTitleArray->Count()>0 && iActiveTabIndex>=0
    		&& iActiveTabIndex<iTabTitleArray->Count())
    	{
    		gc.SetPenStyle(CGraphicsContext::ESolidPen);
    		gc.SetPenColor(KRgbWhite);
    		const CFont* font = CEikonEnv::Static()->LegendFont();
    		gc.UseFont(font);
    		TRect textRect(aRect);
    		TInt baseline =	textRect.iBr.iY - 5;
    		TBuf<200> titleText;
    		titleText.FillZ();
    		titleText.Copy(iTabTitleArray->MdcaPoint(iActiveTabIndex));
    		gc.DrawText(titleText,textRect, baseline, CGraphicsContext::ECenter, 1);
    	}
    }
    The header file is like this
    Code:
    #ifndef ___CUSTOMTABCONTROL_H__
    #define ___CUSTOMTABCONTROL_H__
    #include <coecntrl.h>
    #include <AknsDrawUtils.h>// skin
    #include <AknsBasicBackgroundControlContext.h> //skin
    
    // FORWARD DECLARATIONS
    class CGulIcon;
    
    /////////////////////////////////////////////////////////////////////////////////////////////////
    
    // custom tab control
    /////////////////////////////////////////////////////////////////////////////////////////////////
    
    typedef enum
    {
      SHIFT_LEFT=0,
      SHIFT_RIGHT
    } SHIFT_DIR;
    
    class CCustomTabControl: public CCoeControl, MCoeControlObserver
    {
    public:
    
        static CCustomTabControl* NewL(const TRect& aRect,const CCoeControl* aParent);
        ~CCustomTabControl();
    	TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType);
    	void HandleControlEventL(CCoeControl* aControl,TCoeEvent aEventType);
    
        CCustomTabControl(const TRect& aRect):iRect(aRect)
        {
        }
    
    	TTypeUid::Ptr MopSupplyObject(TTypeUid aId);
    
    	void Draw(const TRect& aRect) const;
    	
    	void SetFocusedBackgroundImage(CFbsBitmap* aBackgroundImage) // takes the ownership
    	{
    		iFocusedBackgroundImage=aBackgroundImage;
    	}
    	void SetDeFocusedBackgroundImage(CFbsBitmap* aBackgroundImage) // takes the ownership
    	{
    		iDeFocusedBackgroundImage=aBackgroundImage;
    	}
    
    	TInt ActiveTabIndex()
    	{
    		return iActiveTabIndex;
    	}
    
        void SetDefaultTabByIndex(TInt aIndex);	
    
    	void SetIconArray(CArrayPtr<CGulIcon>* 	aIconArray) // takes the ownership
    	{
    		if(iIconArray != NULL)
    		{
    			iIconArray->ResetAndDestroy();
    		}
    		iIconArray=aIconArray;
    	}
    
    	void SetTabTitleArray(CDesCArrayFlat* aTabTitleArray)// takes the ownership
    	{
    		if(iTabTitleArray != NULL)
    		{
    			iTabTitleArray->Reset();
    		}
    		iTabTitleArray=aTabTitleArray;
    	}
    
    	void SetNumberOfTabsToBeShown(TInt aCount)
    	{
    		//if(aCount>0)
    		//	iNumberOfTabsToBeShown=aCount;
    	}
    
    	void SetFocus(TBool aFocus)
    	{
    		iFocused=aFocus;
    		DrawDeferred();
    	}
    
    	TBool IsFocused()
    	{
    		return iFocused;
    	}
    	void MakeVisible(TBool aVisible)
    	{
    		iVisible=aVisible;
    		CCoeControl::MakeVisible(aVisible);
    	}
    	TBool IsVisible()
    	{
    		return iVisible;
    	}
    	
    	TInt TabCount()	
    	{
    		if(iIconArray)
    			return iIconArray->Count();
    		else
    			return 0;
    	}
    	
    	TInt TabIdFromIndex(TInt aIndex)
    	{
    		return aIndex;
    	}
    	void ShiftTabsBack();
    private:
        void ConstructL(const TRect& aRect,const CCoeControl* aParent);
        void ShiftTabs(SHIFT_DIR aDir);
    
    private:
    	MAknsControlContext* 	iBackGround;
    	TRect					iRect;
    	//CFbsBitmap*     		iBackgroundImage;
    	
    	CFbsBitmap*     		iFocusedBackgroundImage;
    	CFbsBitmap*     		iDeFocusedBackgroundImage;
    
    	TInt					iActiveTabIndex; // Currently active tab
    	TInt					iActiveTabPosition; // Position of the active tab; currently assumed to be 2
    	CArrayPtr<CGulIcon>* 	iIconArray;
    	CDesCArrayFlat* 		iTabTitleArray;
    	TInt					iNumberOfTabsToBeShown;
    	TBool  					iFocused;
    	TBool  					iVisible;
    	
    };
    
    #endif ___CUSTOMTABCONTROL_H__
    "Nobody will believe in you unless you believe in yourself."

  2. #2
    Registered User
    Join Date
    Dec 2005
    Posts
    1,236

    Re: How to create a custom tab control

    Hi,

    What do you mean by integration.
    Do you want to add the header and the cpp in your application?
    Regards,
    Sriky

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    1,236

    Re: How to create a custom tab control

    Are you saying to instantiate the control if so you could have a control class sth like

    class CControlClass : public CCoeControl
    {
    ....
    }

    void CControlClass::ContructL()
    {
    CreateWindowL();
    SetRect( aRect);
    CCustomTabControl::NewL( rect, this);
    ActivateL();
    }
    Regards,
    Sriky

  4. #4
    Regular Contributor
    Join Date
    Mar 2008
    Location
    Bangalore, India
    Posts
    68

    Re: How to create a custom tab control

    Yes

    I have added these files to my application

    the NewL() of this function takes an argument of CCoeControl* as a parent. I want to only have a Single view with my custom tab control

    i have simple app which uses the existing tab control from framework
    i want to make my custom and use in it.
    "Nobody will believe in you unless you believe in yourself."

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    1,236

    Re: How to create a custom tab control

    Yup I thought so is your View derived of CAknView if so follow my previous post I hope it helps :)
    Regards,
    Sriky

  6. #6
    Regular Contributor
    Join Date
    Mar 2008
    Location
    Bangalore, India
    Posts
    68

    Re: How to create a custom tab control

    Thanks for help...

    i will work on it
    "Nobody will believe in you unless you believe in yourself."

Similar Threads

  1. Replies: 3
    Last Post: 2009-02-28, 13:57
  2. custom menu control
    By vickey in forum Symbian User Interface
    Replies: 1
    Last Post: 2008-02-18, 15:28
  3. CEikEdwin control in a TabScreen View
    By stefass in forum Symbian User Interface
    Replies: 2
    Last Post: 2006-02-16, 11:07
  4. How to display Tab Page control?
    By bittony in forum Symbian User Interface
    Replies: 2
    Last Post: 2005-02-15, 07:48
  5. How to create a custom control
    By alok_bbk in forum Symbian User Interface
    Replies: 1
    Last Post: 2004-10-18, 05:42

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×