You Are Here:

Community: Wiki

This page was last modified on 25 September 2009, at 11:38.

Grid example

From Forum Nokia Wiki

Reviewer Approved   

The CGridContainer illustrates how to make a simple container using CAknGrid. The grid is constructed in the MakeGridBoxL() method. Note that the S60 phones have several different screen sizes and layouts so the grid size and number calculations should be dynamically adaptable to the current screen size as shown in the example where the max size of Grid item is 80x80 and the grid has at least 3 vertical and 3 horizontal lines. You could modify these values to fit your application usage scenario.

You could modify the grid scrolling setting with SetPrimaryScrollingType() and SetSecondaryScrollingType() methods.

Note that the string format used with the text array (which is set with the SetItemTextArray() method) depends on the definitions for the cells for your grid items. In this example there is one text cell (index 1) and one graphics cell (index 0) so the string format is “1\tMyText” where the 1 is a zero based icon index.

You can add more cells into the grid items by defining more graphical cells (using SetupFormGfxCell() for example) and text cells (using SetupFormTextCell() for example)

Contents

Headers required

#include <akngrid.h> //CAknGrid 
#include <coecntrl.h> //CCoeControl
#include <gulicon.h> //CGulIcon

Library needed

LIBRARY  avkon.lib //CAknGrid

Capability required

None


Grid_Container.cpp

#include "Grid_Container.h"
 
CGridContainer::CGridContainer(void)
{
 
}
 
CGridContainer::~CGridContainer()
{
delete iMyGrid;
}
 
void CGridContainer::ConstructL()
{
CreateWindowL();
SetRect(CEikonEnv::Static()->EikAppUi()->ClientRect());
 
ActivateL();
SetMenuL();
DrawNow();
}
 
void CGridContainer::SizeChanged()
{
MakeGridBoxL();
}
 
void CGridContainer::HandleResourceChange(TInt aType)
{
TRect rect;
 
if ( aType==KEikDynamicLayoutVariantSwitch )
{
AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);
SetRect(rect);
}
 
CCoeControl::HandleResourceChange(aType);
}
 
