Creating a Concrete Grid Class

You will need to implement your grid as a class derived from CAknGrid. In the SimpleGrid example, the grid class is CSimpleGridGrid. This is contained within a standard Symbian OS container CSimpleGridContainer. The code for the AppUi and Container will not be analyzed in depth here, as there is little difference between it and the code in the vertical list examples. There are, however, a few points worthy of note:

• In the AppUi, the container is constructed using new(Eleave), and the MOP parent is set before calling ConstructL(), rather than calling NewL() and then setting the MOP parent. This is because the scroll bars will appear only if the MOP parent is set before the call to ConstructL().

• In the container's SizeChanged() method, the extent of the grid is set using Rect().Size(), rather than the grid's minimum size. This is because using the minimum size affects the redrawing of the grid when scrolling.

The grid needs to construct itself from resource, set up its icons and then display itself. This is performed in CSimpleGridGamesGrid::ConstructL(), which is called from the container. The container passes the resources for the icon file name (R_ICON_FILE_NAME) and the grid (R_SIMPLEGRID_GAMES_GRID), which was defined previously in Defining a Grid Using Resources.

void CSimpleGridGamesGrid::ConstructL(Tlnt aGridResource, Tint alconFileResource) TResourceReader reader;

CEikonEnv::Static()->CreateResourceReaderLC(reader, aGridResource);

ConstructFromResourceL(reader); CleanupStack::PopAndDestroy();

SetupGridlconsL(alconFileResource);

SizeChangedQ;

The base class has a ConstructFromResourceL() method, which you should call with a resource reader created using the GRID resource. Once constructed, you can set up the icons for the grid. This is performed by calling the SetupGridlconsL() method. Apart from the difference in the actual icons, the code for adding icons is the same as for lists. See the SimpleList example in the Using Vertical Lists section for details of how to add icons.

„ You do not need to set up the scroll bars for the grid as you did with lists.

TfP CAknGrid takes care of them for you.

The last thing you need to do is draw the grid. You do this by overriding its SizeChanged() method, void CSimpleGridGamesGrid::SizeChanged()

CAknGrid::SizeChanged(); SetupGrid();

void CSimpleGridGamesGrid::SetupGrid()

// Setup text foreground and background colours to default AknListBoxLayouts::SetupStandardGrid(*this);

// Get local copies of data we will need

CFormattedCellListBoxltemDrawer* itemDrawer = this->ltemDrawer();

Tint cellWidth = ColumnWidth();

Tint cellHeight = ltemHeight();

// Set up graphics subcells

AknListBoxLayouts::SetupFormGfxCell( •this, //the grid itemDrawer, // the grid's drawer 0, // index of the graphic within item strings 0, // left position 0, // top position 0, //right - unused 0, // bottom - unused cellWidth, // width of graphic KGraphicsHeight, // height of graphic

TPoint(cellWidth , KGraphicsHeight)); // end position

// Set up text subcells const CFont* KFont = LatinBold12();

Tint baseline = cellHeight - KFont->DescentlnPixels() -1 ;

// N.B. although color is commented out // in the header file, it is still used!

AknListBoxl_ayouts::SetupFormTextCell( •this, // the grid itemDrawer // the grid's drawer 1, // index of text within item strings KFont, // the font for the text KTextColor, // the color of the text 0, // left margin 0, // right margin - unused baseline, // Baseline cellWidth, // text width (take margin into account if set) CGraphicsContext::ECenter // Alignment TPoint(0, KGraphicsHeight) // start position TPoint(cellWidth, cellHeight) // end position

You should call the base class SizeChanged() method, so that you retain Important functionality in your overridden method. Then you set up the cell layout using a number of static methods defined in AknListBoxLayouts.

The default foreground and background colors are set using the SetupStandardGrid() method. You can determine how the graphics and text will appear in each cell by setting up the subcells using SetupFormGfxCell() for the graphics subcell and SetupFormTextCell() for the text subcell. Each method has an index parameter, which refers to the position in the item strings of the graphic or text. All item strings used here are of the form "n\tText", where n is an icon number and Text is the text that will appear beneath that icon. Therefore, the index provided for the graphics subcell is 0 (the first position in the string), and the index for the text subcell is 1 (second position). When setting up the graphics subcell you must provide the following parameters:

• A reference to the grid itself.

• The index, as already discussed.

• Left position—the position in pixels relative to the left-hand side of the cell.

• Top position—the position in pixels relative to the top side of the cell.

• The height of the graphic.

• Start position—the coordinates of the start of the graphic.

• End position—the coordinates of the end of the graphic.

When setting up the text subcell you must provide the following parameters:

• The index, as already discussed.

• The color of the text as a Tint—this is an index into the color palette, the values of which can be found in the Appendix of the Series 60 Ul guide, part of the SDK documentation.

• Left margin position—position in pixels relative to the left-hand side of the cell.

• Baseline position—the position between the ascent and descent of the font. See Chapter 11 or the SDK documentation (Character ascent, descent, height and baseline) for further information.

• The alignment of the text.

• Start Position—the coordinates of the start of the text area.

• End Position—the coordinates of the start of the text area.

t The header file comments out the color parameter of

T}P AknListBoxLayouts::SetupFormTextCell(), but it is actually used.

+1 0

Average user rating: 5 stars out of 1 votes

Post a comment

  • Receive news updates via email from this site