ebmDevMag.com home
ebm Developer's Magazine
links

developer's mag
main page

article
part 1
part 2
part 3
part 4
part 5


2 - Just a Simple Child

The CList is only one of the ways to display rows of data (CTable is another). However, while CTable manages its own memory, CList doesn't - it simply exists to put a graphical wrapper on whatever you throw at it. As such, you can throw just about anything at it, and get quite useful results.

From a simplistic point of view, a list box is row upon row of data. The control actually requires very little from the data - it knows how to draw itself, and just needs a little help with how high to make a row, and what to draw in a particular row. The ebm CList class abstracts to that level, providing a very efficient method of displaying data, especially data you already have set up in memory. A typical class would be:


class CStringList : public CList
{
private:

const char **m_entry; // pointer to array of strings

CWindow *m_window;
int m_rowHeight; // value cached for use in GetRowHeight() for speed

U16 GetRowHeight(S32 row);
void DrawRow(RECT *rect, S32 row);

public:

CStringList(int id,int dx,int dy,const char **pointerArray,int numRows);
};
//------------------------------------------------------------------------------
CStringList::CStringList(int id,int dx,int dy,
const char **pointerArray,
int numRows)
: CList(id,dx,dy,numRows,0),
m_entry(pointerArray),
m_window(0),
m_rowHeight(0)
{
}
//------------------------------------------------------------------------------
U16 CStringList::GetRowHeight(S32 row)
{
// rather than calc the row height each time, for speed we save the value the
// first time into m_rowHeight, as well as m_window
if ( NULL==m_window ) // first time in?
{
m_window=GetWindow(); // store value to avoid repeatedly calling GetWindow()
m_rowHeight=GUI_FontHeight(m_window->GetFont());
}
return m_rowHeight;
}
//------------------------------------------------------------------------------
void CStringList::DrawRow(RECT *rect, S32 row)
{
// given the rectangle describing the row, draw it
m_window->DrawText(m_entry[row], rect->x, rect->y);
}

We can't use the CList directly (two necessary functions DrawRow() and GetRowHeight() are not implemented), but by creating a child class, we get access to the CList functions, while customizing the display with our own display calls.

Previous Section
Next Section

Copyright © 2001-2006 ebmDevMag.com - Legal Notice