void CGridContainer::MakeGridBoxL()
{
TInt MySetIndex(0);
 
if(iMyGrid)
{
MySetIndex = iMyGrid->CurrentItemIndex();
}
 
delete iMyGrid;
iMyGrid = NULL;
iMyGrid = new( ELeave ) CAknGrid;
iMyGrid->SetContainerWindowL( *this );
 
CAknGridM* GridViewM = new( ELeave ) CAknGridM;
iMyGrid->SetModel( GridViewM );
 
iMyGrid->ConstructL( this, EAknListBoxSelectionGrid );
 
TRect MyRect(Rect());
iMyGrid->SetRect(MyRect);
 
TInt NumH = (MyRect.Height() / 80);
TInt NumW = (MyRect.Width() / 80);
 
if(NumH < 3)
{
NumH = 3;
}
 
if(NumW < 3)
{
NumW = 3;
}
 
TSize SizeOfItems;
 
SizeOfItems.iWidth = ( MyRect.iBr.iX - MyRect.iTl.iX ) / NumW;
SizeOfItems.iHeight = ( MyRect.iBr.iY - MyRect.iTl.iY )/ NumH;
 
iMyGrid->SetLayoutL(EFalse,ETrue,ETrue,NumW,NumH,SizeOfItems);
 
iMyGrid->SetPrimaryScrollingType(
CAknGridView::EScrollIncrementLineAndLoops);
iMyGrid->SetSecondaryScrollingType(
CAknGridView::EScrollIncrementLineAndLoops);
iMyGrid->SetCurrentDataIndex(0);
 
CArrayPtr<CGulIcon>* icons = new( ELeave ) CAknIconArray(2);
CleanupStack::PushL(icons);
 
const CFont* MyUseFont =
AknLayoutUtils::FontFromId(EAknLogicalFontSecondaryFont);
 
TPoint TxtStartPoint(0,
(SizeOfItems.iHeight - (MyUseFont->HeightInPixels() + 3)));
TPoint TxtEndddPoint(SizeOfItems.iWidth, SizeOfItems.iHeight);
 
TPoint StartIcon(0,0);
TPoint EnddIcon(SizeOfItems.iWidth, TxtStartPoint.iY);
 
TSize IconsSize((((EnddIcon.iX - StartIcon.iX) * 9) / 10),
(((EnddIcon.iY - StartIcon.iY) * 9) / 10));
 
iMyGrid->Model()->SetItemTextArray(GetArrayL(icons, IconsSize));
iMyGrid->ItemDrawer()->FormattedCellData()->SetIconArrayL(icons);
CleanupStack::Pop(); // icons
 
iMyGrid->ScrollBarFrame()->SetScrollBarVisibilityL(
CEikScrollBarFrame::EAuto, CEikScrollBarFrame::EAuto);
 
iMyGrid->HandleItemAdditionL();
 
AknListBoxLayouts::SetupStandardGrid(*iMyGrid);
 
AknListBoxLayouts::SetupFormGfxCell(*iMyGrid, iMyGrid->ItemDrawer(),
0, 0, 0, 0, 0, SizeOfItems.iWidth,(EnddIcon.iY - StartIcon.iY),
StartIcon, EnddIcon);
 
TInt BaseLine = (TxtEndddPoint.iY -
MyUseFont->BaselineOffsetInPixels() - 3);
 
AknListBoxLayouts::SetupFormTextCell(*iMyGrid, iMyGrid->ItemDrawer(),
1, MyUseFont,
215,
3,
0,
BaseLine,
SizeOfItems.iWidth,
CGraphicsContext::ECenter,
TxtStartPoint,
TxtEndddPoint
);
 
 
TInt ItemsCount = iMyGrid->Model()->ItemTextArray()->MdcaCount();
 
if(ItemsCount > MySetIndex && MySetIndex >= 0)
iMyGrid->SetCurrentItemIndex(MySetIndex);
else if(ItemsCount > 0)
iMyGrid->SetCurrentItemIndex(0);
 
iMyGrid->MakeVisible( ETrue );
iMyGrid->SetFocus( ETrue );
iMyGrid->ActivateL();
iMyGrid->DrawNow();
}
 
 
CDesCArray* CGridContainer::GetArrayL(CArrayPtr<CGulIcon>* aIcon,
const TSize& aIconsize)
{
CDesCArrayFlat* MyArray = new(ELeave)CDesCArrayFlat(1);
CleanupStack::PushL(MyArray);
 
// Append Text string to MyArray in here as "1\tMytext",
// where 1 is the zero based icon index
// also remember to load and add icons in here... and also set them
// to the right size
 
CleanupStack::Pop(MyArray);
return MyArray;
}
 
void CGridContainer::Draw(const TRect& /*aRect*/) const
{
CWindowGc& gc = SystemGc();
gc.Clear(Rect());
}
 
TKeyResponse CGridContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode
aEventCode)
{
TKeyResponse Ret = EKeyWasNotConsumed;
 
if(iMyGrid)
{
Ret = iMyGrid->OfferKeyEventL(aKeyEvent,aEventCode);
}
 
return Ret;
}
 
CCoeControl* CGridContainer::ComponentControl( TInt /*aIndex*/) const
{
return iMyGrid;
}
 
TInt CGridContainer::CountComponentControls() const
{
if(iMyGrid)
return 1;
else
return 0;
}

Grid_Container.h

class CGridContainer : public CCoeControl
{
public:
CGridContainer(void);
void ConstructL(void);
~CGridContainer();
public:
TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent,
TEventCode aType);
CCoeControl* ComponentControl( TInt aIndex) const;
TInt CountComponentControls() const;
private:
virtual void SizeChanged();
virtual void HandleResourceChange(TInt aType);
void Draw(const TRect& aRect) const;
CDesCArray* GetArrayL(CArrayPtr<CGulIcon>* aIcon,
const TSize& aIconsize);
void MakeGridBoxL(void);
private:
CAknGrid* iMyGrid;
};

Other Related Resources

Grids

Using Grids API's

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
User Rating: qfnZuserE5FratingQNx1E2E0000X